Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(electron): preserve unknown resource sizes

morluto79d2b718963b
diff --git a/docs/electron-observation.md b/docs/electron-observation.md
index 24eb05162..b869e4b76 100644
--- a/docs/electron-observation.md
+++ b/docs/electron-observation.md
@@ -56,7 +56,9 @@ rea inspect-electron-page http://127.0.0.1:9223 TARGET_ID \
The normalized result contains canonical local paths, frame and DOM
structure, resource metadata, stable script/resource identities, explicit
-completeness, and content-addressed script artifacts. Script metadata
+completeness, and content-addressed script artifacts. Resource sizes are unknown
+when omitted or invalid; supplied invalid sizes also produce an explicit
+completeness diagnostic. Script metadata
retains its execution-context frame ID when CDP supplies one. The capture also
inventories worker, service-worker, and shared-worker targets with
validated opener-target and parent-frame IDs. Worker discovery uses passive
diff --git a/src/browser/CdpCaptureDocuments.ts b/src/browser/CdpCaptureDocuments.ts
index f6f8b5545..bff87bb79 100644
--- a/src/browser/CdpCaptureDocuments.ts
+++ b/src/browser/CdpCaptureDocuments.ts
@@ -4,6 +4,7 @@ import {
allowedSanitizedUrl,
admitRecords,
numberValue,
+ resourceContentSize,
recordValue,
recordsValue,
requiredRecord,
@@ -117,12 +118,8 @@ export const captureResources = (
completeness?.exclude("resources", "invalid_protocol_value");
continue;
}
- const rawContentSize = numberValue(resource.contentSize);
- const contentSize =
- rawContentSize !== undefined && rawContentSize >= 0
- ? rawContentSize
- : null;
- if (resource.contentSize !== undefined && contentSize === null)
+ const contentSize = resourceContentSize(resource.contentSize);
+ if (!contentSize.ok)
completeness?.exclude("resources", "invalid_protocol_value");
items.push({
rawUrl: cdpStringValue(resource.url) ?? "",
@@ -130,7 +127,7 @@ export const captureResources = (
origin: url.origin,
type,
mime_type: mimeType,
- content_size: contentSize,
+ content_size: contentSize.ok ? contentSize.value : null,
});
}
return { items };
diff --git a/src/browser/CdpCaptureValues.ts b/src/browser/CdpCaptureValues.ts
index b0f8f4d4f..c7585040a 100644
--- a/src/browser/CdpCaptureValues.ts
+++ b/src/browser/CdpCaptureValues.ts
@@ -3,6 +3,7 @@ import { BrowserObservationError } from "../domain/browserObservationError.js";
import type { BrowserObservationOperation } from "../domain/browserObservationErrors.js";
import { sanitizeBrowserUrl } from "../domain/browserObservation.js";
import { createWebTextArtifact } from "../domain/webContentArtifact.js";
+import { err, ok, type Result } from "../domain/result.js";
export type UnknownRecord = Readonly<Record<string, unknown>>;
@@ -30,6 +31,17 @@ export const cdpStringValue = (value: unknown): string | undefined =>
export const numberValue = (value: unknown): number | undefined =>
typeof value === "number" && Number.isFinite(value) ? value : undefined;
+/** An omitted resource size is unknown; a supplied invalid size is malformed. */
+export const resourceContentSize = (
+ value: unknown,
+): Result<number | null, { readonly _tag: "InvalidCdpResourceSize" }> => {
+ if (value === undefined) return ok(null);
+ const size = numberValue(value);
+ return size !== undefined && size >= 0
+ ? ok(size)
+ : err({ _tag: "InvalidCdpResourceSize" });
+};
+
/** Preserve partial script declarations; omitted or invalid fields remain unknown. */
export const scriptMetadataValues = (
value: UnknownRecord | undefined,
diff --git a/src/browser/CdpElectronInspection.ts b/src/browser/CdpElectronInspection.ts
index e66bb1337..54d5106fd 100644
--- a/src/browser/CdpElectronInspection.ts
+++ b/src/browser/CdpElectronInspection.ts
@@ -11,6 +11,7 @@ import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import {
delayWithCancellation,
numberValue,
+ resourceContentSize,
recordValue,
requiredRecord,
cdpStringValue,
@@ -291,14 +292,14 @@ const captureResources = async (
completeness.exclude("resources", "out_of_target_scope");
continue;
}
+ const contentSize = resourceContentSize(resource.contentSize);
+ if (!contentSize.ok)
+ completeness.exclude("resources", "invalid_protocol_value");
const item = {
file_path: path,
type: cdpStringValue(resource.type) ?? "Other",
mime_type: cdpStringValue(resource.mimeType) ?? "",
- content_size:
- numberValue(resource.contentSize) === undefined
- ? null
- : Math.max(0, numberValue(resource.contentSize) ?? 0),
+ content_size: contentSize.ok ? contentSize.value : null,
};
const resourceKey = `electron_resource_${digestCanonicalValue(item, "CDP capture")}`;
if (seen.has(resourceKey)) continue;
diff --git a/src/browser/CdpResourceContentSize.test.ts b/src/browser/CdpResourceContentSize.test.ts
new file mode 100644
index 000000000..d2058498a
--- /dev/null
+++ b/src/browser/CdpResourceContentSize.test.ts
@@ -0,0 +1,21 @@
+import { expect, it } from "vitest";
+import { resourceContentSize } from "./CdpCaptureValues.js";
+
+it.each([
+ [undefined, null],
+ [0, 0],
+ [128, 128],
+ [0.5, 0.5],
+])("preserves optional CDP size %s as %s", (input, value) => {
+ expect(resourceContentSize(input)).toEqual({ ok: true, value });
+});
+
+it.each([-1, null, "4", NaN, Infinity, -Infinity, {}])(
+ "rejects supplied invalid CDP size %s without inventing zero",
+ (value) => {
+ expect(resourceContentSize(value)).toEqual({
+ ok: false,
+ error: { _tag: "InvalidCdpResourceSize" },
+ });
+ },
+);
diff --git a/tests/boundary/cli/electronResourceSizeCli.test.ts b/tests/boundary/cli/electronResourceSizeCli.test.ts
new file mode 100644
index 000000000..8864089a2
--- /dev/null
+++ b/tests/boundary/cli/electronResourceSizeCli.test.ts
@@ -0,0 +1,88 @@
+import { writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import { pathToFileURL } from "node:url";
+import { expect } from "vitest";
+import { parseEvidence } from "../../../src/domain/evidence.js";
+import { electronPageInspectionSchema } from "../../../src/domain/javascript/electronObservation.js";
+import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+import { cliTest } from "../../support/cli/cliFixture.js";
+
+cliTest(
+ "Electron resource sizes preserve unknown values and invalid-metadata coverage",
+ async ({ cli, onTestFinished }) => {
+ const root = await createTestTempDirectory("rea-electron-resource-sizes-");
+ const main = join(root, "index.html");
+ await writeFile(main, "<html/>");
+ const sizes = [-1, 0, 128, undefined, "4", null];
+ const resources: Array<{
+ url: string;
+ type: string;
+ mimeType: string;
+ contentSize?: unknown;
+ }> = [];
+ for (const [index, contentSize] of sizes.entries()) {
+ const path = join(root, `resource-${index}.js`);
+ await writeFile(path, "fixture");
+ resources.push({
+ url: pathToFileURL(path).href,
+ type: "Script",
+ mimeType: "text/javascript",
+ ...(contentSize === undefined ? {} : { contentSize }),
+ });
+ }
+ const browser = await startFakeCdpBrowser({
+ electronFileUrl: pathToFileURL(main).href,
+ commandResult: ({ method }) =>
+ method === "Page.getResourceTree"
+ ? {
+ frameTree: {
+ frame: { id: "main", url: pathToFileURL(main).href },
+ resources,
+ },
+ }
+ : undefined,
+ });
+ onTestFinished(() => browser.close());
+ const output = await cli.run({
+ arguments: [
+ "inspect-electron-page",
+ browser.endpoint,
+ "electron-page",
+ "--observation-ms",
+ "0",
+ "--json",
+ ],
+ });
+ expect(output.exitCode, output.stderr).toBe(0);
+ const inspection = electronPageInspectionSchema.parse(
+ parseEvidence(output.json).normalized_result,
+ );
+ expect(inspection.resources).toHaveLength(6);
+ const byPath = new Map(
+ inspection.resources.map((item) => [item.file_path, item]),
+ );
+ expect(
+ sizes.map(
+ (_size, index) =>
+ byPath.get(join(root, `resource-${index}.js`))?.content_size,
+ ),
+ ).toEqual([null, 0, 128, null, null, null]);
+ expect(
+ inspection.resources.every(
+ (item) =>
+ item.type === "Script" && item.mime_type === "text/javascript",
+ ),
+ ).toBe(true);
+ expect(inspection.completeness.excluded).toContainEqual({
+ section: "resources",
+ reason: "invalid_protocol_value",
+ count: 3,
+ });
+ expect(
+ browser.commands.filter(
+ ({ method }) => method === "Target.detachFromTarget",
+ ),
+ ).toHaveLength(1);
+ },
+);