Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(electron): snapshot script frame identity at ingestion

morluto6b991d3aa73b
diff --git a/src/browser/CdpElectronInspection.test.ts b/src/browser/CdpElectronInspection.test.ts
index 925b10e9f..018d15ec7 100644
--- a/src/browser/CdpElectronInspection.test.ts
+++ b/src/browser/CdpElectronInspection.test.ts
@@ -7,6 +7,13 @@ import { afterEach, describe, expect, it } from "vitest";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import { captureFrames } from "./CdpElectronInspection.js";
+import type { CdpConnection } from "./CdpConnection.js";
+import {
+ ingestElectronScriptEvent,
+ type ElectronScriptDraft,
+} from "./CdpElectronScriptEvents.js";
+import { captureElectronScripts } from "./CdpElectronScripts.js";
+import { inspectElectronPageInputSchema } from "../domain/javascript/electronObservation.js";
const temporary: string[] = [];
@@ -61,4 +68,94 @@ describe("Electron frame capture", () => {
expect(completeness.snapshot().conditions).toContain("attach_limited");
},
);
+
+ it("preserves script records across context teardown and ID reuse", async () => {
+ const root = await mkdtemp(join(tmpdir(), "rea-electron-script-frame-"));
+ temporary.push(root);
+ const scriptPath = join(root, "app.js");
+ await writeFile(scriptPath, "export const app = true;");
+ const scripts: ElectronScriptDraft[] = [];
+ const executionContextFrames = new Map<string, string>();
+ const completeness = new CdpCaptureCompleteness();
+ const ingest = (method: string, params: unknown): void =>
+ ingestElectronScriptEvent({
+ event: { method, params },
+ scripts,
+ executionContextFrames,
+ completeness,
+ });
+ const context = (frameId: string) => ({
+ context: { id: 7, auxData: { frameId } },
+ });
+ const parsed = (
+ scriptId: string,
+ hash: string,
+ url = pathToFileURL(scriptPath).href,
+ ) => ({
+ scriptId,
+ url,
+ hash,
+ executionContextId: 7,
+ });
+
+ ingest("Runtime.executionContextCreated", context("frame-old"));
+ ingest("Debugger.scriptParsed", parsed("script-old", "shared"));
+ ingest("Runtime.executionContextDestroyed", { executionContextId: 7 });
+ ingest("Runtime.executionContextCreated", context("frame-new"));
+ ingest("Debugger.scriptParsed", parsed("script-new", "shared"));
+ ingest("Runtime.executionContextsCleared", {});
+ ingest("Debugger.scriptParsed", parsed("script-unknown", "unknown"));
+ ingest("Runtime.executionContextCreated", context("frame-outside"));
+ ingest("Debugger.scriptParsed", parsed("script-outside", "outside"));
+ ingest(
+ "Debugger.scriptParsed",
+ parsed(
+ "script-out-of-scope",
+ "out-of-scope",
+ "https://example.test/app.js",
+ ),
+ );
+
+ const capture = await captureElectronScripts({
+ connection: {
+ send: async () => {
+ throw new Error("Source capture must remain disabled in this test");
+ },
+ } as unknown as CdpConnection,
+ sessionId: undefined,
+ request: inspectElectronPageInputSchema.parse({
+ cdp_endpoint: "http://127.0.0.1:9222",
+ target_id: "page-1",
+ observation_ms: 0,
+ include_script_sources: false,
+ }),
+ scripts,
+ frameIds: new Set(["frame-old", "frame-new"]),
+ completeness,
+ });
+
+ expect(capture.total).toBe(4);
+ const sharedScripts = capture.items.filter(
+ (item) => item.cdp_hash === "shared",
+ );
+ expect(new Set(sharedScripts.map((item) => item.frame_id))).toEqual(
+ new Set(["frame-old", "frame-new"]),
+ );
+ expect(new Set(sharedScripts.map((item) => item.script_key)).size).toBe(2);
+ expect(
+ Object.fromEntries(
+ capture.items
+ .filter((item) => item.cdp_hash !== "shared")
+ .map((item) => [item.cdp_hash, item.frame_id]),
+ ),
+ ).toEqual({
+ unknown: null,
+ outside: null,
+ });
+ expect(completeness.snapshot().excluded).toContainEqual({
+ section: "scripts",
+ reason: "out_of_target_scope",
+ count: 1,
+ });
+ });
});
diff --git a/src/browser/CdpElectronInspection.ts b/src/browser/CdpElectronInspection.ts
index 8d64baec7..e66bb1337 100644
--- a/src/browser/CdpElectronInspection.ts
+++ b/src/browser/CdpElectronInspection.ts
@@ -192,7 +192,6 @@ const captureElectronContent = async (
...(context.signal === undefined ? {} : { signal: context.signal }),
request: context.input,
scripts: state.scripts,
- executionContextFrames: state.executionContextFrames,
frameIds,
completeness: state.completeness,
});
diff --git a/src/browser/CdpElectronScriptEvents.ts b/src/browser/CdpElectronScriptEvents.ts
index 263e60705..a7d2e907d 100644
--- a/src/browser/CdpElectronScriptEvents.ts
+++ b/src/browser/CdpElectronScriptEvents.ts
@@ -14,7 +14,7 @@ export interface ElectronScriptDraft {
readonly length: number | null;
readonly isModule: boolean | null;
readonly language: string | null;
- readonly executionContextKey: string | null;
+ readonly frameId: string | null;
}
/** Retain script/frame metadata from passive Electron events. */
@@ -50,12 +50,16 @@ export const ingestElectronScriptEvent = (input: {
input.completeness.exclude("scripts", "unattributed_origin");
return;
}
+ const contextKey = executionContextKey(value?.executionContextId);
input.scripts.push({
scriptId,
rawUrl,
...scriptMetadataValues(value),
language: cdpStringValue(value?.scriptLanguage) ?? null,
- executionContextKey: executionContextKey(value?.executionContextId),
+ frameId:
+ contextKey === null
+ ? null
+ : (input.executionContextFrames.get(contextKey) ?? null),
});
};
diff --git a/src/browser/CdpElectronScripts.ts b/src/browser/CdpElectronScripts.ts
index 854b49413..ae1d24586 100644
--- a/src/browser/CdpElectronScripts.ts
+++ b/src/browser/CdpElectronScripts.ts
@@ -17,7 +17,6 @@ interface ElectronScriptCaptureInput {
readonly signal?: AbortSignal;
readonly request: InspectElectronPageInput;
readonly scripts: readonly ElectronScriptDraft[];
- readonly executionContextFrames: ReadonlyMap<string, string>;
readonly frameIds: ReadonlySet<string>;
readonly completeness: CdpCaptureCompleteness;
}
@@ -35,14 +34,10 @@ export const captureElectronScripts = async (
input.completeness.exclude("scripts", "out_of_target_scope");
continue;
}
- const mappedFrame =
- script.executionContextKey === null
- ? undefined
- : input.executionContextFrames.get(script.executionContextKey);
const identity = {
frame_id:
- mappedFrame !== undefined && input.frameIds.has(mappedFrame)
- ? mappedFrame
+ script.frameId !== null && input.frameIds.has(script.frameId)
+ ? script.frameId
: null,
file_path: path,
cdp_hash: script.hash,