fix(frida): name the argument each Frida input error rejects (#2217)
Frida session lookups, script lookups, unreadable script files and the instrument-with-frida PID and script checks returned invalid_request with no issues, while the remediation told callers to correct the listed arguments. Attach the session_id, script_id, path, target or script issue with the reason so CLI and MCP callers can see what to fix. Fixes #2213 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
109769319a
commit
86de439ba4
@@ -986,3 +986,39 @@ it("serializes script loading against concurrent close and unloads the loaded sc
|
||||
expect(bindings.device.session.scripts[0]?.isDestroyed).toBe(true);
|
||||
expect(bindings.device.session.isDetached()).toBe(true);
|
||||
});
|
||||
|
||||
it("names the unknown session or script ID it rejects", async () => {
|
||||
const { manager, started } = await startLocalAttachedSession();
|
||||
if (!started.ok) throw started.error;
|
||||
const missing = "00000000-0000-4000-8000-000000000009";
|
||||
const sessionIssue = {
|
||||
path: ["session_id"],
|
||||
reason: "invalid_value",
|
||||
message: `No open Frida session ${missing}; start one with start_frida_session.`,
|
||||
};
|
||||
for (const result of [
|
||||
await manager.closeSession(missing),
|
||||
await manager.resumeSession(missing),
|
||||
await manager.loadScript(missing, { sourceKind: "inline", source: "" }),
|
||||
await manager.unloadScript(missing, missing),
|
||||
])
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisInputError", issues: [sessionIssue] },
|
||||
});
|
||||
expect(
|
||||
await manager.unloadScript(started.value.sessionId, missing),
|
||||
).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
issues: [
|
||||
{
|
||||
path: ["script_id"],
|
||||
reason: "invalid_value",
|
||||
message: `No loaded Frida script ${missing} in this session.`,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
await manager.closeAll();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { AnalysisInputError } from "../../domain/analysisErrorCore.js";
|
||||
|
||||
/** A session ID that names no open Frida session owned by this process. */
|
||||
export const unknownFridaSession = (
|
||||
operation: string,
|
||||
sessionId: string,
|
||||
): AnalysisInputError =>
|
||||
new AnalysisInputError(operation, undefined, [
|
||||
{
|
||||
path: ["session_id"],
|
||||
reason: "invalid_value",
|
||||
message: `No open Frida session ${sessionId}; start one with start_frida_session.`,
|
||||
},
|
||||
]);
|
||||
|
||||
/** A script ID that names no loaded script in the selected session. */
|
||||
export const unknownFridaScript = (
|
||||
operation: string,
|
||||
scriptId: string,
|
||||
): AnalysisInputError =>
|
||||
new AnalysisInputError(operation, undefined, [
|
||||
{
|
||||
path: ["script_id"],
|
||||
reason: "invalid_value",
|
||||
message: `No loaded Frida script ${scriptId} in this session.`,
|
||||
},
|
||||
]);
|
||||
@@ -220,7 +220,18 @@ export const registerFridaCommands = (
|
||||
(options.scriptFile === undefined)
|
||||
)
|
||||
return projectAnalysisError(
|
||||
new AnalysisInputError(CLI_COMMANDS.instrumentWithFrida),
|
||||
new AnalysisInputError(
|
||||
CLI_COMMANDS.instrumentWithFrida,
|
||||
undefined,
|
||||
[
|
||||
{
|
||||
path: ["script"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
"Provide exactly one of --script or --script-file.",
|
||||
},
|
||||
],
|
||||
),
|
||||
);
|
||||
const remote = remoteFrom(options);
|
||||
const selected =
|
||||
@@ -248,7 +259,17 @@ export const registerFridaCommands = (
|
||||
};
|
||||
if (targetInput === undefined)
|
||||
return projectAnalysisError(
|
||||
new AnalysisInputError(CLI_COMMANDS.instrumentWithFrida),
|
||||
new AnalysisInputError(
|
||||
CLI_COMMANDS.instrumentWithFrida,
|
||||
undefined,
|
||||
[
|
||||
{
|
||||
path: ["target"],
|
||||
reason: "invalid_value",
|
||||
message: "attach mode needs a positive integer process ID.",
|
||||
},
|
||||
],
|
||||
),
|
||||
);
|
||||
const result = await withProviderCleanup(() =>
|
||||
service.instrument(
|
||||
|
||||
@@ -31,6 +31,10 @@ import {
|
||||
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import {
|
||||
unknownFridaScript,
|
||||
unknownFridaSession,
|
||||
} from "../application/frida/fridaInputErrors.js";
|
||||
|
||||
const MAX_SESSION_CAPTURE_BYTES = 16 * 1024 * 1024;
|
||||
const MAX_MESSAGE_METADATA_BYTES = 128 * 1024;
|
||||
@@ -334,7 +338,7 @@ export class FridaInstrumentationManager implements FridaInstrumentationPort {
|
||||
): Promise<Result<FridaScriptObservation, AnalysisError>> {
|
||||
const managed = this.#sessions.get(sessionId);
|
||||
if (managed === undefined || managed.state === "detached")
|
||||
return err(new AnalysisInputError("load_frida_script"));
|
||||
return err(unknownFridaSession("load_frida_script", sessionId));
|
||||
|
||||
let scriptSource: string;
|
||||
try {
|
||||
@@ -354,7 +358,13 @@ export class FridaInstrumentationManager implements FridaInstrumentationPort {
|
||||
cause.code,
|
||||
{ cause },
|
||||
)
|
||||
: new AnalysisInputError("load_frida_script", { cause }),
|
||||
: new AnalysisInputError("load_frida_script", { cause }, [
|
||||
{
|
||||
path: ["path"],
|
||||
reason: "invalid_value",
|
||||
message: `Cannot read Frida script file ${sourcePath ?? "<inline>"}.`,
|
||||
},
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -419,7 +429,7 @@ export class FridaInstrumentationManager implements FridaInstrumentationPort {
|
||||
): Promise<Result<null, AnalysisError>> {
|
||||
const managed = this.#sessions.get(sessionId);
|
||||
if (managed === undefined || managed.state === "detached")
|
||||
return err(new AnalysisInputError("resume_frida_session"));
|
||||
return err(unknownFridaSession("resume_frida_session", sessionId));
|
||||
if (managed.mode !== "spawn" || managed.state !== "paused")
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
@@ -454,8 +464,10 @@ export class FridaInstrumentationManager implements FridaInstrumentationPort {
|
||||
): Promise<Result<null, AnalysisError>> {
|
||||
const managed = this.#sessions.get(sessionId);
|
||||
const entry = managed?.scripts.get(scriptId);
|
||||
if (managed === undefined || entry === undefined)
|
||||
return err(new AnalysisInputError("unload_frida_script"));
|
||||
if (managed === undefined)
|
||||
return err(unknownFridaSession("unload_frida_script", sessionId));
|
||||
if (entry === undefined)
|
||||
return err(unknownFridaScript("unload_frida_script", scriptId));
|
||||
const cleanupReason = await this.#releaseScript(managed, entry);
|
||||
return cleanupReason === undefined
|
||||
? ok(null)
|
||||
@@ -536,7 +548,7 @@ export class FridaInstrumentationManager implements FridaInstrumentationPort {
|
||||
> {
|
||||
const managed = this.#sessions.get(sessionId);
|
||||
if (managed === undefined)
|
||||
return err(new AnalysisInputError("close_frida_session"));
|
||||
return err(unknownFridaSession("close_frida_session", sessionId));
|
||||
const scriptFailures = new Map<string, string>();
|
||||
const cleanupFailures: string[] = [];
|
||||
let parentSessionReleased =
|
||||
|
||||
@@ -12,6 +12,7 @@ import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
|
||||
import type { ToolResultDelivery } from "./toolResult.js";
|
||||
import { unknownFridaSession } from "../application/frida/fridaInputErrors.js";
|
||||
|
||||
/** Register Frida device discovery and persistent instrumentation operations. */
|
||||
export const registerFridaTools = (
|
||||
@@ -199,7 +200,7 @@ export const registerFridaTools = (
|
||||
return delivery.toCallToolResult(
|
||||
{
|
||||
ok: false as const,
|
||||
error: new AnalysisInputError(status.name),
|
||||
error: unknownFridaSession(status.name, input.session_id),
|
||||
},
|
||||
status,
|
||||
);
|
||||
|
||||
@@ -234,3 +234,43 @@ it("preserves observed devices when CLI provider cleanup fails", async () => {
|
||||
details: { diagnostics: { partial_observation: { devices } } },
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
["attach", "abc", "--script", "send(1)"],
|
||||
"target",
|
||||
"attach mode needs a positive integer process ID.",
|
||||
],
|
||||
[
|
||||
["attach", "71"],
|
||||
"script",
|
||||
"Provide exactly one of --script or --script-file.",
|
||||
],
|
||||
])(
|
||||
"names the rejected instrument-with-frida argument for %j",
|
||||
async (argv, path, message) => {
|
||||
let starts = 0;
|
||||
const provider = {
|
||||
async startSession() {
|
||||
starts += 1;
|
||||
throw new Error("Frida must not start for invalid input");
|
||||
},
|
||||
} as unknown as FridaInstrumentationPort;
|
||||
const cli = createCli({}, undefined, provider);
|
||||
let stdout = "";
|
||||
await cli.serve(["instrument-with-frida", ...argv, "--json"], {
|
||||
env: {},
|
||||
exit: () => undefined,
|
||||
stdout: (text) => {
|
||||
stdout += text;
|
||||
},
|
||||
});
|
||||
expect(JSON.parse(stdout)).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [{ path: [path], reason: "invalid_value", message }],
|
||||
},
|
||||
});
|
||||
expect(starts).toBe(0);
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user