// @effect-diagnostics nodeBuiltinImport:off /** * Contained reads of files the Claude harness persisted for a workflow run. * * Containment rules (lifted from the reviewed #3650 inspection service): * - the resolved realpath must live under the configured Claude projects * directory (where the harness persists workflow scripts and transcripts) — * realpath re-containment defeats symlink escapes, including a symlinked * leaf file; * - only the caller's expected extension is served; * - reads are size-capped rather than failed, with a truncation marker. * * The client-supplied path is a hint from the workflow's runHandles; it is * never trusted beyond these checks. */ import * as NodeFS from "node:fs"; import * as NodeFSP from "node:fs/promises"; import * as NodePath from "node:path"; import { OrchestrationWorkflowFileError } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as HostProcess from "@t3tools/shared/HostProcess"; function workflowFilesRoot(home: string, configDir?: string): string { return NodePath.join(configDir?.trim() || NodePath.join(home, ".claude"), "projects"); } export const readContainedWorkflowFile = Effect.fn("orchestration.readContainedWorkflowFile")( function* (input: { readonly path: string; readonly extension: string; readonly byteCap: number; readonly tail?: boolean; readonly configDir?: string; }) { if (!NodePath.isAbsolute(input.path) || NodePath.extname(input.path) !== input.extension) { return yield* new OrchestrationWorkflowFileError({ reason: "invalid-path", path: input.path, }); } const home = yield* HostProcess.HomeDirectory; const root = yield* Effect.tryPromise({ try: () => NodeFSP.realpath(workflowFilesRoot(home, input.configDir)), catch: (cause) => new OrchestrationWorkflowFileError({ reason: "root-unavailable", path: input.path, cause, }), }); // Realpath the FILE itself (not just its directory): a symlink named // like a transcript inside a contained directory must not escape. const resolved = yield* Effect.tryPromise({ try: () => NodeFSP.realpath(input.path), catch: (cause) => new OrchestrationWorkflowFileError({ reason: "not-found", path: input.path, cause }), }); if (resolved !== root && !resolved.startsWith(`${root}${NodePath.sep}`)) { return yield* new OrchestrationWorkflowFileError({ reason: "outside-root", path: resolved }); } if (NodePath.extname(resolved) !== input.extension) { return yield* new OrchestrationWorkflowFileError({ reason: "wrong-extension", path: resolved, }); } // fd inode vs path inode catches a swapped leaf, not a swapped intermediate // dir; root is 0700 ~/.claude, so that already needs the uid we run as. // Both containment checks fail with their own tagged reason; "read-failed" // is reserved for genuine platform failures with the real cause attached. const read = yield* Effect.tryPromise({ try: async () => { const handle = await NodeFSP.open( resolved, NodeFS.constants.O_RDONLY | (NodeFS.constants.O_NONBLOCK ?? 0), ); try { const stat = await handle.stat(); if (!stat.isFile()) { return { failure: "not-regular-file" as const }; } const pathStat = await NodeFSP.lstat(resolved); if (stat.ino !== pathStat.ino || stat.dev !== pathStat.dev) { return { failure: "changed-during-read" as const }; } const buffer = Buffer.alloc(Math.min(stat.size, input.byteCap + (input.tail ? 1 : 0))); const truncated = stat.size > buffer.length; const offset = input.tail ? stat.size - buffer.length : 0; const { bytesRead } = await handle.read(buffer, 0, buffer.length, offset); return { contents: buffer.subarray(0, bytesRead).toString("utf8"), truncated, }; } finally { await handle.close(); } }, catch: (cause) => new OrchestrationWorkflowFileError({ reason: "read-failed", path: resolved, cause }), }); if ("failure" in read) { return yield* new OrchestrationWorkflowFileError({ reason: read.failure, path: resolved }); } return { path: resolved, contents: read.contents, truncated: read.truncated }; }, );