From 5953c0a9cb02fa7901ce4eb727d5a0f82bb42645 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 29 Nov 2026 09:50:15 +0100 Subject: [PATCH 1/7] test(server): cover a regular file in PATH Co-authored-by: GPT 6.2 Sol --- .../src/process/NonBlockingSpawner.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/apps/server/src/process/NonBlockingSpawner.test.ts b/apps/server/src/process/NonBlockingSpawner.test.ts index 81563cbab70dc..349f138f39d95 100644 --- a/apps/server/src/process/NonBlockingSpawner.test.ts +++ b/apps/server/src/process/NonBlockingSpawner.test.ts @@ -11,6 +11,7 @@ import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; +import * as Redacted from "effect/Redacted"; import * as ChildProcess from "effect/process/ChildProcess"; import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; @@ -29,6 +30,11 @@ const outcome = (command: ChildProcess.Command) => onSuccess: (output) => ({ output }), onFailure: (error) => ({ reason: error.reason._tag, message: error.message }), }), + Effect.catchDefect((defect) => + Effect.succeed({ + defect: defect instanceof Error ? { ...defect, message: defect.message } : defect, + }), + ), ); describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpawner", () => { @@ -81,6 +87,20 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpa ChildProcess.pipeTo(ChildProcess.make("tail", ["-n", "1"])), ), "a missing command": ChildProcess.make("t3code-missing-command", ["--version"]), + "a missing command with a regular file in PATH": ChildProcess.make("tool", [], { + env: { PATH: `${root}/fresh/tool:${root}/target` }, + }), + "a missing command with a regular file last in PATH": ChildProcess.make("tool", [], { + env: { PATH: `${root}/target:${root}/fresh/tool` }, + }), + "an explicit path through a regular file": ChildProcess.make(`${root}/fresh/tool/x`, []), + "a NUL argument takes precedence over ENOTDIR": ChildProcess.make(`${root}/fresh/tool/x`, [ + "invalid\0argument", + ]), + "a redacted NUL argument takes precedence over ENOTDIR": ChildProcess.make( + `${root}/fresh/tool/x`, + [Redacted.make("invalid\0argument")], + ), "a missing command in a cwd": ChildProcess.make("t3code-missing-command", [], { cwd: root, }), From a7592e9c57d7dc43eddabb5ccba4e84637c26342 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 29 Nov 2026 09:50:54 +0100 Subject: [PATCH 2/7] fix(server): skip regular files when searching PATH on Linux Co-authored-by: GPT 6.2 Sol --- apps/server/src/process/NonBlockingSpawner.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/server/src/process/NonBlockingSpawner.ts b/apps/server/src/process/NonBlockingSpawner.ts index 0807b287cb9cc..b9aa2c71db4f5 100644 --- a/apps/server/src/process/NonBlockingSpawner.ts +++ b/apps/server/src/process/NonBlockingSpawner.ts @@ -109,7 +109,8 @@ const findExecutable = Effect.fnUntraced(function* ( // Joined as text: the kernel resolves `..` after a symlink differently from a normalized path. const fromCwd = (path: string) => cwd === undefined || path.startsWith("/") ? path : `${cwd}/${path}`; - const candidates = command.command.includes("/") + const explicitPath = command.command.includes("/"); + const candidates = explicitPath ? [fromCwd(command.command)] : (childPath(command.options) ?? DEFAULT_PATH) .split(":") @@ -121,6 +122,8 @@ const findExecutable = Effect.fnUntraced(function* ( candidate, NodeFS.constants.X_OK, ); + // Like execvp, skip PATH entries that cannot be traversed as directories. + if (!explicitPath && error?.code === "ENOTDIR") continue; // Any other errno is left for `env` to report through its exit code. if (error === undefined || (error.code !== "ENOENT" && error.code !== "EACCES")) return; denied ||= error.code === "EACCES"; From 56fca34cc71568f764fd80f100b45183163866ba Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:12:09 +0100 Subject: [PATCH 3/7] fix(server): preserve the final PATH lookup error Co-authored-by: GPT 6.2 Sol --- apps/server/src/process/NonBlockingSpawner.ts | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/apps/server/src/process/NonBlockingSpawner.ts b/apps/server/src/process/NonBlockingSpawner.ts index b9aa2c71db4f5..05afcc3f2fe0c 100644 --- a/apps/server/src/process/NonBlockingSpawner.ts +++ b/apps/server/src/process/NonBlockingSpawner.ts @@ -60,21 +60,33 @@ const childPath = ({ env, extendEnv }: ChildProcess.CommandOptions) => { return value === undefined || !Redacted.isRedacted(value) ? value : Redacted.value(value); }; -// The spawn error Effect's Node spawner reports when execvp fails with `code`. -const spawnFailure = (command: ChildProcess.StandardCommand, code: "ENOENT" | "EACCES") => { - const syscall = `spawn ${command.command}`; +// Linux spawn errnos and their Effect error reasons. +const spawnErrors = { + ENOENT: { reason: "NotFound", errno: -2 }, + EACCES: { reason: "PermissionDenied", errno: -13 }, + ENOTDIR: { reason: "BadResource", errno: -20 }, +} as const; + +const spawnFailure = (command: ChildProcess.StandardCommand, code: keyof typeof spawnErrors) => { + const { reason, errno } = spawnErrors[code]; + // Node throws ENOTDIR synchronously, without attaching command details to the errno. + const syscall = code === "ENOTDIR" ? "spawn" : `spawn ${command.command}`; return PlatformError.systemError({ - _tag: code === "ENOENT" ? "NotFound" : "PermissionDenied", + _tag: reason, module: "ChildProcess", method: "spawn", pathOrDescriptor: CommandLine.display([command.command, ...command.args], { format: "posix" }), syscall, cause: Object.assign(new Error(`${syscall} ${code}`), { - errno: code === "ENOENT" ? -2 : -13, + errno, code, syscall, - path: command.command, - spawnargs: command.args.map((arg) => (Redacted.isRedacted(arg) ? "" : arg)), + ...(code === "ENOTDIR" + ? {} + : { + path: command.command, + spawnargs: command.args.map((arg) => (Redacted.isRedacted(arg) ? "" : arg)), + }), }), }); }; @@ -99,8 +111,8 @@ const cwdFailure = (cwd: string, cause: NodeJS.ErrnoException) => /** * Fails as a native spawn does when the command is missing or not executable: callers read - * that error to tell a CLI that is not installed from one that failed. execvp reports EACCES - * when a candidate exists but cannot be run, and ENOENT otherwise. + * that error to tell a CLI that is not installed from one that failed. When PATH is exhausted, + * execvp reports EACCES if any candidate was denied, or the last lookup error otherwise. */ const findExecutable = Effect.fnUntraced(function* ( command: ChildProcess.StandardCommand, @@ -115,20 +127,19 @@ const findExecutable = Effect.fnUntraced(function* ( : (childPath(command.options) ?? DEFAULT_PATH) .split(":") .map((entry) => `${fromCwd(entry || ".")}/${command.command}`); - let denied = false; + let failure: keyof typeof spawnErrors = "ENOENT"; for (const candidate of candidates) { const error = yield* check( candidate.slice(0, candidate.lastIndexOf("/")) || "/", candidate, NodeFS.constants.X_OK, ); - // Like execvp, skip PATH entries that cannot be traversed as directories. - if (!explicitPath && error?.code === "ENOTDIR") continue; - // Any other errno is left for `env` to report through its exit code. - if (error === undefined || (error.code !== "ENOENT" && error.code !== "EACCES")) return; - denied ||= error.code === "EACCES"; + const code = error?.code; + // Other errors, including ENOTDIR for explicit paths, are left for env to report. + if (code !== "ENOENT" && code !== "EACCES" && (code !== "ENOTDIR" || explicitPath)) return; + if (failure !== "EACCES") failure = code; } - return yield* spawnFailure(command, denied ? "EACCES" : "ENOENT"); + return yield* spawnFailure(command, failure); }); const make = Effect.gen(function* () { From 595101b4b6a05b38be2634733bd9fe4082bb295b Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:32:49 +0100 Subject: [PATCH 4/7] fix(server): preserve Node's non-directory spawn error Co-authored-by: GPT 6.2 Sol --- apps/server/src/process/NonBlockingSpawner.ts | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/apps/server/src/process/NonBlockingSpawner.ts b/apps/server/src/process/NonBlockingSpawner.ts index 05afcc3f2fe0c..188dcc1bcfbf3 100644 --- a/apps/server/src/process/NonBlockingSpawner.ts +++ b/apps/server/src/process/NonBlockingSpawner.ts @@ -60,33 +60,21 @@ const childPath = ({ env, extendEnv }: ChildProcess.CommandOptions) => { return value === undefined || !Redacted.isRedacted(value) ? value : Redacted.value(value); }; -// Linux spawn errnos and their Effect error reasons. -const spawnErrors = { - ENOENT: { reason: "NotFound", errno: -2 }, - EACCES: { reason: "PermissionDenied", errno: -13 }, - ENOTDIR: { reason: "BadResource", errno: -20 }, -} as const; - -const spawnFailure = (command: ChildProcess.StandardCommand, code: keyof typeof spawnErrors) => { - const { reason, errno } = spawnErrors[code]; - // Node throws ENOTDIR synchronously, without attaching command details to the errno. - const syscall = code === "ENOTDIR" ? "spawn" : `spawn ${command.command}`; +// The spawn error Effect's Node spawner reports when execvp fails with `code`. +const spawnFailure = (command: ChildProcess.StandardCommand, code: "ENOENT" | "EACCES") => { + const syscall = `spawn ${command.command}`; return PlatformError.systemError({ - _tag: reason, + _tag: code === "ENOENT" ? "NotFound" : "PermissionDenied", module: "ChildProcess", method: "spawn", pathOrDescriptor: CommandLine.display([command.command, ...command.args], { format: "posix" }), syscall, cause: Object.assign(new Error(`${syscall} ${code}`), { - errno, + errno: code === "ENOENT" ? -2 : -13, code, syscall, - ...(code === "ENOTDIR" - ? {} - : { - path: command.command, - spawnargs: command.args.map((arg) => (Redacted.isRedacted(arg) ? "" : arg)), - }), + path: command.command, + spawnargs: command.args.map((arg) => (Redacted.isRedacted(arg) ? "" : arg)), }), }); }; @@ -121,13 +109,12 @@ const findExecutable = Effect.fnUntraced(function* ( // Joined as text: the kernel resolves `..` after a symlink differently from a normalized path. const fromCwd = (path: string) => cwd === undefined || path.startsWith("/") ? path : `${cwd}/${path}`; - const explicitPath = command.command.includes("/"); - const candidates = explicitPath + const candidates = command.command.includes("/") ? [fromCwd(command.command)] : (childPath(command.options) ?? DEFAULT_PATH) .split(":") .map((entry) => `${fromCwd(entry || ".")}/${command.command}`); - let failure: keyof typeof spawnErrors = "ENOENT"; + let failure: "ENOENT" | "EACCES" | "ENOTDIR" = "ENOENT"; for (const candidate of candidates) { const error = yield* check( candidate.slice(0, candidate.lastIndexOf("/")) || "/", @@ -135,10 +122,16 @@ const findExecutable = Effect.fnUntraced(function* ( NodeFS.constants.X_OK, ); const code = error?.code; - // Other errors, including ENOTDIR for explicit paths, are left for env to report. - if (code !== "ENOENT" && code !== "EACCES" && (code !== "ENOTDIR" || explicitPath)) return; + // Other errors are left for env to report through its exit code. + if (code !== "ENOENT" && code !== "EACCES" && code !== "ENOTDIR") return; if (failure !== "EACCES") failure = code; } + if (failure === "ENOTDIR") { + // Node throws synchronously here, and callers recognize ENOTDIR on the defect itself. + return yield* Effect.die( + Object.assign(new Error("spawn ENOTDIR"), { errno: -20, code: "ENOTDIR", syscall: "spawn" }), + ); + } return yield* spawnFailure(command, failure); }); From 3ffab5e8cc7c46a9bca1f46a1ea38b354c5ec44e Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:34:03 +0100 Subject: [PATCH 5/7] fix(server): keep the untouched errno comment as main has it Co-authored-by: Claude Opus 5.5 --- apps/server/src/process/NonBlockingSpawner.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/process/NonBlockingSpawner.ts b/apps/server/src/process/NonBlockingSpawner.ts index 188dcc1bcfbf3..765d0fa7521f0 100644 --- a/apps/server/src/process/NonBlockingSpawner.ts +++ b/apps/server/src/process/NonBlockingSpawner.ts @@ -122,7 +122,7 @@ const findExecutable = Effect.fnUntraced(function* ( NodeFS.constants.X_OK, ); const code = error?.code; - // Other errors are left for env to report through its exit code. + // Any other errno is left for `env` to report through its exit code. if (code !== "ENOENT" && code !== "EACCES" && code !== "ENOTDIR") return; if (failure !== "EACCES") failure = code; } From 8712f788f124f1c39cdbf45c6edce2af86683da8 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:51:07 +0100 Subject: [PATCH 6/7] fix(server): validate NUL arguments before executable lookup Co-authored-by: GPT 6.2 Sol --- apps/server/src/process/NonBlockingSpawner.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/apps/server/src/process/NonBlockingSpawner.ts b/apps/server/src/process/NonBlockingSpawner.ts index 765d0fa7521f0..8ec630e8c4399 100644 --- a/apps/server/src/process/NonBlockingSpawner.ts +++ b/apps/server/src/process/NonBlockingSpawner.ts @@ -144,8 +144,16 @@ const make = Effect.gen(function* () { // Runs inside the NonBlockingSpawner.spawn span. const prepareStandard = Effect.fnUntraced(function* (input: ChildProcess.StandardCommand) { const { cwd: givenCwd, shell, ...options } = input.options; - // Effect rejects a shell command with arguments, and a NUL, itself. - if ((shell && input.args.length > 0) || input.command.includes("\0")) return input; + // Let Effect validate shell arguments and reject NULs before checking the executable. + if ( + (shell && input.args.length > 0) || + input.command.includes("\0") || + input.args.some((arg) => + (Redacted.isRedacted(arg) ? Redacted.value(arg) : arg).includes("\0"), + ) + ) { + return input; + } // Node's shell mode on Unix, spelled out so the trampoline can run it. const command = shell ? ChildProcess.make(typeof shell === "string" ? shell : "/bin/sh", ["-c", input.command]) From e452553b07a29f2600016211713c7938cbb5e6eb Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:52:26 +0100 Subject: [PATCH 7/7] fix(server): fold the NUL argument check into the existing command check Co-authored-by: Claude Opus 5.5 --- apps/server/src/process/NonBlockingSpawner.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/server/src/process/NonBlockingSpawner.ts b/apps/server/src/process/NonBlockingSpawner.ts index 8ec630e8c4399..e9f7d2544a248 100644 --- a/apps/server/src/process/NonBlockingSpawner.ts +++ b/apps/server/src/process/NonBlockingSpawner.ts @@ -144,11 +144,10 @@ const make = Effect.gen(function* () { // Runs inside the NonBlockingSpawner.spawn span. const prepareStandard = Effect.fnUntraced(function* (input: ChildProcess.StandardCommand) { const { cwd: givenCwd, shell, ...options } = input.options; - // Let Effect validate shell arguments and reject NULs before checking the executable. + // Effect rejects a shell command with arguments, and a NUL, itself. if ( (shell && input.args.length > 0) || - input.command.includes("\0") || - input.args.some((arg) => + [input.command, ...input.args].some((arg) => (Redacted.isRedacted(arg) ? Redacted.value(arg) : arg).includes("\0"), ) ) {