Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(ghidra): parse quoted JVM environment options before admission
  • Loading branch information
morluto committed Oct 13, 2026
commit 9d70fa84d09aa91f6cd1b5bc877fd1aba0df870d
51 changes: 34 additions & 17 deletions src/ghidra/GhidraJavaLaunch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@ import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
import { dirname, join } from "node:path";
import { execFileOutput } from "../process/ExecFileOutput.js";

/** JVM option environments remove quotes, including quotes inside an option. */
const jvmEnvironmentArguments = (value: string): readonly string[] => {
const arguments_: string[] = [];
let argument = "";
let quote: "'" | '"' | undefined;
for (const character of value) {
if (quote !== undefined) {
if (character === quote) quote = undefined;
else argument += character;
} else if (character === "'" || character === '"') quote = character;
else if (/\s/u.test(character)) {
if (argument !== "") arguments_.push(argument);
argument = "";
} else argument += character;
}
if (quote !== undefined)
throw new Error(
"Invalid quoted Ghidra LaunchSupport JVM environment option",
);
if (argument !== "") arguments_.push(argument);
return arguments_;
};

const rejectJvmPlatformOverrides = (arguments_: readonly string[]): void => {
if (
arguments_.some((argument) => /^-Dos\.(?:arch|name)(?:=|$)/u.test(argument))
)
throw new Error(
"Ghidra LaunchSupport may not override os.arch or os.name; the native decompiler was validated for the inspected JVM platform.",
);
};

/** Launch coordinates obtained from Ghidra's own release configuration helper. */
export const ghidraJavaLaunch = async (options: {
readonly platform: NodeJS.Platform;
Expand Down Expand Up @@ -87,15 +119,7 @@ export const ghidraJavaLaunch = async (options: {
"_JAVA_OPTIONS",
"JDK_JAVA_OPTIONS",
] as const) {
const value = environment[key] ?? "";
if (
value
.split(/\s+/u)
.some((option) => /^-Dos\.(?:arch|name)(?:=|$)/u.test(option))
)
throw new Error(
"Ghidra LaunchSupport may not override os.arch or os.name; the native decompiler was validated for the inspected JVM platform.",
);
rejectJvmPlatformOverrides(jvmEnvironmentArguments(environment[key] ?? ""));
}
const configuredArguments = await execFileOutput(
command,
Expand All @@ -118,14 +142,7 @@ export const ghidraJavaLaunch = async (options: {
)
)
throw new Error("Invalid Ghidra LaunchSupport JVM argument");
if (
vmArguments.some((argument) =>
/^-Dos\.(?:arch|name)(?:=|$)/u.test(argument),
)
)
throw new Error(
"Ghidra LaunchSupport may not override os.arch or os.name; the native decompiler was validated for the inspected JVM platform.",
);
rejectJvmPlatformOverrides(vmArguments);
return {
command,
environment,
Expand Down
43 changes: 32 additions & 11 deletions tests/boundary/providers/ghidra/ghidraJavaLaunch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if (mode === "env-failure" || (probe === "-vmargs" && mode === "vm-failure")) {
process.stderr.write("failed " + probe + "\\n");
process.exitCode = 3;
} else if (probe === "-envvars") {
process.stdout.write(mode === "env-malformed" ? "invalid environment\\n" : mode === "env-injection" ? "JAVA_TOOL_OPTIONS=-Dos.arch=x86_64\\n" : "GHIDRA_MAXMEM=1G\\n");
process.stdout.write(mode === "env-malformed" ? "invalid environment\\n" : mode.startsWith("env-injection:") ? mode.slice("env-injection:".length) + "\\n" : "GHIDRA_MAXMEM=1G\\n");
} else {
const vmArguments = mode === "vm-malformed"
? "invalid JVM argument\\n"
Expand Down Expand Up @@ -239,17 +239,38 @@ describe.skipIf(process.platform === "win32")(
},
);

it("rejects LaunchSupport helper-environment JVM platform injection before starting analysis", async () => {
const { session, launcher, analysisArguments } =
await fixture("env-injection");
it.each([
["JAVA_TOOL_OPTIONS", "-Dos.arch=x86_64"],
["JAVA_TOOL_OPTIONS", '"-Dos.arch=x86_64"'],
["_JAVA_OPTIONS", "'-Dos.name=Windows'"],
["JDK_JAVA_OPTIONS", '-D"os.name"=Windows'],
["JDK_JAVA_OPTIONS", "-D'os.arch'=x86_64"],
])(
"rejects helper-environment platform injection from %s=%s before analysis",
async (key, value) => {
const { session, launcher, analysisArguments } = await fixture(
`env-injection:${key}=${value}`,
);
const launched = await launcher.launch(session);
expect(launched.ok).toBe(false);
expect(analysisArguments).toEqual([]);
if (launched.ok)
throw new Error("Expected a JVM platform override failure");
expect(launched.error.cause).toMatchObject({
message: expect.stringContaining(
"may not override os.arch or os.name",
),
});
},
);

it("admits a quoted non-platform option without mistaking its value for a platform argument", async () => {
const { session, launcher, analysisArguments } = await fixture(
'env-injection:JAVA_TOOL_OPTIONS=-Ddescription="text -Dos.arch=x86_64"',
);
const launched = await launcher.launch(session);
expect(launched.ok).toBe(false);
expect(analysisArguments).toEqual([]);
if (launched.ok)
throw new Error("Expected a JVM platform override failure");
expect(launched.error.cause).toMatchObject({
message: expect.stringContaining("may not override os.arch or os.name"),
});
if (!launched.ok) throw launched.error;
expect(analysisArguments).toHaveLength(1);
});

it("bounds retained UTF-8 warnings and reports original byte counts", async () => {
Expand Down
Loading