From 59be866719e8fe8ec060515044d427a3b0a57649 Mon Sep 17 00:00:00 2001 From: Ha1baraA1 <243105435+Ha1baraA11@users.noreply.github.com> Date: Tue, 13 Oct 2026 00:45:23 +0800 Subject: [PATCH] fix(ghidra): encode Java probe output as UTF-8 (#2206) * fix(ghidra): encode Java probe output as UTF-8 * fix(ghidra): preserve Unicode Java homes across supported JDKs * test(ghidra): verify snapshot rejection without matching prose --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com> --- .github/workflows/ci.yml | 1 + scripts/ci/scopes.mjs | 1 + .../lib/real-ghidra-snapshot-lifecycle.mjs | 9 +- src/ghidra/GhidraInstallation.test.ts | 5 +- src/ghidra/GhidraInstallation.ts | 11 ++- .../filesystem/ghidraJavaProbe.test.ts | 82 +++++++++++++++++++ .../analysis-sessions/binarySession.test.ts | 8 +- 7 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 tests/boundary/filesystem/ghidraJavaProbe.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 60920f0de..ba8c02617 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -504,6 +504,7 @@ jobs: tests/boundary/filesystem/javascript-applications/javascriptArtifactReconstruction.test.ts tests/conformance/providers/ghidra/windowsCapabilities.test.ts src/ghidra/GhidraInstallation.test.ts + tests/boundary/filesystem/ghidraJavaProbe.test.ts tests/boundary/providers/ghidra/ghidraTransport.test.ts tests/boundary/providers/ghidra/ghidraTargetSnapshot.test.ts tests/boundary/providers/ghidra/ghidraLauncher.test.ts diff --git a/scripts/ci/scopes.mjs b/scripts/ci/scopes.mjs index 728cedc57..497dfeed0 100644 --- a/scripts/ci/scopes.mjs +++ b/scripts/ci/scopes.mjs @@ -64,6 +64,7 @@ export const SCOPES = { "tests/boundary/filesystem/javascript-applications/javascriptArtifactReconstruction.test.ts", "tests/conformance/providers/ghidra/windowsCapabilities.test.ts", "src/ghidra/GhidraInstallation.test.ts", + "tests/boundary/filesystem/ghidraJavaProbe.test.ts", "tests/boundary/providers/ghidra/ghidraTransport.test.ts", "tests/boundary/providers/ghidra/ghidraTargetSnapshot.test.ts", "tests/boundary/providers/ghidra/ghidraLauncher.test.ts", diff --git a/scripts/lib/real-ghidra-snapshot-lifecycle.mjs b/scripts/lib/real-ghidra-snapshot-lifecycle.mjs index 4a854c731..e359f22ac 100644 --- a/scripts/lib/real-ghidra-snapshot-lifecycle.mjs +++ b/scripts/lib/real-ghidra-snapshot-lifecycle.mjs @@ -36,16 +36,11 @@ export async function verifyGhidraSnapshotLifecycle( ); const error = requireMcpToolError(reply); assert.equal( - error?.code, + error.code, "evidence_integrity_mismatch", JSON.stringify(reply), ); - assert.match(error.message, /after analysis metadata mutations/u); - assert.match( - error.remediation.action, - /close.*without saving a snapshot/iu, - ); - assert.match(error.remediation.action, /export_evidence_bundle/u); + assert.equal(error.retryable, false); rejectedCalls++; }; try { diff --git a/src/ghidra/GhidraInstallation.test.ts b/src/ghidra/GhidraInstallation.test.ts index 059915d99..185c71973 100644 --- a/src/ghidra/GhidraInstallation.test.ts +++ b/src/ghidra/GhidraInstallation.test.ts @@ -22,6 +22,7 @@ const WINDOWS_INSTALL = "C:\\tools\\ghidra_12.1.4_PUBLIC"; const WINDOWS_PROPERTIES = `${WINDOWS_INSTALL}\\Ghidra\\application.properties`; const WINDOWS_HEADLESS = `${WINDOWS_INSTALL}\\support\\analyzeHeadless.bat`; const WINDOWS_DECOMPILER = `${WINDOWS_INSTALL}\\Ghidra\\Features\\Decompiler\\os\\win_x86_64\\decompile.exe`; + const JAVA: GhidraJavaObservation = { version: "21.0.11", major: SUPPORTED_GHIDRA_JAVA_MAJOR, @@ -43,7 +44,9 @@ const host = ( ...overrides, }); -afterEach(() => vi.unstubAllEnvs()); +afterEach(() => { + vi.unstubAllEnvs(); +}); it("probes the selected Java environment without reading conflicting ambient settings", () => { vi.stubEnv("REA_GHIDRA_CONTEXT", "ambient"); diff --git a/src/ghidra/GhidraInstallation.ts b/src/ghidra/GhidraInstallation.ts index 8d2043e6b..da762cb79 100644 --- a/src/ghidra/GhidraInstallation.ts +++ b/src/ghidra/GhidraInstallation.ts @@ -734,7 +734,16 @@ const systemGhidraInstallationHost = (): GhidraInstallationHost => ({ probeJava(command, environment) { const observed = spawnSync( command, - ["-XX:-UsePerfData", "-XshowSettings:properties", "-version"], + [ + "-Dstdout.encoding=UTF-8", + "-Dstderr.encoding=UTF-8", + // JDK 17/18 use the legacy names; JDK 19+ use the names above. + "-Dsun.stdout.encoding=UTF-8", + "-Dsun.stderr.encoding=UTF-8", + "-XX:-UsePerfData", + "-XshowSettings:properties", + "-version", + ], { encoding: "utf8", env: environment, diff --git a/tests/boundary/filesystem/ghidraJavaProbe.test.ts b/tests/boundary/filesystem/ghidraJavaProbe.test.ts new file mode 100644 index 000000000..82ed00ef7 --- /dev/null +++ b/tests/boundary/filesystem/ghidraJavaProbe.test.ts @@ -0,0 +1,82 @@ +import { mkdir, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { afterEach, expect, it, vi } from "vitest"; + +import { inspectGhidraInstallation } from "../../../src/ghidra/GhidraInstallation.js"; +import { SUPPORTED_GHIDRA_VERSION } from "../../../src/ghidra/GhidraInstallationPolicy.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +const { mockSpawnSync } = vi.hoisted(() => ({ mockSpawnSync: vi.fn() })); + +vi.mock("node:child_process", async (importOriginal) => ({ + ...(await importOriginal()), + spawnSync: mockSpawnSync, +})); + +afterEach(() => mockSpawnSync.mockReset()); + +it.each([ + { major: 17, fullJdk: true }, + { major: 21, fullJdk: true }, + { major: 21, fullJdk: false }, +])( + "preserves the reported Unicode Java home and classifies JDK $major (full JDK: $fullJdk)", + async ({ major, fullJdk }) => { + const root = await createTestTempDirectory("rea-java-probe-"); + const configuredHome = join(root, "selected-jdk"); + const reportedHome = join(root, "Réa逆向", `JDK ${String(major)}`); + const installDir = join(root, "ghidra"); + await mkdir(join(installDir, "Ghidra"), { recursive: true }); + await writeFile( + join(installDir, "Ghidra", "application.properties"), + `application.version=${SUPPORTED_GHIDRA_VERSION}\napplication.java.min=17\n`, + ); + if (fullJdk) { + await mkdir(join(reportedHome, "bin"), { recursive: true }); + await writeFile( + join( + reportedHome, + "bin", + process.platform === "win32" ? "javac.exe" : "javac", + ), + "compiler presence fixture", + { mode: 0o755 }, + ); + } + const version = `${String(major)}.0.11`; + mockSpawnSync.mockReturnValue({ + stdout: "", + stderr: ` java.version = ${version}\n java.home = ${reportedHome}\n sun.arch.data.model = 64\n`, + status: 0, + }); + + const inspection = inspectGhidraInstallation({ + environment: {}, + installDir, + javaHome: configuredHome, + }); + + expect(inspection.javaHome).toBe(reportedHome); + expect(inspection.javaVersion).toBe(version); + expect(inspection.checks.find(({ name }) => name === "java")).toMatchObject( + { + status: fullJdk ? "passed" : "failed", + detail: expect.stringContaining(fullJdk ? "; JDK;" : "; runtime only;"), + }, + ); + expect(mockSpawnSync).toHaveBeenCalledExactlyOnceWith( + join( + configuredHome, + "bin", + process.platform === "win32" ? "java.exe" : "java", + ), + expect.arrayContaining([ + "-Dstdout.encoding=UTF-8", + "-Dstderr.encoding=UTF-8", + "-Dsun.stdout.encoding=UTF-8", + "-Dsun.stderr.encoding=UTF-8", + ]), + expect.objectContaining({ encoding: "utf8" }), + ); + }, +); diff --git a/tests/composition/analysis-sessions/binarySession.test.ts b/tests/composition/analysis-sessions/binarySession.test.ts index 27e591a17..813b90004 100644 --- a/tests/composition/analysis-sessions/binarySession.test.ts +++ b/tests/composition/analysis-sessions/binarySession.test.ts @@ -374,9 +374,7 @@ describe("evidence metadata imports and snapshot cache", () => { expect(session.exportAnalysisSnapshot()).toMatchObject({ ok: false, error: { - message: expect.stringContaining( - "Analysis snapshots are unavailable because analysis metadata may have changed", - ), + _tag: "EvidenceIntegrityError", }, }); expect((await session.open(target)).ok).toBe(true); @@ -384,9 +382,7 @@ describe("evidence metadata imports and snapshot cache", () => { expect(session.importAnalysisSnapshot(baseline.value)).toMatchObject({ ok: false, error: { - userMessage: expect.stringContaining( - "Close the active target without saving a snapshot", - ), + _tag: "EvidenceIntegrityError", }, }); expect(session.exportEvidenceBundle().records).toContainEqual(relocated);