refactor(process): give POSIX PATH search order one owner (#10891)

* refactor(process): give POSIX PATH search order one owner

Program lookup, the packet-tool locator and Jadx's Java lookup each split
PATH and resolved empty and relative entries themselves. They now take the
candidates from posixPathCandidates in src/process/executableSearch and keep
only their own probe and failure reporting. A counted architecture guard
keeps new PATH splits from returning; the remaining Windows and Ghidra
searches are baselined.

* refactor(process): move the colon-split PATH searches onto the search owner

REA's launcher-alias lookup and Ghidra's macOS Java lookup split PATH on
":" and skipped relative entries themselves; they now use
posixPathCandidates with relativeEntries: "skip", and the guard counts
colon splits of PATH as well.

* fix(process): produce PATH candidates lazily so an unused entry cannot fail an earlier match

* refactor(process): give Windows PATH/PATHEXT search order the executable search owner (#10916)

* refactor(process): give Windows PATH/PATHEXT search order the executable search owner

* fix(process): keep later PATH entries searchable after a stray quote or a removed working directory

* fix(reverse): refuse a Windows objdump or Rizin command that PATH does not find (#10953)

* fix(reverse): refuse an objdump or Rizin command Windows PATH does not find instead of launching the bare name

* fix(reverse): report a Windows lookup refusal after cancellation and to capability discovery

* fix(desktop): search a Windows host's PATH as Windows does when doctor looks up a program

programOnPath sent every host through the POSIX PATH search, so on Windows a
relative PATH entry reached absoluteCallerPath with platform linux and the
Windows working directory, which resolved against itself until the stack
overflowed. programOnPath now searches PATH and PATHEXT through
windowsPathCandidates on a Windows host, and absoluteCallerPath refuses a
non-POSIX working directory with a TypeError instead of recursing.
This commit is contained in:
morluto authored and GitHub committed 2026-11-15 18:24:04 +08:00
1 parent 659772952d
commit 422cff00ab
23 files changed
+912 -330

No files matched your search

+24 -73
View File
@@ -1,30 +1,8 @@
import { stat } from "node:fs/promises";
import { win32 } from "node:path";
import { err, ok, type Result } from "../domain/result.js";
import { AbortedWait, waitForAbortable } from "./AbortablePromise.js";
import { snapshotEnvironment } from "./snapshotEnvironment.js";
/** Parse PATH once, preserving quoted semicolons and explicit relative entries. */
export const windowsPathEntries = (value: string): readonly string[] => {
const entries: string[] = [];
let start = 0;
let quoted = false;
for (let index = 0; index < value.length; index += 1) {
if (value[index] === '"') quoted = !quoted;
else if (value[index] === ";" && !quoted) {
entries.push(value.slice(start, index));
start = index + 1;
}
}
entries.push(value.slice(start));
return entries.flatMap((entry) => {
if (entry === "") return [];
if (entry.startsWith('"') && entry.endsWith('"') && entry.length >= 2)
return entry.length === 2 ? [] : [entry.slice(1, -1)];
return entry.includes('"') ? [] : [entry];
});
};
import { windowsPathCandidates } from "./executableSearch.js";
export type WindowsExecutableFailure =
| { readonly kind: "missing" }
@@ -38,7 +16,8 @@ export type WindowsExecutableFailure =
};
/**
* Resolve a default bare command against PATH/PATHEXT only. Never return a bare
* Resolve a default bare command against PATH/PATHEXT only, probing the
* search owner's candidates (src/process/executableSearch) in order. Never return a bare
* name on failure: handing it to Windows would restart implicit CWD lookup.
* Callers own the lookup deadline and translation to their operation errors.
*/
@@ -52,56 +31,28 @@ export const findWindowsExecutable = async (
},
): Promise<Result<string, WindowsExecutableFailure>> => {
if (options.signal.aborted) return err({ kind: "interrupted" });
const selected = snapshotEnvironment(environment, "win32");
const extensions = (selected.PATHEXT ?? ".COM;.EXE;.BAT;.CMD")
.split(";")
.filter((extension) => extension !== "");
for (const extension of extensions)
if (!extension.startsWith(".") || /[\\/:\0]/u.test(extension))
return err({ kind: "invalid_extension", extension });
const explicitExtension = win32.extname(executable);
const allowed =
explicitExtension !== ""
? options.allowBatch === true ||
[".exe", ".com"].includes(explicitExtension.toLowerCase())
? [""]
: []
: options.allowBatch === true
? extensions
: extensions.filter((extension) =>
[".exe", ".com"].includes(extension.toLowerCase()),
);
const cwd = options.cwd ?? process.cwd();
for (const directory of windowsPathEntries(selected.PATH ?? "")) {
for (const extension of allowed) {
const candidates = windowsPathCandidates(executable, environment, options);
if (!candidates.ok) return candidates;
for (const { path: candidate } of candidates.value) {
if (options.signal.aborted) return err({ kind: "interrupted" });
try {
const metadata = await waitForAbortable(stat(candidate), options.signal);
if (metadata instanceof AbortedWait || options.signal.aborted)
return err({ kind: "interrupted" });
if (metadata.isFile()) return ok(candidate);
} catch (cause: unknown) {
if (options.signal.aborted) return err({ kind: "interrupted" });
const candidate = win32.resolve(
cwd,
directory,
`${executable}${extension}`,
);
try {
const metadata = await waitForAbortable(
stat(candidate),
options.signal,
);
if (metadata instanceof AbortedWait || options.signal.aborted)
return err({ kind: "interrupted" });
if (metadata.isFile()) return ok(candidate);
} catch (cause: unknown) {
if (options.signal.aborted) return err({ kind: "interrupted" });
const code =
typeof cause === "object" && cause !== null
? Reflect.get(cause, "code")
: undefined;
if (code === "ENOENT" || code === "ENOTDIR") continue;
return err({
kind: "inspection",
path: candidate,
code: typeof code === "string" ? code : null,
cause,
});
}
const code =
typeof cause === "object" && cause !== null
? Reflect.get(cause, "code")
: undefined;
if (code === "ENOENT" || code === "ENOTDIR") continue;
return err({
kind: "inspection",
path: candidate,
code: typeof code === "string" ? code : null,
cause,
});
}
}
return err({ kind: "missing" });