From e65b3f08a59d6bd33a699045821ae590d6eea197 Mon Sep 17 00:00:00 2001 From: Ryan Bahadori Date: Fri, 20 Nov 2026 10:30:20 -0800 Subject: [PATCH] fix(server): reject provider task ids that would escape the session directory A task_started event's session and task ids become path segments under the Claude temp root. path.join normalizes '..', so a malformed or spoofed id could read a file outside the session's tasks directory and stream it to the client as command output. Reject ids that are not plain tokens before the tailer starts; the tool result still carries the final read. Reported in #26489. --- .../Adapters/ClaudeTaskOutputTail.test.ts | 45 +++++++++++++++++++ .../Adapters/ClaudeTaskOutputTail.ts | 5 +++ 2 files changed, 50 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts index 51c9a7848f10..af05a7e3aa9b 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts @@ -101,6 +101,51 @@ describe("tailClaudeTaskOutput", () => { ).pipe(Effect.provide(NodeServices.layer)), ); + it.live("does not follow a task id that would leave the session directory", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { root, tasks } = yield* makeTaskFile; + // Claude's task ids are tokens, so a traversal id reaches this file instead of a task file. + const escaped = path.join(tasks, "..", "leak.output"); + yield* fileSystem.makeDirectory(path.dirname(escaped), { recursive: true }); + yield* fileSystem.writeFileString(escaped, "secret\n"); + const chunks = yield* Queue.unbounded(); + const fiber = yield* tailClaudeTaskOutput({ + root, + sessionId: SESSION_ID, + taskId: "../leak", + stop: yield* Deferred.make(), + onChunk: (chunk) => Queue.offer(chunks, chunk), + }).pipe(Effect.forkScoped); + yield* Effect.sleep("200 millis"); + assert.equal(yield* Queue.size(chunks), 0); + yield* Fiber.interrupt(fiber); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + it.live("does not follow a session id that would leave the project directory", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { root } = yield* makeTaskFile; + const escaped = path.join(root, SESSION_ID, "tasks", "leak.output"); + yield* fileSystem.makeDirectory(path.dirname(escaped), { recursive: true }); + yield* fileSystem.writeFileString(escaped, "secret\n"); + const chunks = yield* Queue.unbounded(); + const fiber = yield* tailClaudeTaskOutput({ + root, + sessionId: `../${SESSION_ID}`, + taskId: "leak", + stop: yield* Deferred.make(), + onChunk: (chunk) => Queue.offer(chunks, chunk), + }).pipe(Effect.forkScoped); + yield* Effect.sleep("200 millis"); + assert.equal(yield* Queue.size(chunks), 0); + yield* Fiber.interrupt(fiber); + }).pipe(Effect.provide(NodeServices.layer)), + ); + it.effect("reads what was written last when the tool result stops it", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts index 7eac250d7afd..dceefeb38df0 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts @@ -13,6 +13,9 @@ const MAX_LOCATE_ATTEMPTS = 40; /** Skip ahead of fast writers; only the tail is displayed. */ const MAX_READ_BYTES = 256 * 1024; +/** Session and task ids arrive from the provider stream and become path segments below. */ +const SAFE_TASK_OUTPUT_ID = /^[A-Za-z0-9_-]+$/; + /** Claude Code's per-user temp root, or none where it has no uid (Windows). */ export function claudeTaskOutputRoot( environment: Readonly> | undefined, @@ -65,6 +68,8 @@ export const tailClaudeTaskOutput = Effect.fn("ClaudeTaskOutputTail.tail")(funct readonly stop: Deferred.Deferred; readonly onChunk: (chunk: string) => Effect.Effect; }) { + // A rejected id drops live output only; the tool result still carries the final read. + if (!SAFE_TASK_OUTPUT_ID.test(input.sessionId) || !SAFE_TASK_OUTPUT_ID.test(input.taskId)) return; const fileSystem = yield* FileSystem.FileSystem; const pause = Deferred.await(input.stop).pipe( Effect.as(true),