TypeScript
fix(server): Antigravity file writes cannot escape the workspace through a linked ancestor (#26184)
Julius Marminge82515785bb6e
diff --git a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.tsindex c991487b8d0..4876465a960 100644--- a/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts@@ -260,6 +260,19 @@ describe("AntigravityAdapterV2 client file system", () => {).pipe(Effect.exit);assert.isTrue(Exit.isFailure(linkedDirWrite), "a write under an escaping directory link");assert.isFalse(yield* fileSystem.exists(path.join(outside, "new.txt")));+ const nestedLinkedDirWrite = yield* writeTextFile(+ {+ sessionId: "mock-session-1",+ path: path.join(workspace, "linked-dir", "a", "b", "file.txt"),+ content: "planted",+ },+ context("fs/write_text_file"),+ ).pipe(Effect.exit);+ assert.isTrue(+ Exit.isFailure(nestedLinkedDirWrite),+ "a write to missing directories under an escaping directory link",+ );+ assert.isFalse(yield* fileSystem.exists(path.join(outside, "a")));const insideLink = path.join(workspace, "inside-link.ts");yield* fileSystem.symlink(insidePath, insideLink);const viaInsideLink = yield* readTextFile(diff --git a/apps/server/src/provider/acp/AntigravityClientFiles.ts b/apps/server/src/provider/acp/AntigravityClientFiles.tsindex ebbc234e149..4efbc28a80c 100644--- a/apps/server/src/provider/acp/AntigravityClientFiles.ts+++ b/apps/server/src/provider/acp/AntigravityClientFiles.ts@@ -1,4 +1,5 @@import * as Effect from "effect/Effect";+import * as Option from "effect/Option";import type * as FileSystem from "effect/FileSystem";import type * as Path from "effect/Path";import * as EffectAcpErrors from "effect-acp/errors";@@ -25,23 +26,27 @@ const resolveClientFilePath = Effect.fn("AntigravityClientFiles.resolveClientFilconst outside = EffectAcpErrors.AcpRequestError.invalidParams(`Path '${input.requestPath}' is outside the session workspace.`,);- const real = yield* input.fileSystem.realPath(resolved).pipe(- Effect.catch(() =>- Effect.gen(function* () {- // Fall back to the parent only for missing files, never dangling or unreadable links.- const entryExists = yield* input.fileSystem.readLink(resolved).pipe(- Effect.as(true),- Effect.catch(() => input.fileSystem.exists(resolved)),- Effect.orElseSucceed(() => true),- );- if (entryExists) return yield* outside;- const parent = yield* input.fileSystem- .realPath(path.dirname(resolved))- .pipe(Effect.orElseSucceed(() => path.dirname(resolved)));- return path.join(parent, path.basename(resolved));- }),- ),- );+ // Canonicalize the deepest existing ancestor, so a link above missing directories cannot escape.+ let candidate = resolved;+ const missingSuffix: Array<string> = [];+ let real: string;+ while (true) {+ const canonical = yield* input.fileSystem.realPath(candidate).pipe(Effect.option);+ if (Option.isSome(canonical)) {+ real = path.join(canonical.value, ...missingSuffix);+ break;+ }+ // Walk up only past missing entries, never dangling or unreadable links.+ const entryExists = yield* input.fileSystem.readLink(candidate).pipe(+ Effect.as(true),+ Effect.catch(() => input.fileSystem.exists(candidate)),+ Effect.orElseSucceed(() => true),+ );+ const parent = path.dirname(candidate);+ if (entryExists || parent === candidate) return yield* outside;+ missingSuffix.unshift(path.basename(candidate));+ candidate = parent;+ }const roots = yield* Effect.forEach(input.allowedRoots, (root) =>input.fileSystem.realPath(root).pipe(Effect.orElseSucceed(() => root)),);