fix: worktree exclusion checks realpath and folds case on Windows
A symlink whose spelling looks harmless can resolve into the worktrees directory, so the filter now also runs against the realpath. Windows comparisons case fold, using the injectable HostProcessPlatform reference. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
1a1998e1b1
commit
a87e825bc5
2 files changed
+56
-6
No files matched your search
@@ -320,6 +320,33 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("excludes sandboxes reached through a symlink into the worktrees dir", () =>
|
||||
Effect.gen(function* () {
|
||||
const path = yield* Path.Path;
|
||||
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
|
||||
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
|
||||
const configBaseDir = yield* makeTempDir("t3code-scanner-base-");
|
||||
const linkParent = yield* makeTempDir("t3code-scanner-links-");
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
|
||||
// The recorded cwd is a symlink whose own spelling looks harmless;
|
||||
// only its realpath reveals the managed sandbox.
|
||||
const worktreeCwd = path.join(configBaseDir, "worktrees", "t3code", "wt-3");
|
||||
yield* fileSystem.makeDirectory(worktreeCwd, { recursive: true });
|
||||
const symlinkCwd = path.join(linkParent, "innocent-project");
|
||||
yield* fileSystem.symlink(worktreeCwd, symlinkCwd);
|
||||
yield* writeTranscript({
|
||||
filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),
|
||||
contents: claudeSessionLine(symlinkCwd),
|
||||
mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),
|
||||
});
|
||||
|
||||
const result = yield* runScan({ claudeHomePath, codexHomePath, configBaseDir });
|
||||
|
||||
expect(result.candidates).toEqual([]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("skips malformed transcripts without failing the scan", () =>
|
||||
Effect.gen(function* () {
|
||||
const path = yield* Path.Path;
|
||||
|
||||
@@ -25,6 +25,8 @@ import * as Option from "effect/Option";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
|
||||
|
||||
import * as ServerConfig from "../config.ts";
|
||||
import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
|
||||
import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
|
||||
@@ -87,12 +89,25 @@ const decoder = new TextDecoder();
|
||||
* sandboxes as projects is never right. Matches this server's configured
|
||||
* worktrees directory plus the conventional `.t3/worktrees` layout, which
|
||||
* also catches sandboxes from other T3 homes on the same machine. Separators
|
||||
* are normalized so the prefix match holds on Windows too.
|
||||
* are normalized (and, on Windows, case folded) so the prefix match holds
|
||||
* there too. Callers check both the recorded spelling and its realpath so a
|
||||
* symlink into the worktrees directory cannot bypass the filter.
|
||||
*/
|
||||
function isT3ManagedWorktree(resolvedPath: string, worktreesDir: string): boolean {
|
||||
const normalized = `${resolvedPath.replaceAll("\\", "/")}/`;
|
||||
const worktreesPrefix = `${worktreesDir.replaceAll("\\", "/")}/`;
|
||||
return normalized.startsWith(worktreesPrefix) || normalized.includes("/.t3/worktrees/");
|
||||
function normalizeForWorktreeMatch(value: string, caseFold: boolean): string {
|
||||
const normalized = `${value.replaceAll("\\", "/")}/`;
|
||||
return caseFold ? normalized.toLowerCase() : normalized;
|
||||
}
|
||||
|
||||
function isT3ManagedWorktree(
|
||||
candidatePath: string,
|
||||
worktreesDir: string,
|
||||
caseFold: boolean,
|
||||
): boolean {
|
||||
const normalized = normalizeForWorktreeMatch(candidatePath, caseFold);
|
||||
return (
|
||||
normalized.startsWith(normalizeForWorktreeMatch(worktreesDir, caseFold)) ||
|
||||
normalized.includes("/.t3/worktrees/")
|
||||
);
|
||||
}
|
||||
|
||||
/** Extract `cwd` from a session-meta record, tolerating the shapes each CLI writes. */
|
||||
@@ -127,6 +142,9 @@ export const make = Effect.gen(function* () {
|
||||
const serverSettings = yield* ServerSettings.ServerSettingsService;
|
||||
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
|
||||
const worktreesDir = path.resolve(serverConfig.worktreesDir);
|
||||
// Windows filesystems are case-insensitive, so path prefix checks there
|
||||
// must case fold.
|
||||
const foldWorktreeCase = (yield* HostProcessPlatform) === "win32";
|
||||
|
||||
const listDirectory = (directory: string) =>
|
||||
fileSystem.readDirectory(directory).pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));
|
||||
@@ -335,7 +353,7 @@ export const make = Effect.gen(function* () {
|
||||
|
||||
for (const candidate of raw) {
|
||||
const resolved = path.resolve(expandHomePath(candidate.cwd.trim()));
|
||||
if (isT3ManagedWorktree(resolved, worktreesDir)) continue;
|
||||
if (isT3ManagedWorktree(resolved, worktreesDir, foldWorktreeCase)) continue;
|
||||
let key = realPathKeys.get(resolved);
|
||||
if (key === undefined) {
|
||||
const stats = yield* statOption(resolved);
|
||||
@@ -345,6 +363,11 @@ export const make = Effect.gen(function* () {
|
||||
continue;
|
||||
}
|
||||
key = yield* fileSystem.realPath(resolved).pipe(Effect.orElseSucceed(() => resolved));
|
||||
// A symlink can point into the worktrees directory even when its own
|
||||
// spelling doesn't; check again with links resolved.
|
||||
if (isT3ManagedWorktree(key, worktreesDir, foldWorktreeCase)) {
|
||||
key = "";
|
||||
}
|
||||
realPathKeys.set(resolved, key);
|
||||
}
|
||||
if (key === "") continue;
|
||||
|
||||
Reference in new issue
Block a user