fix(github): redact credentials from github_repo_analyze remoteUrl (#4577)

github_repo_analyze returned `git remote get-url origin` verbatim. A remote
with embedded credentials (https://x-access-token:<token>@github.com/...,
the form CI checkouts and some credential helpers write) put the token into
MCP tool output, where it reaches the model and session transcripts.

Strip URL userinfo before returning it. Greedy up to the last `@` in the
authority, so a raw `@` in a password cannot leave a fragment behind.
scp-form remotes (git@host:o/r) carry no secret and are unchanged.

Found while reviewing #4571, which widened the same parser.

Co-authored-by: r <r@x>
This commit is contained in:
rUvandr authored and GitHub committed 2026-10-29 09:41:56 -04:00
1 parent 8c0ad74e50
commit 27b48e6ba4
2 files changed
+24 -1

No files matched your search

@@ -69,3 +69,17 @@ describe('github_repo_analyze owner/repo from the origin remote', () => {
expect(r.repository).toBe('acme/widgets');
});
});
describe('github_repo_analyze never echoes credentials from the origin remote', () => {
it.each([
['https://x-access-token:ghp_SECRET@github.com/ruvnet/ruflo.git', 'https://github.com/ruvnet/ruflo.git'],
['https://oauth2:p@ss@gitlab.com/acme/next.js.git', 'https://gitlab.com/acme/next.js.git'],
['ssh://git@github.com/ruvnet/ruflo.git', 'ssh://github.com/ruvnet/ruflo.git'],
['git@github.com:ruvnet/ruflo.git', 'git@github.com:ruvnet/ruflo.git'],
])('%s is reported as %s', async (remoteUrl, expected) => {
gitWithRemote(remoteUrl);
const r = await analyze();
expect(r.remoteUrl).toBe(expected);
expect(JSON.stringify(r)).not.toMatch(/SECRET|p@ss/);
});
});
@@ -142,6 +142,15 @@ function sanitizeLabels(value: unknown): string[] | null {
return out;
}
/**
* Drop userinfo from a URL-form remote (`https://user:token@host/o/r`) so a
* credential embedded in the origin URL never reaches tool output. scp-form
* remotes (`git@host:o/r`) carry no secret and are returned unchanged.
*/
export function redactRemoteUrl(url: string): string {
return url.replace(/^([a-z][a-z0-9+.-]*:\/\/)[^/]*@/i, '$1');
}
/** Check if gh CLI is available */
function hasGhCli(): boolean {
return run('gh --version') !== null;
@@ -219,7 +228,7 @@ export const githubTools: MCPTool[] = [
repository: repoKey,
branch: repoInfo.branch,
metrics: repoInfo.metrics,
remoteUrl: remoteUrl || null,
remoteUrl: remoteUrl ? redactRemoteUrl(remoteUrl) : null,
lastAnalyzed: repoInfo.lastAnalyzed,
};
}