diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.ts index 74e45710d4..7ec461f29e 100644 --- a/apps/server/src/mcp/McpHttpServer.test.ts +++ b/apps/server/src/mcp/McpHttpServer.test.ts @@ -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", diff --git a/apps/server/src/mcp/toolkits/preview/handlers.ts b/apps/server/src/mcp/toolkits/preview/handlers.ts index a79ddd8429..181c324093 100644 --- a/apps/server/src/mcp/toolkits/preview/handlers.ts +++ b/apps/server/src/mcp/toolkits/preview/handlers.ts @@ -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({