fix(ghidra): honor current directory in PATH discovery

This commit is contained in:
morluto committed 2026-10-27 05:49:17 +08:00
1 parent e1ec737d9b
commit 81adb8977a
3 files changed
+172 -3

No files matched your search

+3 -2
View File
@@ -1092,13 +1092,14 @@ export const discoverGhidraInstallationOnPath = (
if (platform !== process.platform) return undefined;
const path = platform === "win32" ? win32 : posix;
const pathValue = environment.PATH;
if (pathValue === undefined || pathValue.length === 0) return undefined;
if (pathValue === undefined) return undefined;
const launchers =
platform === "win32"
? ["ghidra.bat", "ghidraRun.bat", "ghidra.exe", "ghidraRun.exe"]
: ["ghidra", "ghidraRun"];
for (const directory of pathValue.split(pathDelimiter)) {
if (directory.length === 0) continue;
// POSIX empty PATH entries select cwd, including an entirely empty PATH.
if (directory.length === 0 && platform === "win32") continue;
for (const launcher of launchers) {
let resolved: string;
try {
+53 -1
View File
@@ -1,4 +1,4 @@
import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
import { chmod, mkdir, readFile, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { expect } from "vitest";
@@ -230,3 +230,55 @@ setTimeout(() => process.exit(92), 12000);
}
},
);
cliTest.skipIf(!["darwin", "linux"].includes(process.platform))(
"doctor discovers Ghidra through an empty PATH component without executing its launchers",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-ghidra-cwd-doctor-");
const install = join(root, "installation");
const marker = join(root, "launcher-execution");
await mkdir(join(install, "Ghidra"), { recursive: true });
await mkdir(join(install, "support"), { recursive: true });
await writeFile(
join(install, "Ghidra/application.properties"),
"application.version=12.1.4\napplication.java.min=21\n",
);
await writeFile(marker, "unexecuted");
const launcher = `#!${process.execPath}\nrequire("node:fs").writeFileSync(${JSON.stringify(marker)}, "executed");\n`;
for (const name of ["ghidraRun", "support/analyzeHeadless"])
await writeFile(join(install, name), launcher, { mode: 0o700 });
await symlink(join(install, "ghidraRun"), join(root, "ghidra"));
const result = await cli.run({
arguments: ["doctor", "--provider", "ghidra", "--json"],
cwd: root,
environment: {
PATH: "",
JAVA_HOME: join(root, "missing-jdk"),
REA_LOG_LEVEL: "silent",
},
});
// The fixture deliberately has no JDK or native decompiler. Discovery must
// retain the selected installation while reporting those later failures.
expect(result.exitCode, `${result.stdout}\n${result.stderr}`).toBe(1);
expect(result.json).toMatchObject({
providerInspections: expect.arrayContaining([
expect.objectContaining({
id: "ghidra",
configured: true,
checks: expect.arrayContaining([
expect.objectContaining({
name: "configuration",
ok: true,
code: null,
detail: install,
}),
expect.objectContaining({ name: "installation", ok: true }),
expect.objectContaining({ name: "headless", ok: true }),
]),
}),
]),
});
expect(await readFile(marker, "utf8")).toBe("unexecuted");
},
);
@@ -0,0 +1,116 @@
import { execFile } from "node:child_process";
import { mkdir, readFile, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { expect, it } from "vitest";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
const execute = promisify(execFile);
it.skipIf(process.platform === "win32")(
"doctor preserves POSIX PATH cwd entries and discovery order without executing launchers",
async () => {
const root = await createTestTempDirectory("rea-ghidra-cwd-discovery-");
const marker = join(root, "launched");
const installation = async (name: string) => {
const directory = join(root, name);
await mkdir(join(directory, "Ghidra"), { recursive: true });
await mkdir(join(directory, "support"), { recursive: true });
await writeFile(
join(directory, "Ghidra/application.properties"),
"application.version=12.1.4\n",
);
const launcher = join(directory, "ghidraRun");
await writeFile(
launcher,
'#!/bin/sh\nprintf "launched\\n" >> "$REA_PATH_TEST_MARKER"\n',
{ mode: 0o700 },
);
await writeFile(join(directory, "support/analyzeHeadless"), "", {
mode: 0o700,
});
return { directory, launcher };
};
const local = await installation("local");
const other = await installation("other");
await symlink(local.launcher, join(root, "ghidra"));
const absent = join(root, "absent");
const paths = [
`:${other.directory}`,
`${absent}::${other.directory}`,
`${absent}:`,
"",
`${other.directory}:`,
];
const environment = {
...process.env,
JAVA_HOME: join(root, "missing-jdk"),
GHIDRA_INSTALL_DIR: undefined,
REA_PATH_TEST_MARKER: marker,
};
// A real exec search establishes what the empty components select.
for (const path of paths.slice(0, 4))
await execute("ghidra", [], {
cwd: root,
env: { ...environment, PATH: path },
});
const before = await readFile(marker, "utf8");
const doctorModule = new URL(
"../../../../src/ghidra/GhidraDoctor.ts",
import.meta.url,
).href;
const resourceModule = new URL(
"../../../fixtures/inspectionResources.ts",
import.meta.url,
).href;
const source = `
import { inspectSystemGhidraProvider } from ${JSON.stringify(doctorModule)};
import { createInspectionResources } from ${JSON.stringify(resourceModule)};
const scope = createInspectionResources();
try {
const cases = ${JSON.stringify(paths)}.map(PATH => ({ ...process.env, PATH }));
cases.push({ ...process.env, PATH: undefined });
cases.push({ ...process.env, PATH: '', GHIDRA_INSTALL_DIR: ${JSON.stringify(absent)} });
const results = [];
for (const env of cases) {
const result = await inspectSystemGhidraProvider(env, scope.owner);
results.push({ configured: result.configured, configuration: result.checks.find(c => c.name === 'configuration') });
}
console.log(JSON.stringify(results));
} finally { await scope.close(); }
`;
const { stdout } = await execute(
process.execPath,
[
"--import",
import.meta.resolve("tsx"),
"--input-type=module",
"--eval",
source,
],
{ cwd: root, env: environment, timeout: 20_000 },
);
const results: unknown = JSON.parse(stdout);
expect(results).toEqual([
...[
local.directory,
local.directory,
local.directory,
local.directory,
other.directory,
].map((detail) => ({
configured: true,
configuration: expect.objectContaining({ detail, ok: true }),
})),
{
configured: false,
configuration: expect.objectContaining({ code: "not_configured" }),
},
{
configured: true,
configuration: expect.objectContaining({ detail: absent, ok: true }),
},
]);
expect(await readFile(marker, "utf8")).toBe(before);
},
);