Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(mcp): honor Frida partial evidence retention acknowledgments

morluto36615d9165a8
diff --git a/src/server/registerFridaTools.ts b/src/server/registerFridaTools.ts
index fb2e06bb4..a56ec9578 100644
--- a/src/server/registerFridaTools.ts
+++ b/src/server/registerFridaTools.ts
@@ -6,7 +6,9 @@ import { FridaInstrumentationService } from "../application/frida/FridaInstrumen
import type { FridaInstrumentationPort } from "../application/frida/FridaInstrumentationPort.js";
import type { FridaRemoteConnection } from "../application/frida/FridaInstrumentationPort.js";
import { toolContract } from "../contracts/toolContracts.js";
+import type { ToolContract } from "../contracts/toolContractTypes.js";
import { evidenceSchema } from "../domain/evidence.js";
+import type { AnalysisError } from "../domain/analysisErrorBase.js";
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
import { jsonValueSchema } from "../domain/jsonValue.js";
import { logToolExecution } from "./toolLogging.js";
@@ -121,8 +123,12 @@ export const registerFridaTools = (
}),
);
if (!result.ok) {
- recordPartialEvidence(result.error, recordEvidence);
- return delivery.toCallToolResult(result, start);
+ return deliverPartialEvidenceError(
+ result,
+ start,
+ delivery,
+ recordEvidence,
+ );
}
return delivery.toCallToolResult(
{
@@ -162,8 +168,12 @@ export const registerFridaTools = (
}),
);
if (!result.ok) {
- recordPartialEvidence(result.error, recordEvidence);
- return delivery.toCallToolResult(result, load);
+ return deliverPartialEvidenceError(
+ result,
+ load,
+ delivery,
+ recordEvidence,
+ );
}
return delivery.toEvidenceToolResult(
result.value,
@@ -295,8 +305,12 @@ export const registerFridaTools = (
() => operation,
);
if (!result.ok) {
- recordPartialEvidence(result.error, recordEvidence);
- return delivery.toCallToolResult(result, instrument);
+ return deliverPartialEvidenceError(
+ result,
+ instrument,
+ delivery,
+ recordEvidence,
+ );
}
return delivery.toEvidenceToolResult(
result.value.evidence,
@@ -307,13 +321,21 @@ export const registerFridaTools = (
);
};
-const recordPartialEvidence = (
- error: { readonly partialObservation?: unknown },
+const deliverPartialEvidenceError = (
+ result: { readonly ok: false; readonly error: AnalysisError },
+ contract: ToolContract,
+ delivery: ToolResultDelivery,
recordEvidence: EvidenceWriter["recordEvidence"] | undefined,
-): void => {
- if (error.partialObservation === undefined) return;
- const parsed = evidenceSchema.safeParse(error.partialObservation);
- if (parsed.success) recordEvidence?.(parsed.data);
+): ReturnType<ToolResultDelivery["toCallToolResult"]> => {
+ const parsed = evidenceSchema.safeParse(result.error.partialObservation);
+ if (!parsed.success || recordEvidence === undefined)
+ return delivery.toCallToolResult(result, contract);
+ return delivery.toRecordedPartialErrorToolResult(
+ result.error,
+ parsed.data.evidence_id,
+ recordEvidence(parsed.data),
+ contract.returnsTargetContent,
+ );
};
const remoteConnection = (
diff --git a/tests/boundary/mcp/fridaLifecycleMcp.test.ts b/tests/boundary/mcp/fridaLifecycleMcp.test.ts
index 2f1853125..4c657668e 100644
--- a/tests/boundary/mcp/fridaLifecycleMcp.test.ts
+++ b/tests/boundary/mcp/fridaLifecycleMcp.test.ts
@@ -11,11 +11,29 @@ import type {
} from "frida";
import { expect, it, onTestFinished } from "vitest";
+import { FridaScriptLoadCancelledError } from "../../../src/application/frida/FridaInstrumentationPort.js";
+import type {
+ FridaInstrumentationPort,
+ FridaScriptObservation,
+} from "../../../src/application/frida/FridaInstrumentationPort.js";
+import type { EvidenceWriter } from "../../../src/application/investigation/InvestigationRecordPort.js";
import { toolContract } from "../../../src/contracts/toolContracts.js";
+import type { AnalysisPartialObservation } from "../../../src/domain/analysisErrorBase.js";
+import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
+import { EvidenceIntegrityError } from "../../../src/domain/evidenceErrors.js";
import { evidenceBundleSchema } from "../../../src/domain/evidenceBundle.js";
+import type { Evidence } from "../../../src/domain/evidence.js";
+import { jsonObjectSchema } from "../../../src/domain/jsonValue.js";
+import { nativeCallPartialObservationSchema } from "../../../src/domain/native/nativeCallPartialObservation.js";
+import { err, ok } from "../../../src/domain/result.js";
import { FridaInstrumentationManager } from "../../../src/frida/FridaInstrumentationManager.js";
+import { silentLogger } from "../../../src/logger.js";
import { createServer } from "../../../src/server/createServer.js";
+import { EvidenceMcpServer } from "../../../src/server/EvidenceMcpServer.js";
+import { registerFridaTools } from "../../../src/server/registerFridaTools.js";
+import { createToolResultDelivery } from "../../../src/server/toolResult.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
+import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
const gate = () => {
let markEntered = () => {};
@@ -209,6 +227,193 @@ const connectedFixture = async () => {
return { ...native, manager, session, server, client, start, load };
};
+type PartialRecording = "acknowledged" | "failed" | "unavailable";
+type PartialObservation = "evidence" | "non-evidence" | "unavailable";
+
+const unusedFridaPortMethod = (): never => {
+ throw new Error("Unused Frida test port method");
+};
+
+const nonEvidencePartialObservation = (): AnalysisPartialObservation =>
+ nativeCallPartialObservationSchema.parse({
+ kind: "native-call-observation",
+ target: {
+ path: "/fixture/target",
+ sha256: "b".repeat(64),
+ architecture: "arm64",
+ arguments: [],
+ environment: {},
+ working_directory: null,
+ },
+ process: {
+ pid: null,
+ stdout: null,
+ stderr: null,
+ other_stops: [],
+ },
+ debugger: { version: null },
+ events: [],
+ coverage: { status: "partial", reason: "cancelled" },
+ limitations: [],
+ });
+
+const callPartialFridaLoadError = async (
+ recording: PartialRecording,
+ partial: PartialObservation,
+) => {
+ const script: FridaScriptObservation = {
+ scriptId: "00000000-0000-4000-8000-000000000002",
+ sourceKind: "inline",
+ sourcePath: null,
+ sourceSha256: "a".repeat(64),
+ messages: [{ type: "send", payload: "partial" }],
+ messagesTruncated: false,
+ };
+ const failure =
+ partial === "evidence"
+ ? new FridaScriptLoadCancelledError(script)
+ : new AnalysisCancelledError("load_frida_script", {
+ ...(partial === "non-evidence"
+ ? { partialObservation: nonEvidencePartialObservation() }
+ : {}),
+ });
+ const provider: FridaInstrumentationPort = {
+ async listDevices() {
+ return unusedFridaPortMethod();
+ },
+ async listProcesses() {
+ return unusedFridaPortMethod();
+ },
+ async startSession() {
+ return unusedFridaPortMethod();
+ },
+ async loadScript() {
+ return err(failure);
+ },
+ async resumeSession() {
+ return unusedFridaPortMethod();
+ },
+ async unloadScript() {
+ return unusedFridaPortMethod();
+ },
+ status() {
+ return unusedFridaPortMethod();
+ },
+ async closeSession() {
+ return unusedFridaPortMethod();
+ },
+ async closeAll() {
+ return unusedFridaPortMethod();
+ },
+ };
+ const attempts: Evidence[] = [];
+ const recordEvidence: EvidenceWriter["recordEvidence"] | undefined =
+ recording === "unavailable"
+ ? undefined
+ : (evidence) => {
+ attempts.push(evidence);
+ return recording === "acknowledged"
+ ? ok("added" as const)
+ : err(new EvidenceIntegrityError("sentinel record failure"));
+ };
+ const delivery = createToolResultDelivery(1024 * 1024);
+ const server = new EvidenceMcpServer(
+ { name: "frida-partial-probe", version: "1" },
+ { capabilities: {} },
+ recordEvidence,
+ delivery,
+ );
+ registerFridaTools(server, provider, silentLogger, delivery, recordEvidence);
+ const client = new Client({
+ name: "frida-partial-probe-client",
+ version: "1",
+ });
+ const [clientTransport, serverTransport] =
+ InMemoryTransport.createLinkedPair();
+ await server.connect(serverTransport);
+ await client.connect(clientTransport);
+ try {
+ const response = await client.callTool({
+ name: "load_frida_script",
+ arguments: {
+ session_id: "00000000-0000-4000-8000-000000000001",
+ source_kind: "inline",
+ source: "send('partial');",
+ },
+ });
+ return {
+ response,
+ error: parseMcpToolError(response).error,
+ attempts,
+ };
+ } finally {
+ await client.close();
+ await server.close();
+ }
+};
+
+it("projects acknowledged Frida partial Evidence as a retained reference", async () => {
+ const { response, error, attempts } = await callPartialFridaLoadError(
+ "acknowledged",
+ "evidence",
+ );
+ expect(response.isError).toBe(true);
+ expect(response.structuredContent).toBeUndefined();
+ expect(attempts).toHaveLength(1);
+ const details = jsonObjectSchema.parse(error.details);
+ expect(details.partial_observation).toEqual({
+ kind: "retained-evidence",
+ evidence_id: attempts[0]?.evidence_id,
+ });
+});
+
+it("preserves inline Frida partial Evidence when its writer rejects it", async () => {
+ const { response, error, attempts } = await callPartialFridaLoadError(
+ "failed",
+ "evidence",
+ );
+ expect(response.isError).toBe(true);
+ expect(response.structuredContent).toBeUndefined();
+ expect(attempts).toHaveLength(1);
+ expect(error.code).toBe("cancelled");
+ const details = jsonObjectSchema.parse(error.details);
+ expect(details.partial_observation).toEqual(attempts[0]);
+});
+
+it.each([
+ ["unavailable writer", "unavailable", "evidence"],
+ ["non-Evidence partial", "acknowledged", "non-evidence"],
+ ["missing partial", "acknowledged", "unavailable"],
+] as const)(
+ "preserves Frida error projection for %s",
+ async (_, recording, partial) => {
+ const { response, error, attempts } = await callPartialFridaLoadError(
+ recording,
+ partial,
+ );
+ expect(response.isError).toBe(true);
+ expect(response.structuredContent).toBeUndefined();
+ const details = jsonObjectSchema.parse(error.details);
+ if (partial === "evidence") {
+ expect(details.partial_observation).toMatchObject({
+ operation: "load_frida_script",
+ normalized_result: {
+ script_id: "00000000-0000-4000-8000-000000000002",
+ },
+ });
+ } else if (partial === "non-evidence") {
+ expect(details.partial_observation).toEqual(
+ nonEvidencePartialObservation(),
+ );
+ } else {
+ expect(details).not.toHaveProperty("partial_observation");
+ }
+ expect(attempts).toHaveLength(
+ partial === "evidence" && recording !== "unavailable" ? 1 : 0,
+ );
+ },
+);
+
const scriptLoadFixture = async (stage: "create" | "load" = "load") => {
const fixture = await connectedFixture();
const id = await fixture.start();