feat(ghidra): enable Windows x64 read-only analysis (#699)

* feat(ghidra): enable Windows x64 read-only analysis

* test(ghidra): exercise native Windows runtime boundaries

* test(ghidra): verify current Windows inventory and isolate ACL observer

* fix(ghidra): align Windows admission and address P0 review
This commit is contained in:
N0zoM1z0
2026-10-06 16:53:33 +08:00
committed by GitHub
parent cf061159ab
commit 26881da76a
65 changed files with 4091 additions and 356 deletions
@@ -0,0 +1,95 @@
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { createDoctorHostFixture } from "../../../src/application/Doctor.fixture.js";
import { systemSetupHost } from "../../../src/application/SetupHost.js";
import { runSetup } from "../../../src/application/Setup.js";
import { options } from "../../../src/application/Setup.fixture.js";
import { supportedClients } from "../../../src/application/SupportedClients.js";
import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const roots: string[] = [];
afterEach(async () => {
await Promise.all(
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
);
});
describe("setup with configured but unavailable providers", () => {
it("preserves Hopper settings and makes the second setup perform no writes", async () => {
const root = await createTestTempDirectory("rea-setup-unavailable-");
roots.push(root);
const client = supportedClients(root, "darwin", {}).find(
(candidate) => candidate.name === "cursor",
);
if (client === undefined) throw new Error("Cursor registration is missing");
const original = '{"theme":"dark"}\n';
await mkdir(dirname(client.configPath), { recursive: true });
await writeFile(client.configPath, original);
const environment = { HOPPER_LAUNCHER_PATH: "/custom/Hopper" };
const doctor = createDoctorHostFixture({
validTarget: () => Promise.resolve(false),
executable: () => Promise.resolve(false),
clientRegistrations: () =>
readClientRegistrationStatuses(root, undefined, {
platform: "darwin",
environment: {},
}),
providerInspections: () =>
Promise.resolve([
{
id: "hopper",
configured: true,
available: false,
providerVersion: "6",
registrationEnvironment: environment,
checks: [
{
name: "hopper-license",
ok: false,
code: "unavailable",
detail: "Runtime license is unavailable",
classification: "missing_analysis_engine",
remediation: null,
},
],
},
]),
});
const host = {
...systemSetupHost(doctor),
detectedClients: () => Promise.resolve([client]),
supportedClients: () => Promise.resolve([client]),
};
const selected = {
...options(true),
clientIds: ["cursor"],
installSkill: false,
proposeHopper: false,
// This lane checks registration effects; no deep provider is required.
readinessScope: { clients: [], providers: [], skill: false },
};
const first = await runSetup(selected, host);
expect(first.status).toBe("ready");
expect(first.clients.cursor).toMatchObject({ status: "configured" });
const configured = await readFile(client.configPath, "utf8");
expect(JSON.parse(configured)).toMatchObject({
theme: "dark",
mcpServers: { rea: { env: environment } },
});
await expect(
readFile(`${client.configPath}.rea.backup`, "utf8"),
).resolves.toBe(original);
const second = await runSetup(selected, host);
expect(second.status).toBe("ready");
expect(second.plannedActions).toEqual([]);
expect(second.appliedActions).toEqual([]);
await expect(readFile(client.configPath, "utf8")).resolves.toBe(configured);
await expect(
readFile(`${client.configPath}.rea.backup`, "utf8"),
).resolves.toBe(original);
});
});
@@ -14,6 +14,7 @@ import {
spawnOwnedProviderProcess,
type ProviderProcessDiagnostic,
} from "../../../src/process/ProviderProcess.js";
import { WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON } from "../../../src/process/WindowsAuthority.js";
import {
spawnProviderProcessFixture,
stopProviderProcessFixture,
@@ -69,7 +70,7 @@ describe("provider process runtime and wait primitives", () => {
PrivateRuntimeRoot.create({ platform: "win32" }),
).rejects.toMatchObject({
code: "private-runtime-root-authority-unavailable",
message: expect.stringContaining("chmod(0700)"),
message: WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON,
});
});
@@ -5,6 +5,8 @@ import { fileURLToPath } from "node:url";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
import { createGhidraTestRuntime } from "../../../fixtures/ghidraRuntime.js";
import type { PrivateRuntimeRoot } from "../../../../src/process/PrivateRuntimeRoot.js";
import {
ghidraHeadlessArguments,
@@ -21,6 +23,7 @@ const fixturePath = fileURLToPath(
),
);
const roots: string[] = [];
const runtimes: PrivateRuntimeRoot[] = [];
const expectIsolatedEnvironment = (
environment: Record<string, string>,
@@ -32,6 +35,8 @@ const expectIsolatedEnvironment = (
...(process.platform === "win32"
? {
USERPROFILE: join(runtimeRoot, "home"),
APPDATA: join(runtimeRoot, "config"),
LOCALAPPDATA: join(runtimeRoot, "cache"),
TEMP: join(runtimeRoot, "tmp"),
TMP: join(runtimeRoot, "tmp"),
}
@@ -42,7 +47,10 @@ const expectIsolatedEnvironment = (
XDG_DATA_HOME: join(runtimeRoot, "data"),
GHIDRA_JAVA_OPTIONS: "",
JAVA_TOOL_OPTIONS: "",
JDK_JAVA_OPTIONS: "",
JDK_JAVA_OPTIONS:
process.platform === "win32"
? `"-Duser.home=${join(runtimeRoot, "home")}" "-Djava.io.tmpdir=${join(runtimeRoot, "tmp")}" "-XX:-UsePerfData"`
: "",
_JAVA_OPTIONS: "",
JAVA_HOME: javaHome,
REA_PROCESS_RUN_ID: "d6fcbb66-e829-4ff6-a535-0035aec63139",
@@ -59,6 +67,7 @@ beforeAll(async () => {
});
afterEach(async () => {
vi.unstubAllEnvs();
await Promise.all(runtimes.splice(0).map((runtime) => runtime.close()));
await Promise.all(
roots.splice(0).map((path) => rm(path, { recursive: true, force: true })),
);
@@ -191,8 +200,11 @@ describe("Ghidra headless launcher", () => {
vi.stubEnv("JAVA_TOOL_OPTIONS", "-Duser.home=/unapproved/home");
vi.stubEnv("JDK_JAVA_OPTIONS", "-XX:MaxRAMPercentage=99");
vi.stubEnv("_JAVA_OPTIONS", "-Xmx99G");
const runtimeRoot = await createTestTempDirectory("rea-launcher-test-");
roots.push(runtimeRoot);
const parent = await createTestTempDirectory("rea-launcher-test-");
roots.push(parent);
const runtime = await createGhidraTestRuntime(parent);
runtimes.push(runtime);
const runtimeRoot = runtime.path;
const token = "secret-token-that-must-not-leak";
const javaHome =
process.platform === "win32" ? "C:\\Java\\jdk-21" : "/opt/jdk-21";
@@ -226,16 +238,11 @@ describe("Ghidra headless launcher", () => {
descriptor_has_token: true,
});
expectIsolatedEnvironment(capture.environment, runtimeRoot, javaHome);
expect(capture.environment.GHIDRA_HEADLESS_JAVA_OPTIONS).toContain(
`-Duser.home=${join(runtimeRoot, "home")}`,
);
expect(capture.environment.GHIDRA_HEADLESS_JAVA_OPTIONS).toBe(
`-Duser.home=${join(runtimeRoot, "home")} -Djava.io.tmpdir=${join(runtimeRoot, "tmp")}`,
process.platform === "win32"
? ""
: `-Duser.home=${join(runtimeRoot, "home")} -Djava.io.tmpdir=${join(runtimeRoot, "tmp")}`,
);
if (process.platform === "win32")
expect(capture.environment.GHIDRA_HEADLESS_JAVA_OPTIONS).not.toContain(
'"',
);
if (process.platform !== "win32")
expect(
(await stat(join(runtimeRoot, "ownership.json"))).mode & 0o777,
@@ -5,18 +5,38 @@ import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
import { createGhidraTestRuntime } from "../../../fixtures/ghidraRuntime.js";
import type { PrivateRuntimeRoot } from "../../../../src/process/PrivateRuntimeRoot.js";
import { createGhidraTargetSnapshot } from "../../../../src/ghidra/GhidraTargetSnapshot.js";
const roots: string[] = [];
const runtimes: PrivateRuntimeRoot[] = [];
afterEach(async () => {
await Promise.all(runtimes.splice(0).map((runtime) => runtime.close()));
await Promise.all(
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
);
});
describe("Ghidra target snapshot", () => {
it("uses the selected installation platform instead of the ambient host", async () => {
const root = await createTestTempDirectory("rea-ghidra-platform-");
roots.push(root);
const source = join(root, "source.exe");
const bytes = Buffer.from("platform fixture");
const sha256 = createHash("sha256").update(bytes).digest("hex");
await writeFile(source, bytes);
const snapshot = await createGhidraTargetSnapshot(source, root, sha256, {
platform: "linux",
});
expect(snapshot.admission).toBeUndefined();
await expect(readFile(snapshot.path)).resolves.toEqual(bytes);
await expect(
createGhidraTargetSnapshot(source, root, sha256, { platform: "win32" }),
).rejects.toThrow(/No native private runtime owns/u);
});
it("copies an exact digest-bound target into the private runtime", async () => {
const root = await createTestTempDirectory("rea-ghidra-snapshot-");
roots.push(root);
@@ -24,27 +44,39 @@ describe("Ghidra target snapshot", () => {
const bytes = Buffer.from("native PE fixture");
const sha256 = createHash("sha256").update(bytes).digest("hex");
await writeFile(source, bytes);
const runtime = await createGhidraTestRuntime(root);
runtimes.push(runtime);
const snapshot = await createGhidraTargetSnapshot(source, root, sha256);
const snapshot = await createGhidraTargetSnapshot(
source,
runtime.path,
sha256,
);
expect(snapshot).toEqual({
path: join(root, `target-${sha256.slice(0, 12)}.exe`),
expect(snapshot).toMatchObject({
path: join(runtime.path, `target-${sha256.slice(0, 12)}.exe`),
sha256,
});
await expect(readFile(snapshot.path)).resolves.toEqual(bytes);
});
it("removes a snapshot whose digest differs from admission", async () => {
it("rejects a mismatched digest and removes the failed runtime snapshot", async () => {
const root = await createTestTempDirectory("rea-ghidra-snapshot-");
roots.push(root);
const source = join(root, "source with unsafe extension.%PATH%");
await writeFile(source, "changed target");
const runtime = await createGhidraTestRuntime(root);
runtimes.push(runtime);
const expected = "a".repeat(64);
const snapshotPath = join(root, `target-${expected.slice(0, 12)}.bin`);
const snapshotPath = join(
runtime.path,
`target-${expected.slice(0, 12)}.bin`,
);
await expect(
createGhidraTargetSnapshot(source, root, expected),
createGhidraTargetSnapshot(source, runtime.path, expected),
).rejects.toThrow(/digest mismatch/u);
await runtime.close();
await expect(access(snapshotPath)).rejects.toMatchObject({
code: "ENOENT",
});
@@ -4,12 +4,19 @@ import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
import {
createGhidraTestRuntime,
publishGhidraTestEndpoint,
} from "../../../fixtures/ghidraRuntime.js";
import type { PrivateRuntimeRoot } from "../../../../src/process/PrivateRuntimeRoot.js";
import { observeGhidraEndpoint } from "../../../../src/ghidra/GhidraTransport.js";
const roots: string[] = [];
const runtimes: PrivateRuntimeRoot[] = [];
afterEach(async () => {
await Promise.all(runtimes.splice(0).map((runtime) => runtime.close()));
await Promise.all(
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
);
@@ -33,9 +40,10 @@ describe("Ghidra local transport", () => {
it("accepts only an exact IPv4 loopback endpoint record", async () => {
const root = await createTestTempDirectory("rea-ghidra-transport-");
roots.push(root);
const path = join(root, "bridge-endpoint.json");
await writeFile(
path,
const runtime = await createGhidraTestRuntime(root);
runtimes.push(runtime);
const path = await publishGhidraTestEndpoint(
runtime,
`${JSON.stringify({ host: "127.0.0.1", port: 49152 })}\n`,
);
@@ -59,8 +67,12 @@ describe("Ghidra local transport", () => {
async (value) => {
const root = await createTestTempDirectory("rea-ghidra-transport-");
roots.push(root);
const path = join(root, "bridge-endpoint.json");
await writeFile(path, JSON.stringify(value));
const runtime = await createGhidraTestRuntime(root);
runtimes.push(runtime);
const path = await publishGhidraTestEndpoint(
runtime,
JSON.stringify(value),
);
await expect(
observeGhidraEndpoint({