diff --git a/src/domain/fridaInputErrors.ts b/src/domain/fridaInputErrors.ts index ff7ec280ad..26365facb4 100644 --- a/src/domain/fridaInputErrors.ts +++ b/src/domain/fridaInputErrors.ts @@ -1,27 +1,67 @@ -import { AnalysisInputError } from "./analysisErrorCore.js"; +import { + AnalysisCapabilityUnavailableError, + AnalysisInputError, +} from "./analysisErrorCore.js"; -/** A session ID that names no open Frida session owned by this process. */ +/** + * A session ID that names no Frida session this server holds. The ID + * satisfies the input contract, so the refusal names the lifecycle tools + * rather than schema guidance. + */ export const unknownFridaSession = ( operation: string, sessionId: string, ): AnalysisInputError => - new AnalysisInputError(operation, undefined, [ + new AnalysisInputError( + operation, { - path: ["session_id"], - reason: "invalid_value", - message: `No open Frida session ${sessionId}; start one with start_frida_session.`, + conformsToInputContract: true, + userMessage: `No Frida session ${sessionId} is held by this REA server.`, + remediationAction: + "Use a session_id that start_frida_session returned in this server process; close_frida_session releases a session's ID. Call start_frida_session to begin a new session.", }, - ]); + [ + { + path: ["session_id"], + reason: "invalid_value", + message: `No open Frida session ${sessionId}; start one with start_frida_session.`, + }, + ], + ); + +/** + * A retained session whose target detached. It accepts no more scripts or + * resumes, but still holds captured messages and needs an explicit close. + */ +export const detachedFridaSession = ( + operation: string, + sessionId: string, +): AnalysisCapabilityUnavailableError => { + const reason = `The target of Frida session ${sessionId} detached; the session accepts no more scripts or resumes.`; + return new AnalysisCapabilityUnavailableError("frida", operation, reason, { + userMessage: reason, + remediationAction: `Call frida_session_status with session_id ${sessionId} to read its remaining messages, then close_frida_session to release it. Call start_frida_session to instrument the target again.`, + }); +}; /** A script ID that names no loaded script in the selected session. */ export const unknownFridaScript = ( operation: string, scriptId: string, ): AnalysisInputError => - new AnalysisInputError(operation, undefined, [ + new AnalysisInputError( + operation, { - path: ["script_id"], - reason: "invalid_value", - message: `No loaded Frida script ${scriptId} in this session.`, + conformsToInputContract: true, + userMessage: `No loaded Frida script ${scriptId} in this session.`, + remediationAction: + "Use a script_id that load_frida_script returned for this session.", }, - ]); + [ + { + path: ["script_id"], + reason: "invalid_value", + message: `No loaded Frida script ${scriptId} in this session.`, + }, + ], + ); diff --git a/src/frida/FridaInstrumentationManager.test.ts b/src/frida/FridaInstrumentationManager.test.ts index 0ad8f9a6ec..d16c0da29c 100644 --- a/src/frida/FridaInstrumentationManager.test.ts +++ b/src/frida/FridaInstrumentationManager.test.ts @@ -1877,6 +1877,28 @@ it("drains startup when the provider loader initiates shutdown", async () => { expect(manager.status(started.value.sessionId)).toBeUndefined(); }); +it("refuses work on a detached session with status-then-close recovery, not a new session", async () => { + const { bindings, manager, started } = await startLocalAttachedSession(); + if (!started.ok) throw started.error; + const { sessionId } = started.value; + bindings.device.session.markDetached(); + const recovery = `Call frida_session_status with session_id ${sessionId} to read its remaining messages, then close_frida_session to release it. Call start_frida_session to instrument the target again.`; + for (const result of [ + await manager.resumeSession(sessionId), + await manager.loadScript(sessionId, { sourceKind: "inline", source: "" }), + ]) + expect(result).toMatchObject({ + ok: false, + error: { + _tag: "AnalysisCapabilityUnavailableError", + remediationAction: recovery, + }, + }); + // The detached session is still held, so status and close keep working. + expect(manager.status(sessionId)?.state).toBe("detached"); + expect(await manager.closeSession(sessionId)).toMatchObject({ ok: true }); +}); + it("names the unknown session or script ID it rejects", async () => { const { manager, started } = await startLocalAttachedSession(); if (!started.ok) throw started.error; @@ -1894,7 +1916,14 @@ it("names the unknown session or script ID it rejects", async () => { ]) expect(result).toMatchObject({ ok: false, - error: { _tag: "AnalysisInputError", issues: [sessionIssue] }, + error: { + _tag: "AnalysisInputError", + issues: [sessionIssue], + // A well-formed ID is not a schema problem; recovery names the lifecycle. + conformsToInputContract: true, + remediationAction: + "Use a session_id that start_frida_session returned in this server process; close_frida_session releases a session's ID. Call start_frida_session to begin a new session.", + }, }); expect( await manager.unloadScript(started.value.sessionId, missing), diff --git a/src/frida/FridaInstrumentationManager.ts b/src/frida/FridaInstrumentationManager.ts index e48a91ef56..f53120caaa 100644 --- a/src/frida/FridaInstrumentationManager.ts +++ b/src/frida/FridaInstrumentationManager.ts @@ -44,6 +44,7 @@ import type { JsonValue } from "../domain/jsonValue.js"; import { err, ok, type Result } from "../domain/result.js"; import { unknownFridaScript, + detachedFridaSession, unknownFridaSession, } from "../domain/fridaInputErrors.js"; import { @@ -671,7 +672,8 @@ export class FridaInstrumentationManager implements FridaInstrumentationPort { }), ); } - if (managed.detached) return err(unknownFridaSession(operation, sessionId)); + if (managed.detached) + return err(detachedFridaSession(operation, sessionId)); return ok(managed); }