From 661dec7e6be222a9f4e9a6b39d0e94b45c658b8d Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Thu, 19 Nov 2026 19:42:53 +0800 Subject: [PATCH] perf(browser): bound retained network body verification memory (#12231) perf(browser): bound memory for retained body verification --- src/domain/browserNetworkEvidence.test.ts | 33 ++++++++++++++++++ src/domain/browserNetworkEvidence.ts | 41 +++++++++++++++++------ 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/domain/browserNetworkEvidence.test.ts b/src/domain/browserNetworkEvidence.test.ts index c469155400..59de3f7dfb 100644 --- a/src/domain/browserNetworkEvidence.test.ts +++ b/src/domain/browserNetworkEvidence.test.ts @@ -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); +}); diff --git a/src/domain/browserNetworkEvidence.ts b/src/domain/browserNetworkEvidence.ts index 9c101896c5..28ba3830b6 100644 --- a/src/domain/browserNetworkEvidence.ts +++ b/src/domain/browserNetworkEvidence.ts @@ -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"],