Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2/**
3 * Read-only access to persisted workflow scripts for the Agents surface's
4 * "{} script" affordance.
5 *
6 * Containment rules (lifted from the reviewed #3650 inspection service):
7 * - the resolved realpath must live under ~/.claude/projects (where the
8 * Claude harness persists workflow scripts) — realpath re-containment
9 * defeats symlink escapes, including a symlinked leaf file;
10 * - only .js leaf files are served;
11 * - reads are size-capped rather than failed, with a truncation marker.
12 *
13 * The client-supplied path is a hint from the workflow's runHandles; it is
14 * never trusted beyond these checks.
15 */
16import * as NodeFSP from "node:fs/promises";
17import * as NodePath from "node:path";
18
19import { OrchestrationGetWorkflowScriptError } from "@t3tools/contracts";
20import * as Effect from "effect/Effect";
21import * as HostProcess from "@t3tools/shared/HostProcess";
22
23const SCRIPT_BYTE_CAP = 256 * 1024;
24
25function scriptsRoot(home: string): string {
26 return NodePath.join(home, ".claude", "projects");
27}
28
29export const readWorkflowScript = Effect.fn("orchestration.readWorkflowScript")(function* (input: {
30 readonly scriptPath: string;
31}) {
32 const requested = input.scriptPath;
33
34 if (!NodePath.isAbsolute(requested) || NodePath.extname(requested) !== ".js") {
35 return yield* new OrchestrationGetWorkflowScriptError({
36 reason: "invalid-path",
37 scriptPath: requested,
38 });
39 }
40
41 const home = yield* HostProcess.HomeDirectory;
42 const root = yield* Effect.tryPromise({
43 try: () => NodeFSP.realpath(scriptsRoot(home)),
44 catch: (cause) =>
45 new OrchestrationGetWorkflowScriptError({
46 reason: "root-unavailable",
47 scriptPath: requested,
48 cause,
49 }),
50 });
51
52 // Realpath the FILE itself (not just its directory): a symlink named
53 // like a script inside a contained directory must not escape.
54 const resolved = yield* Effect.tryPromise({
55 try: () => NodeFSP.realpath(requested),
56 catch: (cause) =>
57 new OrchestrationGetWorkflowScriptError({
58 reason: "not-found",
59 scriptPath: requested,
60 cause,
61 }),
62 });
63
64 if (resolved !== root && !resolved.startsWith(`${root}${NodePath.sep}`)) {
65 return yield* new OrchestrationGetWorkflowScriptError({
66 reason: "outside-root",
67 scriptPath: resolved,
68 });
69 }
70 if (NodePath.extname(resolved) !== ".js") {
71 return yield* new OrchestrationGetWorkflowScriptError({
72 reason: "not-js",
73 scriptPath: resolved,
74 });
75 }
76
77 // TOCTOU-safe read (review finding): open FIRST, then verify what was
78 // actually opened via the file descriptor. Re-checking the path after
79 // open would race against a swap; fstat on the handle cannot. The two
80 // containment checks fail with their own tagged reasons (not manufactured
81 // Errors folded into read-failed); "read-failed" is reserved for genuine
82 // platform failures with the real cause attached.
83 const read = yield* Effect.tryPromise({
84 try: async () => {
85 const handle = await NodeFSP.open(resolved, "r");
86 try {
87 const stat = await handle.stat();
88 if (!stat.isFile()) {
89 return { failure: "not-regular-file" as const };
90 }
91 // The opened inode must be the same one realpath resolved to: a
92 // process swapping the path between realpath and open changes the
93 // inode, which this comparison catches.
94 const pathStat = await NodeFSP.lstat(resolved);
95 if (stat.ino !== pathStat.ino || stat.dev !== pathStat.dev) {
96 return { failure: "changed-during-read" as const };
97 }
98 const truncated = stat.size > SCRIPT_BYTE_CAP;
99 const buffer = Buffer.alloc(Math.min(stat.size, SCRIPT_BYTE_CAP));
100 const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
101 return {
102 contents: buffer.subarray(0, bytesRead).toString("utf8"),
103 truncated,
104 };
105 } finally {
106 await handle.close();
107 }
108 },
109 catch: (cause) =>
110 new OrchestrationGetWorkflowScriptError({
111 reason: "read-failed",
112 scriptPath: resolved,
113 cause,
114 }),
115 });
116 if ("failure" in read) {
117 return yield* new OrchestrationGetWorkflowScriptError({
118 reason: read.failure,
119 scriptPath: resolved,
120 });
121 }
122
123 return {
124 scriptPath: resolved,
125 contents: read.contents,
126 truncated: read.truncated,
127 };
128});