feat(ghidra): add authenticated Windows loopback transport
Keep private Unix sockets on supported POSIX hosts and add a Windows P0 transport that binds the Java bridge to IPv4 loopback on an ephemeral port. Publish only a strict, token-free endpoint record through an atomic rename, validate it before connecting, and retain the same authenticated request protocol, bounded queue, handshake, cancellation, and shutdown semantics across both transports.
This commit is contained in:
Vendored
+25
-6
@@ -1,23 +1,25 @@
|
||||
import { rm } from "node:fs/promises";
|
||||
import { rename, rm, writeFile } from "node:fs/promises";
|
||||
import { createServer } from "node:net";
|
||||
|
||||
const [
|
||||
socketPath,
|
||||
endpointPath,
|
||||
token,
|
||||
runId,
|
||||
providerVersion,
|
||||
profileDigest,
|
||||
targetSha256,
|
||||
transport,
|
||||
mode = "success",
|
||||
] = process.argv.slice(2);
|
||||
|
||||
if (
|
||||
socketPath === undefined ||
|
||||
endpointPath === undefined ||
|
||||
token === undefined ||
|
||||
runId === undefined ||
|
||||
providerVersion === undefined ||
|
||||
profileDigest === undefined ||
|
||||
targetSha256 === undefined
|
||||
targetSha256 === undefined ||
|
||||
!["unix-socket", "authenticated-loopback-tcp"].includes(transport)
|
||||
) {
|
||||
process.exit(64);
|
||||
}
|
||||
@@ -25,7 +27,7 @@ if (
|
||||
if (mode === "exit") process.exit(73);
|
||||
if (mode === "silent") setInterval(() => undefined, 1_000);
|
||||
else {
|
||||
await rm(socketPath, { force: true });
|
||||
await rm(endpointPath, { force: true });
|
||||
const server = createServer((socket) => {
|
||||
socket.setEncoding("utf8");
|
||||
let buffer = "";
|
||||
@@ -128,7 +130,24 @@ else {
|
||||
}
|
||||
});
|
||||
});
|
||||
server.listen(socketPath);
|
||||
if (transport === "unix-socket") server.listen(endpointPath);
|
||||
else {
|
||||
server.listen(0, "127.0.0.1", async () => {
|
||||
const address = server.address();
|
||||
if (address === null || typeof address === "string") process.exit(65);
|
||||
const pending = `${endpointPath}.pending`;
|
||||
await writeFile(
|
||||
pending,
|
||||
`${JSON.stringify({
|
||||
schema_version: 1,
|
||||
host: "127.0.0.1",
|
||||
port: address.port,
|
||||
})}\n`,
|
||||
{ flag: "wx" },
|
||||
);
|
||||
await rename(pending, endpointPath);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const sessionInfo = ({
|
||||
|
||||
@@ -79,6 +79,18 @@ describe("Ghidra bridge truthfulness", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("binds the Windows fallback to authenticated IPv4 loopback", () => {
|
||||
expect(bridgeSource).toContain(
|
||||
'descriptor.transport.equals("unix-socket")',
|
||||
);
|
||||
expect(bridgeSource).toContain(
|
||||
'descriptor.transport.equals("authenticated-loopback-tcp")',
|
||||
);
|
||||
expect(bridgeSource).toContain('InetAddress.getByName("127.0.0.1")');
|
||||
expect(bridgeSource).toContain('endpoint.addProperty("host", "127.0.0.1")');
|
||||
expect(bridgeSource).toContain("StandardCopyOption.ATOMIC_MOVE");
|
||||
});
|
||||
|
||||
it("does not admit mutation methods into the Java dispatch", () => {
|
||||
for (const method of [
|
||||
'case "set_comment"',
|
||||
|
||||
@@ -17,6 +17,7 @@ import type {
|
||||
GhidraLaunchSession,
|
||||
GhidraLauncher,
|
||||
} from "../src/ghidra/GhidraLauncher.js";
|
||||
import type { GhidraTransportKind } from "../src/ghidra/GhidraTransport.js";
|
||||
import { GHIDRA_SESSION_CAPABILITIES } from "../src/ghidra/GhidraSessionValues.js";
|
||||
|
||||
const fixturePath = fileURLToPath(
|
||||
@@ -46,7 +47,7 @@ type FixtureMode =
|
||||
|
||||
class FixtureLauncher implements GhidraLauncher {
|
||||
readonly runtimeRoots: string[] = [];
|
||||
readonly socketPaths: string[] = [];
|
||||
readonly endpointPaths: string[] = [];
|
||||
readonly tokens: string[] = [];
|
||||
readonly processes: ChildProcess[] = [];
|
||||
|
||||
@@ -54,7 +55,7 @@ class FixtureLauncher implements GhidraLauncher {
|
||||
|
||||
async launch(session: GhidraLaunchSession) {
|
||||
this.runtimeRoots.push(session.runtimeRoot);
|
||||
this.socketPaths.push(session.socketPath);
|
||||
this.endpointPaths.push(session.endpointPath);
|
||||
this.tokens.push(session.token);
|
||||
const projectRoot = join(session.runtimeRoot, "project");
|
||||
await mkdir(projectRoot, { recursive: true });
|
||||
@@ -62,12 +63,13 @@ class FixtureLauncher implements GhidraLauncher {
|
||||
process.execPath,
|
||||
[
|
||||
fixturePath,
|
||||
session.socketPath,
|
||||
session.endpointPath,
|
||||
session.token,
|
||||
session.runId,
|
||||
session.providerVersion,
|
||||
session.profileDigest,
|
||||
session.targetSha256,
|
||||
session.transport,
|
||||
this.mode,
|
||||
],
|
||||
{ stdio: ["ignore", "pipe", "pipe"] },
|
||||
@@ -90,12 +92,16 @@ const clientFor = (
|
||||
readonly startupTimeoutMs?: number;
|
||||
readonly requestTimeoutMs?: number;
|
||||
readonly onDiagnostic?: (event: GhidraDiagnostic) => void;
|
||||
readonly transport?: GhidraTransportKind;
|
||||
} = {},
|
||||
): GhidraClient => {
|
||||
const client = new GhidraClient({
|
||||
launcher,
|
||||
targetPath: fixturePath,
|
||||
targetSha256: TARGET_SHA256,
|
||||
...(options.transport === undefined
|
||||
? {}
|
||||
: { transport: options.transport }),
|
||||
providerVersion: PROVIDER_VERSION,
|
||||
profileDigest: PROFILE_DIGEST,
|
||||
startupTimeoutMs: options.startupTimeoutMs ?? 1_000,
|
||||
@@ -132,7 +138,25 @@ describe("GhidraClient", () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(Buffer.byteLength(launcher.socketPaths[0] ?? "")).toBeLessThan(108);
|
||||
expect(Buffer.byteLength(launcher.endpointPaths[0] ?? "")).toBeLessThan(
|
||||
108,
|
||||
);
|
||||
});
|
||||
|
||||
it("completes the same authenticated handshake over loopback TCP", async () => {
|
||||
const launcher = new FixtureLauncher();
|
||||
const client = clientFor(launcher, {
|
||||
transport: "authenticated-loopback-tcp",
|
||||
});
|
||||
|
||||
await expect(client.start()).resolves.toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
bridge_version: 4,
|
||||
target: { sha256: TARGET_SHA256 },
|
||||
},
|
||||
});
|
||||
expect(launcher.endpointPaths[0]).toMatch(/bridge-endpoint\.json$/u);
|
||||
});
|
||||
|
||||
it("correlates an admitted inventory request after startup", async () => {
|
||||
@@ -369,7 +393,8 @@ describe("GhidraClient", () => {
|
||||
|
||||
expect(client.diagnostics()).toMatchObject({
|
||||
runtime_root: launcher.runtimeRoots[0],
|
||||
socket_path: launcher.socketPaths[0],
|
||||
transport: "unix-socket",
|
||||
endpoint_path: launcher.endpointPaths[0],
|
||||
project_root: join(launcher.runtimeRoots[0] ?? "", "project"),
|
||||
process_id: expect.any(Number),
|
||||
});
|
||||
|
||||
@@ -72,7 +72,8 @@ describe("Ghidra headless launcher", () => {
|
||||
});
|
||||
const launched = await launcher.launch({
|
||||
runtimeRoot,
|
||||
socketPath: join(runtimeRoot, "bridge.sock"),
|
||||
transport: "unix-socket",
|
||||
endpointPath: join(runtimeRoot, "bridge.sock"),
|
||||
token,
|
||||
runId: "d6fcbb66-e829-4ff6-a535-0035aec63139",
|
||||
targetPath: "/tmp/fixture",
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { observeGhidraEndpoint } from "../src/ghidra/GhidraTransport.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
|
||||
describe("Ghidra local transport", () => {
|
||||
it("observes Unix readiness without interpreting endpoint content", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-ghidra-transport-"));
|
||||
roots.push(root);
|
||||
const path = join(root, "bridge.sock");
|
||||
|
||||
await expect(
|
||||
observeGhidraEndpoint({ transport: "unix-socket", path }),
|
||||
).resolves.toEqual({ ok: true, value: null });
|
||||
await writeFile(path, "fixture");
|
||||
await expect(
|
||||
observeGhidraEndpoint({ transport: "unix-socket", path }),
|
||||
).resolves.toEqual({ ok: true, value: { path } });
|
||||
});
|
||||
|
||||
it("accepts only an exact IPv4 loopback endpoint record", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-ghidra-transport-"));
|
||||
roots.push(root);
|
||||
const path = join(root, "bridge-endpoint.json");
|
||||
await writeFile(
|
||||
path,
|
||||
`${JSON.stringify({ schema_version: 1, host: "127.0.0.1", port: 49152 })}\n`,
|
||||
);
|
||||
|
||||
await expect(
|
||||
observeGhidraEndpoint({
|
||||
transport: "authenticated-loopback-tcp",
|
||||
path,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
ok: true,
|
||||
value: { host: "127.0.0.1", port: 49152 },
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ schema_version: 1, host: "0.0.0.0", port: 49152 },
|
||||
{ schema_version: 1, host: "127.0.0.1", port: 0 },
|
||||
{ schema_version: 1, host: "127.0.0.1", port: 49152, token: "leak" },
|
||||
])(
|
||||
"rejects an invalid or expanded TCP endpoint: $host:$port",
|
||||
async (value) => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-ghidra-transport-"));
|
||||
roots.push(root);
|
||||
const path = join(root, "bridge-endpoint.json");
|
||||
await writeFile(path, JSON.stringify(value));
|
||||
|
||||
await expect(
|
||||
observeGhidraEndpoint({
|
||||
transport: "authenticated-loopback-tcp",
|
||||
path,
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "start", message: "Ghidra TCP endpoint is invalid" },
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user