Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

workflowScriptQuery.ts

127 lines4.3 KB

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 NodeOS from "node:os";
18import * as NodePath from "node:path";
19​
20import { OrchestrationGetWorkflowScriptError } from "@t3tools/contracts";
21import * as Effect from "effect/Effect";
22​
23const SCRIPT_BYTE_CAP = 256 * 1024;
24​
25function scriptsRoot(): string {
26 return NodePath.join(NodeOS.homedir(), ".claude", "projects");
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 root = yield* Effect.tryPromise({
42 try: () => NodeFSP.realpath(scriptsRoot()),
43 catch: (cause) =>
44 new OrchestrationGetWorkflowScriptError({
45 reason: "root-unavailable",
46 scriptPath: requested,
47 cause,
48 }),
49 });
50​
51 // Realpath the FILE itself (not just its directory): a symlink named
52 // like a script inside a contained directory must not escape.
53 const resolved = yield* Effect.tryPromise({
54 try: () => NodeFSP.realpath(requested),
55 catch: (cause) =>
56 new OrchestrationGetWorkflowScriptError({
57 reason: "not-found",
58 scriptPath: requested,
59 cause,
60 }),
61 });
62​
63 if (resolved !== root && !resolved.startsWith(`${root}${NodePath.sep}`)) {
64 return yield* new OrchestrationGetWorkflowScriptError({
65 reason: "outside-root",
66 scriptPath: resolved,
67 });
68 }
69 if (NodePath.extname(resolved) !== ".js") {
70 return yield* new OrchestrationGetWorkflowScriptError({
71 reason: "not-js",
72 scriptPath: resolved,
73 });
74 }
75​
76 // TOCTOU-safe read (review finding): open FIRST, then verify what was
77 // actually opened via the file descriptor. Re-checking the path after
78 // open would race against a swap; fstat on the handle cannot. The two
79 // containment checks fail with their own tagged reasons (not manufactured
80 // Errors folded into read-failed); "read-failed" is reserved for genuine
81 // platform failures with the real cause attached.
82 const read = yield* Effect.tryPromise({
83 try: async () => {
84 const handle = await NodeFSP.open(resolved, "r");
85 try {
86 const stat = await handle.stat();
87 if (!stat.isFile()) {
88 return { failure: "not-regular-file" as const };
89 }
90 // The opened inode must be the same one realpath resolved to: a
91 // process swapping the path between realpath and open changes the
92 // inode, which this comparison catches.
93 const pathStat = await NodeFSP.lstat(resolved);
94 if (stat.ino !== pathStat.ino || stat.dev !== pathStat.dev) {
95 return { failure: "changed-during-read" as const };
96 }
97 const truncated = stat.size > SCRIPT_BYTE_CAP;
98 const buffer = Buffer.alloc(Math.min(stat.size, SCRIPT_BYTE_CAP));
99 const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
100 return {
101 contents: buffer.subarray(0, bytesRead).toString("utf8"),
102 truncated,
103 };
104 } finally {
105 await handle.close();
106 }
107 },
108 catch: (cause) =>
109 new OrchestrationGetWorkflowScriptError({
110 reason: "read-failed",
111 scriptPath: resolved,
112 cause,
113 }),
114 });
115 if ("failure" in read) {
116 return yield* new OrchestrationGetWorkflowScriptError({
117 reason: read.failure,
118 scriptPath: resolved,
119 });
120 }
121​
122 return {
123 scriptPath: resolved,
124 contents: read.contents,
125 truncated: read.truncated,
126 };
127});