Showing the changes to apps/server/src/orchestration/workflowScriptQuery.test.ts (1 of 46 files). Show all
Unified · Split
| @@ -0,0 +1,75 @@ | ||
| 1 | +// @effect-diagnostics nodeBuiltinImport:off | |
| 2 | +import * as NodeFS from "node:fs"; | |
| 3 | +import * as NodeOS from "node:os"; | |
| 4 | +import * as NodePath from "node:path"; | |
| 5 | +import { it as effectIt } from "@effect/vitest"; | |
| 6 | +import * as Effect from "effect/Effect"; | |
| 7 | +import { afterAll, assert, describe } from "vite-plus/test"; | |
| 8 | +import { readWorkflowScript } from "./workflowScriptQuery.ts"; | |
| 9 | + | |
| 10 | +const root = NodePath.join(NodeOS.homedir(), ".claude", "projects", "__wf_script_test__"); | |
| 11 | +NodeFS.mkdirSync(root, { recursive: true }); | |
| 12 | +const scriptPath = NodePath.join(root, "run.js"); | |
| 13 | +NodeFS.writeFileSync(scriptPath, "export const meta = {};\n"); | |
| 14 | +const outside = NodePath.join(NodeOS.tmpdir(), "wf-outside.js"); | |
| 15 | +NodeFS.writeFileSync(outside, "evil\n"); | |
| 16 | +const link = NodePath.join(root, "sneaky.js"); | |
| 17 | +try { | |
| 18 | + NodeFS.symlinkSync(outside, link); | |
| 19 | +} catch (error) { | |
| 20 | + // Tolerate only "already exists" from a prior run — any other failure | |
| 21 | + // (EPERM etc.) must fail setup, or the escape test below would pass | |
| 22 | + // vacuously on "not-found" without testing containment. | |
| 23 | + if ((error as NodeJS.ErrnoException).code !== "EEXIST") { | |
| 24 | + throw error; | |
| 25 | + } | |
| 26 | +} | |
| 27 | +if (!NodeFS.lstatSync(link).isSymbolicLink()) { | |
| 28 | + throw new Error("test setup: sneaky.js must be a symlink"); | |
| 29 | +} | |
| 30 | + | |
| 31 | +afterAll(() => { | |
| 32 | + NodeFS.rmSync(root, { recursive: true, force: true }); | |
| 33 | + NodeFS.rmSync(outside, { force: true }); | |
| 34 | +}); | |
| 35 | + | |
| 36 | +describe("readWorkflowScript containment", () => { | |
| 37 | + effectIt.effect("serves a real script under the projects root", () => | |
| 38 | + Effect.gen(function* () { | |
| 39 | + const result = yield* readWorkflowScript({ scriptPath }); | |
| 40 | + assert.include(result.contents, "export const meta"); | |
| 41 | + assert.equal(result.truncated, false); | |
| 42 | + }), | |
| 43 | + ); | |
| 44 | + | |
| 45 | + effectIt.effect("rejects relative and non-js paths", () => | |
| 46 | + Effect.gen(function* () { | |
| 47 | + const relative = yield* Effect.exit(readWorkflowScript({ scriptPath: "run.js" })); | |
| 48 | + assert.equal(relative._tag, "Failure"); | |
| 49 | + const nonJs = yield* Effect.exit( | |
| 50 | + readWorkflowScript({ scriptPath: scriptPath.replace(".js", ".ts") }), | |
| 51 | + ); | |
| 52 | + assert.equal(nonJs._tag, "Failure"); | |
| 53 | + }), | |
| 54 | + ); | |
| 55 | + | |
| 56 | + effectIt.effect("rejects paths outside the root and symlink escapes", () => | |
| 57 | + Effect.gen(function* () { | |
| 58 | + const escaped = yield* Effect.exit(readWorkflowScript({ scriptPath: outside })); | |
| 59 | + assert.equal(escaped._tag, "Failure"); | |
| 60 | + // A symlink INSIDE the root pointing outside must fail specifically on | |
| 61 | + // realpath re-containment — a "not-found" would mean the link was | |
| 62 | + // never exercised and the assertion proves nothing. | |
| 63 | + const sneaky = yield* Effect.exit( | |
| 64 | + readWorkflowScript({ scriptPath: link }).pipe( | |
| 65 | + Effect.flip, | |
| 66 | + Effect.map((error) => error.reason), | |
| 67 | + ), | |
| 68 | + ); | |
| 69 | + assert.equal(sneaky._tag, "Success"); | |
| 70 | + if (sneaky._tag === "Success") { | |
| 71 | + assert.equal(sneaky.value, "outside-root"); | |
| 72 | + } | |
| 73 | + }), | |
| 74 | + ); | |
| 75 | +}); | |