fix(frida): give detached and unknown sessions lifecycle recovery

A session whose target detached was refused with the unknown-session error,
"No open Frida session; start one with start_frida_session", although the
session is still held, frida_session_status still returns its remaining
messages and close_frida_session must still release it. Following the advice
abandoned the session and its messages.

load_frida_script and resume_frida_session now refuse a detached session with
a capability refusal naming frida_session_status, then close_frida_session.
Unknown session and script IDs conform to the input contract, so their
recovery names the lifecycle tools instead of schema guidance, matching the
gdb and rizin session errors.

Refs #15329
This commit is contained in:
morluto
2026-11-30 18:21:08 -05:00
parent 4b19f283d3
commit a33ef00a87
3 changed files with 85 additions and 14 deletions
+52 -12
View File
@@ -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.`,
},
],
);
+30 -1
View File
@@ -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),
+3 -1
View File
@@ -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);
}