fix(process): continue the PATH search past an unreadable relative entry

With the working directory removed and PATH starting with "." or an
empty entry, the lookup stopped at that entry with no-working-directory.
A spawned search finds nothing under a removed directory and moves on,
so the command still ran a later match by its bare name, while Evidence
reported an assumed identity without path, digest or version. The
lookup now skips such an entry and reports the unread working directory
only when no later entry matches; an explicit relative command still
reports it at once.
This commit is contained in:
Entrpi committed 2026-11-01 16:20:31 +11:00
1 parent 7ce99121bb
commit 12244b7a38
3 files changed
+37 -13

No files matched your search

+20 -6
View File
@@ -19,7 +19,7 @@ export type ExecutableLookup =
readonly cause: unknown;
}
| { readonly status: "missing"; readonly checked: readonly string[] }
/** A relative command or PATH entry needed the working directory, which could not be read. */
/** A relative command, or a relative PATH entry with no later match, needed the working directory, which could not be read. */
| { readonly status: "no-working-directory"; readonly cause: unknown }
/** The caller's signal or deadline ended the lookup before it finished. */
| {
@@ -40,9 +40,12 @@ export interface ExecutableLookupBounds {
* contains a path separator is the selected path; PATH search continues past
* denied directories, and an existing file without execute permission is not
* a match. The working directory is read only when the search reaches a
* relative command or PATH entry, since it may have been removed. A
* file-system call cannot itself be cancelled, so an interrupted lookup stops
* waiting for it and lets it settle unobserved.
* relative command or PATH entry, since it may have been removed. When that
* read fails, the search skips the relative entry, as a spawned search finds
* nothing under a removed directory, and reports the unread working directory
* only if no later entry matches. A file-system call cannot itself be
* cancelled, so an interrupted lookup stops waiting for it and lets it settle
* unobserved.
*/
export const lookupExecutable = async (
command: string,
@@ -72,6 +75,9 @@ export const lookupExecutable = async (
const interruption = lookupInterruption(signal, timeoutMs);
try {
const checked: string[] = [];
// A relative PATH entry skipped because the working directory could not
// be read; without a later match, the command is not known to be missing.
let skipped: { readonly cause: unknown } | undefined;
for (const entry of entries) {
// Resolved only when reached: a match in an earlier entry never needs
// the working directory that a later relative entry would read.
@@ -81,7 +87,13 @@ export const lookupExecutable = async (
? located(entry)
: appendCallerPath(located(entry), command);
} catch (cause) {
return { status: "no-working-directory", cause };
// An explicit relative command has no other candidate.
if (explicit) return { status: "no-working-directory", cause };
skipped ??= { cause };
checked.push(
`${appendCallerPath(entry === "" ? "." : entry, command)} (working directory unreadable)`,
);
continue;
}
const metadata = await interruption.race(stat(candidate));
if (metadata.status === "interrupted") return metadata;
@@ -111,7 +123,9 @@ export const lookupExecutable = async (
`${candidate}${systemErrorSuffix(systemErrorCode(executable.cause))}`,
);
}
return { status: "missing", checked };
return skipped === undefined
? { status: "missing", checked }
: { status: "no-working-directory", cause: skipped.cause };
} finally {
interruption.release();
}
@@ -363,7 +363,7 @@ posix(
);
posix(
"reads the working directory only when the PATH search reaches a relative entry",
"reads the working directory only when the PATH search reaches a relative entry, and skips that entry when the read fails",
async () => {
const { executable } = await rizinExecutableFixture();
const directory = dirname(executable);
@@ -376,8 +376,12 @@ posix(
expect(workingDirectory).not.toHaveBeenCalled();
await expect(
lookupExecutable("rizin", workingDirectory, `.${delimiter}${directory}`),
).resolves.toMatchObject({ status: "no-working-directory" });
).resolves.toEqual({ status: "found", path: executable });
expect(workingDirectory).toHaveBeenCalledOnce();
// Without a later match, the unread entry leaves the command unknown.
await expect(
lookupExecutable("rizin", workingDirectory, "."),
).resolves.toMatchObject({ status: "no-working-directory" });
},
);
@@ -47,9 +47,12 @@ posix(
},
);
posix(
"identifies a PATH match before a relative entry without reading the working directory",
async () => {
posix.each([
["before", false],
["after", true],
] as const)(
"identifies a PATH match %s a relative entry whose working directory cannot be read",
async (_order, relativeFirst) => {
const { executable, path } = await rizinExecutableFixture();
const run = versionProbeRunner(() => "rizin 0.9.1 @ linux-x86-64");
const service = new TestReverseEngineeringService({
@@ -57,7 +60,9 @@ posix(
lookup: lookupExecutable,
environment: {
REA_RIZIN_COMMAND: "rizin",
PATH: `${dirname(executable)}${delimiter}.`,
PATH: relativeFirst
? `.${delimiter}${dirname(executable)}`
: `${dirname(executable)}${delimiter}.`,
},
});
const cwd = unreadableWorkingDirectory();
@@ -69,7 +74,8 @@ posix(
provider_executable_path: executable,
provider_identity: "banner",
});
expect(cwd).not.toHaveBeenCalled();
// A relative entry before the match is reached and skipped.
expect(cwd).toHaveBeenCalledTimes(relativeFirst ? 1 : 0);
} finally {
cwd.mockRestore();
}