diff --git a/src/browser/CdpCaptureDocuments.test.ts b/src/browser/CdpCaptureDocuments.test.ts index 9fd647a19a..970f2ee0f2 100644 --- a/src/browser/CdpCaptureDocuments.test.ts +++ b/src/browser/CdpCaptureDocuments.test.ts @@ -627,48 +627,41 @@ describe("CDP DOM snapshot empty strings", () => { }); }); -it.each([7, null, "without-hash"])( - "retains base URL evidence with malformed fragment %s", - (urlFragment) => { +it("preserves the base URL when fragment syntax is unreadable", () => { + for (const urlFragment of [7, null, "without-hash"]) { expect( observeCdpFrameUrl({ url: `${origin}/page?x=1#old`, urlFragment }), ).toEqual({ kind: "invalid_fragment", scopeUrl: `${origin}/page?x=1#old` }); - }, -); + } +}); -it.each([7, "missing-hash"])( - "retains foreign frame scope independently of malformed fragment %s", - (urlFragment) => { - const completeness = new CdpCaptureCompleteness(); - const captured = captureFrames( - frameTreeReply({ - frameTree: { - frame: { id: "root", url: origin }, - childFrames: [ - { - frame: { - id: "foreign", - parentId: "root", - url: "https://foreign.test/page", - urlFragment, - }, - }, - ], - }, - }), - new Set([origin]), - undefined, - completeness, - ); - expect(captured.items.map((frame) => frame.frame_id)).toEqual(["root"]); - expect(completeness.snapshot().excluded).toEqual( - expect.arrayContaining([ - { section: "frames", reason: "invalid_protocol_value", count: 1 }, - { section: "frames", reason: "disallowed_origin", count: 1 }, - ]), - ); - expect(completeness.snapshot().policy_filtered_sections).toContain( - "frames", - ); - }, -); +it("counts both malformed foreign fragments and their origin exclusions", () => { + const completeness = new CdpCaptureCompleteness(); + const captured = captureFrames( + frameTreeReply({ + frameTree: { + frame: { id: "root", url: origin }, + childFrames: [7, "missing-hash"].map((urlFragment, index) => ({ + frame: { + id: `foreign-${index}`, + parentId: "root", + url: `https://foreign.test/page-${index}`, + urlFragment, + }, + })), + }, + }), + new Set([origin]), + undefined, + completeness, + ); + const snapshot = completeness.snapshot(); + expect(captured.items.map((frame) => frame.frame_id)).toEqual(["root"]); + expect(snapshot.excluded).toEqual( + expect.arrayContaining([ + { section: "frames", reason: "invalid_protocol_value", count: 2 }, + { section: "frames", reason: "disallowed_origin", count: 2 }, + ]), + ); + expect(snapshot.policy_filtered_sections).toContain("frames"); +}); diff --git a/src/browser/CdpEndpoint.test.ts b/src/browser/CdpEndpoint.test.ts index 6be07845d0..6e7a9d6e8e 100644 --- a/src/browser/CdpEndpoint.test.ts +++ b/src/browser/CdpEndpoint.test.ts @@ -7,11 +7,8 @@ import { WEB_RUNTIME_LIMITS } from "../domain/webRuntime.js"; import { afterEach, describe, expect, it } from "vitest"; -import { fakeCdpSend } from "../../tests/fixtures/fakeCdpSend.js"; -import type { CdpConnection } from "./CdpConnection.js"; import { discoverCdpEndpoint, - discoverCdpBrowserSocket, readCdpJson, withCdpDiscoveryDeadline, } from "./CdpEndpoint.js"; @@ -168,55 +165,22 @@ describe("CDP discovery cancellation ownership", () => { expect((failure as AnalysisCancelledError).cause).toBe(reason); }); - it("does not replace a primary discovery failure when cancellation arrives later", async () => { - const primary = new BrowserObservationError( - "inspect_web_page", - "protocol_error", - ); - const controller = new AbortController(); - const failure = await withCdpDiscoveryDeadline( - "inspect_web_page", - controller.signal, - async () => { - throw primary; - }, - 100, - ).catch((error: unknown) => error); - controller.abort(new Error("late caller cancellation")); - - expect(failure).toBe(primary); - }); - - it("does not replace a plain physical failure rejected before a late abort", async () => { - const primary = new Error("socket failed before cancellation"); - const controller = new AbortController(); - const pending = withCdpDiscoveryDeadline( - "inspect_web_page", - controller.signal, - () => Promise.reject(primary), - 100, - ).catch((error: unknown) => error); - controller.abort(new Error("later caller cancellation")); - const failure = await pending; - - expect(failure).toBe(primary); - }); - - it("classifies its own discovery deadline as timeout", async () => { - const failure = await withCdpDiscoveryDeadline( - "inspect_web_page", - undefined, - (signal) => - new Promise((_resolve, reject) => { - signal.addEventListener("abort", () => reject(signal.reason), { - once: true, - }); - }), - 1, - ).catch((error: unknown) => error); - - expect(failure).toBeInstanceOf(BrowserObservationError); - expect(failure).toMatchObject({ reason: "timeout" }); + it("keeps typed and plain discovery failures produced before a caller abort", async () => { + const failures = [ + new BrowserObservationError("inspect_web_page", "protocol_error"), + new Error("socket failed before cancellation"), + ]; + for (const primary of failures) { + const controller = new AbortController(); + const pending = withCdpDiscoveryDeadline( + "inspect_web_page", + controller.signal, + () => Promise.reject(primary), + 100, + ).catch((error: unknown) => error); + controller.abort(new Error("later caller cancellation")); + expect(await pending).toBe(primary); + } }); it("normalizes a typed cancellation caused by its own deadline to timeout", async () => { @@ -270,13 +234,17 @@ describe("CDP discovery cancellation ownership", () => { controller.signal, (signal) => new Promise((_resolve, reject) => { - signal.addEventListener("abort", () => reject(signal.reason), { - once: true, - }); + signal.addEventListener( + "abort", + () => { + controller.abort(new Error("caller stopped after timeout")); + reject(signal.reason); + }, + { once: true }, + ); }), 0, ).catch((error: unknown) => error); - controller.abort(new Error("caller stopped after timeout")); expect(failure).toBeInstanceOf(BrowserObservationError); expect(failure).toMatchObject({ reason: "timeout" }); @@ -326,73 +294,6 @@ it("names the HTTP discovery document budget when that response deadline expires ); }); -const socketDiscoveryConnection = ( - targetReply: unknown, -): Pick => ({ - send: fakeCdpSend((method) => { - if (method === "Browser.getVersion") - return { - protocolVersion: "1.3", - product: "fixture", - revision: "fixture", - userAgent: "fixture", - jsVersion: "fixture", - }; - if (method === "Target.getTargets") return targetReply; - throw new Error(method); - }), -}); - -it.each([ - { label: "absent document", reply: undefined }, - { label: "null document", reply: null }, - { label: "array document", reply: [] }, - { label: "missing target list", reply: {} }, - { label: "null target list", reply: { targetInfos: null } }, - { label: "string target list", reply: { targetInfos: "invalid" } }, - { label: "object target list", reply: { targetInfos: {} } }, -])("rejects an $label during browser-socket discovery", async ({ reply }) => { - await expect( - discoverCdpBrowserSocket( - socketDiscoveryConnection(reply), - "ws://127.0.0.1:9222/devtools/browser/fixture", - "list_browser_targets", - ), - ).rejects.toMatchObject({ - reason: "invalid_endpoint_response", - detail: expect.stringContaining("CDP Target.getTargets"), - }); -}); - -it("reports a malformed browser-socket version as an invalid endpoint response", async () => { - await expect( - discoverCdpBrowserSocket( - { - send: fakeCdpSend((method) => - method === "Browser.getVersion" - ? { protocolVersion: "", product: "fixture" } - : { targetInfos: [] }, - ), - }, - "ws://127.0.0.1:9222/devtools/browser/fixture", - "list_browser_targets", - ), - ).rejects.toMatchObject({ - reason: "invalid_endpoint_response", - detail: expect.stringContaining("CDP Browser.getVersion"), - }); -}); - -it("retains an explicitly empty browser-socket target inventory", async () => { - await expect( - discoverCdpBrowserSocket( - socketDiscoveryConnection({ targetInfos: [] }), - "ws://127.0.0.1:9222/devtools/browser/fixture", - "list_browser_targets", - ), - ).resolves.toMatchObject({ targets: [], limitations: [] }); -}); - it("preserves an independent typed cancellation when the discovery deadline also expires", async () => { const primary = new AnalysisCancelledError("inspect_web_page", { cause: "independent cancellation", @@ -408,68 +309,3 @@ it("preserves an independent typed cancellation when the discovery deadline also ).catch((cause: unknown) => cause); expect(failure).toBe(primary); }); - -it("preserves browser-socket sibling identities and reports malformed optional metadata", async () => { - const discovery = await discoverCdpBrowserSocket( - { - send: fakeCdpSend((method) => - method === "Browser.getVersion" - ? { - protocolVersion: "1.3", - product: "Chrome/fixture", - revision: "r", - userAgent: "fixture", - jsVersion: "v8", - } - : { - targetInfos: [ - { - targetId: "valid", - type: "page", - url: "https://example.test/", - title: "Example", - attached: false, - }, - { - targetId: "partial", - type: "page", - url: "https://example.test/other", - title: 42, - attached: "yes", - }, - { targetId: "broken", type: "page" }, - { - id: "wrong-identity-field", - type: "page", - url: "https://example.test/", - }, - ], - }, - ), - }, - "ws://127.0.0.1:9222/devtools/browser/fixture", - "list_browser_targets", - ); - expect(discovery.targets).toEqual([ - { - id: "valid", - type: "page", - url: "https://example.test/", - title: "Example", - attached: false, - }, - { - id: "partial", - type: "page", - url: "https://example.test/other", - title: "", - attached: null, - }, - ]); - expect(discovery.limitations).toHaveLength(4); - expect( - discovery.limitations?.every((limitation) => - limitation.startsWith("CDP Target.getTargets row"), - ), - ).toBe(true); -}); diff --git a/src/browser/CdpFrameIdentity.test.ts b/src/browser/CdpFrameIdentity.test.ts index e7f80a43d1..7c2db61bc4 100644 --- a/src/browser/CdpFrameIdentity.test.ts +++ b/src/browser/CdpFrameIdentity.test.ts @@ -12,25 +12,21 @@ const cdpFrameEventFacts = (event: CdpEvent) => { }; describe("CDP frame identity ownership", () => { - it.each([ - ["missing", {}], - ["null", { frame: null }], - ["non-object", { frame: "frame" }], - ])("does not infer a root from a %s frame object", (_label, params) => { - expect( - selectCdpFrameIdentity( - cdpFrameEventFacts({ method: "Page.frameNavigated", params }), - "selected-main", - ), - ).toMatchObject({ - applicability: "applicable", - selection: "unknown", - isRootReplacement: false, - identityIssue: "missing_frame", - parentRelation: "unknown", - parentId: null, - parentIssue: null, - }); + it("keeps missing or malformed frame objects from becoming root evidence", () => { + for (const params of [{}, { frame: null }, { frame: "frame" }]) { + expect( + selectCdpFrameIdentity( + cdpFrameEventFacts({ method: "Page.frameNavigated", params }), + "selected-main", + ), + ).toMatchObject({ + selection: "unknown", + frameId: null, + identityIssue: "missing_frame", + parentRelation: "unknown", + isRootReplacement: false, + }); + } }); it("recognizes a valid frame with no parent property as a root", () => { diff --git a/src/browser/CdpPageCaptureScripts.test.ts b/src/browser/CdpPageCaptureScripts.test.ts index 93fd023d1f..5a65565f41 100644 --- a/src/browser/CdpPageCaptureScripts.test.ts +++ b/src/browser/CdpPageCaptureScripts.test.ts @@ -24,17 +24,13 @@ const script = (scriptId: string): CapturedScript => ({ frameId: null, }); -const capture = async ( - sources: Readonly>, - includeScriptSources = true, - signal?: AbortSignal, -) => { +const capture = async (sources: Readonly>) => { const input = inspectWebPageInputSchema.parse({ cdp_endpoint: "http://127.0.0.1:9222", target_id: "page-1", allowed_origins: ["https://example.test"], observation_ms: 0, - include_script_sources: includeScriptSources, + include_script_sources: true, }); const events = new CdpCaptureEvents(input, new Set(input.allowed_origins)); for (const id of Object.keys(sources)) events.scripts.set(id, script(id)); @@ -51,7 +47,6 @@ const capture = async ( discovery: undefined, target: undefined, input, - ...(signal === undefined ? {} : { signal }), } as unknown as CaptureContext; const result = await captureScripts({ context, @@ -60,7 +55,7 @@ const capture = async ( resources: [], frameIds: new Set(), }); - return { result, events, send }; + return { result, events }; }; it("admits a prefix and fitting suffix around an oversized script source", async () => { @@ -217,18 +212,3 @@ it.each([ } }, ); - -it("leaves source-off capture unchanged and does not issue source reads", async () => { - const { result, events, send } = await capture({ first: "unused" }, false); - - expect(send).not.toHaveBeenCalled(); - expect(result.inventory.items[0]?.source).toEqual({ - included: false, - reason: "source capture was not selected", - }); - expect(events.completeness.snapshot().excluded).toContainEqual({ - section: "script_sources", - reason: "not_approved", - count: 1, - }); -}); diff --git a/src/domain/pngHeader.test.ts b/src/domain/pngHeader.test.ts index a0c680b084..ca4165a219 100644 --- a/src/domain/pngHeader.test.ts +++ b/src/domain/pngHeader.test.ts @@ -17,38 +17,51 @@ it("reads encoded dimensions without changing screenshot bytes", () => { expect(bytes.equals(original)).toBe(true); }); -it.each([12, 13, 14, 15])("rejects ASCII-folded IHDR byte at %s", (offset) => { - const bytes = Buffer.from(png); - bytes[offset] = (bytes[offset] ?? 0) | 0x80; - bytes.writeUInt32BE(crc32(bytes.subarray(12, 29)), 29); - expect(bytes.subarray(12, 16).toString("ascii")).toBe("IHDR"); - expect(readPngHeaderDimensions(bytes)).toBeUndefined(); -}); - -it("rejects incomplete headers, invalid signatures and zero dimensions", () => { - expect(readPngHeaderDimensions(png.subarray(0, 23))).toBeUndefined(); - const badSignature = Buffer.from(png); - badSignature[0] = 0; - expect(readPngHeaderDimensions(badSignature)).toBeUndefined(); - for (const offset of [16, 20]) { - const zero = Buffer.from(png); - zero.writeUInt32BE(0, offset); - zero.writeUInt32BE(crc32(zero.subarray(12, 29)), 29); - expect(readPngHeaderDimensions(zero)).toBeUndefined(); +it("rejects a high-bit lookalike at any position in the IHDR chunk name", () => { + for (const offset of [12, 13, 14, 15]) { + const bytes = Buffer.from(png); + bytes[offset] = (bytes[offset] ?? 0) | 0x80; + bytes.writeUInt32BE(crc32(bytes.subarray(12, 29)), 29); + expect( + readPngHeaderDimensions(bytes), + `chunk-name offset ${offset}`, + ).toBeUndefined(); } }); -it.each([0, 12, 14, 0xffff_ffff])("rejects IHDR data length %s", (length) => { - const bytes = Buffer.from(png); - bytes.writeUInt32BE(length, 8); - expect(readPngHeaderDimensions(bytes)).toBeUndefined(); +it("rejects incomplete or corrupted IHDR data", () => { + const badSignature = Buffer.from(png); + badSignature[0] = 0; + const badChecksum = Buffer.from(png); + badChecksum[29] = (badChecksum[29] ?? 0) ^ 1; + expect(readPngHeaderDimensions(png.subarray(0, 32))).toBeUndefined(); + expect(readPngHeaderDimensions(badSignature)).toBeUndefined(); + expect(readPngHeaderDimensions(badChecksum)).toBeUndefined(); }); -it.each([16, 20])("rejects out-of-range PNG dimension at %s", (offset) => { - const bytes = Buffer.from(png); - bytes.writeUInt32BE(0x8000_0000, offset); - bytes.writeUInt32BE(crc32(bytes.subarray(12, 29)), 29); - expect(readPngHeaderDimensions(bytes)).toBeUndefined(); +it("requires the fixed IHDR data length", () => { + for (const length of [0, 12, 14, 0xffff_ffff]) { + const bytes = Buffer.from(png); + bytes.writeUInt32BE(length, 8); + expect( + readPngHeaderDimensions(bytes), + `declared length ${length}`, + ).toBeUndefined(); + } +}); + +it("requires both dimensions to be positive PNG integers", () => { + for (const offset of [16, 20]) { + for (const dimension of [0, 0x8000_0000]) { + const bytes = Buffer.from(png); + bytes.writeUInt32BE(dimension, offset); + bytes.writeUInt32BE(crc32(bytes.subarray(12, 29)), 29); + expect( + readPngHeaderDimensions(bytes), + `dimension ${dimension} at ${offset}`, + ).toBeUndefined(); + } + } }); it("accepts the largest dimension representable by the PNG specification", () => { @@ -60,13 +73,3 @@ it("accepts the largest dimension representable by the PNG specification", () => height: 1, }); }); - -it.each([24, 32, "crc"])("rejects incomplete or corrupt IHDR %s", (kind) => { - const bytes = Buffer.from(png); - if (kind === "crc") bytes[29] = (bytes[29] ?? 0) ^ 1; - expect( - readPngHeaderDimensions( - typeof kind === "number" ? bytes.subarray(0, kind) : bytes, - ), - ).toBeUndefined(); -}); diff --git a/tests/boundary/browser/cdpBrowserProvider.browserSocket.test.ts b/tests/boundary/browser/cdpBrowserProvider.browserSocket.test.ts index ac59a331bd..0042e52888 100644 --- a/tests/boundary/browser/cdpBrowserProvider.browserSocket.test.ts +++ b/tests/boundary/browser/cdpBrowserProvider.browserSocket.test.ts @@ -217,7 +217,7 @@ describe("CdpBrowserProvider: browser socket eviction and per-call scoping", () }); }); -it("retains browser-socket siblings and reports invalid attachment as unknown, not omitted", async () => { +it("retains browser-socket siblings and identifies malformed rows by their discovery position", async () => { const browser = await startFakeCdpBrowser({ commandResult: ({ method }) => method === "Target.getTargets" @@ -238,9 +238,8 @@ it("retains browser-socket siblings and reports invalid attachment as unknown, n attached: false, }, { - targetId: 1, + targetId: "missing-url", type: "page", - url: "https://example.test/invalid", }, { id: "alias", type: "page", url: "https://example.test/alias" }, ], @@ -266,17 +265,22 @@ it("retains browser-socket siblings and reports invalid attachment as unknown, n ["first", null, ""], ["second", false, "Good"], ]); - const limitations = result.value.limitations.join("\n"); - expect(limitations).toContain( - 'reported malformed attached metadata "unknown"', + expect(result.value.limitations).toEqual( + expect.arrayContaining([ + expect.stringContaining( + 'CDP Target.getTargets row 0 target first reported malformed attached metadata "unknown"', + ), + expect.stringContaining( + "CDP Target.getTargets row 0 target first reported malformed title metadata 3", + ), + expect.stringContaining( + "CDP Target.getTargets row 2 was excluded because its target identity is malformed", + ), + expect.stringContaining( + "CDP Target.getTargets row 3 was excluded because its target identity is malformed", + ), + ]), ); - expect(limitations).toContain("reported malformed title metadata 3"); - expect( - result.value.limitations.filter((item) => - item.includes("target identity is malformed"), - ), - ).toHaveLength(2); - expect(limitations).not.toContain("omitted attachment"); } finally { await provider.close(); } @@ -323,29 +327,53 @@ describe("CdpBrowserProvider: browser socket failures and cleanup", () => { await waitFor(() => expect(browser.activeConnectionCount()).toBe(0)); }); - it("rejects malformed browser-socket discovery and closes the socket", async () => { + it("rejects malformed discovery replies and admits an explicitly empty inventory", async () => { + let malformed: + | { method: string; reply: Record } + | undefined; const browser = await startFakeCdpBrowser({ - commandResult: ({ method }) => - method === "Browser.getVersion" - ? { product: "FakeChrome/1.0" } - : undefined, - }); - trackBrowser(browser); - const result = await new CdpBrowserProvider().listTargets( - listBrowserTargetsInputSchema.parse({ - cdp_endpoint: browser.browserWebSocketUrl, - }), - ); - - expect(result).toMatchObject({ - ok: false, - error: { - _tag: "BrowserObservationError", - operation: "list_browser_targets", - reason: "invalid_endpoint_response", + commandResult: ({ method }) => { + if (method === malformed?.method) return malformed.reply; + return method === "Target.getTargets" ? { targetInfos: [] } : undefined; }, }); - await waitFor(() => expect(browser.activeConnectionCount()).toBe(0)); + trackBrowser(browser); + const provider = new CdpBrowserProvider(); + const input = listBrowserTargetsInputSchema.parse({ + cdp_endpoint: browser.browserWebSocketUrl, + }); + try { + for (const invalidReply of [ + { + method: "Browser.getVersion", + reply: { product: "FakeChrome/1.0", protocolVersion: "" }, + }, + { method: "Target.getTargets", reply: { targetInfos: null } }, + ]) { + malformed = invalidReply; + expect(await provider.listTargets(input)).toMatchObject({ + ok: false, + error: { + _tag: "BrowserObservationError", + operation: "list_browser_targets", + reason: "invalid_endpoint_response", + detail: expect.stringContaining(`CDP ${invalidReply.method}`), + }, + }); + await waitFor(() => expect(browser.activeConnectionCount()).toBe(0)); + } + + malformed = undefined; + expect(await provider.listTargets(input)).toMatchObject({ + ok: true, + value: { + targets: [], + excluded: { disallowed_origin: 0, non_page: 0, unsupported_url: 0 }, + }, + }); + } finally { + await provider.close(); + } }); it("reports a refused browser socket as unreachable", async () => { diff --git a/tests/boundary/browser/cdpBrowserProvider.discovery_authorization.test.ts b/tests/boundary/browser/cdpBrowserProvider.discovery_authorization.test.ts index 2a52e998c9..db83777c2a 100644 --- a/tests/boundary/browser/cdpBrowserProvider.discovery_authorization.test.ts +++ b/tests/boundary/browser/cdpBrowserProvider.discovery_authorization.test.ts @@ -35,207 +35,202 @@ const inspectWithNetworkEvents = async ( return { browser, result }; }; -describe("CdpBrowserProvider: discovery authorization 1", () => { - it("discovers pages and captures a selected target without an origin list", async () => { - const browser = await startFakeCdpBrowser(); - trackBrowser(browser); - const provider = new CdpBrowserProvider(); - const listed = await provider.listTargets( - listBrowserTargetsInputSchema.parse({ cdp_endpoint: browser.endpoint }), +it("applies exact origin selection while retaining caller-visible URLs", async () => { + const browser = await startFakeCdpBrowser(); + trackBrowser(browser); + const provider = new CdpBrowserProvider(); + const cdp_endpoint = browser.endpoint; + const allowed = { + target_id: "allowed-page", + origin: browser.allowedOrigin, + url: `${browser.allowedOrigin}/app?token=page-secret#fragment`, + }; + try { + const unfiltered = await provider.listTargets( + listBrowserTargetsInputSchema.parse({ cdp_endpoint }), ); + if (!unfiltered.ok) throw unfiltered.error; + expect(unfiltered.value.targets).toMatchObject([ + allowed, + { + target_id: "disallowed-page", + origin: "https://private.example.test", + url: "https://private.example.test/app?token=forbidden", + }, + ]); + expect(unfiltered.value.excluded).toEqual({ + disallowed_origin: 0, + unsupported_url: 1, + non_page: 1, + }); - if (!listed.ok) throw listed.error; - expect(listed.value.targets.map(({ target_id }) => target_id)).toContain( - "allowed-page", - ); - expect(listed.value.excluded.disallowed_origin).toBe(0); - - const inspected = await provider.inspectPage( - inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - target_id: "allowed-page", - observation_ms: 0, - }), - ); - if (!inspected.ok) throw inspected.error; - expect(inspected.value.target.origin).toBe(browser.allowedOrigin); - }); - - it("filters by exact origin and preserves local URL details", async () => { - const browser = await startFakeCdpBrowser(); - trackBrowser(browser); - const provider = new CdpBrowserProvider(); - const result = await provider.listTargets( + const filtered = await provider.listTargets( listBrowserTargetsInputSchema.parse({ - cdp_endpoint: browser.endpoint, + cdp_endpoint, allowed_origins: [browser.allowedOrigin], }), ); - - if (!result.ok) throw result.error; - expect(result.value.targets).toEqual([ - expect.objectContaining({ - target_id: "allowed-page", - origin: browser.allowedOrigin, - url: `${browser.allowedOrigin}/app?token=page-secret#fragment`, - }), - ]); - expect(result.value.excluded).toEqual({ + if (!filtered.ok) throw filtered.error; + expect(filtered.value.targets).toMatchObject([allowed]); + expect(filtered.value.excluded).toEqual({ disallowed_origin: 1, unsupported_url: 1, non_page: 1, }); - expect(JSON.stringify(result.value)).not.toContain("forbidden"); - expect(JSON.stringify(result.value)).toContain("page-secret#fragment"); - }); - - it.each([ - ["absolute", true, ""], - ["host/path", "host-path", ""], - ["root-relative", "root-relative", ""], - ["prefixed host/path", "prefixed", "Loading "], - ] as const)( - "preserves URL-shaped %s target titles", - async (_label, urlShapedAllowedTitle, prefix) => { - const browser = await startFakeCdpBrowser({ urlShapedAllowedTitle }); - trackBrowser(browser); - const provider = new CdpBrowserProvider(); - const listInput = { - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - }; - const input = { - ...listInput, - target_id: "allowed-page", - }; - const authority = browser.allowedOrigin.replace(/^https?:\/\//u, ""); - const expected = - _label === "absolute" - ? `${browser.allowedOrigin}/app?startup=title-secret#fragment` - : _label === "root-relative" - ? "/app?startup=title-secret#fragment" - : `${prefix}${authority}/app?startup=title-secret#fragment`; - const listed = await provider.listTargets( - listBrowserTargetsInputSchema.parse(listInput), - ); - if (!listed.ok) throw listed.error; - expect(listed.value.targets[0]?.title).toBe(expected); - - const inspected = await provider.inspectPage( - inspectWebPageInputSchema.parse({ ...input, observation_ms: 0 }), - ); - if (!inspected.ok) throw inspected.error; - expect(inspected.value.target.title).toBe(expected); - expect(JSON.stringify({ listed, inspected })).toContain("title-secret"); - }, - ); - - it("uses page-scoped discovery sockets as direct target transports", async () => { - const browser = await startFakeCdpBrowser({ - pageScopedVersionWebSocket: true, - omitTargetWebSocket: true, - additionalPageWithWebSocket: true, - additionalPageWithoutWebSocket: true, - }); - trackBrowser(browser); - const provider = new CdpBrowserProvider(); - const listed = await provider.listTargets( - listBrowserTargetsInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - }), - ); - if (!listed.ok) throw listed.error; - expect(listed.value.targets.map(({ target_id }) => target_id)).toEqual([ - "allowed-page", - "allowed-page-with-socket", - ]); - expect(listed.value.limitations).toContain( - "1 otherwise allowed page target(s) lacked a validated direct CDP WebSocket and were excluded.", - ); + expect(JSON.stringify(filtered.value)).not.toContain("forbidden"); const inspected = await provider.inspectPage( inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", + cdp_endpoint, + target_id: allowed.target_id, observation_ms: 0, }), ); if (!inspected.ok) throw inspected.error; - expect(inspected.value.target.target_id).toBe("allowed-page"); - expect(inspected.value.network.requests).toHaveLength(1); + expect(inspected.value.target).toMatchObject({ + target_id: allowed.target_id, + origin: allowed.origin, + url: `${browser.allowedOrigin}/app?token=frame-secret`, + }); + } finally { + await provider.close(); + } +}); - const second = await provider.inspectPage( - inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page-with-socket", - observation_ms: 0, - }), +it.each([ + ["absolute", true, ""], + ["host/path", "host-path", ""], + ["root-relative", "root-relative", ""], + ["prefixed host/path", "prefixed", "Loading "], +] as const)( + "preserves URL-shaped %s target titles", + async (_label, urlShapedAllowedTitle, prefix) => { + const browser = await startFakeCdpBrowser({ urlShapedAllowedTitle }); + trackBrowser(browser); + const provider = new CdpBrowserProvider(); + const listInput = { + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + }; + const input = { + ...listInput, + target_id: "allowed-page", + }; + const authority = browser.allowedOrigin.replace(/^https?:\/\//u, ""); + const expected = + _label === "absolute" + ? `${browser.allowedOrigin}/app?startup=title-secret#fragment` + : _label === "root-relative" + ? "/app?startup=title-secret#fragment" + : `${prefix}${authority}/app?startup=title-secret#fragment`; + const listed = await provider.listTargets( + listBrowserTargetsInputSchema.parse(listInput), ); - if (!second.ok) throw second.error; - expect(second.value.target.target_id).toBe("allowed-page-with-socket"); + if (!listed.ok) throw listed.error; + expect(listed.value.targets[0]?.title).toBe(expected); + + const inspected = await provider.inspectPage( + inspectWebPageInputSchema.parse({ ...input, observation_ms: 0 }), + ); + if (!inspected.ok) throw inspected.error; + expect(inspected.value.target.title).toBe(expected); + expect(JSON.stringify({ listed, inspected })).toContain("title-secret"); + }, +); + +it("lists only usable direct transports and accounts for exclusions before capture", async () => { + const browser = await startFakeCdpBrowser({ + pageScopedVersionWebSocket: true, + omitTargetWebSocket: true, + additionalPageWithWebSocket: true, + additionalPageWithoutWebSocket: true, + }); + trackBrowser(browser); + const provider = new CdpBrowserProvider(); + const cdp_endpoint = browser.endpoint; + const allowed_origins = [browser.allowedOrigin]; + const visibleIds = ["allowed-page", "allowed-page-with-socket"]; + const transportNotice = + "otherwise allowed page target(s) lacked a validated direct CDP WebSocket"; + const requestedScope = + "Only page targets with a validated CDP transport whose current URL matches an explicitly requested exact origin are listed."; + try { + for (const scenario of [ + { + origins: [], + ids: visibleIds, + disallowed: 0, + transportExclusions: ["2"], + scope: + "All page targets with supported HTTP(S) URLs and a validated CDP transport are listed.", + }, + { + origins: allowed_origins, + ids: visibleIds, + disallowed: 1, + transportExclusions: ["1"], + scope: requestedScope, + }, + { + origins: ["https://unlisted.example.test"], + ids: [], + disallowed: 4, + transportExclusions: [], + scope: requestedScope, + }, + ]) { + const listed = await provider.listTargets( + listBrowserTargetsInputSchema.parse({ + cdp_endpoint, + allowed_origins: scenario.origins, + }), + ); + if (!listed.ok) throw listed.error; + expect(listed.value.targets.map(({ target_id }) => target_id)).toEqual( + scenario.ids, + ); + expect(listed.value.excluded).toEqual({ + disallowed_origin: scenario.disallowed, + unsupported_url: 1, + non_page: 1, + }); + expect(listed.value.limitations).toContain(scenario.scope); + expect( + listed.value.limitations.filter((message) => + message.includes(transportNotice), + ), + ).toEqual( + scenario.transportExclusions.map( + (count) => `${count} ${transportNotice} and were excluded.`, + ), + ); + } + expect(browser.commands).toEqual([]); + expect(browser.openedConnectionCount()).toBe(0); + + for (const target_id of visibleIds) { + const inspected = await provider.inspectPage( + inspectWebPageInputSchema.parse({ + cdp_endpoint, + allowed_origins, + target_id, + observation_ms: 0, + }), + ); + if (!inspected.ok) throw inspected.error; + expect(inspected.value.target.target_id).toBe(target_id); + if (target_id === "allowed-page") + expect(inspected.value.network.requests).toHaveLength(1); + } const methods = browser.commands.map(({ method }) => method); expect(methods).not.toContain("Target.attachToTarget"); expect(methods).not.toContain("Target.detachFromTarget"); expect( browser.commands.every(({ sessionId }) => sessionId === undefined), ).toBe(true); - }); -}); - -describe("CdpBrowserProvider: listing coverage", () => { - it.each([ - { filtered: false, missingSocket: false }, - { filtered: false, missingSocket: true }, - { filtered: true, missingSocket: false }, - { filtered: true, missingSocket: true }, - ])( - "qualifies listing coverage by transport admission (origin filter: $filtered, missing socket: $missingSocket)", - async ({ filtered, missingSocket }) => { - const browser = await startFakeCdpBrowser({ - pageScopedVersionWebSocket: true, - additionalPageWithWebSocket: true, - additionalPageWithoutWebSocket: missingSocket, - }); - trackBrowser(browser); - const listed = await new CdpBrowserProvider().listTargets( - listBrowserTargetsInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: filtered ? [browser.allowedOrigin] : [], - }), - ); - if (!listed.ok) throw listed.error; - expect(listed.value.targets.map(({ target_id }) => target_id)).toEqual([ - "allowed-page", - "allowed-page-with-socket", - ]); - expect(listed.value.excluded).toEqual({ - disallowed_origin: filtered ? 1 : 0, - unsupported_url: 1, - non_page: 1, - }); - expect(listed.value.limitations).toContain( - filtered - ? "Only page targets with a validated CDP transport whose current URL matches an explicitly requested exact origin are listed." - : "All page targets with supported HTTP(S) URLs and a validated CDP transport are listed.", - ); - const excluded = (filtered ? 0 : 1) + (missingSocket ? 1 : 0); - expect( - listed.value.limitations.filter((message) => - message.includes("lacked a validated direct CDP WebSocket"), - ), - ).toEqual( - excluded === 0 - ? [] - : [ - `${String(excluded)} otherwise allowed page target(s) lacked a validated direct CDP WebSocket and were excluded.`, - ], - ); - expect(browser.commands).toEqual([]); - }, - ); + } finally { + await provider.close(); + } }); it("fails with a protocol error after an unidentifiable frame event and detaches", async () => { @@ -298,31 +293,6 @@ it("fails capture on a contradictory self-parented frame instead of accepting it await expect.poll(() => browser.activeConnectionCount()).toBe(0); }); -it("tracks a known root replacement through later frame-scope events", async () => { - const { result } = await inspectWithNetworkEvents((command, origin) => - command.method === "Network.enable" - ? [ - frameNavigated(command, { - id: "replacement-main", - loaderId: "replacement-loader", - url: `${origin}/app?token=frame-secret`, - }), - frameNavigated(command, { - id: "replacement-main", - loaderId: "outside-loader", - url: "https://outside.invalid/path", - }), - ] - : undefined, - ); - // Both events select the replacement main frame; the document change is - // reported before the later document's scope. - expect(result).toMatchObject({ - ok: false, - error: { _tag: "BrowserObservationError", reason: "target_changed" }, - }); -}); - it("treats a same-URL and same-loader root frame ID change as a capture change", async () => { const { result } = await inspectWithNetworkEvents((command, origin) => command.method === "Network.enable" @@ -362,30 +332,6 @@ describe("CdpBrowserProvider: discovery authorization 2", () => { ]); }); - it("rejects missing and disallowed page targets before attaching", async () => { - const browser = await startFakeCdpBrowser(); - trackBrowser(browser); - const provider = new CdpBrowserProvider(); - for (const targetId of ["missing", "disallowed-page"]) { - const result = await provider.inspectPage( - inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: targetId, - observation_ms: 0, - }), - ); - expect(result.ok).toBe(false); - if (!result.ok) - expect(result.error).toMatchObject({ - _tag: "BrowserObservationError", - reason: - targetId === "missing" ? "target_not_found" : "target_not_allowed", - }); - } - expect(browser.commands).toHaveLength(0); - }); - it("returns typed errors for malformed discovery and disconnects", async () => { const malformed = await startFakeCdpBrowser({ malformedDiscovery: true }); trackBrowser(malformed); @@ -479,7 +425,6 @@ describe("CdpBrowserProvider: target selection detail", () => { "Target disallowed-page must be an HTTP(S) page in the selected origin scope.", ], ] as const; - const inspected = new Map(); for (const [operation, run] of operations) for (const [targetId, reason, detail, remediation] of selections) { const result = await run({ @@ -496,10 +441,6 @@ describe("CdpBrowserProvider: target selection detail", () => { message: detail, remediation: { action: remediation }, }); - // Only the attributed operation differs from the inspect projection. - if (operation === "inspect_web_page") - inspected.set(targetId, projected); - else expect(projected).toEqual(inspected.get(targetId)); } expect(browser.commands).toHaveLength(0); expect(browser.openedConnectionCount()).toBe(0); diff --git a/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts b/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts index 00c88a882a..d8d8476531 100644 --- a/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts +++ b/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts @@ -7,7 +7,6 @@ import { describe, expect, it } from "vitest"; import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js"; import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js"; -import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js"; import { discoverWebMcpToolsInputSchema } from "../../../src/domain/webMcpDiscovery.js"; import { captureWebScreenshotInputSchema } from "../../../src/domain/webScreenshot.js"; import { observeWebSessionInputSchema } from "../../../src/domain/browserSession.js"; @@ -63,163 +62,145 @@ it("preserves CDP's separate frame fragment in all passive URL projections", asy final_url: url, }); }); -describe("CdpBrowserProvider: approved accessibility and source capture", () => { - it("captures approved accessibility text inline", async () => { - const browser = await startFakeCdpBrowser(); - trackBrowser(browser); - const request = inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - observation_ms: 0, - include_accessibility_text: true, - }); - const result = await new CdpBrowserProvider().inspectPage(request); - - if (!result.ok) throw result.error; - expect(result.value.accessibility).toMatchObject({ - text_capture: { - status: "included", - retained_bytes: 27, - }, - nodes: [expect.objectContaining({ name: "Submit report" })], - }); +it("captures approved accessibility text inline", async () => { + const browser = await startFakeCdpBrowser(); + trackBrowser(browser); + const request = inspectWebPageInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + observation_ms: 0, + include_accessibility_text: true, }); + const result = await new CdpBrowserProvider().inspectPage(request); - it("includes allowed script source only after explicit approval", async () => { - const browser = await startFakeCdpBrowser(); - trackBrowser(browser); - const provider = new CdpBrowserProvider(); - const result = await provider.inspectPage( - inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - observation_ms: 0, - include_script_sources: true, - }), - ); + if (!result.ok) throw result.error; + expect(result.value.accessibility).toMatchObject({ + text_capture: { + status: "included", + retained_bytes: 27, + }, + nodes: [expect.objectContaining({ name: "Submit report" })], + }); +}); - if (!result.ok) throw result.error; - expect(result.value.scripts.items[0]?.source).toEqual( - expect.objectContaining({ - included: true, - artifact: expect.objectContaining({ - text: "export const observed = 'source-secret';", - bytes: 40, - sha256: expect.stringMatching(/^[a-f0-9]{64}$/u), - }), - }), - ); - expect(browser.commands.map((command) => command.method)).toContain( +it("gates script source reads and distinguishes empty text from malformed replies", async () => { + const scriptText = "export const observed = 'source-secret';"; + let malformedReply: Record | undefined; + const browser = await startFakeCdpBrowser({ + eventsBeforeReply: ["Debugger.enable"], + commandEvents: (command, origin) => + command.method === "Debugger.enable" + ? ["nonempty", "empty"].map((scriptId) => ({ + method: "Debugger.scriptParsed", + sessionId: command.sessionId, + params: { + scriptId, + url: `${origin}/${scriptId}.js`, + hash: scriptId, + executionContextId: 1, + }, + })) + : undefined, + commandResult: ({ method, params }) => + method === "Debugger.getScriptSource" + ? params.scriptId === "nonempty" + ? { scriptSource: scriptText } + : (malformedReply ?? { scriptSource: "" }) + : undefined, + }); + trackBrowser(browser); + const provider = new CdpBrowserProvider(); + const input = inspectWebPageInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + observation_ms: 0, + }); + const scriptSources = ( + items: readonly { + readonly url: string; + readonly source: unknown; + }[], + ) => Object.fromEntries(items.map(({ url, source }) => [url, source])); + const origin = browser.allowedOrigin; + const nonempty = `${origin}/nonempty.js`; + const empty = `${origin}/empty.js`; + try { + const unapproved = await provider.inspectPage(input); + if (!unapproved.ok) throw unapproved.error; + const omitted = { + included: false, + reason: "source capture was not selected", + }; + expect(scriptSources(unapproved.value.scripts.items)).toEqual({ + [nonempty]: omitted, + [empty]: omitted, + }); + expect(unapproved.value.completeness.excluded).toContainEqual({ + section: "script_sources", + reason: "not_approved", + count: 2, + }); + expect(browser.commands.map(({ method }) => method)).not.toContain( "Debugger.getScriptSource", ); - }); - it.each([{}, { scriptSource: null }, { scriptSource: 123 }])( - "rejects malformed source replies instead of inventing empty source: %j", - async (reply) => { - const browser = await startFakeCdpBrowser({ - commandResult: ({ method }) => - method === "Debugger.getScriptSource" ? reply : undefined, - }); - trackBrowser(browser); - const result = await new CdpBrowserProvider().inspectPage( - inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - target_id: "allowed-page", - observation_ms: 0, - include_script_sources: true, - }), - ); - - expect(result).toMatchObject({ + const approvedInput = { ...input, include_script_sources: true }; + for (const reply of [{}, { scriptSource: null }]) { + malformedReply = reply; + const before = browser.commands.length; + expect( + await provider.inspectPage(approvedInput), + JSON.stringify(reply), + ).toMatchObject({ ok: false, error: { operation: "inspect_web_page", reason: "protocol_error", - userMessage: expect.stringContaining( - "Debugger.getScriptSource returned a malformed result at /scriptSource", - ), + command: "Debugger.getScriptSource", + pointer: "/scriptSource", }, }); - expect(browser.commands.map(({ method }) => method)).toContain( + const commands = browser.commands.slice(before); + expect(commands).toContainEqual( + expect.objectContaining({ + method: "Debugger.getScriptSource", + params: { scriptId: "empty" }, + }), + ); + expect(commands.map(({ method }) => method)).toContain( "Target.detachFromTarget", ); - }, - ); + await expect.poll(() => browser.activeConnectionCount()).toBe(0); + } - it("retains an explicitly empty producer script", async () => { - const browser = await startFakeCdpBrowser({ - commandResult: ({ method }) => - method === "Debugger.getScriptSource" - ? { scriptSource: "" } - : undefined, - }); - trackBrowser(browser); - const result = await new CdpBrowserProvider().inspectPage( - inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - target_id: "allowed-page", - observation_ms: 0, - include_script_sources: true, - }), - ); - - if (!result.ok) throw result.error; - expect(result.value.scripts.items[0]?.source).toMatchObject({ - included: true, - artifact: { text: "", bytes: 0 }, - }); - }); - - it("fetches and validates source maps only after separate approval", async () => { - const browser = await startFakeCdpBrowser({ - sourceMapBody: JSON.stringify({ - version: 3, - names: [], - sources: ["src/main.ts"], - sourcesContent: ["export const original = true;"], - mappings: "AAAA", - }), - }); - trackBrowser(browser); - const result = await new CdpBrowserProvider().analyzeBundle( - analyzeWebBundleInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - observation_ms: 0, - fetch_source_maps: true, - }), - ); - - if (!result.ok) throw result.error; - expect(result.value.observations.source_maps).toMatchObject({ - status: "included", - items: [ - { - status: "included", - declared_url: `${browser.allowedOrigin}/app.js.map?token=map-secret`, - original_sources: [ - expect.objectContaining({ - source: `${browser.allowedOrigin}/src/main.ts`, - }), - ], - mappings: [expect.any(Object)], + malformedReply = undefined; + const approved = await provider.inspectPage(approvedInput); + if (!approved.ok) throw approved.error; + expect(scriptSources(approved.value.scripts.items)).toMatchObject({ + [nonempty]: { + included: true, + artifact: { + text: scriptText, + bytes: 40, + sha256: + "73b50401804ea39bee7e36e2067fc3e079c92873bdc3d1e35b578bad72e9e0d3", }, - ], + }, + [empty]: { + included: true, + artifact: { + text: "", + bytes: 0, + sha256: + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + }, + }, }); - const sourceMapRequest = browser.httpRequests.find(({ url }) => - url.startsWith("/app.js.map"), - ); - expect(sourceMapRequest).toEqual({ - url: "/app.js.map?token=map-secret", - authorization: undefined, - cookie: undefined, - referer: undefined, - }); - }); + } finally { + await provider.close(); + } }); it.each(["after-replay", "before-replay", "replacement", "malformed"])( diff --git a/tests/boundary/browser/cdpCaptureInvalidation.test.ts b/tests/boundary/browser/cdpCaptureInvalidation.test.ts index 27b8f01f45..d568cfae5f 100644 --- a/tests/boundary/browser/cdpCaptureInvalidation.test.ts +++ b/tests/boundary/browser/cdpCaptureInvalidation.test.ts @@ -5,129 +5,121 @@ import { projectAnalysisError } from "../../../src/domain/analysisErrorProjectio import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js"; import { frameNavigated, trackBrowser } from "./cdpBrowserProvider.support.js"; -// A proven document change is reported before scope, carrying the failure it -// caused; an unproven navigation (no loader) is judged by its current scope. -it.each( - [ - { method: "Network.enable", sourceFails: false }, - { method: "DOMSnapshot.captureSnapshot", sourceFails: false }, - { method: "DOMSnapshot.captureSnapshot", sourceFails: true }, - ].flatMap((stage) => [ - { ...stage, loader: true }, - { ...stage, loader: false }, - ]), -)( - "reports navigation to another origin during $method (loader $loader, source failure $sourceFails)", - async ({ method: stage, sourceFails, loader }) => { - const browser = await startFakeCdpBrowser({ - eventsBeforeReply: [stage], - commandEvents: (command) => - command.method === stage - ? [ - frameNavigated(command, { - id: "frame-main", - ...(loader ? { loaderId: "outside-document" } : {}), - url: "https://outside.example/page", - }), - ] - : undefined, - commandError: ({ method }) => - sourceFails && method === "Debugger.getScriptSource" - ? { code: -32000, message: "No script for id" } - : undefined, - }); - trackBrowser(browser); - const result = await new CdpBrowserProvider().inspectPage( - inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - target_id: "allowed-page", - observation_ms: 0, - include_script_sources: true, - }), - ); - if (result.ok) throw new Error("Expected observed navigation refusal"); - expect(projectAnalysisError(result.error)).toMatchObject( - loader - ? { - code: "target_unavailable", - message: expect.stringContaining("target changed"), - details: { - operation: "inspect_web_page", - reason: "target_changed", - }, - } - : { - code: "target_unavailable", - message: expect.stringContaining("permitted HTTP(S) origin scope"), - remediation: { action: expect.stringContaining("allowed_origins") }, - details: { - operation: "inspect_web_page", - reason: "target_not_allowed", - }, - }, - ); - if (sourceFails) - expect(result.error.cause).toMatchObject({ reason: "protocol_error" }); - expect(browser.commands.map(({ method }) => method)).toContain( - "Target.detachFromTarget", - ); - }, -); +it("distinguishes document invalidation, origin refusal, and an independent script-read failure", async () => { + const outside = "https://outside.example/page"; + const foreign = [ + { loaderId: "outside-document", reason: "target_changed" }, + { reason: "target_not_allowed" }, + ] as const; + const scenarios = [ + ...["Network.enable", "DOMSnapshot.captureSnapshot"].flatMap((command) => + foreign.map(({ reason, ...identity }) => ({ + navigation: { command, url: outside, identity }, + sourceFails: false, + reason, + })), + ), + { + navigation: { + command: "DOMSnapshot.captureSnapshot", + identity: { loaderId: "reloaded-document" }, + }, + sourceFails: true, + reason: "target_changed", + }, + { + navigation: { + command: "DOMSnapshot.captureSnapshot", + url: outside, + identity: {}, + }, + sourceFails: true, + reason: "target_not_allowed", + }, + { sourceFails: true, reason: "protocol_error" }, + ] as const; -it.each([ - { changed: true, sourceFails: true, reason: "target_changed" }, - { changed: false, sourceFails: true, reason: "protocol_error" }, - { changed: false, sourceFails: false, reason: null }, -])( - "preserves observed navigation before a stale source-read failure: $changed/$sourceFails", - async ({ changed, sourceFails, reason }) => { - const browser = await startFakeCdpBrowser({ - eventsBeforeReply: ["DOMSnapshot.captureSnapshot"], - commandEvents: (command, origin) => - changed && command.method === "DOMSnapshot.captureSnapshot" - ? [ - frameNavigated(command, { - id: "frame-main", - loaderId: "reloaded-document", - url: origin + "/reloaded", - }), - ] - : undefined, - commandError: ({ method }) => - sourceFails && method === "Debugger.getScriptSource" - ? { code: -32000, message: "No script for id" } - : undefined, - }); - trackBrowser(browser); - const result = await new CdpBrowserProvider().inspectPage( - inspectWebPageInputSchema.parse({ - cdp_endpoint: browser.endpoint, - target_id: "allowed-page", - observation_ms: 0, - include_script_sources: true, - }), - ); - if (reason === null) { - if (!result.ok) throw result.error; - expect( - result.value.scripts.items.some(({ source }) => source.included), - ).toBe(true); - return; - } - expect(result).toMatchObject({ ok: false, error: { reason } }); - if (result.ok) throw new Error("Expected a capture refusal"); - if (changed) { - expect(projectAnalysisError(result.error)).toMatchObject({ - code: "target_unavailable", - message: expect.stringContaining("target changed"), - remediation: { - action: expect.stringContaining("Retry after navigation settles"), - }, + let scenario: (typeof scenarios)[number] = scenarios[0]!; + const browser = await startFakeCdpBrowser({ + eventsBeforeReply: ["Network.enable", "DOMSnapshot.captureSnapshot"], + commandEvents: (command, origin) => { + const navigation = + "navigation" in scenario ? scenario.navigation : undefined; + if (navigation?.command !== command.method) return undefined; + return [ + frameNavigated(command, { + id: "frame-main", + ...navigation.identity, + url: "url" in navigation ? navigation.url : origin + "/reloaded", + }), + ]; + }, + commandError: ({ method }) => + scenario.sourceFails && method === "Debugger.getScriptSource" + ? { code: -32_000, message: "No script for id" } + : undefined, + }); + trackBrowser(browser); + const provider = new CdpBrowserProvider(); + const input = inspectWebPageInputSchema.parse({ + cdp_endpoint: browser.endpoint, + target_id: "allowed-page", + observation_ms: 0, + include_script_sources: true, + }); + const sourceRejection = { + reason: "protocol_error", + command: "Debugger.getScriptSource", + code: -32_000, + reportedMessage: "No script for id", + }; + try { + for (const expected of scenarios) { + scenario = expected; + const label = JSON.stringify(expected); + const before = browser.commands.length; + const result = await provider.inspectPage(input); + if (result.ok) throw new Error(`Expected refusal: ${label}`); + const diagnostic = projectAnalysisError(result.error); + expect(diagnostic.details, label).toMatchObject({ + operation: "inspect_web_page", + reason: expected.reason, }); - expect(result.error.cause).toMatchObject({ reason: "protocol_error" }); + if (expected.reason === "target_changed") { + expect(diagnostic, label).toMatchObject({ + code: "target_unavailable", + message: expect.stringContaining("target changed"), + remediation: { + action: expect.stringContaining("Retry after navigation settles"), + }, + }); + } else if (expected.reason === "target_not_allowed") { + expect(diagnostic, label).toMatchObject({ + code: "target_unavailable", + message: expect.stringContaining("permitted HTTP(S) origin scope"), + remediation: { action: expect.stringContaining("allowed_origins") }, + }); + } + if (expected.sourceFails) { + const rejection = + expected.reason === "protocol_error" + ? result.error + : result.error.cause; + expect(rejection, label).toMatchObject(sourceRejection); + } + + const methods = browser.commands + .slice(before) + .map(({ method }) => method); + if ( + "navigation" in expected && + expected.navigation.command === "Network.enable" + ) + expect(methods, label).not.toContain("DOMSnapshot.captureSnapshot"); + expect(methods, label).toContain("Target.detachFromTarget"); + await expect.poll(() => browser.activeConnectionCount()).toBe(0); } - expect(browser.commands.map(({ method }) => method)).toContain( - "Target.detachFromTarget", - ); - }, -); + } finally { + await provider.close(); + } +}); diff --git a/tests/boundary/browser/cdpObservationWindowScope.test.ts b/tests/boundary/browser/cdpObservationWindowScope.test.ts index 490909b74c..439debb29a 100644 --- a/tests/boundary/browser/cdpObservationWindowScope.test.ts +++ b/tests/boundary/browser/cdpObservationWindowScope.test.ts @@ -52,20 +52,6 @@ const laterInWindowRequest = ( }, }); -const expectNoCommittedFrameWithInvalidTimeline = ( - observation: Awaited>["observation"], -) => { - expect(observation.timeline).not.toContainEqual( - expect.objectContaining({ type: "navigation_committed" }), - ); - expect(observation.completeness.excluded).toContainEqual( - expect.objectContaining({ - section: "timeline", - reason: "invalid_protocol_value", - }), - ); -}; - it("fails closed on an untimestamped cross-origin main-frame commit during clock sampling", async () => { const { observation } = await observeDuringClockSampling( (command, origin) => { @@ -97,22 +83,11 @@ it("fails closed on an untimestamped cross-origin main-frame commit during clock ); }); -it("keeps an unidentifiable frame commit unknown without inventing a main-frame event", async () => { - const { observation } = await observeDuringClockSampling((command) => - command.method === "Performance.getMetrics" - ? [frameNavigated(command, { url: "https://outside.invalid/unknown" })] - : undefined, - ); - - expect(observation.window.end_reason).toBe("window_elapsed"); - expectNoCommittedFrameWithInvalidTimeline(observation); - expect(observation.completeness.unavailable_sections).toContain("timeline"); -}); - -it("records a malformed child identity without treating it as the main frame", async () => { +it("accounts for each malformed frame commit without inventing a main-frame navigation", async () => { const { observation } = await observeDuringClockSampling((command) => command.method === "Performance.getMetrics" ? [ + frameNavigated(command, { url: "https://outside.invalid/unknown" }), frameNavigated(command, { id: 3, parentId: "frame-main", @@ -123,7 +98,17 @@ it("records a malformed child identity without treating it as the main frame", a ); expect(observation.window.end_reason).toBe("window_elapsed"); - expectNoCommittedFrameWithInvalidTimeline(observation); + expect(observation.timeline).not.toContainEqual( + expect.objectContaining({ type: "navigation_committed" }), + ); + expect(observation.completeness.unavailable_sections).toContain("timeline"); + expect(observation.completeness.excluded).toContainEqual( + expect.objectContaining({ + section: "timeline", + reason: "invalid_protocol_value", + count: 2, + }), + ); }); it("updates the main frame from a pre-arm root replacement without inventing a navigation row", async () => { diff --git a/tests/boundary/browser/cdpScreenshotDocumentIdentity.test.ts b/tests/boundary/browser/cdpScreenshotDocumentIdentity.test.ts index ed751042f3..89c529f99f 100644 --- a/tests/boundary/browser/cdpScreenshotDocumentIdentity.test.ts +++ b/tests/boundary/browser/cdpScreenshotDocumentIdentity.test.ts @@ -59,102 +59,111 @@ describe("screenshot authorization during metrics reads", () => { ), ).toBe(false); }); - // A proven document change is reported before scope; an unproven one - // (no loader) is refused by the current document's scope. - it.each([ - { stage: 1, expectedCaptures: 0, loader: true }, - { stage: 2, expectedCaptures: 1, loader: true }, - { stage: 1, expectedCaptures: 0, loader: false }, - { stage: 2, expectedCaptures: 1, loader: false }, - ])( - "stops capture after scope loss during metrics read $stage (loader $loader)", - async ({ stage, expectedCaptures, loader }) => { - let reads = 0; - const { browser, result } = await captureWithFake({ - eventsBeforeReply: ["Page.getLayoutMetrics"], - commandResult: ({ method }) => - method === "Page.getLayoutMetrics" - ? withCssViewport({ - visualViewport: { - pageX: 0, - pageY: 0, - clientWidth: 2, - clientHeight: 2, - scale: 1, - }, - }) - : undefined, - commandEvents: (command) => - command.method === "Page.getLayoutMetrics" && ++reads === stage - ? [ - frameNavigated(command, { - id: "frame-main", - ...(loader ? { loaderId: "outside-loader" } : {}), - url: "https://outside.test/", - }), - ] - : undefined, - }); - expect(result).toMatchObject({ - ok: false, - error: { reason: loader ? "target_changed" : "target_not_allowed" }, - }); - expect( - browser.commands.filter( - ({ method }) => method === "Page.captureScreenshot", - ), - ).toHaveLength(expectedCaptures); - await expect.poll(() => browser.activeConnectionCount()).toBe(0); - }, - ); - - it.each(["navigation", "uncertain identity"])( - "stops before the first screenshot after %s during metrics", - async (change) => { - const { browser, result } = await captureWithFake({ - eventsBeforeReply: ["Page.getLayoutMetrics"], - commandEvents: (command, origin) => - command.method === "Page.getLayoutMetrics" - ? [ - frameNavigated( - command, - change === "navigation" - ? { - id: "frame-main", - loaderId: "new-loader", - url: `${origin}/app`, - } - : { - id: "candidate-root", - parentId: "candidate-root", - url: `${origin}/app`, - }, - ), - ] - : undefined, - }); - expect(result).toMatchObject({ - ok: false, - error: { - reason: change === "navigation" ? "target_changed" : "protocol_error", + it("stops at either metrics read when navigation invalidates capture", async () => { + const viewport = { + pageX: 0, + pageY: 0, + clientWidth: 2, + clientHeight: 2, + scale: 1, + }; + const beforeAndAfter = [ + { read: 1, captures: 0 }, + { read: 2, captures: 1 }, + ]; + const foreign = "https://outside.test/"; + const scenarios = [ + ...beforeAndAfter.flatMap((phase) => [ + { + ...phase, + url: foreign, + identity: { id: "frame-main", loaderId: "outside-loader" }, + reason: "target_changed", }, - }); - expect( - browser.commands.some( - ({ method }) => method === "Page.captureScreenshot", - ), - ).toBe(false); - }, - ); + { + ...phase, + url: foreign, + identity: { id: "frame-main" }, + reason: "target_not_allowed", + }, + ]), + { + read: 1, + captures: 0, + identity: { id: "frame-main", loaderId: "new-loader" }, + reason: "target_changed", + }, + { + read: 1, + captures: 0, + identity: { id: "candidate-root", parentId: "candidate-root" }, + reason: "protocol_error", + }, + ] as const; + let scenario: (typeof scenarios)[number] | undefined; + let reads = 0; + const browser = await startFakeCdpBrowser({ + eventsBeforeReply: ["Page.getLayoutMetrics"], + commandResult: ({ method }) => + method === "Page.getLayoutMetrics" + ? withCssViewport({ visualViewport: viewport }) + : undefined, + commandEvents: (command, origin) => + command.method === "Page.getLayoutMetrics" && + scenario !== undefined && + ++reads === scenario.read + ? [ + frameNavigated(command, { + ...scenario.identity, + url: + "url" in scenario + ? scenario.url + : `${origin}/app?token=frame-secret`, + }), + ] + : undefined, + }); + onTestFinished(() => browser.close()); + const provider = new CdpBrowserProvider(); + const input = captureWebScreenshotInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + }); + try { + for (const transition of scenarios) { + scenario = transition; + reads = 0; + const label = `metrics read ${transition.read}: ${JSON.stringify(transition.identity)}`; + const before = browser.commands.length; + expect(await provider.captureScreenshot(input), label).toMatchObject({ + ok: false, + error: { reason: transition.reason }, + }); + expect( + browser.commands + .slice(before) + .filter(({ method }) => method === "Page.captureScreenshot"), + label, + ).toHaveLength(transition.captures); + await expect.poll(() => browser.activeConnectionCount()).toBe(0); + } + } finally { + await provider.close(); + } + }); }); describe("screenshot initial root identity", () => { - it.each( - [ - { label: "missing", identity: {} }, - { label: "wrong type", identity: { id: 7 } }, - { label: "empty", identity: { id: "" } }, - { label: "parented", identity: { id: "frame-main", parentId: "parent" } }, + it("requires a valid root before measuring or capturing even with a readable child", async () => { + const invalidRoots = [ + { label: "missing ID", identity: {} }, + { label: "wrong ID type", identity: { id: 7 } }, + { label: "empty ID", identity: { id: "" } }, + { + label: "parented root", + identity: { id: "frame-main", parentId: "parent" }, + }, { label: "malformed parent", identity: { id: "frame-main", parentId: null }, @@ -163,54 +172,66 @@ describe("screenshot initial root identity", () => { label: "self parent", identity: { id: "frame-main", parentId: "frame-main" }, }, - ].flatMap(({ label, identity }) => - [false, true].map((withChild) => ({ label, identity, withChild })), - ), - )( - "rejects $label root identity without borrowing a child ($withChild)", - async ({ identity, withChild }) => { - const { browser, result } = await captureWithFake({ - commandResult: ({ method }, origin) => - method === "Page.getFrameTree" - ? { - frameTree: { - frame: { ...identity, url: origin + "/app" }, - ...(withChild - ? { - childFrames: [ - { - frame: { - id: "frame-child", - parentId: "frame-main", - url: origin + "/child", - }, - }, - ], - } - : {}), - }, - } - : undefined, + ]; + let identity: Readonly> = {}; + const browser = await startFakeCdpBrowser({ + commandResult: ({ method }, origin) => + method === "Page.getFrameTree" + ? { + frameTree: { + frame: { ...identity, url: `${origin}/app?token=frame-secret` }, + childFrames: [ + { + frame: { + id: "frame-child", + parentId: "frame-main", + url: `${origin}/child`, + }, + }, + ], + }, + } + : undefined, + }); + onTestFinished(() => browser.close()); + const provider = new CdpBrowserProvider(); + const input = captureWebScreenshotInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + }); + try { + for (const invalid of invalidRoots) { + identity = invalid.identity; + const before = browser.commands.length; + const result = await provider.captureScreenshot(input); + if (result.ok) throw new Error(`Expected refusal for ${invalid.label}`); + expect(projectAnalysisError(result.error), invalid.label).toMatchObject( + { + code: "unreadable_output", + details: { reason: "protocol_error" }, + }, + ); + const methods = browser.commands + .slice(before) + .map(({ method }) => method); + expect(methods, invalid.label).not.toContain("Page.getLayoutMetrics"); + expect(methods, invalid.label).not.toContain("Page.captureScreenshot"); + } + + identity = { id: "frame-main", loaderId: "loader-main" }; + const valid = await provider.captureScreenshot(input); + if (!valid.ok) throw valid.error; + expect(valid.value.target).toMatchObject({ + target_id: "allowed-page", + url: `${browser.allowedOrigin}/app?token=frame-secret`, }); - expect(result).toMatchObject({ - ok: false, - error: { reason: "protocol_error" }, - }); - if (result.ok) throw new Error("Expected malformed root refusal"); - const projection = projectAnalysisError(result.error); - expect(projection.message).not.toContain( - "outside the permitted HTTP(S) origin scope", - ); - expect(projection.remediation.action).not.toContain("allowed_origins"); - expect(browser.commands.map(({ method }) => method)).not.toContain( - "Page.getLayoutMetrics", - ); - expect(browser.commands.map(({ method }) => method)).not.toContain( - "Page.captureScreenshot", - ); - await expect.poll(() => browser.activeConnectionCount()).toBe(0); - }, - ); + expect(valid.value.viewport).toEqual({ width: 1, height: 1 }); + } finally { + await provider.close(); + } + await expect.poll(() => browser.activeConnectionCount()).toBe(0); + }); it("retains a known foreign origin refusal when the root ID is malformed", async () => { const { browser, result } = await captureWithFake({ @@ -239,135 +260,107 @@ describe("screenshot initial root identity", () => { describe("screenshot viewport freshness", () => { it.each([ - { phase: "normal capture", stage: 2, imageSize: 4, captures: 1 }, - { phase: "before recovery", stage: 2, imageSize: 1, captures: 1 }, - { phase: "during recovery", stage: 3, imageSize: 1, captures: 2 }, - ])( - "rejects viewport changes $phase", - async ({ stage, imageSize, captures }) => { - let reads = 0; - const { browser, result } = await captureWithFake({ - commandResult: ({ method }) => - method === "Page.getLayoutMetrics" - ? withCssViewport({ - visualViewport: { - pageX: 0, - pageY: ++reads >= stage ? 50 : 0, - clientWidth: 4, - clientHeight: 4, - scale: 1, - }, - }) - : method === "Page.captureScreenshot" - ? screenshotData(imageSize, imageSize) - : undefined, - }); - expect(result).toMatchObject({ - ok: false, - error: { reason: "target_changed" }, - }); - expect( - browser.commands.filter( - ({ method }) => method === "Page.captureScreenshot", - ), - ).toHaveLength(captures); - }, - ); - - it.each([ - { label: "resize", css: { clientWidth: 8 }, pixel: { clientWidth: 8 } }, - { label: "zoom", css: { scale: 2 }, pixel: { scale: 2 } }, - { - label: "device pixel density", - css: {}, - pixel: { clientWidth: 8, clientHeight: 8 }, - }, - ])( - "rejects $label during recovery even when the image is large enough", - async ({ css, pixel }) => { - let reads = 0; - let captures = 0; - const base = { - pageX: 0, - pageY: 0, - clientWidth: 4, - clientHeight: 4, - scale: 1, - }; - const { result } = await captureWithFake({ - commandResult: ({ method }) => { - if (method === "Page.getLayoutMetrics") { - const changed = ++reads === 3; - return { - cssVisualViewport: { ...base, ...(changed ? css : {}) }, - visualViewport: { ...base, ...(changed ? pixel : {}) }, - }; - } - return method === "Page.captureScreenshot" - ? ++captures === 1 - ? screenshotData(1, 1) - : screenshotData(16, 16) - : undefined; - }, - }); - expect(result).toMatchObject({ - ok: false, - error: { reason: "target_changed" }, - }); - expect(captures).toBe(2); - }, - ); - - it.each([2, 3])("validates refreshed metrics read %s", async (stage) => { + { phase: "normal capture", imageSize: 4 }, + { phase: "before recovery", imageSize: 1 }, + ])("rejects scrolling $phase without retrying", async ({ imageSize }) => { let reads = 0; - const { browser, result } = await captureWithFake({ - commandResult: ({ method }) => - method === "Page.getLayoutMetrics" - ? ++reads === stage - ? {} - : withCssViewport({ - visualViewport: { - pageX: 0, - pageY: 0, - clientWidth: 2, - clientHeight: 2, - scale: 1, - }, - }) - : undefined, - }); - expect(result).toMatchObject({ - ok: false, - error: { - reason: "protocol_error", - command: "Page.getLayoutMetrics", - pointer: "/visualViewport", - }, - }); - expect( - browser.commands.filter( - ({ method }) => method === "Page.captureScreenshot", - ), - ).toHaveLength(stage - 1); - }); - - it("reports a screenshot reply without image data at the protocol boundary", async () => { const { browser, result } = await captureWithFake({ commandResult: ({ method }) => method === "Page.getLayoutMetrics" ? withCssViewport({ visualViewport: { pageX: 0, - pageY: 0, - clientWidth: 2, - clientHeight: 2, + pageY: ++reads === 1 ? 0 : 50, + clientWidth: 4, + clientHeight: 4, scale: 1, }, }) : method === "Page.captureScreenshot" - ? { data: 5 } + ? screenshotData(imageSize, imageSize) : undefined, }); + expect(result).toMatchObject({ + ok: false, + error: { + reason: "target_changed", + detail: expect.stringContaining("visual viewport changed"), + }, + }); + expect( + browser.commands.filter( + ({ method }) => method === "Page.captureScreenshot", + ), + ).toHaveLength(1); + }); + + it("rejects independently changed CSS size, zoom, and pixel dimensions during recovery", async () => { + const base = { + pageX: 0, + pageY: 0, + clientWidth: 4, + clientHeight: 4, + scale: 1, + }; + const changes = [ + { label: "CSS width", css: { clientWidth: 8 }, pixels: {} }, + { label: "page zoom", css: { scale: 2 }, pixels: { scale: 2 } }, + { label: "pixel width", css: {}, pixels: { clientWidth: 8 } }, + { label: "pixel height", css: {}, pixels: { clientHeight: 8 } }, + ]; + let change: (typeof changes)[number] | undefined; + let metricsReads = 0; + let captures = 0; + const browser = await startFakeCdpBrowser({ + commandResult: ({ method }) => { + if (method === "Page.getLayoutMetrics") { + const current = ++metricsReads >= 3 ? change : undefined; + return { + cssVisualViewport: { ...base, ...current?.css }, + visualViewport: { ...base, ...current?.pixels }, + }; + } + if (method === "Page.captureScreenshot") + return ++captures === 1 + ? screenshotData(1, 1) + : screenshotData(16, 16); + return undefined; + }, + }); + onTestFinished(() => browser.close()); + const input = captureWebScreenshotInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + }); + const provider = new CdpBrowserProvider(); + try { + for (const scenario of changes) { + change = scenario; + metricsReads = 0; + captures = 0; + expect( + await provider.captureScreenshot(input), + scenario.label, + ).toMatchObject({ + ok: false, + error: { + reason: "target_changed", + detail: expect.stringContaining("visual viewport changed"), + }, + }); + expect(captures, scenario.label).toBe(2); + } + } finally { + await provider.close(); + } + }); + + it("reports a malformed screenshot reply at the data field and detaches", async () => { + const { browser, result } = await captureWithFake({ + commandResult: ({ method }) => + method === "Page.captureScreenshot" ? { data: 5 } : undefined, + }); expect(result).toMatchObject({ ok: false, error: { @@ -376,180 +369,90 @@ describe("screenshot viewport freshness", () => { pointer: "/data", }, }); - expect( - browser.commands.some( - ({ method }) => method === "Target.detachFromTarget", - ), - ).toBe(true); + await expect.poll(() => browser.activeConnectionCount()).toBe(0); }); }); -describe("screenshot visual viewport capture", () => { - it("recovers a cropped surface using the reported viewport clip and page scale", async () => { - let captures = 0; - const browser = await startFakeCdpBrowser({ - commandResult: ({ method }) => - method === "Page.getLayoutMetrics" - ? withCssViewport({ - visualViewport: { - pageX: 12, - pageY: 34, - clientWidth: 640, - clientHeight: 360, - scale: 2, - }, - }) - : method === "Page.captureScreenshot" - ? ++captures === 1 - ? screenshotData(320, 180) - : screenshotData(1280, 720) - : undefined, - }); - onTestFinished(async () => { - await browser.close(); - }); - const result = await new CdpBrowserProvider().captureScreenshot( - captureWebScreenshotInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - }), - ); - expect(result.ok).toBe(true); - const layoutMetrics = browser.commands.findIndex( - ({ method }) => method === "Page.getLayoutMetrics", - ); - const screenshotIndex = browser.commands.findIndex( - ({ method }) => method === "Page.captureScreenshot", - ); - expect(layoutMetrics).toBeGreaterThanOrEqual(0); - expect(screenshotIndex).toBeGreaterThan(layoutMetrics); - const screenshots = browser.commands.filter( - ({ method }) => method === "Page.captureScreenshot", - ); - expect(screenshots).toHaveLength(2); - expect(screenshots[0]?.params).toMatchObject({ - captureBeyondViewport: false, - }); - expect(screenshots[0]?.params).not.toHaveProperty("clip"); - expect(screenshots[1]?.params).toMatchObject({ - fromSurface: true, - captureBeyondViewport: true, - clip: { x: 12, y: 34, width: 640, height: 360, scale: 2 }, - }); - if (!result.ok) throw result.error; - expect(result.value.viewport).toEqual({ width: 1280, height: 720 }); +it("uses CSS clip coordinates and scaled device-pixel dimensions for clipped recovery", async () => { + let captures = 0; + const recovered = screenshotData(2560, 1440); + const { browser, result } = await captureWithFake({ + commandResult: ({ method }) => + method === "Page.getLayoutMetrics" + ? { + cssVisualViewport: { + pageX: 12, + pageY: 34, + clientWidth: 640, + clientHeight: 360, + scale: 2, + }, + visualViewport: { + pageX: 24, + pageY: 68, + clientWidth: 1280, + clientHeight: 720, + scale: 2, + }, + } + : method === "Page.captureScreenshot" + ? ++captures === 1 + ? screenshotData(1280, 720) + : recovered + : undefined, }); -}); - -describe("screenshot CSS and device-pixel coordinates", () => { - it("uses CSS clip coordinates when the reported viewport has twice as many device pixels", async () => { - let captures = 0; - const browser = await startFakeCdpBrowser({ - commandResult: ({ method }) => - method === "Page.getLayoutMetrics" - ? { - cssVisualViewport: { - pageX: 12, - pageY: 34, - clientWidth: 640, - clientHeight: 360, - scale: 1, - }, - visualViewport: { - pageX: 24, - pageY: 68, - clientWidth: 1280, - clientHeight: 720, - scale: 1, - }, - } - : method === "Page.captureScreenshot" - ? ++captures === 1 - ? screenshotData(800, 400) - : screenshotData(1280, 720) - : undefined, - }); - onTestFinished(() => browser.close()); - const result = await new CdpBrowserProvider().captureScreenshot( - captureWebScreenshotInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - }), - ); - expect(result.ok).toBe(true); - const screenshots = browser.commands.filter( - ({ method }) => method === "Page.captureScreenshot", - ); - expect(screenshots).toHaveLength(2); - expect(screenshots[1]?.params.clip).toEqual({ - x: 12, - y: 34, - width: 640, - height: 360, - scale: 1, - }); + if (!result.ok) throw result.error; + const screenshots = browser.commands.filter( + ({ method }) => method === "Page.captureScreenshot", + ); + expect(screenshots).toHaveLength(2); + expect(screenshots[0]?.params).toMatchObject({ + captureBeyondViewport: false, }); + expect(screenshots[0]?.params).not.toHaveProperty("clip"); + expect(screenshots[1]?.params).toMatchObject({ + fromSurface: true, + captureBeyondViewport: true, + clip: { x: 12, y: 34, width: 640, height: 360, scale: 2 }, + }); + expect(result.value.viewport).toEqual({ width: 2560, height: 1440 }); + expect(result.value.artifact.data_base64).toBe(recovered.data); }); describe("screenshot surface compatibility", () => { - it.each([ - { - label: "scrollbars", - width: 2033, - height: 1521, - imageWidth: 2048, - imageHeight: 1536, - }, - { - label: "higher pixel density", - width: 640, - height: 360, - imageWidth: 1280, - imageHeight: 720, - }, - ])( - "preserves the original surface with $label", - async ({ width, height, imageWidth, imageHeight }) => { - const image = screenshotData(imageWidth, imageHeight); - const browser = await startFakeCdpBrowser({ - commandResult: ({ method }) => - method === "Page.getLayoutMetrics" - ? withCssViewport({ - visualViewport: { - pageX: 0, - pageY: 0, - clientWidth: width, - clientHeight: height, - scale: 1, - }, - }) - : method === "Page.captureScreenshot" - ? image - : undefined, - }); - onTestFinished(() => browser.close()); - const result = await new CdpBrowserProvider().captureScreenshot( - captureWebScreenshotInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - }), - ); - if (!result.ok) throw result.error; - expect(result.value.viewport).toEqual({ - width: imageWidth, - height: imageHeight, - }); - expect(result.value.artifact.data_base64).toBe(image.data); - expect( - browser.commands.filter( - ({ method }) => method === "Page.captureScreenshot", - ), - ).toHaveLength(1); - }, - ); + it("preserves an original high-density surface including pixels outside the visual viewport", async () => { + const image = screenshotData(1295, 735); + const cssViewport = { + pageX: 0, + pageY: 0, + clientWidth: 640, + clientHeight: 360, + scale: 1, + }; + const { browser, result } = await captureWithFake({ + commandResult: ({ method }) => + method === "Page.getLayoutMetrics" + ? { + cssVisualViewport: cssViewport, + visualViewport: { + ...cssViewport, + clientWidth: 1280, + clientHeight: 720, + }, + } + : method === "Page.captureScreenshot" + ? image + : undefined, + }); + if (!result.ok) throw result.error; + expect(result.value.viewport).toEqual({ width: 1295, height: 735 }); + expect(result.value.artifact.data_base64).toBe(image.data); + expect( + browser.commands.filter( + ({ method }) => method === "Page.captureScreenshot", + ), + ).toHaveLength(1); + }); it("refuses a clipped recovery that still omits visible content", async () => { const browser = await startFakeCdpBrowser({ @@ -621,198 +524,117 @@ describe("screenshot surface compatibility", () => { }); }); -describe("screenshot viewport protocol validation", () => { - it.each([ - ["missing metrics", {}], - [ - "non-numeric coordinates", - { - visualViewport: { - pageX: "unknown", - pageY: 0, - clientWidth: 10, - clientHeight: 10, - scale: 1, - }, - }, - ], - [ - "non-finite coordinates", - { - visualViewport: { - pageX: Number.POSITIVE_INFINITY, - pageY: 0, - clientWidth: 10, - clientHeight: 10, - scale: 1, - }, - }, - ], - [ - "non-positive dimensions", - { - visualViewport: { - pageX: 0, - pageY: 0, - clientWidth: 0, - clientHeight: 10, - scale: 1, - }, - }, - ], - [ - "non-positive scale", - { - visualViewport: { - pageX: 0, - pageY: 0, - clientWidth: 10, - clientHeight: 10, - scale: 0, - }, - }, - ], - ])("rejects %s before screenshot capture", async (_label, metrics) => { - const browser = await startFakeCdpBrowser({ - commandResult: ({ method }) => - method === "Page.getLayoutMetrics" - ? withCssViewport(metrics) - : undefined, - }); - onTestFinished(async () => { - await browser.close(); - }); - const result = await new CdpBrowserProvider().captureScreenshot( - captureWebScreenshotInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - }), - ); - expect(result).toMatchObject({ - ok: false, - error: { reason: "protocol_error" }, - }); - expect( - browser.commands.some( - ({ method }) => method === "Page.captureScreenshot", - ), - ).toBe(false); +it("rejects malformed viewport metrics at the relevant field before capture can proceed", async () => { + const viewport = { + pageX: 0, + pageY: 0, + clientWidth: 2, + clientHeight: 2, + scale: 1, + }; + const valid = withCssViewport({ visualViewport: viewport }); + const scenarios = [ + { read: 1, reply: {}, pointer: "/visualViewport", captures: 0 }, + { + read: 1, + reply: { ...valid, cssVisualViewport: { ...viewport, pageX: null } }, + pointer: "/cssVisualViewport/pageX", + captures: 0, + }, + { + read: 1, + reply: { ...valid, visualViewport: { ...viewport, clientWidth: 0 } }, + pointer: "/visualViewport/clientWidth", + captures: 0, + }, + { + read: 1, + reply: { ...valid, cssVisualViewport: { ...viewport, scale: 0 } }, + pointer: "/cssVisualViewport/scale", + captures: 0, + }, + { + read: 2, + reply: { visualViewport: viewport }, + pointer: "/cssVisualViewport", + captures: 1, + }, + { + read: 3, + reply: { ...valid, visualViewport: { ...viewport, scale: null } }, + pointer: "/visualViewport/scale", + captures: 2, + }, + ] as const; + let scenario: (typeof scenarios)[number] = scenarios[0]; + let reads = 0; + const browser = await startFakeCdpBrowser({ + commandResult: ({ method, params }) => { + if (method === "Page.getLayoutMetrics") + return ++reads === scenario.read ? scenario.reply : valid; + if (method === "Page.captureScreenshot") { + const size = params.clip === undefined ? 1 : 2; + return screenshotData(size, size); + } + return undefined; + }, }); + onTestFinished(() => browser.close()); + const provider = new CdpBrowserProvider(); + const input = captureWebScreenshotInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + }); + const screenshotCount = () => + browser.commands.filter(({ method }) => method === "Page.captureScreenshot") + .length; + try { + for (const invalid of scenarios) { + scenario = invalid; + reads = 0; + const before = screenshotCount(); + const label = `metrics reply ${invalid.read} at ${invalid.pointer}`; + expect(await provider.captureScreenshot(input), label).toMatchObject({ + ok: false, + error: { + reason: "protocol_error", + command: "Page.getLayoutMetrics", + pointer: invalid.pointer, + }, + }); + expect(screenshotCount() - before, label).toBe(invalid.captures); + await expect.poll(() => browser.activeConnectionCount()).toBe(0); + } + } finally { + await provider.close(); + } }); -describe("screenshot committed document identity", () => { - it.each(["requested-only", "same-url-history"])( - "accepts unchanged identity after %s", - async (kind) => { - const browser = await startFakeCdpBrowser({ - commandEvents: ({ method, sessionId }, origin) => - method !== "Page.captureScreenshot" - ? undefined - : [ - { - method: - kind === "requested-only" - ? "Page.frameRequestedNavigation" - : "Page.navigatedWithinDocument", - ...(sessionId === undefined ? {} : { sessionId }), - params: { - frameId: "frame-main", - url: `${origin}${kind === "requested-only" ? "/pending" : "/app?token=frame-secret"}`, - reason: "scriptInitiated", - disposition: "currentTab", - navigationType: "historyApi", - }, - }, - ], - }); - onTestFinished(async () => browser.close()); - const result = await new CdpBrowserProvider().captureScreenshot( - captureWebScreenshotInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - }), - ); - expect(result.ok).toBe(true); - }, - ); - - it.each([ - ["missing screenshot data", {}], - ["invalid base64 screenshot data", { data: "not base64%%%" }], - ])("reports %s as a protocol error", async (_label, result) => { - const { result: observation } = await captureWithFake({ - commandResult: ({ method }) => - method === "Page.captureScreenshot" ? result : undefined, - }); - expect(observation).toMatchObject({ - ok: false, - error: { reason: "protocol_error" }, - }); +it("rejects an unidentifiable navigation and releases the screenshot connection", async () => { + const { browser, result } = await captureWithFake({ + commandEvents: (command) => + command.method === "Page.captureScreenshot" + ? [ + { + method: "Page.frameNavigated", + ...(command.sessionId === undefined + ? {} + : { sessionId: command.sessionId }), + params: undefined, + }, + ] + : undefined, }); - - it.each([ - { offset: 8, value: 14 }, - { offset: 16, value: 0x8000_0000 }, - { offset: 20, value: 0x8000_0000 }, - ])("rejects invalid screenshot IHDR metadata", async ({ offset, value }) => { - const bytes = Buffer.from( - "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jv4sAAAAASUVORK5CYII=", - "base64", - ); - bytes.writeUInt32BE(value, offset); - bytes.writeUInt32BE(crc32(bytes.subarray(12, 29)), 29); - const { result } = await captureWithFake({ - commandResult: ({ method }) => - method === "Page.captureScreenshot" - ? { data: bytes.toString("base64") } - : undefined, - }); - expect(result).toMatchObject({ - ok: false, - error: { reason: "protocol_error" }, - }); - }); - it.each(["loader-main", "loader-reloaded"])( - "distinguishes %s for the same URL", - async (screenshotDocumentLoader) => { - const { result } = await captureWithFake({ screenshotDocumentLoader }); - if (screenshotDocumentLoader === "loader-main") - expect(result.ok).toBe(true); - else - expect(result).toMatchObject({ - ok: false, - error: { reason: "target_changed" }, - }); - }, - ); - - it("defers an unidentifiable navigation failure until screenshot cleanup completes", async () => { - const { browser, result } = await captureWithFake({ - commandEvents: (command) => - command.method === "Page.captureScreenshot" - ? [ - { - method: "Page.frameNavigated", - ...(command.sessionId === undefined - ? {} - : { sessionId: command.sessionId }), - params: undefined, - }, - ] - : undefined, - }); - expect(result).toMatchObject({ - ok: false, - error: { reason: "protocol_error" }, - }); - await expect.poll(() => browser.activeConnectionCount()).toBe(0); + expect(result).toMatchObject({ + ok: false, + error: { reason: "protocol_error" }, }); + await expect.poll(() => browser.activeConnectionCount()).toBe(0); }); describe("screenshot frame identity transitions", () => { - it("does not recover a cropped image after uncertain root identity", async () => { + it("rejects a self-parented root candidate and detaches without retrying the cropped screenshot", async () => { const { browser, result } = await captureWithFake({ commandResult: ({ method }) => method === "Page.getLayoutMetrics" @@ -846,278 +668,268 @@ describe("screenshot frame identity transitions", () => { ({ method }) => method === "Page.captureScreenshot", ), ).toHaveLength(1); - }); - - it("rejects a self-parented root candidate as malformed identity", async () => { - const { browser, result } = await captureWithFake({ - commandEvents: (command, origin) => - command.method === "Page.captureScreenshot" - ? [ - frameNavigated(command, { - id: "candidate-root", - parentId: "candidate-root", - url: `${origin}/app?token=frame-secret`, - }), - ] - : undefined, - }); - expect(result).toMatchObject({ - ok: false, - error: { reason: "protocol_error" }, - }); await expect.poll(() => browser.activeConnectionCount()).toBe(0); }); - - it("keeps a malformed known child as screenshot coverage", async () => { - const { result } = await captureWithFake({ - commandEvents: (command) => - command.method === "Page.captureScreenshot" - ? [ - frameNavigated(command, { - id: 7, - parentId: "frame-main", - url: "https://outside.invalid/child", - }), - ] - : undefined, - }); - if (!result.ok) throw result.error; - expect(result.value.completeness.excluded).toContainEqual( - expect.objectContaining({ - section: "screenshot", - reason: "invalid_protocol_value", - }), - ); - }); - - it("detects a same-URL root replacement and scopes later events to its new identity", async () => { - const { result } = await captureWithFake({ - eventsBeforeReply: ["Page.captureScreenshot"], - commandEvents: (command, origin) => - command.method === "Page.captureScreenshot" - ? [ - frameNavigated(command, { - id: "replacement-main", - loaderId: "loader-main", - url: `${origin}/app?token=frame-secret`, - }), - { - method: "Page.navigatedWithinDocument", - ...(command.sessionId === undefined - ? {} - : { sessionId: command.sessionId }), - params: { - frameId: "replacement-main", - url: "https://outside.invalid/path", - }, - }, - ] - : undefined, - }); - expect(result).toMatchObject({ - ok: false, - error: { reason: "target_changed" }, - }); - }); }); -describe("screenshot navigation event applicability", () => { - it("ignores non-navigation events with selected-frame or malformed parameters", async () => { - const { result } = await captureWithFake({ - commandEvents: (command) => - command.method === "Page.captureScreenshot" - ? [ - { - method: "Runtime.consoleAPICalled", - sessionId: command.sessionId, - params: { - type: "log", - args: [], - executionContextId: 1, - timestamp: 1, - }, - }, - { - method: "Page.lifecycleEvent", - sessionId: command.sessionId, - params: { - frameId: "frame-main", - loaderId: "loader-main", - name: "DOMContentLoaded", - timestamp: 1, - }, - }, - { - method: "Runtime.consoleAPICalled", - sessionId: command.sessionId, - params: { frameId: "frame-main", args: "not-an-array" }, - }, - ] - : undefined, - }); - expect(result.ok).toBe(true); - }); - - it.each([ - "Page.frameRequestedNavigation", - "Page.navigatedWithinDocument", - ] as const)("ignores a known non-main frame's %s event", async (method) => { - const { result } = await captureWithFake({ - commandEvents: (command) => - command.method === "Page.captureScreenshot" - ? [ - { - method, - sessionId: command.sessionId, - params: { - frameId: "frame-child", - url: "https://outside.invalid/child", - }, - }, - ] - : undefined, - }); - expect(result.ok).toBe(true); +it("preserves the screenshot when navigation events leave the selected document unchanged", async () => { + const image = screenshotData(1, 1); + const { result } = await captureWithFake({ + extraCollections: true, + eventsBeforeReply: ["Page.captureScreenshot"], + commandResult: ({ method }) => + method === "Page.captureScreenshot" ? image : undefined, + commandEvents: (command, origin) => { + if (command.method !== "Page.captureScreenshot") return undefined; + const sessionId = command.sessionId; + const selectedUrl = `${origin}/app?token=frame-secret`; + const outside = "https://outside.invalid/"; + return [ + ...["frame-main", "frame-child"].map((frameId) => ({ + method: "Page.frameRequestedNavigation", + sessionId, + params: { + frameId, + url: outside, + reason: "scriptInitiated", + disposition: "currentTab", + }, + })), + { + method: "Page.navigatedWithinDocument", + sessionId, + params: { + frameId: "frame-main", + url: selectedUrl, + navigationType: "historyApi", + }, + }, + { + method: "Page.navigatedWithinDocument", + sessionId, + params: { + frameId: "frame-child", + url: `${outside}child`, + navigationType: "historyApi", + }, + }, + frameNavigated(command, { + id: "frame-main", + loaderId: "loader-main", + url: selectedUrl, + }), + { + method: "Runtime.consoleAPICalled", + sessionId, + params: { + type: "log", + args: [], + executionContextId: 1, + timestamp: 1, + }, + }, + { + method: "Page.lifecycleEvent", + sessionId, + params: { + frameId: "frame-main", + loaderId: "loader-main", + name: "DOMContentLoaded", + timestamp: 1, + }, + }, + { + method: "Runtime.consoleAPICalled", + sessionId, + params: { frameId: "frame-main", args: "not-an-array" }, + }, + frameNavigated(command, { + id: 7, + parentId: "frame-main", + url: `${outside}malformed-child`, + }), + ]; + }, }); + if (!result.ok) throw result.error; + expect(result.value.artifact.data_base64).toBe(image.data); + expect(result.value.completeness.excluded).toEqual([ + { section: "screenshot", reason: "invalid_protocol_value", count: 1 }, + ]); + expect(result.value.completeness.attach_limited_sections).toEqual([]); }); -it.each( - [7, "missing-hash"].flatMap((fragment) => - ["initial", "event", "final"].flatMap((phase) => - ["allowed", "foreign", "missing"].map((scope) => ({ - fragment, - phase, - scope, - })), - ), - ), -)( - "keeps malformed screenshot fragments distinct from scope exits: $phase/$scope/$fragment", - async ({ fragment, phase, scope }) => { - const browser = await startFakeCdpBrowser({ - commandResult: ({ method }, origin, reads) => - method === "Page.getFrameTree" && - (phase === "initial" || (phase === "final" && reads > 1)) - ? { - frameTree: { - frame: { - id: "frame-main", - loaderId: "loader-main", - ...(scope === "missing" - ? {} - : { - url: - scope === "foreign" - ? "https://foreign.test/" - : `${origin}/app?token=frame-secret`, - }), - urlFragment: fragment, - }, - }, - } - : undefined, - commandEvents: ({ method, sessionId }, origin) => - phase === "event" && method === "Page.captureScreenshot" - ? [ - { - method: "Page.frameNavigated", - ...(sessionId === undefined ? {} : { sessionId }), - params: { - frame: { - id: "frame-main", - loaderId: "loader-main", - ...(scope === "missing" - ? {} - : { - url: - scope === "foreign" - ? "https://foreign.test/" - : `${origin}/app?token=frame-secret`, - }), - urlFragment: fragment, - }, - }, - }, - ] - : undefined, - }); - onTestFinished(async () => browser.close()); - const result = await new CdpBrowserProvider().captureScreenshot( - captureWebScreenshotInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - }), - ); - expect(result).toMatchObject({ - ok: false, - error: { - reason: scope === "foreign" ? "target_not_allowed" : "protocol_error", - }, - }); - }, -); - -it("retains a foreign base scope even when its fragment is malformed", async () => { +it("rejects a changed root frame or loader even when the screenshot URL is unchanged", async () => { + const replacements = [ + { id: "replacement-main", loaderId: "loader-main" }, + { id: "frame-main", loaderId: "loader-reloaded" }, + ] as const; + let replacement: (typeof replacements)[number] = replacements[0]; const browser = await startFakeCdpBrowser({ - commandEvents: ({ method, sessionId }) => - method === "Page.captureScreenshot" + eventsBeforeReply: ["Page.captureScreenshot"], + commandEvents: (command, origin) => + command.method === "Page.captureScreenshot" ? [ - { - method: "Page.frameNavigated", - ...(sessionId === undefined ? {} : { sessionId }), - params: { - frame: { - id: "frame-main", - loaderId: "foreign", - url: "https://foreign.test/", - urlFragment: 7, - }, - }, - }, + frameNavigated(command, { + ...replacement, + url: `${origin}/app?token=frame-secret`, + }), ] : undefined, }); - onTestFinished(async () => browser.close()); - const result = await new CdpBrowserProvider().captureScreenshot( - captureWebScreenshotInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - }), - ); - expect(result).toMatchObject({ - ok: false, - error: { reason: "target_changed" }, + onTestFinished(() => browser.close()); + const provider = new CdpBrowserProvider(); + const input = captureWebScreenshotInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", }); + try { + for (const changed of replacements) { + replacement = changed; + const before = browser.commands.length; + expect( + await provider.captureScreenshot(input), + JSON.stringify(changed), + ).toMatchObject({ + ok: false, + error: { reason: "target_changed" }, + }); + expect( + browser.commands + .slice(before) + .filter(({ method }) => method === "Page.getFrameTree"), + ).toHaveLength(1); + await expect.poll(() => browser.activeConnectionCount()).toBe(0); + } + } finally { + await provider.close(); + } }); -it.each([24, 32, "crc"])( - "rejects incomplete or corrupt PNG IHDR: %s", - async (kind) => { - const png = Buffer.from( - "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jv4sAAAAASUVORK5CYII=", - "base64", - ); - if (kind === "crc") png[29] = (png[29] ?? 0) ^ 1; - const bytes = typeof kind === "number" ? png.subarray(0, kind) : png; - const browser = await startFakeCdpBrowser({ - commandResult: ({ method }) => - method === "Page.captureScreenshot" - ? { data: bytes.toString("base64") } - : undefined, - }); - onTestFinished(async () => browser.close()); - const result = await new CdpBrowserProvider().captureScreenshot( - captureWebScreenshotInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - }), - ); - expect(result).toMatchObject({ - ok: false, - error: { reason: "protocol_error" }, - }); - }, -); +it("separates unreadable screenshot document URLs from known scope loss at each capture phase", async () => { + const phases = [ + { phase: "initial", captures: 0 }, + { phase: "event", captures: 1 }, + { phase: "final", captures: 1 }, + ] as const; + const invalidDocuments = [ + { source: "allowed", fragment: 7, reason: "protocol_error" }, + { source: "foreign", fragment: 7, reason: "target_not_allowed" }, + { source: "missing", reason: "protocol_error" }, + ] as const; + const scenarios = [ + ...phases.flatMap((phase) => + invalidDocuments.map((document) => ({ + ...phase, + ...document, + loader: "loader-main", + })), + ), + { + phase: "event", + captures: 1, + source: "foreign", + fragment: 7, + reason: "target_changed", + loader: "new-loader", + }, + ] as const; + let scenario: (typeof scenarios)[number] | undefined; + let treeReads = 0; + const frame = (origin: string) => { + if (scenario === undefined) throw new Error("Missing screenshot scenario"); + return { + id: "frame-main", + loaderId: scenario.loader, + ...(scenario.source === "missing" + ? {} + : { + url: + scenario.source === "foreign" + ? "https://foreign.test/" + : `${origin}/app?token=frame-secret`, + urlFragment: scenario.fragment, + }), + }; + }; + const browser = await startFakeCdpBrowser({ + eventsBeforeReply: ["Page.captureScreenshot"], + commandResult: ({ method }, origin) => { + if (method !== "Page.getFrameTree") return undefined; + treeReads += 1; + return (scenario?.phase === "initial" && treeReads === 1) || + (scenario?.phase === "final" && treeReads === 2) + ? { frameTree: { frame: frame(origin) } } + : undefined; + }, + commandEvents: (command, origin) => + scenario?.phase === "event" && command.method === "Page.captureScreenshot" + ? [frameNavigated(command, frame(origin))] + : undefined, + }); + onTestFinished(() => browser.close()); + const provider = new CdpBrowserProvider(); + const input = captureWebScreenshotInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + }); + try { + for (const invalid of scenarios) { + scenario = invalid; + treeReads = 0; + const before = browser.commands.length; + const label = `${invalid.phase}/${invalid.source}/${invalid.loader}`; + expect(await provider.captureScreenshot(input), label).toMatchObject({ + ok: false, + error: { reason: invalid.reason }, + }); + expect( + browser.commands + .slice(before) + .filter(({ method }) => method === "Page.captureScreenshot"), + label, + ).toHaveLength(invalid.captures); + await expect.poll(() => browser.activeConnectionCount()).toBe(0); + } + } finally { + await provider.close(); + } +}); + +it("rejects noncanonical base64 and unreadable PNG without attempting a clipped capture", async () => { + let data = ""; + const browser = await startFakeCdpBrowser({ + commandResult: ({ method }) => + method === "Page.captureScreenshot" ? { data } : undefined, + }); + onTestFinished(() => browser.close()); + const provider = new CdpBrowserProvider(); + const input = captureWebScreenshotInputSchema.parse({ + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + }); + const png = screenshotData(1, 1).data; + const invalidHeader = Buffer.from(png, "base64"); + invalidHeader[0] = 0; + const screenshotCount = () => + browser.commands.filter(({ method }) => method === "Page.captureScreenshot") + .length; + try { + // Keep the length divisible by four so the first reply reaches the canonical byte check. + for (const invalid of [` ${png}`, invalidHeader.toString("base64")]) { + data = invalid; + const before = screenshotCount(); + expect(await provider.captureScreenshot(input)).toMatchObject({ + ok: false, + error: { reason: "protocol_error" }, + }); + expect(screenshotCount() - before).toBe(1); + await expect.poll(() => browser.activeConnectionCount()).toBe(0); + } + } finally { + await provider.close(); + } +}); diff --git a/tests/boundary/browser/cdpTargetChanged.test.ts b/tests/boundary/browser/cdpTargetChanged.test.ts index fbbffa0d95..464ba3d56a 100644 --- a/tests/boundary/browser/cdpTargetChanged.test.ts +++ b/tests/boundary/browser/cdpTargetChanged.test.ts @@ -325,8 +325,9 @@ describe("screenshot capture", () => { it("reports a navigation that fails the screenshot command as target_changed", async () => { const browser = await start({ + eventsBeforeReply: ["Page.captureScreenshot"], commandEvents: (command, origin) => - command.method === "Page.getLayoutMetrics" + command.method === "Page.captureScreenshot" ? [ frameNavigated(command, { id: "frame-main", @@ -348,8 +349,15 @@ describe("screenshot capture", () => { }), ); if (result.ok) throw new Error("Expected target_changed"); - expect(result.error).toMatchObject({ reason: "target_changed" }); - expect(result.error.cause).toBeDefined(); + expect(result.error).toMatchObject({ + reason: "target_changed", + cause: { + reason: "protocol_error", + command: "Page.captureScreenshot", + code: -32_000, + reportedMessage: "Target navigated", + }, + }); }); }); diff --git a/tests/boundary/browser/sourceMapCaptureExclusions.test.ts b/tests/boundary/browser/sourceMapCaptureExclusions.test.ts index bd0e82e951..451a444669 100644 --- a/tests/boundary/browser/sourceMapCaptureExclusions.test.ts +++ b/tests/boundary/browser/sourceMapCaptureExclusions.test.ts @@ -4,105 +4,148 @@ import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalys import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js"; import { trackBrowser } from "./cdpBrowserProvider.support.js"; -it.each(["none", "excluded", "mixed", "redirect", "not-selected"] as const)( - "preserves captured source-map exclusion coverage: %s", - async (mode) => { - const source = "export const observed = true;"; - const browser = await startFakeCdpBrowser({ - sourceMapBody: JSON.stringify({ - version: 3, - names: [], - sources: ["main.js"], - sourcesContent: [source], - mappings: "AAAA", - }), - ...(mode === "redirect" - ? { sourceMapRedirect: "https://outside.example.test/main.js.map" } - : {}), - commandResult: ({ method }) => - method === "Debugger.getScriptSource" - ? { scriptSource: source } - : undefined, - commandEvents: (command, origin) => { - if (command.method !== "Debugger.enable") return undefined; - const script = (id: string, sourceMapURL?: string) => ({ - method: "Debugger.scriptParsed", - ...(command.sessionId === undefined - ? {} - : { sessionId: command.sessionId }), - params: { - scriptId: id, - url: `${origin}/${id}.js`, - hash: id, - length: Buffer.byteLength(source), - isModule: true, - executionContextId: 1, - scriptLanguage: "JavaScript", - ...(sourceMapURL === undefined ? {} : { sourceMapURL }), - }, - }); - return [ - script( - "main", - mode === "none" - ? undefined - : mode === "redirect" || mode === "not-selected" - ? "/app.js.map" - : "https://outside.example.test/main.js.map", - ), - ...(mode === "mixed" ? [script("second", "/app.js.map")] : []), - ]; - }, - }); - trackBrowser(browser); - const provider = new CdpBrowserProvider(); - try { +it("distinguishes unrequested maps, map exclusions, and usable map evidence", async () => { + const modes = [ + "not-selected", + "none", + "excluded", + "mixed", + "redirect", + ] as const; + let mode: (typeof modes)[number] = modes[0]; + const localMap = "/app.js.map?token=map-secret"; + const foreignMap = "https://outside.example.test/main.js.map"; + const browser = await startFakeCdpBrowser({ + eventsBeforeReply: ["Debugger.enable"], + sourceMapBody: JSON.stringify({ + version: 3, + names: [], + sources: ["src/main.ts"], + sourcesContent: ["export const original = true;"], + mappings: "AAAA", + }), + sourceMapReply: (url, response) => { + if (mode !== "redirect" || url !== localMap) return false; + response.writeHead(302, { location: foreignMap }).end(); + return true; + }, + commandEvents: (command, origin) => { + if (command.method !== "Debugger.enable") return undefined; + const script = (id: string, sourceMapURL?: string) => ({ + method: "Debugger.scriptParsed", + sessionId: command.sessionId, + params: { + scriptId: id, + url: `${origin}/${id}.js`, + hash: id, + isModule: true, + executionContextId: 1, + scriptLanguage: "JavaScript", + ...(sourceMapURL === undefined ? {} : { sourceMapURL }), + }, + }); + if (mode === "none") return [script("main")]; + if (mode === "mixed") + return [script("main", foreignMap), script("second", localMap)]; + return [script("main", mode === "excluded" ? foreignMap : localMap)]; + }, + }); + trackBrowser(browser); + const provider = new CdpBrowserProvider(); + const input = { + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + observation_ms: 0, + }; + const originalSource = `${browser.allowedOrigin}/src/main.ts`; + try { + for (const scenario of modes) { + mode = scenario; + const before = browser.httpRequests.length; const result = await provider.analyzeBundle( analyzeWebBundleInputSchema.parse({ - cdp_endpoint: browser.endpoint, - allowed_origins: [browser.allowedOrigin], - target_id: "allowed-page", - observation_ms: 0, - fetch_source_maps: mode !== "not-selected", + ...input, + ...(mode === "not-selected" ? {} : { fetch_source_maps: true }), }), ); if (!result.ok) throw result.error; const maps = result.value.observations.source_maps; if (mode === "none" || mode === "not-selected") { - expect(maps).toEqual({ + expect(maps, mode).toEqual({ status: "not_requested", requested: 0, processed: 0, items: [], }); - expect(result.value.completeness.status).toBe("complete"); - expect(result.value.unknowns).toEqual([]); + expect(result.value.completeness.status, mode).toBe("complete"); + expect(result.value.unknowns, mode).toEqual([]); } else { - expect(maps.status).toBe(mode === "mixed" ? "partial" : "unavailable"); - expect(result.value.completeness.status).toBe("partial"); - expect(result.value.unknowns).toContainEqual( + expect(result.value.completeness.status, mode).toBe("partial"); + expect(result.value.unknowns, mode).toContainEqual( expect.objectContaining({ dimension: "source_maps" }), ); if (mode === "redirect") { + expect(maps, mode).toMatchObject({ + status: "unavailable", + items: [{ status: "policy_filtered" }], + }); expect(maps.excluded).toBeUndefined(); - expect(maps.items[0]?.status).toBe("policy_filtered"); } else { - expect(maps.excluded).toEqual([ + expect(maps.excluded, mode).toEqual([ { section: "source_maps", reason: "disallowed_origin", count: 1 }, ]); - expect(maps.requested).toBe(mode === "mixed" ? 1 : 0); - expect(maps.processed).toBe(mode === "mixed" ? 1 : 0); - if (mode === "mixed") expect(maps.items[0]?.status).toBe("included"); + if (mode === "excluded") + expect(maps, mode).toMatchObject({ + status: "unavailable", + requested: 0, + processed: 0, + items: [], + }); + else + expect(maps, mode).toMatchObject({ + status: "partial", + requested: 1, + processed: 1, + items: [ + { + status: "included", + declared_url: `${browser.allowedOrigin}${localMap}`, + original_sources: [{ source: originalSource }], + mappings: [ + { + generated_line: 1, + generated_column: 0, + source: originalSource, + original_line: 1, + original_column: 0, + }, + ], + }, + ], + }); } } - expect(JSON.stringify(result.value)).not.toContain( + expect(JSON.stringify(result.value), mode).not.toContain( "outside.example.test", ); - expect( - browser.httpRequests.filter(({ url }) => url.startsWith("/app.js.map")), - ).toHaveLength(mode === "mixed" || mode === "redirect" ? 1 : 0); - } finally { - await provider.close(); + const mapRequests = browser.httpRequests + .slice(before) + .filter(({ url }) => url.startsWith("/app.js.map")); + expect(mapRequests, mode).toEqual( + mode === "mixed" || mode === "redirect" + ? [ + { + url: localMap, + authorization: undefined, + cookie: undefined, + referer: undefined, + }, + ] + : [], + ); } - }, -); + } finally { + await provider.close(); + } +}); diff --git a/tests/boundary/browser/webSourceMapHttpStatus.test.ts b/tests/boundary/browser/webSourceMapHttpStatus.test.ts index 69f9f7a0e8..adce3f5561 100644 --- a/tests/boundary/browser/webSourceMapHttpStatus.test.ts +++ b/tests/boundary/browser/webSourceMapHttpStatus.test.ts @@ -1,4 +1,3 @@ -import { createHash } from "node:crypto"; import { once } from "node:events"; import { createServer } from "node:http"; @@ -9,169 +8,101 @@ import { fetchWebSourceMapsOk } from "../../fixtures/fetchWebSourceMapsOk.js"; import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js"; import { WEB_SOURCE_MAP_LIMITS } from "../../../src/domain/sourceMapLimits.js"; -it.each([ - { status: 300, followsRedirect: false }, - { status: 304, followsRedirect: false }, - { status: 305, followsRedirect: false }, - { status: 306, followsRedirect: false }, - { status: 301, followsRedirect: true }, - { status: 302, followsRedirect: true }, - { status: 303, followsRedirect: true }, - { status: 307, followsRedirect: true }, - { status: 308, followsRedirect: true }, -])( - "follows HTTP $status with Location only when it is a redirect status", - async ({ status, followsRedirect }) => { - const calls: string[] = []; - const server = createServer((request, response) => { - const path = request.url ?? ""; - calls.push(path); - if (path === "/delivered.map") { - response.writeHead(200, { "content-type": "application/json" }).end( - JSON.stringify({ - version: 3, - names: [], - sources: ["original.js"], - mappings: "AAAA", - }), - ); - return; - } - response.writeHead(status, { location: "/delivered.map" }).end(); - }); - try { - const listening = once(server, "listening"); - server.listen(0, "127.0.0.1"); - await listening; - const address = server.address(); - if (address === null || typeof address === "string") - throw new TypeError("Expected TCP listener"); - const origin = `http://127.0.0.1:${String(address.port)}`; - const input = analyzeWebBundleInputSchema.parse({ - cdp_endpoint: "http://127.0.0.1:9222", - target_id: "selected-page", - allowed_origins: [origin], - fetch_source_maps: true, - }); - const url = `${origin}/${String(status)}`; - const result = await fetchWebSourceMapsOk( - [ - { - scriptKey: `scr_${"1".repeat(64)}`, - declaredUrl: url, - fetchUrl: url, - }, - ], - input, +it("follows only redirect statuses and releases discarded HTTP response bodies", async () => { + const redirects = [301, 302, 303, 307, 308]; + const otherStatuses = [300, 304, 305, 306, 400]; + const statuses = [...otherStatuses, ...redirects]; + const calls: string[] = []; + const closedBodies: string[] = []; + const server = createServer((request, response) => { + const path = request.url ?? ""; + calls.push(path); + if (path.startsWith("/delivered/")) { + response.writeHead(200, { "content-type": "application/json" }).end( + JSON.stringify({ + version: 3, + names: [], + sources: ["original.js"], + mappings: "AAAA", + }), ); - - expect(calls).toEqual( - followsRedirect - ? [`/${String(status)}`, "/delivered.map"] - : [`/${String(status)}`], - ); - expect(result.items[0]?.status).toBe( - followsRedirect ? "included" : "fetch_failed", - ); - expect(result.items[0]).toMatchObject({ - declared_url: url, - effective_url: followsRedirect ? `${origin}/delivered.map` : url, - }); - if (!followsRedirect) - expect(result.items[0]?.limitation).toBe( - `Source-map server returned HTTP ${String(status)}.`, - ); - } finally { - server.closeAllConnections(); - if (server.listening) - await new Promise((resolve, reject) => - server.close((error) => - error === undefined ? resolve() : reject(error), - ), - ); + return; } - }, -); - -it("keeps the declared URL and reports malformed redirect Location evidence", async () => { - const declaredUrl = "https://app.example.test/declared.map"; - const input = analyzeWebBundleInputSchema.parse({ - cdp_endpoint: "http://127.0.0.1:9222", - target_id: "selected-page", - allowed_origins: ["https://app.example.test"], - fetch_source_maps: true, + const status = Number(path.slice(1)); + if (!statuses.includes(status)) { + response.writeHead(404).end(); + return; + } + response.writeHead(status, { location: `/delivered/${status}.map` }); + if (status === 302 || status === 400) { + response.once("close", () => closedBodies.push(path)); + response.write("unfinished response body"); + } else response.end(); }); - const result = await fetchWebSourceMapsOk( - [ - { - scriptKey: `scr_${"1".repeat(64)}`, - declaredUrl, - fetchUrl: declaredUrl, - }, - ], - input, - undefined, - { - fetch: () => - Promise.resolve( - new Response(null, { - status: 302, - headers: { location: "http://[invalid" }, - }), + try { + const listening = once(server, "listening"); + server.listen(0, "127.0.0.1"); + await listening; + const address = server.address(); + if (address === null || typeof address === "string") + throw new TypeError("Expected TCP listener"); + const origin = `http://127.0.0.1:${address.port}`; + const input = analyzeWebBundleInputSchema.parse({ + cdp_endpoint: "http://127.0.0.1:9222", + target_id: "selected-page", + allowed_origins: [origin], + fetch_source_maps: true, + }); + const requests = statuses.map((status) => { + const url = `${origin}/${status}`; + return { scriptKey: `script-${status}`, declaredUrl: url, fetchUrl: url }; + }); + const result = await fetchWebSourceMapsOk(requests, input); + + expect(result).toMatchObject({ + status: "partial", + requested: statuses.length, + processed: statuses.length, + }); + expect(calls.sort()).toEqual( + [ + ...statuses.map((status) => `/${status}`), + ...redirects.map((status) => `/delivered/${status}.map`), + ].sort(), + ); + const byScript = new Map( + result.items.map((item) => [item.script_key, item]), + ); + for (const status of redirects) + expect(byScript.get(`script-${status}`), `HTTP ${status}`).toMatchObject({ + status: "included", + declared_url: `${origin}/${status}`, + effective_url: `${origin}/delivered/${status}.map`, + }); + for (const status of otherStatuses) + expect(byScript.get(`script-${status}`), `HTTP ${status}`).toMatchObject({ + status: "fetch_failed", + declared_url: `${origin}/${status}`, + effective_url: `${origin}/${status}`, + limitation: `Source-map server returned HTTP ${status}.`, + }); + await expect.poll(() => closedBodies.toSorted()).toEqual(["/302", "/400"]); + } finally { + server.closeAllConnections(); + if (server.listening) + await new Promise((resolve, reject) => + server.close((error) => + error === undefined ? resolve() : reject(error), ), - }, - ); - - expect(result.items[0]).toMatchObject({ - status: "fetch_failed", - declared_url: declaredUrl, - effective_url: declaredUrl, - limitation: expect.stringContaining("invalid Location URL"), - }); - expect(result.items[0]?.limitation).toContain(declaredUrl); - expect(result.items[0]?.limitation).toContain("http://[invalid"); + ); + } }); -const mapStart = Buffer.from('{"version":3,"names":[],"sources":["source'); -const mapEnd = Buffer.from('.ts"],"mappings":"AAAA"}'); -const mapWithSourceBytes = (bytes: Uint8Array) => - Buffer.concat([mapStart, bytes, mapEnd]); - -it.each([ - { - name: "invalid JSON", - body: Buffer.from("not-json"), - status: "invalid", - diagnostic: "Source-map JSON is not a version 3 map", - }, - { - name: "invalid UTF-8", - body: mapWithSourceBytes(Uint8Array.of(0xff)), - status: "invalid", - diagnostic: "Source-map response is not valid UTF-8", - }, - { - name: "a valid UTF-8 replacement character", - body: mapWithSourceBytes(Buffer.from("�")), - status: "included", - }, - { - name: "HTTP failure", - body: Buffer.from("service unavailable"), - status: "fetch_failed", - responseStatus: 503, - diagnostic: "Source-map server returned HTTP 503", - }, - { - name: "response limit", - body: mapWithSourceBytes(Buffer.from("name")), - status: "fetch_failed", - fetchBytes: 1, - }, -])("retains a redirected response URL for $name", async (scenario) => { +it("keeps the redirected URL when the body exceeds the fetch limit", async () => { const origin = "https://app.example.test"; const declaredUrl = `${origin}/declared.map`; const fetchedUrl = `${origin}/maps/current.map?token=source-value#fragment`; + const calls: string[] = []; const result = await fetchWebSourceMapsOk( [{ scriptKey: "script", declaredUrl, fetchUrl: declaredUrl }], analyzeWebBundleInputSchema.parse({ @@ -179,126 +110,100 @@ it.each([ target_id: "page", allowed_origins: [origin], fetch_source_maps: true, - ...(scenario.fetchBytes === undefined - ? {} - : { source_map_limits: { fetch_bytes: scenario.fetchBytes } }), + source_map_limits: { fetch_bytes: 1 }, }), undefined, { - fetch: async (url) => - String(url) === declaredUrl + fetch: async (url) => { + calls.push(String(url)); + return String(url) === declaredUrl ? new Response(null, { status: 302, headers: { location: fetchedUrl }, }) - : new Response(scenario.body, { - status: scenario.responseStatus ?? 200, - }), + : new Response('{"version":3,"sources":[],"names":[],"mappings":""}'); + }, }, ); - const item = result.items[0]; - if (item === undefined) throw new Error("Expected a fetched map result"); - expect(item).toMatchObject({ - declared_url: declaredUrl, - effective_url: fetchedUrl, - status: scenario.status, - artifact: - scenario.status === "included" - ? expect.objectContaining({ text: scenario.body.toString("utf8") }) - : null, - }); - if (scenario.status === "invalid") { - expect(item).toMatchObject({ - response_identity: { - bytes: scenario.body.byteLength, - sha256: createHash("sha256").update(scenario.body).digest("hex"), - representation: "fetch-response-body-bytes", + + expect(calls).toEqual([declaredUrl, fetchedUrl]); + expect(result).toMatchObject({ + requested: 1, + processed: 1, + items: [ + { + declared_url: declaredUrl, + effective_url: fetchedUrl, + status: "fetch_failed", + artifact: null, + limitation: expect.stringContaining( + "source_map_limits.fetch_bytes is 1", + ), }, - }); - expect(item.limitation).toContain("Response text was not retained"); - } - if (scenario.diagnostic !== undefined) - expect(item.limitation).toContain(scenario.diagnostic); + ], + }); + expect(result.items[0]).not.toHaveProperty("response_identity"); }); -it.each([ - { cancellationFails: false, reason: null, abortDuringCleanup: false }, - { - cancellationFails: true, - reason: new Error("fixture cancel failure"), - abortDuringCleanup: false, - }, - { cancellationFails: true, reason: undefined, abortDuringCleanup: false }, - { - cancellationFails: true, - reason: new Error("cleanup failed before caller abort"), - abortDuringCleanup: true, - }, -])( - "reserves a fetched URL before reading an over-cap response; cancellation failure=$cancellationFails, caller abort=$abortDuringCleanup", - async ({ cancellationFails, reason, abortDuringCleanup }) => { - const origin = "https://app.example.test"; - const fetchedUrl = `${origin}/${"u".repeat(WEB_SOURCE_MAP_LIMITS.outputBytes)}`; - let cancelled = false; - let read = false; +it("reserves output for the fetched URL before reading and preserves failed cleanup", async () => { + const origin = "https://app.example.test"; + const fetchedUrl = `${origin}/${"u".repeat(WEB_SOURCE_MAP_LIMITS.outputBytes)}`; + const requests = [ + { + scriptKey: "script", + declaredUrl: `${origin}/declared.map`, + fetchUrl: fetchedUrl, + }, + ]; + const input = analyzeWebBundleInputSchema.parse({ + cdp_endpoint: "http://127.0.0.1:9222", + target_id: "page", + allowed_origins: [origin], + fetch_source_maps: true, + }); + const callerReason = { requested: "stop during response cleanup" }; + const scenarios = [ + { label: "closed" }, + { label: "close rejection", failure: new Error("fixture cancel failure") }, + { label: "undefined close rejection", failure: undefined }, + { + label: "caller abort during failed close", + failure: new Error("cleanup failed during caller abort"), + abortDuringCleanup: true, + }, + ] as const; + + for (const scenario of scenarios) { + let cancelled = 0; + let pulls = 0; const controller = new AbortController(); const body = new ReadableStream( { - pull(controller) { - read = true; - controller.enqueue( - new TextEncoder().encode( - '{"version":3,"sources":[],"names":[],"mappings":""}', - ), - ); - controller.close(); + pull(stream) { + pulls += 1; + stream.enqueue(new Uint8Array([123, 125])); + stream.close(); }, cancel() { - cancelled = true; - if (abortDuringCleanup) - controller.abort({ requested: "stop during response cleanup" }); - if (cancellationFails) throw reason; + cancelled += 1; + if ("abortDuringCleanup" in scenario) controller.abort(callerReason); + if ("failure" in scenario) throw scenario.failure; }, }, { highWaterMark: 0 }, ); - const pending = fetchWebSourceMaps( - [ - { - scriptKey: "script", - declaredUrl: `${origin}/declared.map`, - fetchUrl: fetchedUrl, - }, - ], - analyzeWebBundleInputSchema.parse({ - cdp_endpoint: "http://127.0.0.1:9222", - target_id: "page", - allowed_origins: [origin], - fetch_source_maps: true, - }), + const result = await fetchWebSourceMaps( + requests, + input, controller.signal, - { fetch: () => Promise.resolve(new Response(body)) }, + { + fetch: async () => new Response(body), + }, ); - if (cancellationFails) { - const result = await pending; - expect(result.ok).toBe(false); - if (result.ok) throw new Error("Expected output admission failure"); - expect(result.error._tag).toBe("AnalysisResourceConstraintError"); - expect(result.error.message).toContain("Expanded source-map evidence"); - expect(result.error.cleanupIncomplete).toBe(true); - expect(result.error.cleanupResources.length).toBe(1); - expect(result.error.cleanupResources[0] === fetchedUrl).toBe(true); - await expect - .poll(() => - result.error.secondaryFailures.some((failure) => - failure.secondaryFailures.some( - (secondary) => secondary.cause === reason, - ), - ), - ) - .toBe(true); - } else { - await expect(pending).resolves.toMatchObject({ + expect(cancelled, scenario.label).toBe(1); + expect(pulls, scenario.label).toBe(0); + if (!("failure" in scenario)) { + expect(result, scenario.label).toMatchObject({ ok: true, value: { status: "unavailable", @@ -308,11 +213,39 @@ it.each([ limitation: expect.stringContaining("1 requested map"), }, }); + continue; } - expect(cancelled).toBe(true); - expect(read).toBe(false); - }, -); + + if (result.ok) + throw new Error(`Expected output admission failure: ${scenario.label}`); + const { error } = result; + expect(error, scenario.label).toMatchObject({ + _tag: "AnalysisResourceConstraintError", + message: expect.stringContaining("Expanded source-map evidence"), + cleanupIncomplete: true, + }); + expect(error.cleanupResources, scenario.label).toHaveLength(1); + expect(error.cleanupResources[0] === fetchedUrl, scenario.label).toBe(true); + const secondaryFailures = () => + error.secondaryFailures.flatMap((failure) => failure.secondaryFailures); + await expect + .poll(() => + secondaryFailures().some( + (failure) => + failure._tag === "ProviderAdapterError" && + failure.cause === scenario.failure, + ), + ) + .toBe(true); + if ("abortDuringCleanup" in scenario) + expect(secondaryFailures()).toContainEqual( + expect.objectContaining({ + _tag: "AnalysisCancelledError", + cause: callerReason, + }), + ); + } +}); it("fits complete malformed redirect diagnostics within the output budget", async () => { const origin = "https://app.example.test"; @@ -350,11 +283,16 @@ it("fits complete malformed redirect diagnostics within the output budget", asyn items: requests.slice(0, 2).map(({ scriptKey, declaredUrl }) => ({ script_key: scriptKey, declared_url: declaredUrl, + effective_url: declaredUrl, status: "fetch_failed", limitation: expect.stringContaining(JSON.stringify(location)), })), limitation: expect.stringContaining("2 requested maps"), }); + for (const item of result.items) { + expect(item.limitation).toContain("invalid Location URL"); + expect(item.limitation).toContain(item.declared_url); + } expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(budget); expect(cancelled).toEqual( requests.slice(0, 3).map(({ fetchUrl }) => fetchUrl), diff --git a/tests/boundary/browser/webSourceMapResponseCleanup.test.ts b/tests/boundary/browser/webSourceMapResponseCleanup.test.ts deleted file mode 100644 index a089d05ab8..0000000000 --- a/tests/boundary/browser/webSourceMapResponseCleanup.test.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { createServer } from "node:http"; -import { describe, expect, it, onTestFinished } from "vitest"; -import { fetchWebSourceMapsOk } from "../../fixtures/fetchWebSourceMapsOk.js"; -import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js"; - -describe("source-map discarded HTTP response ownership", () => { - it.each([302, 400])( - "closes an unfinished discarded HTTP %s body", - async (status) => { - let didClose = false; - let notifyClosed: (() => void) | undefined; - const closed = new Promise((resolve) => { - notifyClosed = resolve; - }); - const server = createServer((incoming, response) => { - if (incoming.url === "/final.map") { - response.end( - JSON.stringify({ - version: 3, - names: [], - sources: [], - mappings: "", - }), - ); - return; - } - response.writeHead( - status, - status === 302 ? { location: "/final.map" } : {}, - ); - response.write("unfinished response body"); - response.on("close", () => { - didClose = true; - notifyClosed?.(); - }); - }); - const closeServer = async (): Promise => { - server.closeAllConnections(); - if (server.listening) - await new Promise((resolve) => server.close(() => resolve())); - }; - onTestFinished(closeServer); - try { - await new Promise((resolve) => - server.listen(0, "127.0.0.1", resolve), - ); - const address = server.address(); - if (address === null || typeof address === "string") - throw new Error("Expected TCP listener"); - const origin = `http://127.0.0.1:${String(address.port)}`; - const url = `${origin}/first.map`; - const input = analyzeWebBundleInputSchema.parse({ - cdp_endpoint: "http://127.0.0.1:9222", - allowed_origins: [origin], - target_id: "page-1", - fetch_source_maps: true, - }); - const result = await fetchWebSourceMapsOk( - [ - { - scriptKey: `scr_${"1".repeat(64)}`, - declaredUrl: url, - fetchUrl: url, - }, - ], - input, - ); - expect(result.items[0]?.status).toBe( - status === 302 ? "included" : "fetch_failed", - ); - await closed; - expect(didClose).toBe(true); - } finally { - await closeServer(); - } - }, - ); -}); diff --git a/tests/boundary/browser/webSourceMapResponseIdentity.test.ts b/tests/boundary/browser/webSourceMapResponseIdentity.test.ts index 299b147098..99fa00fcae 100644 --- a/tests/boundary/browser/webSourceMapResponseIdentity.test.ts +++ b/tests/boundary/browser/webSourceMapResponseIdentity.test.ts @@ -2,6 +2,7 @@ import { createHash } from "node:crypto"; import { expect, it, vi } from "vitest"; import { fetchWebSourceMaps } from "../../../src/browser/WebSourceMapFetcher.js"; +import { fetchWebSourceMapsOk } from "../../fixtures/fetchWebSourceMapsOk.js"; import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js"; const origin = "https://maps.example.test"; @@ -19,46 +20,120 @@ const input = analyzeWebBundleInputSchema.parse({ const digest = (bytes: Uint8Array | string): string => createHash("sha256").update(bytes).digest("hex"); -it.each([ - { label: "an absent body", body: null }, - { - label: "mapping indices", - body: JSON.stringify({ - version: 3, - sources: ["a.js"], - names: [], - mappings: "ACAA", - }), - }, -])( - "retains completed body identity when $label is invalid", - async ({ body }) => { - const result = await fetchWebSourceMaps([request(1)], input, undefined, { - fetch: () => Promise.resolve(new Response(body, { status: 200 })), +it("preserves redirected response identity across decoding failures and valid UTF-8", async () => { + const mapWithSourceBytes = (source: Uint8Array) => + Buffer.concat([ + Buffer.from('{"version":3,"names":[],"sources":["source'), + source, + Buffer.from('.ts"],"mappings":"AAAA"}'), + ]); + const invalid = [ + { name: "absent", body: null }, + { + name: "mapping-index", + body: JSON.stringify({ + version: 3, + sources: ["a.js"], + names: [], + mappings: "ACAA", + }), + }, + { + name: "invalid-json", + body: Buffer.from("not-json"), + diagnostic: "Source-map JSON is not a version 3 map", + }, + { + name: "invalid-utf8", + body: mapWithSourceBytes(Uint8Array.of(0xff)), + diagnostic: "Source-map response is not valid UTF-8", + }, + ] as const; + const valid = { + name: "replacement-character", + body: mapWithSourceBytes(Buffer.from("�")), + }; + const unavailable = { name: "http-failure", body: "service unavailable" }; + const fixtures = [...invalid, valid, unavailable].map((scenario, index) => ({ + ...scenario, + ...request(index + 1), + effectiveUrl: `${origin}/maps/${scenario.name}.map?token=source-value#fragment`, + })); + const redirects = new Map( + fixtures.map((scenario) => [scenario.fetchUrl, scenario.effectiveUrl]), + ); + const replies = new Map( + fixtures.map((scenario) => [scenario.effectiveUrl, scenario]), + ); + const calls: string[] = []; + const result = await fetchWebSourceMapsOk(fixtures, input, undefined, { + fetch: async (url) => { + const received = String(url); + calls.push(received); + const location = redirects.get(received); + if (location !== undefined) + return new Response(null, { status: 302, headers: { location } }); + const scenario = replies.get(received); + if (scenario === undefined) + throw new Error(`Unexpected request: ${received}`); + return new Response(scenario.body, { + status: scenario.name === unavailable.name ? 503 : 200, + }); + }, + }); + + expect(result).toMatchObject({ + status: "partial", + requested: fixtures.length, + processed: fixtures.length, + }); + expect(calls.sort()).toEqual( + fixtures + .flatMap(({ fetchUrl, effectiveUrl }) => [fetchUrl, effectiveUrl]) + .sort(), + ); + const byScript = new Map(result.items.map((item) => [item.script_key, item])); + for (const fixture of fixtures) { + const item = byScript.get(fixture.scriptKey); + if (item === undefined) + throw new Error(`Missing result for ${fixture.name}`); + expect(item, fixture.name).toMatchObject({ + declared_url: fixture.declaredUrl, + effective_url: fixture.effectiveUrl, }); - if (!result.ok) throw result.error; - expect(result.value).toMatchObject({ - status: "unavailable", - requested: 1, - processed: 1, - items: [ - { - status: "invalid", - artifact: null, - response_identity: { - sha256: digest(body ?? ""), - bytes: Buffer.byteLength(body ?? ""), - representation: "fetch-response-body-bytes", - }, - original_sources: [], - original_module_edges: [], - mappings: [], - limitation: expect.stringContaining("Response text was not retained"), + if (fixture.name === valid.name) { + expect(item).toMatchObject({ + status: "included", + artifact: { text: valid.body.toString("utf8") }, + }); + } else if (fixture.name === unavailable.name) { + expect(item).toMatchObject({ + status: "fetch_failed", + artifact: null, + limitation: expect.stringContaining( + "Source-map server returned HTTP 503", + ), + }); + expect(item).not.toHaveProperty("response_identity"); + } else { + expect(item, fixture.name).toMatchObject({ + status: "invalid", + artifact: null, + response_identity: { + sha256: digest(fixture.body ?? ""), + bytes: Buffer.byteLength(fixture.body ?? ""), + representation: "fetch-response-body-bytes", }, - ], - }); - }, -); + original_sources: [], + original_module_edges: [], + mappings: [], + limitation: expect.stringContaining("Response text was not retained"), + }); + if ("diagnostic" in fixture) + expect(item.limitation, fixture.name).toContain(fixture.diagnostic); + } + } +}); it("retains completed body identity when the deadline lands as the response stream ends", async () => { const body = JSON.stringify({ diff --git a/tests/fixtures/fakeCdpBrowser.ts b/tests/fixtures/fakeCdpBrowser.ts index 5265ba1d40..31d076446e 100644 --- a/tests/fixtures/fakeCdpBrowser.ts +++ b/tests/fixtures/fakeCdpBrowser.ts @@ -96,6 +96,7 @@ export const startFakeCdpBrowser = async ( directPage, options, sessionState, + port, ) ) return; @@ -264,9 +265,12 @@ const handleSimulatedWireFault = ( directPage: boolean, options: FakeOptions, sessionState: SocketSessionState, + port: number, ): boolean => { const commandError = options.commandError?.(command); if (commandError !== undefined) { + if (options.eventsBeforeReply?.includes(command.method) === true) + sendCommandEvents(socket, command, port, options); sendCommandReply(socket, command, { error: commandError }); if (options.closeAfterMethod === command.method) setImmediate(() => socket.close()); @@ -375,15 +379,24 @@ const replyToCommand = ( const eventsFirst = options.eventsBeforeReply?.includes(command.method) === true; if (!eventsFirst) reply(); + sendCommandEvents(socket, command, port, options); + if (eventsFirst) reply(); + if (options.closeAfterMethod === command.method) + setImmediate(() => socket.close()); +}; + +const sendCommandEvents = ( + socket: WebSocket, + command: FakeCdpCommand, + port: number, + options: FakeOptions, +): void => { const customEvents = options.commandEvents?.( command, `http://127.0.0.1:${String(port)}`, ); if (customEvents === undefined) emitEvents(socket, command, port, options); else for (const event of customEvents) socket.send(JSON.stringify(event)); - if (eventsFirst) reply(); - if (options.closeAfterMethod === command.method) - setImmediate(() => socket.close()); }; const serveSourceMap = ( diff --git a/tests/fixtures/fakeCdpBrowserEvents.ts b/tests/fixtures/fakeCdpBrowserEvents.ts index b5d943f89b..a5348ffd90 100644 --- a/tests/fixtures/fakeCdpBrowserEvents.ts +++ b/tests/fixtures/fakeCdpBrowserEvents.ts @@ -709,17 +709,6 @@ const emitCaptureNavigation = ( port: number, options: FakeOptions, ): void => { - if ( - command.method === "Page.captureScreenshot" && - options.screenshotDocumentLoader !== undefined - ) - event(socket, "Page.frameNavigated", command.sessionId, { - frame: { - id: "frame-main", - url: `http://127.0.0.1:${String(port)}/app?token=frame-secret`, - loaderId: options.screenshotDocumentLoader, - }, - }); const navigationUrl = command.method === "DOMSnapshot.captureSnapshot" ? options.navigateDuringCaptureUrl diff --git a/tests/fixtures/fakeCdpBrowserTypes.ts b/tests/fixtures/fakeCdpBrowserTypes.ts index 42390df482..8eeb6bd52c 100644 --- a/tests/fixtures/fakeCdpBrowserTypes.ts +++ b/tests/fixtures/fakeCdpBrowserTypes.ts @@ -83,7 +83,6 @@ export interface FakeOptions { readonly navigateDuringObservationUrl?: string; readonly navigateDuringCaptureUrl?: string; readonly navigateDuringScreenshotUrl?: string; - readonly screenshotDocumentLoader?: string; readonly extraCollections?: boolean; readonly indexedDbDateKeys?: boolean; readonly foreignSessionEvents?: boolean;