mirror of
https://github.com/morluto/rea.git
synced 2026-11-24 07:26:11 +00:00
fix(electron): preserve inspection frame identity and diagnostics (#13232)
This commit is contained in:
1 parent
eba41afd6c
commit
04d399d675
4 files changed
+180
-238
No files matched your search
@@ -3,7 +3,7 @@ import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { expect, it } from "vitest";
|
||||
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
|
||||
|
||||
import { fakeCdpEvents } from "../../tests/fixtures/fakeCdpEvents.js";
|
||||
@@ -432,144 +432,99 @@ it.each([7, null, "", "claimed-parent"])(
|
||||
},
|
||||
);
|
||||
|
||||
describe("Electron frame capture", () => {
|
||||
it.each([7, undefined])(
|
||||
"classifies a malformed child frame ID as unavailable: %s",
|
||||
async (childId) => {
|
||||
const root = await createTestTempDirectory("rea-electron-frame-");
|
||||
const main = join(root, "index.html");
|
||||
const child = join(root, "child.html");
|
||||
await Promise.all([writeFile(main, "main"), writeFile(child, "child")]);
|
||||
const mainUrl = pathToFileURL(main).href;
|
||||
const childUrl = pathToFileURL(child).href;
|
||||
const completeness = new CdpCaptureCompleteness();
|
||||
it("preserves script records across context teardown and ID reuse", async () => {
|
||||
const root = await createTestTempDirectory("rea-electron-script-frame-");
|
||||
const scriptPath = join(root, "app.js");
|
||||
await writeFile(scriptPath, "export const app = true;");
|
||||
const scripts: ElectronScriptDraft[] = [];
|
||||
const executionContextFrames = new Map<string, CdpContextFrameClaim>();
|
||||
const retention = { bytes: 2 };
|
||||
const completeness = new CdpCaptureCompleteness();
|
||||
const ingest = (method: string, params: unknown): void =>
|
||||
ingestElectronScriptEvent({
|
||||
event: receiveCdpEvent({ method, params }),
|
||||
scripts,
|
||||
executionContextFrames,
|
||||
retention,
|
||||
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,
|
||||
});
|
||||
|
||||
const { frames } = await captureFrames(
|
||||
frameTreeReply({
|
||||
frameTree: {
|
||||
frame: { id: "frame-main", url: mainUrl },
|
||||
childFrames: [
|
||||
{
|
||||
frame: {
|
||||
...(childId === undefined ? {} : { id: childId }),
|
||||
url: childUrl,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
completeness,
|
||||
DEFAULT_ASAR_HEADER_ADMISSION,
|
||||
);
|
||||
|
||||
expect(frames.map((frame) => frame.frame_id)).toEqual(["frame-main"]);
|
||||
expect(completeness.snapshot()).toMatchObject({
|
||||
unavailable_sections: ["frames"],
|
||||
excluded: [
|
||||
{ section: "frames", reason: "invalid_protocol_value", count: 1 },
|
||||
],
|
||||
});
|
||||
expect(completeness.snapshot().policy_filtered_sections).not.toContain(
|
||||
"frames",
|
||||
);
|
||||
expect(completeness.snapshot().conditions).toContain("attach_limited");
|
||||
},
|
||||
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",
|
||||
),
|
||||
);
|
||||
|
||||
it("preserves script records across context teardown and ID reuse", async () => {
|
||||
const root = await createTestTempDirectory("rea-electron-script-frame-");
|
||||
const scriptPath = join(root, "app.js");
|
||||
await writeFile(scriptPath, "export const app = true;");
|
||||
const scripts: ElectronScriptDraft[] = [];
|
||||
const executionContextFrames = new Map<string, CdpContextFrameClaim>();
|
||||
const retention = { bytes: 2 };
|
||||
const completeness = new CdpCaptureCompleteness();
|
||||
const ingest = (method: string, params: unknown): void =>
|
||||
ingestElectronScriptEvent({
|
||||
event: receiveCdpEvent({ method, params }),
|
||||
scripts,
|
||||
executionContextFrames,
|
||||
retention,
|
||||
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,
|
||||
});
|
||||
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,
|
||||
frameScopeForScript: (script) =>
|
||||
script.frameId !== null &&
|
||||
new Set(["frame-old", "frame-new"]).has(script.frameId)
|
||||
? { state: "authorized", frameId: script.frameId }
|
||||
: { state: "unknown", frameId: script.frameId },
|
||||
completeness,
|
||||
asarHeaderAdmission: DEFAULT_ASAR_HEADER_ADMISSION,
|
||||
});
|
||||
|
||||
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,
|
||||
frameScopeForScript: (script) =>
|
||||
script.frameId !== null &&
|
||||
new Set(["frame-old", "frame-new"]).has(script.frameId)
|
||||
? { state: "authorized", frameId: script.frameId }
|
||||
: { state: "unknown", frameId: script.frameId },
|
||||
completeness,
|
||||
asarHeaderAdmission: DEFAULT_ASAR_HEADER_ADMISSION,
|
||||
});
|
||||
|
||||
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: "frame-outside",
|
||||
});
|
||||
expect(completeness.snapshot().excluded).toContainEqual({
|
||||
section: "scripts",
|
||||
reason: "out_of_target_scope",
|
||||
count: 1,
|
||||
});
|
||||
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: "frame-outside",
|
||||
});
|
||||
expect(completeness.snapshot().excluded).toContainEqual({
|
||||
section: "scripts",
|
||||
reason: "out_of_target_scope",
|
||||
count: 1,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -852,18 +807,17 @@ it.each([2, 3])(
|
||||
await expect(
|
||||
inspectRaceConnection(connection, mainUrl),
|
||||
).rejects.toMatchObject({
|
||||
_tag: "BrowserObservationError",
|
||||
operation: "inspect_electron_page",
|
||||
reason: "target_changed",
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ stage: 1, loader: undefined },
|
||||
{ stage: 1, loader: "" },
|
||||
{ stage: 2, loader: undefined },
|
||||
{ stage: 2, loader: "" },
|
||||
{ stage: 3, loader: undefined },
|
||||
{ stage: 3, loader: "" },
|
||||
{ stage: 0, loader: undefined },
|
||||
])(
|
||||
"retains uncertainty when the main loader is missing at read $stage",
|
||||
|
||||
@@ -79,6 +79,7 @@ interface ElectronFrameInventory {
|
||||
}
|
||||
|
||||
interface ElectronFrameRead extends ElectronFrameInventory {
|
||||
readonly main: ElectronPageInspection["frames"][number];
|
||||
readonly unprovenDocuments: ReadonlySet<string>;
|
||||
readonly generations: ReadonlyMap<string, number>;
|
||||
}
|
||||
@@ -157,15 +158,15 @@ const runElectronInspection = async (
|
||||
startedAt: string,
|
||||
): Promise<ElectronPageInspection> => {
|
||||
const initialRead = await authorizeInitialFrames(context, state);
|
||||
const initialFrames = initialRead.frames;
|
||||
const main = initialFrames.at(0);
|
||||
if (main === undefined)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
const { frames: initialFrames, main } = initialRead;
|
||||
if (
|
||||
!initialRead.unprovenDocuments.has(main.frame_id) &&
|
||||
unprovenFrameIds(initialRead, state).has(main.frame_id)
|
||||
)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_changed");
|
||||
throw new BrowserObservationError(
|
||||
"inspect_electron_page",
|
||||
"target_changed",
|
||||
);
|
||||
state.mainFrameId = main.frame_id;
|
||||
state.lastProvenMainDocument = state.frameDocuments.get(main.frame_id);
|
||||
authorizeFrameGenerations(initialRead, state);
|
||||
@@ -206,7 +207,10 @@ const runElectronInspection = async (
|
||||
}
|
||||
await report(context.progress, 3, "Electron inspection complete");
|
||||
if (state.navigationDuringCapture)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_changed");
|
||||
throw new BrowserObservationError(
|
||||
"inspect_electron_page",
|
||||
"target_changed",
|
||||
);
|
||||
return {
|
||||
browser: context.discovery.version,
|
||||
target: {
|
||||
@@ -521,8 +525,8 @@ const readCurrentFrames = async (
|
||||
context.asarHeaderAdmission,
|
||||
context.signal,
|
||||
);
|
||||
selectedMainFrame(inventory, state.mainFrameId);
|
||||
return { ...inventory, generations, unprovenDocuments };
|
||||
const main = selectedMainFrame(inventory, state.mainFrameId);
|
||||
return { ...inventory, main, generations, unprovenDocuments };
|
||||
};
|
||||
|
||||
const markUnstableFrameRead = (
|
||||
@@ -605,29 +609,9 @@ const assertStableMainFrame = async (
|
||||
state: ElectronInspectionState,
|
||||
expectedPath: string,
|
||||
): Promise<ElectronPageInspection["frames"]> => {
|
||||
const generations = new Map(state.frameGenerations);
|
||||
const documents = new Map(state.frameDocuments);
|
||||
const result = await context.connection.send(
|
||||
"Page.getFrameTree",
|
||||
{},
|
||||
context.sessionId,
|
||||
context.signal,
|
||||
);
|
||||
const unprovenDocuments = seedFrameDocuments(
|
||||
result,
|
||||
generations,
|
||||
documents,
|
||||
state,
|
||||
);
|
||||
const completeness = new CdpCaptureCompleteness();
|
||||
const inventory = await captureFrames(
|
||||
result,
|
||||
completeness,
|
||||
context.asarHeaderAdmission,
|
||||
context.signal,
|
||||
);
|
||||
const { frames } = inventory;
|
||||
const main = selectedMainFrame(inventory, state.mainFrameId);
|
||||
const read = await readCurrentFrames(context, state, completeness);
|
||||
const { frames, main, generations, unprovenDocuments } = read;
|
||||
if (
|
||||
main.file_path !== expectedPath ||
|
||||
state.navigationDuringCapture ||
|
||||
@@ -636,11 +620,11 @@ const assertStableMainFrame = async (
|
||||
(generations.get(state.mainFrameId) ?? 0) !==
|
||||
(state.frameGenerations.get(state.mainFrameId) ?? 0))
|
||||
)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_changed");
|
||||
markUnstableFrameRead(
|
||||
{ ...inventory, generations, unprovenDocuments },
|
||||
state,
|
||||
);
|
||||
throw new BrowserObservationError(
|
||||
"inspect_electron_page",
|
||||
"target_changed",
|
||||
);
|
||||
markUnstableFrameRead(read, state);
|
||||
if (
|
||||
completeness
|
||||
.snapshot()
|
||||
@@ -850,9 +834,15 @@ const selectedMainFrame = (
|
||||
): ElectronPageInspection["frames"][number] => {
|
||||
const main = inventory.frames[0];
|
||||
if (main === undefined || inventory.parents.get(main.frame_id) !== null)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
throw new BrowserObservationError(
|
||||
"inspect_electron_page",
|
||||
"target_not_allowed",
|
||||
);
|
||||
if (selectedId !== undefined && main.frame_id !== selectedId)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_changed");
|
||||
throw new BrowserObservationError(
|
||||
"inspect_electron_page",
|
||||
"target_changed",
|
||||
);
|
||||
return main;
|
||||
};
|
||||
|
||||
|
||||
@@ -424,26 +424,27 @@ const authorizeTarget = async (
|
||||
asarHeaderAdmission: AsarHeaderAdmission,
|
||||
signal?: AbortSignal,
|
||||
): Promise<CdpEndpointTarget> => {
|
||||
throwIfElectronOperationCancelled(signal, "inspect_electron_page");
|
||||
const operation = "inspect_electron_page";
|
||||
throwIfElectronOperationCancelled(signal, operation);
|
||||
const target = selectCdpDiscoveryTarget(
|
||||
discovery.targets,
|
||||
targetId,
|
||||
"inspect_electron_page",
|
||||
operation,
|
||||
discovery.ambiguousTargetIds,
|
||||
);
|
||||
if (target === undefined)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_found");
|
||||
throw new BrowserObservationError(operation, "target_not_found");
|
||||
if (target.type !== "page")
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
throw new BrowserObservationError(operation, "target_not_allowed");
|
||||
const path = await authorizedElectronFile(
|
||||
target.url,
|
||||
signal,
|
||||
"inspect_electron_page",
|
||||
operation,
|
||||
asarHeaderAdmission,
|
||||
);
|
||||
throwIfElectronOperationCancelled(signal, "inspect_electron_page");
|
||||
throwIfElectronOperationCancelled(signal, operation);
|
||||
if (path === undefined)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
|
||||
throw new BrowserObservationError(operation, "target_not_allowed");
|
||||
return target;
|
||||
};
|
||||
|
||||
|
||||
@@ -855,63 +855,60 @@ describe("CdpElectronProvider packaged pages", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("CdpElectronProvider frame capture", () => {
|
||||
it.each([7, undefined])(
|
||||
"reports a malformed child frame ID as unavailable protocol evidence: %s",
|
||||
async (childId) => {
|
||||
const root = await electronFixture();
|
||||
const main = pathToFileURL(join(root, "index.html")).href;
|
||||
const child = pathToFileURL(join(root, "child.html")).href;
|
||||
const browser = await startFakeCdpBrowser({
|
||||
electronFileUrl: main,
|
||||
commandResult: (command) =>
|
||||
command.method === "Page.getFrameTree"
|
||||
? {
|
||||
frameTree: {
|
||||
frame: {
|
||||
id: "frame-main",
|
||||
loaderId: "loader-frame-main",
|
||||
url: main,
|
||||
},
|
||||
childFrames: [
|
||||
{
|
||||
frame: {
|
||||
...(childId === undefined ? {} : { id: childId }),
|
||||
url: child,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
browsers.push(browser);
|
||||
const result = await new CdpElectronProvider().inspectPage(
|
||||
inspectElectronPageInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "electron-page",
|
||||
observation_ms: 0,
|
||||
}),
|
||||
);
|
||||
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.frames.map((frame) => frame.frame_id)).toEqual([
|
||||
"frame-main",
|
||||
]);
|
||||
expect(result.value.completeness).toMatchObject({
|
||||
unavailable_sections: ["frames"],
|
||||
});
|
||||
expect(result.value.completeness.excluded).toContainEqual({
|
||||
section: "frames",
|
||||
reason: "invalid_protocol_value",
|
||||
count: 1,
|
||||
});
|
||||
expect(result.value.completeness.policy_filtered_sections).not.toContain(
|
||||
"frames",
|
||||
);
|
||||
expect(result.value.completeness.conditions).toContain("attach_limited");
|
||||
},
|
||||
it("keeps valid child frames beside missing and malformed IDs", async () => {
|
||||
const root = await electronFixture();
|
||||
const main = pathToFileURL(join(root, "index.html")).href;
|
||||
const child = {
|
||||
parentId: "frame-main",
|
||||
loaderId: "loader-child",
|
||||
url: pathToFileURL(join(root, "child.html")).href,
|
||||
};
|
||||
const browser = await startFakeCdpBrowser({
|
||||
electronFileUrl: main,
|
||||
commandResult: ({ method }) =>
|
||||
method === "Page.getFrameTree"
|
||||
? {
|
||||
frameTree: {
|
||||
frame: {
|
||||
id: "frame-main",
|
||||
loaderId: "loader-frame-main",
|
||||
url: main,
|
||||
},
|
||||
childFrames: [
|
||||
{ frame: { ...child, id: 7 } },
|
||||
{ frame: { ...child, id: "valid-child" } },
|
||||
{ frame: child },
|
||||
],
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
browsers.push(browser);
|
||||
const result = await new CdpElectronProvider().inspectPage(
|
||||
inspectElectronPageInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
target_id: "electron-page",
|
||||
observation_ms: 0,
|
||||
}),
|
||||
);
|
||||
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.frames.map((frame) => frame.frame_id)).toEqual([
|
||||
"frame-main",
|
||||
"valid-child",
|
||||
]);
|
||||
expect(result.value.completeness).toMatchObject({
|
||||
unavailable_sections: ["frames"],
|
||||
});
|
||||
expect(result.value.completeness.excluded).toContainEqual({
|
||||
section: "frames",
|
||||
reason: "invalid_protocol_value",
|
||||
count: 2,
|
||||
});
|
||||
expect(result.value.completeness.policy_filtered_sections).not.toContain(
|
||||
"frames",
|
||||
);
|
||||
expect(result.value.completeness.conditions).toContain("attach_limited");
|
||||
});
|
||||
|
||||
it("rejects malformed Electron source replies and detaches the selected page", async () => {
|
||||
|
||||
Reference in new issue
Block a user