Reject dangling destination symlinks and fix ..-prefix containment check

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Fable 5 authored and Gabriel De Andrade committed 2026-07-29 16:58:57 -04:00
1 parent ae0c21c3f1
commit f3c19bfe7c
2 files changed
+27 -6

No files matched your search

+17
View File
@@ -401,6 +401,23 @@ it.effect("writes snapshot screenshots into the workspace when savePath is set",
expect(fileSymlink.isError).toBe(true);
expect(NodeFS.readFileSync(victimPath, "utf8")).toBe("original");
// A dangling destination symlink must not be followed into existence.
const danglingTarget = NodePath.join(outsideDir, "not-yet-created.png");
NodeFS.symlinkSync(
danglingTarget,
NodePath.join(testWorkspaceRoot, "evidence", "dangling.png"),
);
const danglingSymlink = yield* callSnapshot("evidence/dangling.png");
expect(danglingSymlink.isError).toBe(true);
expect(NodeFS.existsSync(danglingTarget)).toBe(false);
// A directory whose name merely starts with ".." is inside the root.
const dotDotName = yield* callSnapshot("..screenshots/ok.png");
expect(dotDotName.isError).toBe(false);
expect(
NodeFS.readFileSync(NodePath.join(testWorkspaceRoot, "..screenshots", "ok.png"), "utf8"),
).toBe("png-bytes");
const bothDestinations = yield* server
.callTool({
name: "preview_snapshot",
@@ -172,7 +172,7 @@ const saveSnapshotScreenshot = Effect.fn("PreviewToolkit.saveSnapshotScreenshot"
);
const isOutsideRoot = (canonicalRoot: string, canonical: string) => {
const relative = path.relative(canonicalRoot, canonical);
return relative.startsWith("..") || path.isAbsolute(relative);
return relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative);
};
const canonicalRoot = yield* fileSystem
@@ -207,12 +207,16 @@ const saveSnapshotScreenshot = Effect.fn("PreviewToolkit.saveSnapshotScreenshot"
return yield* fail("savePath must stay inside the workspace root");
}
// If the destination already exists it may itself be a symlink; writing
// would follow it, so its canonical location must also stay inside.
// Refuse to write through a destination that is itself a symlink: the
// write would follow it, and a dangling link (realPath reports NotFound)
// could silently create a file outside the workspace at its target.
const destination = path.join(canonicalParent, path.basename(resolved.absolutePath));
const canonicalDestination = yield* realPathOrNull(destination);
if (canonicalDestination !== null && isOutsideRoot(canonicalRoot, canonicalDestination)) {
return yield* fail("savePath must stay inside the workspace root");
const destinationIsSymlink = yield* fileSystem.readLink(destination).pipe(
Effect.as(true),
Effect.orElseSucceed(() => false),
);
if (destinationIsSymlink) {
return yield* fail("savePath must not be an existing symlink");
}
yield* writeScreenshotFile({