@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
26 lines (25 loc) • 1.4 kB
JavaScript
import { runGit } from "../utils-helpers.js";
/** Git can echo credentials in URLs and HTTP trace headers. */
export function scrubGitAuthText(text) {
return text
.replace(/\b([a-z][a-z\d+.-]*:\/\/)[^\s/?#]*@/gi, "$1")
.replace(/\bx-access-token:[^\s@]+@/gi, "")
.replace(/\b(?:proxy-)?authorization[ \t]*:[^\r\n]*/gi, "Authorization: [redacted]")
.replace(/\b(?:gh[pousr]_|github_pat_)[\w-]+/gi, "[redacted]")
.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, "");
}
export function gitAuthErrorText(error) {
const value = error;
for (const candidate of [value?.stderr, value?.error, value?.message, error]) {
const text = Buffer.isBuffer(candidate) ? candidate.toString("utf8") : candidate;
if (typeof text === "string" && text.trim())
return scrubGitAuthText(text).trim().slice(0, 2048);
}
}
export function gitAuthEnvironment(cwd) {
const configured = runGit(cwd, ["config", "--get", "credential.helper"], 5000);
// A helper can be an arbitrary shell command with inline credentials.
const standard = configured?.match(/^(osxkeychain|libsecret|manager(?:-core)?|wincred|cache|store)(?:\s|$)/)?.[1];
const helper = standard ?? (configured ? "custom (value withheld)" : "unset");
return `SSH_AUTH_SOCK ${process.env.SSH_AUTH_SOCK === undefined ? "unset" : "set"}, credential.helper ${helper}`;
}