test(browser): catch permissive decoding of malformed wire payloads (#12792)

test(browser): catch permissive response body decoding
This commit is contained in:
morluto authored and GitHub committed 2026-11-20 12:40:25 +00:00
1 parent c348fe7fe0
commit fc5ba5473b
4 files changed
+36 -67

No files matched your search

+4 -40
View File
@@ -93,17 +93,10 @@ const startJsonResponse = (
);
};
it("fingerprints large cached bodies and retains CDP padding-bit acceptance", async () => {
const body = Buffer.alloc(BODY_BYTES, 0x81);
const large = await captureCacheFingerprint(body.toString("base64"));
expect(large.complete).toBe(true);
expect(large.items[0]?.value_sha256).toBe(
createHash("sha256").update(body).digest("hex"),
);
it("fingerprints valid cached bytes with nonzero pad bits and rejects invalid bytes", async () => {
const padBits = await captureCacheFingerprint("e31=");
expect(padBits.items[0]?.value_sha256).toBe(
createHash("sha256").update(Buffer.from("e31=", "base64")).digest("hex"),
createHash("sha256").update("{}").digest("hex"),
);
const malformed = await captureCacheFingerprint("%%%not-base64%%%");
@@ -111,7 +104,7 @@ it("fingerprints large cached bodies and retains CDP padding-bit acceptance", as
expect(malformed.items[0]?.value_sha256).toBeNull();
});
it("decodes large JSON response bodies and preserves malformed and pad-bit behavior", () => {
it("decodes large JSON response bodies and accepts nonzero pad bits", () => {
const events = createEvents();
startJsonResponse(events, "large-json", "/large.json");
const encoded = Buffer.from(
@@ -140,20 +133,9 @@ it("decodes large JSON response bodies and preserves malformed and pad-bit behav
expect(events.network.get("pad-bits")?.body_shapes.response?.root_type).toBe(
"object",
);
startJsonResponse(events, "malformed", "/malformed.json");
const malformed = events
.responseBodyRequests()
.find(({ requestId }) => requestId === "malformed");
expect(malformed).toBeDefined();
events.ingestResponseBody(malformed!, {
body: "%%%not-base64%%%",
base64Encoded: true,
});
expect(events.network.get("malformed")?.body_shapes.response).toBeNull();
});
it("counts large binary WebSocket frames without accepting malformed base64", () => {
it("counts bytes from large binary WebSocket frames", () => {
const events = createEvents(false);
const encoded = Buffer.alloc(BODY_BYTES, 0x81).toString("base64");
events.ingest(
@@ -174,22 +156,4 @@ it("counts large binary WebSocket frames without accepting malformed base64", ()
expect(
events.webSocketConnectionsById.get("socket")?.events[0]?.payload_bytes,
).toBe(BODY_BYTES);
events.ingest(
receiveCdpEvent({
method: "Network.webSocketFrameReceived",
params: {
requestId: "socket",
response: { opcode: 2, payloadData: "%%%not-base64%%%" },
},
}),
);
expect(
events.webSocketConnectionsById.get("socket")?.events[1]?.payload_bytes,
).toBeNull();
expect(events.completeness.snapshot().excluded).toContainEqual({
section: "websocket_frames",
reason: "invalid_protocol_value",
count: 1,
});
});
@@ -386,7 +386,12 @@ describe("CdpBrowserProvider: WebSocket connection projection", () => {
describe("CdpBrowserProvider: malformed approved payloads", () => {
it("fails closed on malformed approved response and binary payload encodings", async () => {
const browser = await startFakeCdpBrowser({
invalidResponseBodyBase64: true,
eventsBeforeReply: ["Network.enable"],
commandResult: ({ method }) =>
method === "Network.getResponseBody"
? // Permissive base64 decoding skips the invalid characters and yields JSON.
{ body: "e%%3%0%=", base64Encoded: true }
: undefined,
binaryWebSocketEvent: true,
invalidBinaryWebSocketEvent: true,
});
@@ -396,14 +401,7 @@ describe("CdpBrowserProvider: malformed approved payloads", () => {
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
// Unlike the sibling cases, this scenario asserts outcomes produced
// *after* the observation window closes: the response-body fetch has to
// run and has to reject the malformed base64. A zero-length window
// races that phase, so the expected `partial` status and the
// `json_body_shapes` exclusion below depend on host speed. This window
// is long enough for the fetch to complete, which makes the assertions
// measure the malformed-body rejection instead of the machine's load.
observation_ms: 200,
observation_ms: 0,
include_json_body_shapes: true,
include_websocket_shapes: true,
}),
@@ -423,15 +421,27 @@ describe("CdpBrowserProvider: malformed approved payloads", () => {
payload_bytes: null,
payload_shape: { format: "binary", json_shape: null },
});
expect(result.value.completeness.unavailable_sections).toEqual(
expect.arrayContaining(["json_body_shapes", "websocket_frames"]),
expect(result.value.completeness.excluded).toEqual(
expect.arrayContaining([
{
section: "json_body_shapes",
reason: "invalid_protocol_value",
count: 1,
},
{
section: "websocket_frames",
reason: "invalid_protocol_value",
count: 1,
},
]),
);
});
it("records a malformed response body reply as a body-shape gap", async () => {
const browser = await startFakeCdpBrowser({
commandResult: (command) =>
command.method === "Network.getResponseBody"
eventsBeforeReply: ["Network.enable"],
commandResult: ({ method }) =>
method === "Network.getResponseBody"
? { body: 7, base64Encoded: false }
: undefined,
});
@@ -441,8 +451,7 @@ describe("CdpBrowserProvider: malformed approved payloads", () => {
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
// Long enough for the post-window body fetch, as in the case above.
observation_ms: 200,
observation_ms: 0,
include_json_body_shapes: true,
}),
);
+8 -11
View File
@@ -39,7 +39,7 @@ export const resultFor = (
case "Debugger.getScriptSource":
return scriptSourceResult();
case "Network.getResponseBody":
return responseBodyResult(options);
return responseBodyResult();
case "Page.captureScreenshot":
return screenshotResult();
case "Browser.getVersion":
@@ -140,16 +140,13 @@ const scriptSourceResult = () => ({
scriptSource: "export const observed = 'source-secret';",
});
const responseBodyResult = (options: FakeOptions) =>
options.invalidResponseBodyBase64 === true
? { body: "%%%not-base64%%%", base64Encoded: true }
: {
body: JSON.stringify({
result: { ok: true, token: "response-body-secret" },
items: [{ id: 1 }, { id: 2 }],
}),
base64Encoded: false,
};
const responseBodyResult = () => ({
body: JSON.stringify({
result: { ok: true, token: "response-body-secret" },
items: [{ id: 1 }, { id: 2 }],
}),
base64Encoded: false,
});
const screenshotResult = () => ({
data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==",
-1
View File
@@ -113,7 +113,6 @@ export interface FakeOptions {
/** Deliver a console argument the protocol spells as `{ type: "object", subtype: "null" }`. */
readonly nullConsoleArgument?: boolean;
readonly cachedResponseBody?: string;
readonly invalidResponseBodyBase64?: boolean;
readonly webMcpTools?: boolean;
/** Replaces the primary tool's registration stack; returning `undefined` omits it. */
readonly webMcpStackTrace?: (origin: string) => JsonValue | undefined;