Skip to content

Commit e6284ab

Browse files
fix(server): refuse to discard a destination the clone did not write, and close redaction gaps
discardClone now only empties a directory that has git's .git inside it; a destination that gained unrelated files after a failed clone is left alone and Retry reports it. URL redaction also strips query tokens and handles userinfo containing '@'. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 7ae7d06 commit e6284ab

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ it.effect("reports clone progress from git's stderr and keeps its error text on
213213
"Receiving objects: 40% (4/10), 1.00 MiB | 2.00 MiB/s",
214214
"Receiving objects: 100% (10/10), 2.50 MiB | 2.00 MiB/s, done.",
215215
"fatal: early EOF",
216-
"fatal: unable to access 'https://user:s3cret@github.com/octocat/t3code.git/': could not resolve host",
216+
"fatal: unable to access 'https://user:s3c@ret@github.com/octocat/t3code.git/': could not resolve host",
217217
];
218218
const error = yield* Effect.gen(function* () {
219219
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
@@ -290,6 +290,52 @@ it.effect("strips embedded credentials from the remote URL it reports", () =>
290290
}).pipe(Effect.provide(NodeServices.layer)),
291291
);
292292

293+
it.effect("discards only a directory git wrote to", () =>
294+
Effect.gen(function* () {
295+
const fs = yield* FileSystem.FileSystem;
296+
const path = yield* Path.Path;
297+
const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-source-control-discard-" });
298+
const partial = path.join(parent, "partial");
299+
yield* fs.makeDirectory(path.join(partial, ".git"), { recursive: true });
300+
yield* fs.writeFileString(path.join(partial, "README.md"), "half");
301+
const foreign = path.join(parent, "foreign");
302+
yield* fs.makeDirectory(foreign);
303+
yield* fs.writeFileString(path.join(foreign, "notes.txt"), "mine");
304+
305+
yield* Effect.gen(function* () {
306+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
307+
yield* service.discardClone(partial);
308+
const error = yield* Effect.flip(service.discardClone(foreign));
309+
assert.include(error.detail, "not from the clone");
310+
}).pipe(Effect.provide(makeLayer({})));
311+
312+
// The partial clone is emptied but its directory (the workspace root) stays.
313+
assert.deepStrictEqual(yield* fs.readDirectory(partial), []);
314+
assert.deepStrictEqual(yield* fs.readDirectory(foreign), ["notes.txt"]);
315+
}).pipe(Effect.provide(NodeServices.layer)),
316+
);
317+
318+
it.effect("redacts query tokens and userinfo containing '@' from reported URLs", () =>
319+
Effect.gen(function* () {
320+
const fs = yield* FileSystem.FileSystem;
321+
const path = yield* Path.Path;
322+
const parent = yield* fs.makeTempDirectoryScoped({ prefix: "t3-source-control-redact2-" });
323+
yield* Effect.gen(function* () {
324+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
325+
const query = yield* service.prepareClone({
326+
remoteUrl: "https://github.com/octocat/t3code.git?access_token=s3cret",
327+
destinationPath: path.join(parent, "a"),
328+
});
329+
assert.equal(query.remoteUrl, "https://github.com/octocat/t3code.git");
330+
const nested = yield* service.prepareClone({
331+
remoteUrl: "https://user:pa@[email protected]/octocat/t3code.git",
332+
destinationPath: path.join(parent, "b"),
333+
});
334+
assert.equal(nested.remoteUrl, "https://github.com/octocat/t3code.git");
335+
}).pipe(Effect.provide(makeLayer({})));
336+
}).pipe(Effect.provide(NodeServices.layer)),
337+
);
338+
293339
it.effect("preserves destination probe failures instead of treating them as missing paths", () => {
294340
const fileSystemCause = PlatformError.systemError({
295341
_tag: "PermissionDenied",

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,22 @@ function toRepositoryInfo(
118118
function redactRemoteUrl(remoteUrl: string): string {
119119
try {
120120
const url = new URL(remoteUrl);
121-
if (url.username.length === 0 && url.password.length === 0) return remoteUrl;
121+
// Clone URLs have no legitimate query; when one is present it is a token.
122+
if (url.username.length === 0 && url.password.length === 0 && url.search.length === 0) {
123+
return remoteUrl;
124+
}
122125
url.username = "";
123126
url.password = "";
127+
url.search = "";
124128
return url.toString();
125129
} catch {
126130
return remoteUrl;
127131
}
128132
}
129133

130-
const URL_WITH_USERINFO = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/@]+@/gi;
134+
// Userinfo may itself contain `@`; everything up to the last one before the
135+
// host boundary goes.
136+
const URL_WITH_USERINFO = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/]+@/gi;
131137

132138
/** Drops `user:token@` from any URL embedded in free text. */
133139
function redactUrlCredentials(text: string): string {
@@ -334,6 +340,19 @@ export const make = Effect.gen(function* () {
334340
destinationPath: string,
335341
) {
336342
const normalized = yield* normalizeDestinationPath(destinationPath);
343+
// Only what git left behind may go. The destination was empty when the
344+
// clone started, so anything without a `.git` inside was put there by
345+
// someone else since; refuse rather than delete their files.
346+
const entries = yield* fileSystem
347+
.readDirectory(normalized)
348+
.pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));
349+
if (entries.length > 0 && !entries.includes(".git")) {
350+
return yield* new SourceControlRepositoryError({
351+
operation: "discardClone",
352+
provider: "unknown",
353+
detail: "Destination path contains files that are not from the clone.",
354+
});
355+
}
337356
// The directory itself is the project's workspace root and must stay;
338357
// only git's partial contents go. An interrupted git may still be closing
339358
// files, so removal retries briefly.

0 commit comments

Comments
 (0)