Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(javascript): report malformed runtime context events as excluded (#4755)

Kaoru08226a407619fe15
diff --git a/docs/javascript-runtime-observation.md b/docs/javascript-runtime-observation.md
index 6d8404b1e..2e41929c6 100644
--- a/docs/javascript-runtime-observation.md
+++ b/docs/javascript-runtime-observation.md
@@ -109,7 +109,9 @@ The direct observations are:
- `Runtime.executionContextCreated`,
`Runtime.executionContextDestroyed`, and
`Runtime.executionContextsCleared`, establishing context lifecycle within
- the capture window.
+ the capture window. A created or destroyed event whose context ID is
+ missing or not a safe integer adds no context record; it is retained only
+ as a count reported in `unknowns`.
The Inspector protocol does not directly expose require/import caller edges,
EventEmitter emissions or listener invocation, Electron IPC messages or
diff --git a/src/inspector/V8InspectorCaptureProjection.test.ts b/src/inspector/V8InspectorCaptureProjection.test.ts
index 61bfa5c4b..2c86ece1d 100644
--- a/src/inspector/V8InspectorCaptureProjection.test.ts
+++ b/src/inspector/V8InspectorCaptureProjection.test.ts
@@ -34,6 +34,7 @@ const stateFor = (scripts: CaptureState["scripts"]): CaptureState => ({
metadataBytes: 0,
scriptsObserved: scripts.length,
invalidScripts: 0,
+ invalidContexts: 0,
truncated: false,
truncationReasons: new Set(),
});
diff --git a/src/inspector/V8InspectorCaptureProjection.ts b/src/inspector/V8InspectorCaptureProjection.ts
index ec2c58e78..4b05caa33 100644
--- a/src/inspector/V8InspectorCaptureProjection.ts
+++ b/src/inspector/V8InspectorCaptureProjection.ts
@@ -218,6 +218,7 @@ export const finalizeInspectorCapture = async ({
: [
`${String(unresolvedScripts)} observed script location${unresolvedScripts === 1 ? " remains" : "s remain"} unverified; raw URLs and script metadata are retained.`,
]),
+ ...invalidContextUnknowns(state.invalidContexts),
"Scripts collected before attachment may have been garbage-collected and therefore omitted.",
"A bounded observation window cannot establish that an unobserved script or behavior never occurs.",
"The declared Node/Electron process role is not authenticated by the Inspector protocol.",
@@ -226,6 +227,14 @@ export const finalizeInspectorCapture = async ({
};
};
+/** Report lifecycle events whose context identity could not be parsed. */
+const invalidContextUnknowns = (count: number): string[] =>
+ count === 0
+ ? []
+ : [
+ `${String(count)} execution-context lifecycle ${count === 1 ? "event with a missing or malformed execution-context ID was" : "events with a missing or malformed execution-context ID were"} excluded; the affected contexts' lifecycle state is unknown.`,
+ ];
+
const scriptFromDraft = (
draft: ScriptDraft,
location: JavaScriptRuntimeTargetLocation,
diff --git a/src/inspector/V8InspectorProvider.ts b/src/inspector/V8InspectorProvider.ts
index bf89e227e..93c6a8b4d 100644
--- a/src/inspector/V8InspectorProvider.ts
+++ b/src/inspector/V8InspectorProvider.ts
@@ -72,6 +72,7 @@ export interface CaptureState {
metadataBytes: number;
scriptsObserved: number;
invalidScripts: number;
+ invalidContexts: number;
truncated: boolean;
readonly truncationReasons: Set<string>;
}
@@ -422,6 +423,7 @@ const emptyCaptureState = (): CaptureState => ({
metadataBytes: 0,
scriptsObserved: 0,
invalidScripts: 0,
+ invalidContexts: 0,
truncated: false,
truncationReasons: new Set(),
});
@@ -495,6 +497,7 @@ const ingestContext = (event: CdpEvent, state: CaptureState): void => {
: runtimeContext?.executionContextId,
);
if (key === null) {
+ state.invalidContexts += 1;
retainEvent(state, 0);
return;
}
diff --git a/tests/boundary/inspector/javascriptRuntimeObservation.test.ts b/tests/boundary/inspector/javascriptRuntimeObservation.test.ts
index 293c9aa1f..d840628a6 100644
--- a/tests/boundary/inspector/javascriptRuntimeObservation.test.ts
+++ b/tests/boundary/inspector/javascriptRuntimeObservation.test.ts
@@ -205,6 +205,65 @@ describe("Inspector execution-context lifecycle metadata", () => {
await fake.close();
}
});
+
+ test.each([
+ ["malformed", "2", "1"],
+ ["valid", 2, 1],
+ ] as const)(
+ "accounts for %s context IDs in lifecycle events",
+ async (mode, createdId, destroyedId) => {
+ const fixture = await runtimeFixture();
+ const fake = await startFakeV8Inspector({
+ targetUrl: pathToFileURL(fixture.entry).href,
+ runtimeEvents: [
+ {
+ method: "Runtime.executionContextCreated",
+ params: { context: { id: 1, name: "main", origin: "" } },
+ },
+ {
+ method: "Runtime.executionContextCreated",
+ params: { context: { id: createdId, name: "worker", origin: "" } },
+ },
+ {
+ method: "Runtime.executionContextDestroyed",
+ params: { executionContextId: destroyedId },
+ },
+ ],
+ scriptEvents: [],
+ });
+ try {
+ const result = await new V8InspectorProvider().observe(
+ observeInput(fake.endpoint, fake.targetId, "node"),
+ );
+ if (!result.ok) throw result.error;
+ const invalidContextUnknowns = result.value.unknowns.filter((text) =>
+ text.includes("malformed execution-context ID"),
+ );
+ expect(result.value.capture).toMatchObject({
+ events_observed: 3,
+ events_retained: 3,
+ events_dropped: 0,
+ truncated: false,
+ });
+ if (mode === "malformed") {
+ expect(result.value.execution_contexts).toEqual([
+ { context_key: "1", state: "created", name: "main", origin: "" },
+ ]);
+ expect(invalidContextUnknowns).toEqual([
+ "2 execution-context lifecycle events with a missing or malformed execution-context ID were excluded; the affected contexts' lifecycle state is unknown.",
+ ]);
+ return;
+ }
+ expect(result.value.execution_contexts).toEqual([
+ { context_key: "1", state: "destroyed", name: "main", origin: "" },
+ { context_key: "2", state: "created", name: "worker", origin: "" },
+ ]);
+ expect(invalidContextUnknowns).toEqual([]);
+ } finally {
+ await fake.close();
+ }
+ },
+ );
});
test("rejects unknown runtime observation fields", () => {