test(browser): consolidate CDP workflows and colocate parser coverage

This commit is contained in:
morluto
2026-10-08 04:03:24 +08:00
parent 1fd1989d34
commit 26829045d0
18 changed files with 104 additions and 397 deletions
@@ -115,29 +115,6 @@ describe("DOM metadata document base URLs", () => {
});
describe("empty form destinations with a document base URL", () => {
it.each([
{ nodeName: "FORM", attribute: "action" },
{ nodeName: "BUTTON", attribute: "formaction" },
{ nodeName: "INPUT", attribute: "formaction" },
])(
"resolves an empty $nodeName $attribute to the document",
({ nodeName, attribute }) => {
const pageUrl = `${documentUrl}?selected=fixture#section`;
const baseUrl = `${origin}/assets/`;
const value = snapshot(baseUrl, "/assets/", pageUrl, "");
value.strings[6] = nodeName;
value.strings[14] = attribute;
value.strings[15] = "";
const result = capture(value);
expect(result.urls[3]).toMatchObject({
attribute,
url: pageUrl,
destination_scope: "approved",
});
expect(result.urls[1]?.url).toBe(baseUrl);
},
);
it("keeps an empty form action approved when the document base is foreign", () => {
const foreign = "https://cdn.example.test/assets/";
const value = snapshot(foreign, foreign);
@@ -0,0 +1,66 @@
import { expect, it } from "vitest";
import { CdpCaptureEvents } from "./CdpCaptureEvents.js";
import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
it("preserves an already captured response shape without marking it unavailable", () => {
const origin = "http://127.0.0.1:43127";
const events = new CdpCaptureEvents(
inspectWebPageInputSchema.parse({
cdp_endpoint: origin,
allowed_origins: [origin],
target_id: "allowed-page",
include_json_body_shapes: true,
}),
new Set([origin]),
);
events.ingest({
method: "Network.requestWillBeSent",
params: {
requestId: "request-1",
request: {
url: `${origin}/prior`,
method: "POST",
headers: { "Content-Type": "application/json" },
postData: '{"request":true}',
},
timestamp: 1,
},
});
events.ingest({
method: "Network.responseReceived",
params: {
requestId: "request-1",
response: {
url: `${origin}/prior`,
status: 200,
mimeType: "application/json",
},
},
});
events.ingestResponseBody("request-1", {
body: '{"response":true}',
base64Encoded: false,
});
const capturedShape = events.network.get("request-1")?.body_shapes.response;
expect(capturedShape).not.toBeNull();
events.ingest({
method: "Network.requestWillBeSent",
params: {
requestId: "request-1",
request: { url: `${origin}/continuation`, method: "GET" },
redirectResponse: null,
timestamp: 2,
},
});
expect(events.network.get("request-1")?.body_shapes.response).toEqual(
capturedShape,
);
expect(events.network.get("request-1")?.body_shapes.status).toBe("included");
expect(events.responseBodyRequestIds()).toEqual([]);
expect(events.completeness.snapshot().unavailable_sections).not.toContain(
"json_body_shapes",
);
});
+292
View File
@@ -0,0 +1,292 @@
import { describe, expect, it } from "vitest";
import { safeResponseMetadata } from "./CdpSafeMetadata.js";
const origin = "https://app.example.test";
describe("safe CDP response metadata", () => {
it("drops credential headers and preserves complete local link URLs", () => {
const captured = safeResponseMetadata(
"request-1",
`${origin}/api`,
{
mimeType: "application/json",
headers: {
Authorization: "Bearer credential-secret",
Cookie: "session=cookie-secret",
"Set-Cookie": "session=response-secret",
Link: `</agent?token=link-secret>; rel="mcp service-desc"; title="a,b", <https://private.example.test/x>; rel="mcp"`,
"Content-Security-Policy":
"default-src 'self'; script-src 'nonce-nonce-secret' 'sha256-hash-secret' https://private.example.test",
"Permissions-Policy":
"camera=(), geolocation=(self), invalid secret=()",
"X-Model-Context": "agent-header-secret",
},
},
new Set([origin]),
);
expect(captured.response).toMatchObject({
url: `${origin}/api`,
csp: {
nonce_count: 1,
hash_count: 1,
directives: expect.arrayContaining([
expect.objectContaining({
name: "script-src",
sources: expect.arrayContaining([
{ kind: "external_origin", value: null },
]),
}),
]),
},
links: [
{
href: `${origin}/agent?token=link-secret`,
destination_scope: "approved",
rel: ["mcp", "service-desc"],
as: null,
type: null,
crossorigin: null,
},
expect.objectContaining({
href: "https://private.example.test/x",
destination_scope: "outside_policy",
}),
],
policies: { permissions_policy_features: ["camera", "geolocation"] },
});
expect(captured.agentHints).toEqual(
expect.arrayContaining([
expect.objectContaining({ mechanism: "link_rel" }),
expect.objectContaining({
mechanism: "response_header",
declaration: "x-model-context",
}),
]),
);
const serialized = JSON.stringify(captured);
for (const secret of [
"credential-secret",
"cookie-secret",
"response-secret",
"nonce-secret",
"hash-secret",
"agent-header-secret",
])
expect(serialized).not.toContain(secret);
expect(serialized).toContain("link-secret");
expect(serialized).toContain("private.example.test/x");
});
it("reports well-known agent resources as untrusted observations", () => {
const captured = safeResponseMetadata(
"request-2",
`${origin}/.well-known/mcp?token=secret`,
{ headers: {} },
new Set([origin]),
);
expect(captured.agentHints).toEqual([
{
mechanism: "well_known_resource",
declaration: "/.well-known/mcp",
url: `${origin}/.well-known/mcp?token=secret`,
trust: "page-declared-untrusted",
},
]);
});
it("retains every parsed header, CSP directive, link, relation, and policy feature", () => {
const csp = Array.from(
{ length: 105 },
(_, index) => `x-${String(index)} 'self'`,
).join("; ");
const links = Array.from(
{ length: 105 },
(_, index) => `</${String(index)}>; rel="mcp service-desc"`,
).join(", ");
const permissions = Array.from(
{ length: 205 },
(_, index) => `feature-${String(index)}=()`,
).join(", ");
const headers = Object.fromEntries(
Array.from({ length: 501 }, (_, index) => [
`x-extra-${String(index)}`,
"ok",
]),
);
const captured = safeResponseMetadata(
"request-1",
`${origin}/api`,
{
mimeType: "application/json",
headers: {
...headers,
"Content-Security-Policy": csp,
Link: links,
"Permissions-Policy": permissions,
"X-Model-Context": "present-after-many-headers",
},
},
new Set([origin]),
);
expect(captured.response.csp.directives).toHaveLength(105);
expect(captured.response.links).toHaveLength(105);
expect(captured.response.policies.permissions_policy_features).toHaveLength(
205,
);
expect(captured.agentHints).toContainEqual(
expect.objectContaining({ declaration: "x-model-context" }),
);
});
});
describe("safe CDP response metadata CSP sources", () => {
it("classifies scheme-less CSP host sources by their own host, not the page origin", () => {
const captured = safeResponseMetadata(
"request-csp",
`${origin}/api`,
{
headers: {
"Content-Security-Policy":
"script-src cdn.example.test *.trusted.test * 'self' https://api.other.test",
},
},
new Set([origin]),
);
expect(captured.response.csp.directives).toEqual([
{
name: "script-src",
sources: [
{ kind: "external_origin", value: null },
{ kind: "external_origin", value: null },
{ kind: "external_origin", value: null },
{ kind: "keyword", value: "'self'" },
{ kind: "external_origin", value: null },
],
},
]);
});
it("inherits the protected resource scheme for a scheme-less host source", () => {
const captured = safeResponseMetadata(
"request-csp-scheme",
`${origin}/api`,
{
headers: {
"Content-Security-Policy":
"script-src cdn.example.test app.example.test",
},
},
new Set([origin, "https://cdn.example.test"]),
);
expect(captured.response.csp.directives[0]?.sources).toEqual([
{ kind: "approved_origin", value: "https://cdn.example.test" },
{ kind: "approved_origin", value: origin },
]);
});
});
describe("Link header value delimiters", () => {
const captureLinks = (link: string) =>
safeResponseMetadata(
"owned",
"https://owned.test/page",
{
headers: { Link: link },
},
new Set(["https://owned.test"]),
).response.links;
it("preserves commas in URI references and separates later links", () => {
const links = captureLinks(
'</assets/red,blue.js>; rel="preload"; as=script, </ordinary.js>; rel="modulepreload"',
);
expect(links.map(({ href }) => href)).toEqual([
"https://owned.test/assets/red,blue.js",
"https://owned.test/ordinary.js",
]);
expect(links[0]?.as).toBe("script");
});
it("preserves semicolons and commas inside quoted parameter values", () => {
const links = captureLinks(
'</plugin.js>; rel="preload"; type="text/javascript; charset=utf-8"; title="a,b", </ordinary.js>; rel="modulepreload"',
);
expect(links).toHaveLength(2);
expect(links[0]?.type).toBe("text/javascript; charset=utf-8");
expect(links[1]?.href).toBe("https://owned.test/ordinary.js");
});
it("counts quoted-pair escapes rather than treating every preceding slash as an escape", () => {
const links = captureLinks(
String.raw`</one.js>; title="ends with \\"; rel="preload", </two.js>; rel="modulepreload"`,
);
expect(links.map(({ href }) => href)).toEqual([
"https://owned.test/one.js",
"https://owned.test/two.js",
]);
expect(links[0]?.rel).toEqual(["preload"]);
});
it("keeps a quoted escaped quote from exposing a delimiter", () => {
const links = captureLinks(
String.raw`</one.js>; title="quoted \" comma, value"; rel="preload", </two.js>; rel="modulepreload"`,
);
expect(links).toHaveLength(2);
expect(links[0]?.rel).toEqual(["preload"]);
});
it("retains the existing outside-policy classification", () => {
const links = captureLinks(
'<https://elsewhere.test/red,blue.js>; rel="preload", </ordinary.js>; rel="modulepreload"',
);
expect(links[0]?.destination_scope).toBe("outside_policy");
expect(links[1]?.destination_scope).toBe("approved");
});
});
describe("safe CDP response metadata referrer policy", () => {
it.each<[string | undefined, string | null]>([
[
"no-referrer, strict-origin-when-cross-origin",
"strict-origin-when-cross-origin",
],
["origin, future-policy", "origin"],
["future-policy, no-referrer", "no-referrer"],
["unsafe-url, NO-REFERRER", "no-referrer"],
["origin,, no-referrer", "no-referrer"],
["future-policy", null],
['unsafe-url, "no-referrer"', null],
["no-referrer; report-to=x, origin", null],
["", null],
[undefined, null],
...[
"no-referrer",
"no-referrer-when-downgrade",
"same-origin",
"origin",
"strict-origin",
"origin-when-cross-origin",
"strict-origin-when-cross-origin",
"unsafe-url",
].map((policy): [string, string] => [policy, policy]),
])("normalizes declared Referrer-Policy %s", (raw, expected) => {
const captured = safeResponseMetadata(
"request-referrer",
`${origin}/api`,
{
headers: {
"Referrer-Policy": raw,
"Cross-Origin-Opener-Policy": "same-origin",
},
},
new Set([origin]),
);
expect(captured.response.policies.referrer_policy).toBe(expected);
expect(captured.response.policies.coop).toBe("same-origin");
});
});
@@ -1,39 +0,0 @@
import { expect, it } from "vitest";
import type { Page } from "playwright-core";
import { BrowserScenarioSecrets } from "./BrowserScenarioSecrets.js";
import { performPlaywrightScenarioAction } from "./PlaywrightScenarioActions.js";
import { browserScenarioSchema } from "../domain/browserScenario.js";
it("uses exact accessible-name matching without a caller confirmation field", async () => {
const scenario = browserScenarioSchema.parse({
browser: { mode: "launch", executable_path: "/opt/chromium/chrome" },
start_url: { url: "https://app.example.test/" },
actions: [
{
step_id: "submit",
action: "click",
locator: { kind: "role", role: "button", name: "Submit" },
},
],
});
const calls: unknown[][] = [];
const page = {
getByRole: (...args: unknown[]) => {
calls.push(args);
return { click: async () => undefined };
},
} as unknown as Page;
const secrets = BrowserScenarioSecrets.resolve(scenario, {});
if (secrets === undefined) throw new Error("No secret declarations expected");
const action = scenario.actions.at(0);
if (action === undefined) throw new Error("Expected one scenario action");
await performPlaywrightScenarioAction({
page,
action,
secrets,
});
expect(calls).toEqual([["button", { name: "Submit", exact: true }]]);
});