test(browser): consolidate CDP and source map coverage

Use shared captures to verify discovery, document identity, screenshot recovery, and source map evidence and cleanup across varied responses. Remove redundant tests and unused fixture controls.
This commit is contained in:
morluto committed 2026-11-18 03:51:31 -05:00
1 parent 1592b7f2c0
commit caedefa00d
19 files changed
+1729 -2195

No files matched your search

+34 -41
View File
@@ -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");
});
+24 -188
View File
@@ -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<CdpConnection, "send"> => ({
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);
});
+15 -19
View File
@@ -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", () => {
+3 -23
View File
@@ -24,17 +24,13 @@ const script = (scriptId: string): CapturedScript => ({
frameId: null,
});
const capture = async (
sources: Readonly<Record<string, string>>,
includeScriptSources = true,
signal?: AbortSignal,
) => {
const capture = async (sources: Readonly<Record<string, string>>) => {
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,
});
});
+40 -37
View File
@@ -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();
});
@@ -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<string, unknown> }
| 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 () => {
@@ -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<string, unknown>();
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);
@@ -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<string, unknown> | 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"])(
@@ -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();
}
});
@@ -52,20 +52,6 @@ const laterInWindowRequest = (
},
});
const expectNoCommittedFrameWithInvalidTimeline = (
observation: Awaited<ReturnType<typeof observeWithFake>>["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 () => {
File diff suppressed because it is too large. Load diff
@@ -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",
},
});
});
});
@@ -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();
}
});
@@ -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<void>((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<void>((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<Uint8Array>(
{
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),
@@ -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<void>((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<void> => {
server.closeAllConnections();
if (server.listening)
await new Promise<void>((resolve) => server.close(() => resolve()));
};
onTestFinished(closeServer);
try {
await new Promise<void>((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();
}
},
);
});
@@ -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({
+16 -3
View File
@@ -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 = (
-11
View File
@@ -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
-1
View File
@@ -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;