Skip to content

Commit 1cc9d41

Browse files
only21milclaudemaria-rcks
authored
fix(server): clone errors no longer show a token from the URL's query (pingdotgg#24698)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 4afdaba commit 1cc9d41

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎apps/server/src/sourceControl/SourceControlRepositoryService.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,62 @@ it.effect.each([
383383
}),
384384
);
385385

386+
it.effect.each([
387+
["a plain token", "s3cret"],
388+
["a token with an apostrophe", "s3'cret"],
389+
["a token with a double quote", 's3"cret'],
390+
["a host-only token with an at sign", "abc@secret"],
391+
["an unquoted token with an apostrophe", "s3'cret", true],
392+
["an unquoted token with a double quote", 's3"cret', true],
393+
["an unquoted URL with an apostrophe in its path", "path-token", true],
394+
] as const)("keeps %s in the query out of a failed clone's error text", ([, token, unquoted]) =>
395+
Effect.gen(function* () {
396+
const remote =
397+
token === "abc@secret"
398+
? "https://github.com"
399+
: token === "path-token"
400+
? "https://github.com/octocat/t3code's.git"
401+
: "https://github.com/octocat/t3code.git";
402+
const error = yield* Effect.flip(
403+
cloneInTemporaryDirectory(
404+
{
405+
execute: (input) =>
406+
Effect.gen(function* () {
407+
const cloneUrl = input.args.at(-2)!;
408+
// Git treats @ in a pathless URL's query as userinfo, losing the query delimiter.
409+
const diagnosticUrl =
410+
cloneUrl === "https://github.com?access_token=abc@secret"
411+
? "https://secret"
412+
: cloneUrl;
413+
yield* (
414+
input.progress?.onStderrLine?.(
415+
unquoted
416+
? `fatal: ${diagnosticUrl}/info/refs not valid: is this a git repository?`
417+
: `fatal: unable to access '${diagnosticUrl}/': Could not resolve host: github.com`,
418+
) ?? Effect.void
419+
);
420+
return yield* new GitCommandError({
421+
operation: input.operation,
422+
command: "git",
423+
cwd: input.cwd,
424+
detail: "Git command exited with a non-zero status.",
425+
exitCode: 128,
426+
});
427+
}),
428+
},
429+
{ remoteUrl: `${remote}?access_token=${token}` },
430+
),
431+
);
432+
433+
assert.strictEqual(
434+
error.detail,
435+
unquoted
436+
? `fatal: ${remote} not valid: is this a git repository?`
437+
: `fatal: unable to access '${remote}${token === "abc@secret" ? "/" : ""}': Could not resolve host: github.com`,
438+
);
439+
}),
440+
);
441+
386442
it.effect("strips embedded credentials from the remote URL it reports", () =>
387443
Effect.gen(function* () {
388444
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/sourceControl/SourceControlRepositoryService.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function toRepositoryInfo(
110110
};
111111
}
112112

113-
// Redact credentials from client clone snapshots; git still receives the original URL.
113+
// Redact credentials from client clone snapshots; git still receives the credentials.
114114
function redactRemoteUrl(remoteUrl: string): string {
115115
try {
116116
const url = new URL(remoteUrl);
@@ -128,10 +128,16 @@ function redactRemoteUrl(remoteUrl: string): string {
128128
}
129129

130130
// Userinfo can contain @; remove through the last one before the host boundary.
131-
const URL_WITH_USERINFO = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/]+@/gi;
131+
const URL_WITH_USERINFO = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#]+@/gi;
132+
// Git leaves quotes inside query tokens unescaped; consume through the closing quote.
133+
const QUOTED_URL_QUERY = /(['"])([a-z][a-z0-9+.-]*:\/\/[^\s?]*)\?.*\1(?=[\s:]|$)/gi;
134+
const URL_QUERY = /\b([a-z][a-z0-9+.-]*:\/\/[^\s?]*)\?[^\s]*/gi;
132135

133136
function redactUrlCredentials(text: string): string {
134-
return text.replace(URL_WITH_USERINFO, "$1");
137+
return text
138+
.replace(QUOTED_URL_QUERY, "$1$2$1")
139+
.replace(URL_QUERY, "$1")
140+
.replace(URL_WITH_USERINFO, "$1");
135141
}
136142

137143
// The remote already exists after push failure; advise fixing access and pushing.
@@ -340,11 +346,13 @@ export const make = Effect.gen(function* () {
340346
if (stderrTail.length > 4) stderrTail.shift();
341347
});
342348
};
349+
// Git misreads @ in a pathless URL's query as userinfo and echoes its suffix as the host.
350+
const normalizedCloneUrl = cloneUrl.replace(/^([a-z][a-z0-9+.-]*:\/\/[^/?#]+)\?/i, "$1/?");
343351
yield* git
344352
.execute({
345353
operation: "SourceControlRepositoryService.cloneRepository",
346354
cwd: path.dirname(destinationPath),
347-
args: ["clone", "--progress", "--", cloneUrl, path.basename(destinationPath)],
355+
args: ["clone", "--progress", "--", normalizedCloneUrl, path.basename(destinationPath)],
348356
timeoutMs: options?.timeoutMs === undefined ? CLONE_TIMEOUT_MS : options.timeoutMs,
349357
// Buffered output is unused and progress can be huge; cap it while retaining line callbacks.
350358
maxOutputBytes: 256 * 1024,

0 commit comments

Comments
 (0)