Skip to content

Commit d2239ce

Browse files
committed
fix(fws): contain symlinked restoreFile targets (CodeRabbit security follow-up)
The lexical workspace containment check cannot see through a symlinked ancestor: a link inside the workspace pointing outside it passes the prefix check while the real target resolves elsewhere. When the restore target file currently exists (the destructive case), resolve both the workspace root and the target with fs.realpath and re-check containment before any mutation; resolving both sides keeps a legitimate symlinked workspace root working. Regression: rejects a target that escapes the workspace through a symlinked ancestor (POSIX).
1 parent 178e6f4 commit d2239ce

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎src/services/checkpoints/ShadowCheckpointService.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,23 @@ export abstract class ShadowCheckpointService extends EventEmitter {
444444
throw new Error(`restoreFile target is outside the workspace: ${filePath}`)
445445
}
446446

447+
// The lexical check cannot see through a symlinked ancestor: a link
448+
// inside the workspace pointing outside it passes the prefix check
449+
// while the real target resolves elsewhere. Re-check containment on
450+
// the resolved (real) paths whenever the target file currently
451+
// exists — that is the destructive case. A target that does not exist
452+
// cannot be deleted, and the checkout branch only writes files the
453+
// task-owned shadow repo recorded. Resolving both sides keeps a
454+
// legitimate symlinked workspace root working.
455+
if (await fileExistsAtPath(resolvedTarget)) {
456+
const realWorkspaceRoot = await fs.realpath(this.workspaceDir)
457+
const realRoot = realWorkspaceRoot.endsWith(path.sep) ? realWorkspaceRoot : realWorkspaceRoot + path.sep
458+
const realTarget = await fs.realpath(resolvedTarget)
459+
if (realTarget !== realWorkspaceRoot && !realTarget.startsWith(realRoot)) {
460+
throw new Error(`restoreFile target resolves outside the workspace: ${filePath}`)
461+
}
462+
}
463+
447464
const start = Date.now()
448465
const existed = await this.fileExistsInCommit(commitHash, gitPath)
449466

‎src/services/checkpoints/__tests__/ShadowCheckpointService.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,38 @@ describe.each([[RepoPerTaskCheckpointService, "RepoPerTaskCheckpointService"]])(
260260
expect(await fs.readFile(testFile, "utf-8")).toBe("Ahoy, world!")
261261
})
262262

263+
it("rejects a target that escapes the workspace through a symlinked ancestor", async () => {
264+
// A lexical prefix check passes for a path that goes through a
265+
// symlink inside the workspace pointing outside it; the resolved
266+
// (real) target must be contained as well, or the delete/checkout
267+
// branch would mutate a file the task never owned (CWE-22).
268+
if (process.platform === "win32") {
269+
// Creating symlinks needs elevated privileges on Windows; the
270+
// lexical-containment test above still covers the portable case.
271+
return
272+
}
273+
await fs.writeFile(testFile, "Ahoy, world!")
274+
const commit1 = await service.saveCheckpoint("First checkpoint")
275+
expect(commit1?.commit).toBeTruthy()
276+
277+
const outsideDir = path.join(tmpDir, `outside-${Date.now()}`)
278+
await fs.mkdir(outsideDir, { recursive: true })
279+
const outsideFile = path.join(outsideDir, "sneaky.txt")
280+
await fs.writeFile(outsideFile, "outside")
281+
282+
// A directory link inside the workspace pointing at the outside dir:
283+
// lexically `link/sneaky.txt` is inside the workspace.
284+
await fs.symlink(outsideDir, path.join(service.workspaceDir, "link"), "dir")
285+
286+
await expect(service.restoreFile(commit1!.commit, path.join("link", "sneaky.txt"))).rejects.toThrow(
287+
/outside the workspace/,
288+
)
289+
290+
// The outside file survives: the restore failed before any mutation.
291+
expect(await fileExistsAtPath(outsideFile)).toBe(true)
292+
expect(await fs.readFile(outsideFile, "utf-8")).toBe("outside")
293+
})
294+
263295
it("emits a restore event when a file is restored", async () => {
264296
const restoreListener = vi.fn()
265297
service.on("restore", restoreListener)

0 commit comments

Comments
 (0)