perf(browser): bound retained network body verification memory (#12231)

perf(browser): bound memory for retained body verification
This commit is contained in:
ShuaixinHuang authored and GitHub committed 2026-11-19 06:42:53 -05:00
1 parent 89b2d398eb
commit 661dec7e6b
2 files changed
+64 -10

No files matched your search

+33
View File
@@ -32,3 +32,36 @@ it("validates the actual retained bytes against canonical base64, count, and dig
browserNetworkBodySchema.safeParse({ ...retained, ...mutation }).success,
).toBe(false);
});
it("authenticates the entire body across base64 windows and a padded tail", () => {
const bytes = Buffer.alloc(100_001, 0x41);
bytes[48 * 1024] = 0xff;
bytes[bytes.length - 1] = 0xfe;
const body = {
...retained,
bytes: bytes.length,
content: bytes.toString("base64"),
sha256: createHash("sha256").update(bytes).digest("hex"),
};
expect(browserNetworkBodySchema.safeParse(body).success).toBe(true);
bytes[48 * 1024] = 0;
expect(
browserNetworkBodySchema.safeParse({
...body,
content: bytes.toString("base64"),
}).success,
).toBe(false);
});
it("rejects padding at a nonfinal base64 window boundary", () => {
const first = Buffer.alloc(48 * 1024 - 1, 0x41);
const last = Buffer.from([0x42, 0x43, 0x44]);
expect(
browserNetworkBodySchema.safeParse({
...retained,
content: first.toString("base64") + last.toString("base64"),
bytes: first.length + last.length,
sha256: createHash("sha256").update(first).update(last).digest("hex"),
}).success,
).toBe(false);
});
+31 -10
View File
@@ -1,5 +1,5 @@
import { z } from "zod";
import { sha256Hex } from "../kernel/sha256Hex.js";
import { createHash } from "node:crypto";
export const PLAYWRIGHT_RESPONSE_BODY_PROVENANCE_LIMITATION =
"Captured response-body bytes are the result of Playwright's Response.body() API. Playwright may satisfy that API with a credentialed follow-up load of the request URL when CDP has no body, so transaction and event links do not verify that retained bytes came from the original network transaction.";
@@ -80,20 +80,41 @@ export const browserNetworkBodySchema = z.union([
redacted: z.boolean(),
})
.superRefine((artifact, context) => {
const bytes = Buffer.from(artifact.content, "base64");
if (bytes.toString("base64") !== artifact.content)
context.addIssue({
code: "custom",
path: ["content"],
message: "Network body must use canonical base64",
});
if (bytes.length !== artifact.bytes)
const encoded = artifact.content;
const hash = createHash("sha256");
// Complete quartets decode independently. Round-trip each bounded
// window to verify spelling without copying the entire retained body.
const windowCharacters = 64 * 1024;
let decodedBytes = 0;
for (
let offset = 0;
offset < encoded.length;
offset += windowCharacters
) {
const end = Math.min(offset + windowCharacters, encoded.length);
const window = encoded.slice(offset, end);
const bytes = Buffer.from(window, "base64");
if (
bytes.toString("base64") !== window ||
(end < encoded.length && window.endsWith("="))
) {
context.addIssue({
code: "custom",
path: ["content"],
message: "Network body must use canonical base64",
});
return;
}
decodedBytes += bytes.length;
hash.update(bytes);
}
if (decodedBytes !== artifact.bytes)
context.addIssue({
code: "custom",
path: ["bytes"],
message: "Network body byte count disagrees with retained bytes",
});
if (sha256Hex(bytes) !== artifact.sha256)
if (hash.digest("hex") !== artifact.sha256)
context.addIssue({
code: "custom",
path: ["sha256"],