fix(ghidra): preserve request and shutdown failure diagnostics (#647)
This commit is contained in:
@@ -76,6 +76,8 @@ export class GhidraClient {
|
||||
#runtimeRoot: PrivateRuntimeRoot | undefined;
|
||||
#snapshotPath: string | undefined;
|
||||
#token: string | undefined;
|
||||
// Retain authentication identities for diagnostics from late request settlement.
|
||||
readonly #authenticationTokens = new Set<string>();
|
||||
#runId: string | undefined;
|
||||
readonly #lineage = new ProviderRunLineage();
|
||||
#nextId = 1;
|
||||
@@ -85,7 +87,10 @@ export class GhidraClient {
|
||||
#startupController: AbortController | undefined;
|
||||
#startPromise: Promise<GhidraStartResult> | undefined;
|
||||
#closePromise: Promise<void> | undefined;
|
||||
readonly #failure = bindGhidraSessionFailure(() => this.#diagnostics());
|
||||
readonly #failure = bindGhidraSessionFailure(
|
||||
() => this.#diagnostics(),
|
||||
(value) => this.#redactAuthentication(value),
|
||||
);
|
||||
readonly #wire: GhidraWire;
|
||||
readonly #requestQueue: GhidraRequestQueue;
|
||||
readonly #responseRouter = new GhidraResponseRouter({
|
||||
@@ -287,6 +292,7 @@ export class GhidraClient {
|
||||
}
|
||||
if (deadline.signal.aborted) return this.#startupInterrupted(deadline);
|
||||
this.#token = randomBytes(32).toString("hex");
|
||||
this.#authenticationTokens.add(this.#token);
|
||||
this.#lineage.reset();
|
||||
this.#runId = this.#options.runId ?? randomUUID();
|
||||
const launched = await this.#options.launcher
|
||||
@@ -419,12 +425,23 @@ export class GhidraClient {
|
||||
if (!forceStop && socket !== undefined && !socket.destroyed) {
|
||||
const shutdown = await this.#wire
|
||||
.request("shutdown", {}, { timeoutMs: SHUTDOWN_TIMEOUT_MS })
|
||||
.catch(() =>
|
||||
err(this.#failure("process", "Ghidra shutdown request failed")),
|
||||
.catch((cause: unknown) =>
|
||||
err(
|
||||
this.#failure("process", "Ghidra shutdown request failed", cause),
|
||||
),
|
||||
);
|
||||
if (!shutdown.ok || !isGhidraShutdownAcknowledgement(shutdown.value))
|
||||
this.#logger.warn(
|
||||
{ status: shutdown.ok ? "invalid-acknowledgement" : "failed" },
|
||||
{
|
||||
status: shutdown.ok ? "invalid-acknowledgement" : "failed",
|
||||
...(shutdown.ok
|
||||
? {}
|
||||
: {
|
||||
kind: shutdown.error.kind,
|
||||
message: shutdown.error.message,
|
||||
diagnostics: shutdown.error.diagnostics,
|
||||
}),
|
||||
},
|
||||
"Ghidra bridge shutdown was not confirmed",
|
||||
);
|
||||
}
|
||||
@@ -480,6 +497,12 @@ export class GhidraClient {
|
||||
}
|
||||
}
|
||||
|
||||
#redactAuthentication(value: string): string {
|
||||
for (const token of this.#authenticationTokens)
|
||||
value = value.replaceAll(token, "[REDACTED]");
|
||||
return value;
|
||||
}
|
||||
|
||||
#onProcessDiagnostic(event: ProviderProcessDiagnostic): void {
|
||||
if (event.type === "output") {
|
||||
this.#options.onDiagnostic?.({
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { GhidraRequestQueue } from "./GhidraRequestQueue.js";
|
||||
import { bindGhidraSessionFailure } from "./GhidraSessionError.js";
|
||||
|
||||
const failure = bindGhidraSessionFailure(() => ({
|
||||
target_path: "/tmp/queue-fixture",
|
||||
}));
|
||||
|
||||
describe("Ghidra request queue failure settlement", () => {
|
||||
it.each([new TypeError("Decoder rejected"), "decoder rejected", null])(
|
||||
"preserves a rejected executor cause (%j) and drains the next request",
|
||||
async (cause) => {
|
||||
const executed: string[] = [];
|
||||
const queue = new GhidraRequestQueue(
|
||||
async (method) => {
|
||||
executed.push(method);
|
||||
if (method === "first") throw cause;
|
||||
return ok({ method });
|
||||
},
|
||||
failure,
|
||||
() => Promise.resolve(),
|
||||
);
|
||||
const first = queue.run("first", {}, {});
|
||||
const second = queue.run("second", {}, {});
|
||||
const result = await first;
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "protocol",
|
||||
message: "Ghidra serial request execution rejected unexpectedly",
|
||||
diagnostics: { target_path: "/tmp/queue-fixture" },
|
||||
},
|
||||
});
|
||||
if (result.ok) throw new Error("Expected queue failure");
|
||||
expect(result.error.cause).toBe(cause);
|
||||
expect(result.error.diagnostics).toHaveProperty("failure_cause");
|
||||
await expect(second).resolves.toEqual(ok({ method: "second" }));
|
||||
expect(executed).toEqual(["first", "second"]);
|
||||
},
|
||||
);
|
||||
|
||||
it("does not replace an executor's typed failure", async () => {
|
||||
const original = failure("timeout", "Request deadline elapsed");
|
||||
const queue = new GhidraRequestQueue(
|
||||
() => Promise.resolve(err(original)),
|
||||
failure,
|
||||
() => Promise.resolve(),
|
||||
);
|
||||
const result = await queue.run("first", {}, {});
|
||||
expect(result).toEqual(err(original));
|
||||
if (!result.ok) expect(result.error).toBe(original);
|
||||
});
|
||||
|
||||
it("preserves cancellation cleanup failures and rejects queued work", async () => {
|
||||
const controller = new AbortController();
|
||||
const cause = new Error("Runtime removal failed");
|
||||
let release: (() => void) | undefined;
|
||||
const completed = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const queue = new GhidraRequestQueue(
|
||||
async () => {
|
||||
await completed;
|
||||
return err(failure("cancelled", "Request cancelled"));
|
||||
},
|
||||
failure,
|
||||
async () => {
|
||||
throw cause;
|
||||
},
|
||||
);
|
||||
const active = queue.run("active", {}, { signal: controller.signal });
|
||||
const queued = queue.run("queued", {}, {});
|
||||
controller.abort();
|
||||
release?.();
|
||||
const result = await active;
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "process",
|
||||
diagnostics: { failure_cause: { message: cause.message } },
|
||||
},
|
||||
});
|
||||
if (result.ok) throw new Error("Expected cleanup failure");
|
||||
expect(result.error.cause).toBe(cause);
|
||||
await expect(queued).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "process" },
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -148,13 +148,14 @@ export class GhidraRequestQueue {
|
||||
executionSettled = true;
|
||||
entry.resolve(result);
|
||||
})
|
||||
.catch(() => {
|
||||
.catch((cause: unknown) => {
|
||||
executionSettled = true;
|
||||
entry.resolve(
|
||||
err(
|
||||
this.failure(
|
||||
"protocol",
|
||||
"Ghidra serial request execution rejected unexpectedly",
|
||||
cause,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { bindGhidraSessionFailure } from "./GhidraSessionError.js";
|
||||
|
||||
const diagnostics = {
|
||||
target_path: "/tmp/public-fixture",
|
||||
target_sha256: "a".repeat(64),
|
||||
};
|
||||
|
||||
describe("Ghidra session failure diagnostics", () => {
|
||||
it("retains the original Error while projecting its name, message and code", () => {
|
||||
const cause = Object.assign(
|
||||
new TypeError("Cannot decode /tmp/public-fixture at offset 17"),
|
||||
{ code: "EDECODE" },
|
||||
);
|
||||
const failure = bindGhidraSessionFailure(() => diagnostics)(
|
||||
"protocol",
|
||||
"Request rejected",
|
||||
cause,
|
||||
);
|
||||
expect(failure.cause).toBe(cause);
|
||||
expect(failure.kind).toBe("protocol");
|
||||
expect(failure.diagnostics).toEqual({
|
||||
...diagnostics,
|
||||
failure_cause: {
|
||||
name: "TypeError",
|
||||
message: cause.message,
|
||||
code: "EDECODE",
|
||||
},
|
||||
});
|
||||
expect(failure.diagnostics).not.toHaveProperty("stack");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["decoder rejected", { type: "string", message: "decoder rejected" }],
|
||||
[null, { type: "null", value: null }],
|
||||
[false, { type: "boolean", value: false }],
|
||||
[42, { type: "number", value: 42 }],
|
||||
[{ opaque: true }, { type: "object" }],
|
||||
])("handles a non-Error rejection (%j)", (cause, expected) => {
|
||||
const failure = bindGhidraSessionFailure(() => diagnostics)(
|
||||
"process",
|
||||
"Shutdown rejected",
|
||||
cause,
|
||||
);
|
||||
expect(failure.cause).toBe(cause);
|
||||
expect(failure.diagnostics.failure_cause).toEqual(expected);
|
||||
});
|
||||
|
||||
it("redacts only known authentication material in projected failure fields", () => {
|
||||
const token = "actual-bridge-authentication";
|
||||
const message = `Failure ${token}: http://localhost/secret?token=public-value /tmp/password-fixture`;
|
||||
const cause = Object.assign(new Error(message), { code: `code-${token}` });
|
||||
const failure = bindGhidraSessionFailure(
|
||||
() => diagnostics,
|
||||
(value) => value.replaceAll(token, "[REDACTED]"),
|
||||
)("remote", message, cause, { remoteCode: `code-${token}` });
|
||||
expect(failure.cause).toBe(cause);
|
||||
expect(failure.message).toBe(message.replaceAll(token, "[REDACTED]"));
|
||||
expect(failure.diagnostics).toMatchObject({
|
||||
remote_code: "code-[REDACTED]",
|
||||
remote_message: failure.message,
|
||||
failure_cause: { message: failure.message, code: "code-[REDACTED]" },
|
||||
});
|
||||
expect(JSON.stringify(failure.diagnostics)).not.toContain(token);
|
||||
expect(JSON.stringify(failure.diagnostics)).toContain(
|
||||
"token=public-value /tmp/password-fixture",
|
||||
);
|
||||
});
|
||||
|
||||
it("leaves absent causes and existing timeout metadata unchanged", () => {
|
||||
const failure = bindGhidraSessionFailure(() => diagnostics)(
|
||||
"timeout",
|
||||
"Deadline elapsed",
|
||||
undefined,
|
||||
{ timeoutMs: 5 },
|
||||
);
|
||||
expect(failure.timeoutMs).toBe(5);
|
||||
expect(failure.diagnostics).toEqual(diagnostics);
|
||||
});
|
||||
});
|
||||
@@ -44,6 +44,7 @@ const createGhidraSessionError = (failure: {
|
||||
readonly cause?: unknown;
|
||||
readonly timeoutMs?: number;
|
||||
readonly remoteCode?: string;
|
||||
readonly redact: (value: string) => string;
|
||||
}): GhidraSessionError =>
|
||||
new GhidraSessionError(
|
||||
failure.kind,
|
||||
@@ -52,7 +53,7 @@ const createGhidraSessionError = (failure: {
|
||||
? failure.diagnostics
|
||||
: {
|
||||
...failure.diagnostics,
|
||||
remote_code: failure.remoteCode,
|
||||
remote_code: failure.redact(failure.remoteCode),
|
||||
remote_message: failure.message,
|
||||
},
|
||||
{
|
||||
@@ -68,7 +69,10 @@ const createGhidraSessionError = (failure: {
|
||||
|
||||
/** Bind session-failure construction to a live, token-redacted diagnostic view. */
|
||||
export const bindGhidraSessionFailure =
|
||||
(diagnostics: () => Readonly<Record<string, JsonValue>>) =>
|
||||
(
|
||||
diagnostics: () => Readonly<Record<string, JsonValue>>,
|
||||
redact: (value: string) => string = (value) => value,
|
||||
) =>
|
||||
(
|
||||
kind: GhidraSessionFailureKind,
|
||||
message: string,
|
||||
@@ -77,8 +81,39 @@ export const bindGhidraSessionFailure =
|
||||
): GhidraSessionError =>
|
||||
createGhidraSessionError({
|
||||
kind,
|
||||
message,
|
||||
diagnostics: diagnostics(),
|
||||
message: redact(message),
|
||||
diagnostics: {
|
||||
...diagnostics(),
|
||||
...(cause === undefined
|
||||
? {}
|
||||
: { failure_cause: describeCause(cause, redact) }),
|
||||
},
|
||||
redact,
|
||||
...(cause === undefined ? {} : { cause }),
|
||||
...options,
|
||||
});
|
||||
|
||||
const describeCause = (
|
||||
cause: unknown,
|
||||
redact: (value: string) => string,
|
||||
): JsonValue => {
|
||||
if (cause instanceof Error) {
|
||||
const code = "code" in cause ? cause.code : undefined;
|
||||
return {
|
||||
name: redact(cause.name),
|
||||
message: redact(cause.message),
|
||||
...(typeof code === "string"
|
||||
? { code: redact(code) }
|
||||
: typeof code === "number" && Number.isFinite(code)
|
||||
? { code }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
if (typeof cause === "string")
|
||||
return { type: "string", message: redact(cause) };
|
||||
if (cause === null || typeof cause === "boolean")
|
||||
return { type: cause === null ? "null" : "boolean", value: cause };
|
||||
if (typeof cause === "number" && Number.isFinite(cause))
|
||||
return { type: "number", value: cause };
|
||||
return { type: typeof cause };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user