fix(ghidra): preserve Unicode boundaries without breaking sessions

Validate native annotation text before mutation and escape surrogate code units in Java bridge JSON replies. Reject unsupported text with field and index diagnostics while preserving supported Unicode and line endings.

Probe real CLI/MCP annotation, transport, mapping, rollback and cleanup behavior; replace seven redundant unit tests with E2E and SDK boundary coverage.
This commit is contained in:
morluto
2026-10-09 02:50:33 +08:00
parent 64717ad7da
commit bef234d6ce
8 changed files with 222 additions and 174 deletions
-46
View File
@@ -3,7 +3,6 @@ import { describe, expect, it } from "vitest";
import type { JsonValue } from "../domain/jsonValue.js";
import { functionDossierSchema } from "../domain/hopperValues.js";
import {
parseGhidraFunctionInput,
parseGhidraFunctionResult,
type GhidraFunctionOperation,
} from "./GhidraFunctionValues.js";
@@ -16,51 +15,6 @@ import {
ghidraReferenceEdge,
} from "../domain/ghidraValues.fixture.js";
describe("Ghidra function-analysis boundary values", () => {
it("defaults inputs and rejects undeclared or implicit addresses", () => {
expect(
parseGhidraFunctionInput("procedure_info", { procedure: "main" }),
).toEqual({
ok: true,
value: { document: null, procedure: "main" },
});
expect(
parseGhidraFunctionInput("procedure_references", { procedure: "main" }),
).toEqual({
ok: true,
value: {
document: null,
procedure: "main",
direction: "outgoing",
},
});
expect(
parseGhidraFunctionInput("analyze_function", { procedure: "main" }),
).toMatchObject({
ok: true,
value: { procedure: "main" },
});
expect(
parseGhidraFunctionInput("read_function_instructions", {
procedure: "main",
}),
).toEqual({
ok: true,
value: { document: null, procedure: "main" },
});
expect(parseGhidraFunctionInput("xrefs", {})).toMatchObject({
ok: false,
error: { _tag: "AnalysisInputError" },
});
expect(
parseGhidraFunctionInput("procedure_info", {
procedure: "main",
extra: true,
}),
).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" } });
});
});
describe("Ghidra function-analysis result values", () => {
it("parses provider-classified function facts and reference kinds", () => {
expect(
+1 -115
View File
@@ -1,120 +1,6 @@
import { describe, expect, it } from "vitest";
import {
isGhidraInventoryOperation,
parseGhidraInventoryInput,
parseGhidraInventoryResult,
} from "./GhidraInventoryValues.js";
describe("Ghidra inventory boundary values", () => {
it("admits explicit byte reads and source offset mapping without invented caps", () => {
expect(
parseGhidraInventoryInput("read_bytes", { address: "0x10000" }),
).toEqual({
ok: true,
value: { address: "0x10000", document: null, length: 256 },
});
expect(
parseGhidraInventoryInput("read_bytes", {
address: "0x10000",
length: 4097,
}).ok,
).toBe(true);
expect(
parseGhidraInventoryInput("read_bytes", { address: "0x10000", length: 0 })
.ok,
).toBe(false);
expect(
parseGhidraInventoryInput("address_to_file_offset", {
address: "HEADER:0x1",
}).ok,
).toBe(true);
});
it("checks byte length and completeness before accepting memory Evidence", () => {
const result = {
address: "0x10000",
requested_bytes: 4,
returned_bytes: 2,
bytes_hex: "0410",
complete: false,
};
expect(parseGhidraInventoryResult("read_bytes", result)).toEqual({
ok: true,
value: result,
});
for (const change of [
{ complete: true },
{ returned_bytes: 4 },
{ bytes_hex: "04" },
{ requested_bytes: 1 },
])
expect(
parseGhidraInventoryResult("read_bytes", { ...result, ...change }).ok,
).toBe(false);
expect(
parseGhidraInventoryResult("address_to_file_offset", {
address: "0x10000",
file_offset: -1,
}).ok,
).toBe(false);
});
it("accepts complete inventories and searches without page controls", () => {
expect(parseGhidraInventoryInput("list_procedures", {})).toEqual({
ok: true,
value: { document: null },
});
expect(
parseGhidraInventoryInput("search_strings", { pattern: "needle" }),
).toEqual({
ok: true,
value: {
pattern: "needle",
mode: "literal",
case_sensitive: false,
document: null,
},
});
expect(
parseGhidraInventoryInput("list_procedures", { limit: 100 }),
).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" } });
expect(
parseGhidraInventoryInput("list_documents", { extra: true }),
).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" } });
});
it("keeps the admitted operation set closed", () => {
expect(isGhidraInventoryOperation("list_names")).toBe(true);
expect(isGhidraInventoryOperation("goto_address")).toBe(false);
expect(isGhidraInventoryOperation("set_comment")).toBe(false);
});
it("returns complete typed inventory arrays", () => {
const procedures = Array.from({ length: 700 }, (_, index) => ({
address: `0x${(0x401000 + index).toString(16)}`,
value: `sub_${index}`,
procedure: { external: false, thunk: false, thunk_target: null },
}));
expect(parseGhidraInventoryResult("list_procedures", procedures)).toEqual({
ok: true,
value: procedures,
});
});
it("rejects malformed inventory items", () => {
expect(
parseGhidraInventoryResult("list_procedures", [
{
address: "00401000",
value: "main",
procedure: { external: false, thunk: false, thunk_target: null },
},
]),
).toMatchObject({ ok: false, error: { _tag: "AnalysisOutputError" } });
expect(
parseGhidraInventoryResult("search_strings", [{ address: "0x1000" }]),
).toMatchObject({ ok: false, error: { _tag: "AnalysisOutputError" } });
});
});
import { parseGhidraInventoryResult } from "./GhidraInventoryValues.js";
// Wire-boundary fixture only; real-provider acceptance belongs to the DOS verifier.
describe("containing-procedure complete body evidence", () => {
+1
View File
@@ -46,6 +46,7 @@ export const limitationsFor = (operation: string): readonly string[] => {
return [
...common,
"Names use Ghidra USER_DEFINED source. Name writes preserve the existing namespace; readback uses the fully qualified name. Regular comments map to PRE and inline comments to EOL at the exact function entry. Changes commit together after readback and refreshed analysis; failure rolls them all back.",
"Annotation text rejects NUL and unpaired Unicode surrogates before mutation, with the field and UTF-16 index in the error. Supported Unicode and line endings are preserved.",
"Metadata edits invalidate immutable analysis snapshots and are discarded on close. CLI returns the updated dossier before session cleanup; this is not a saved Ghidra project.",
];
case "inspect_native_load_image":