Skip to content

Commit bf9e985

Browse files
fix(server): skip regular files when searching PATH on Linux (#45432)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 35d24a9 commit bf9e985

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎apps/server/src/process/NonBlockingSpawner.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Fiber from "effect/Fiber";
1111
import * as FileSystem from "effect/FileSystem";
1212
import * as Layer from "effect/Layer";
1313
import * as Path from "effect/Path";
14+
import * as Redacted from "effect/Redacted";
1415
import * as ChildProcess from "effect/process/ChildProcess";
1516
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1617

@@ -29,6 +30,11 @@ const outcome = (command: ChildProcess.Command) =>
2930
onSuccess: (output) => ({ output }),
3031
onFailure: (error) => ({ reason: error.reason._tag, message: error.message }),
3132
}),
33+
Effect.catchDefect((defect) =>
34+
Effect.succeed({
35+
defect: defect instanceof Error ? { ...defect, message: defect.message } : defect,
36+
}),
37+
),
3238
);
3339

3440
describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpawner", () => {
@@ -81,6 +87,20 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpa
8187
ChildProcess.pipeTo(ChildProcess.make("tail", ["-n", "1"])),
8288
),
8389
"a missing command": ChildProcess.make("t3code-missing-command", ["--version"]),
90+
"a missing command with a regular file in PATH": ChildProcess.make("tool", [], {
91+
env: { PATH: `${root}/fresh/tool:${root}/target` },
92+
}),
93+
"a missing command with a regular file last in PATH": ChildProcess.make("tool", [], {
94+
env: { PATH: `${root}/target:${root}/fresh/tool` },
95+
}),
96+
"an explicit path through a regular file": ChildProcess.make(`${root}/fresh/tool/x`, []),
97+
"a NUL argument takes precedence over ENOTDIR": ChildProcess.make(`${root}/fresh/tool/x`, [
98+
"invalid\0argument",
99+
]),
100+
"a redacted NUL argument takes precedence over ENOTDIR": ChildProcess.make(
101+
`${root}/fresh/tool/x`,
102+
[Redacted.make("invalid\0argument")],
103+
),
84104
"a missing command in a cwd": ChildProcess.make("t3code-missing-command", [], {
85105
cwd: root,
86106
}),

‎apps/server/src/process/NonBlockingSpawner.ts‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ const cwdFailure = (cwd: string, cause: NodeJS.ErrnoException) =>
9999

100100
/**
101101
* Fails as a native spawn does when the command is missing or not executable: callers read
102-
* that error to tell a CLI that is not installed from one that failed. execvp reports EACCES
103-
* when a candidate exists but cannot be run, and ENOENT otherwise.
102+
* that error to tell a CLI that is not installed from one that failed. When PATH is exhausted,
103+
* execvp reports EACCES if any candidate was denied, or the last lookup error otherwise.
104104
*/
105105
const findExecutable = Effect.fnUntraced(function* (
106106
command: ChildProcess.StandardCommand,
@@ -114,18 +114,25 @@ const findExecutable = Effect.fnUntraced(function* (
114114
: (childPath(command.options) ?? DEFAULT_PATH)
115115
.split(":")
116116
.map((entry) => `${fromCwd(entry || ".")}/${command.command}`);
117-
let denied = false;
117+
let failure: "ENOENT" | "EACCES" | "ENOTDIR" = "ENOENT";
118118
for (const candidate of candidates) {
119119
const error = yield* check(
120120
candidate.slice(0, candidate.lastIndexOf("/")) || "/",
121121
candidate,
122122
NodeFS.constants.X_OK,
123123
);
124+
const code = error?.code;
124125
// Any other errno is left for `env` to report through its exit code.
125-
if (error === undefined || (error.code !== "ENOENT" && error.code !== "EACCES")) return;
126-
denied ||= error.code === "EACCES";
126+
if (code !== "ENOENT" && code !== "EACCES" && code !== "ENOTDIR") return;
127+
if (failure !== "EACCES") failure = code;
127128
}
128-
return yield* spawnFailure(command, denied ? "EACCES" : "ENOENT");
129+
if (failure === "ENOTDIR") {
130+
// Node throws synchronously here, and callers recognize ENOTDIR on the defect itself.
131+
return yield* Effect.die(
132+
Object.assign(new Error("spawn ENOTDIR"), { errno: -20, code: "ENOTDIR", syscall: "spawn" }),
133+
);
134+
}
135+
return yield* spawnFailure(command, failure);
129136
});
130137

131138
const make = Effect.gen(function* () {
@@ -138,7 +145,14 @@ const make = Effect.gen(function* () {
138145
const prepareStandard = Effect.fnUntraced(function* (input: ChildProcess.StandardCommand) {
139146
const { cwd: givenCwd, shell, ...options } = input.options;
140147
// Effect rejects a shell command with arguments, and a NUL, itself.
141-
if ((shell && input.args.length > 0) || input.command.includes("\0")) return input;
148+
if (
149+
(shell && input.args.length > 0) ||
150+
[input.command, ...input.args].some((arg) =>
151+
(Redacted.isRedacted(arg) ? Redacted.value(arg) : arg).includes("\0"),
152+
)
153+
) {
154+
return input;
155+
}
142156
// Node's shell mode on Unix, spelled out so the trampoline can run it.
143157
const command = shell
144158
? ChildProcess.make(typeof shell === "string" ? shell : "/bin/sh", ["-c", input.command])

0 commit comments

Comments
 (0)