Files
rea/src/domain/paddedBase64.test.ts
morluto 49b4edfe77 fix(browser): validate large base64 without regexp stack overflow
Share padded decoding across CDP response shapes and cache fingerprints,
and count binary WebSocket bytes without allocating a decoded buffer.
Preserve HAR canonical pad bits and validate before allocation.

Replace repeated alphabet quartets in canonical schemas with separate
alphabet, padding and fixed-width length assertions. Cover near-32MiB
encodings under ordinary, Unicode and Unicode-set modes, plus complete
4MiB CLI and MCP retained-evidence export workflows.

Fixes #5253
2026-10-19 21:46:23 +08:00

45 lines
1.3 KiB
TypeScript

import { expect, it } from "vitest";
import { decodePaddedBase64, paddedBase64ByteLength } from "./paddedBase64.js";
it("preserves padded syntax, including empty bodies and nonzero pad bits", () => {
for (const [encoded, decoded] of [
["", ""],
["AA==", "00"],
["AB==", "00"],
["AAA=", "0000"],
["AAB=", "0000"],
["////", "ffffff"],
] as const) {
expect(decodePaddedBase64(encoded)?.toString("hex")).toBe(decoded);
expect(paddedBase64ByteLength(encoded)).toBe(decoded.length / 2);
}
for (const malformed of [
"A",
"AA",
"AAA",
"=AAA",
"A===",
"====",
"AA==A",
"AA-_",
"AA==\n",
"AA==\r",
"AA== ",
"A A=",
"AA==",
]) {
expect(paddedBase64ByteLength(malformed), malformed).toBeUndefined();
expect(decodePaddedBase64(malformed), malformed).toBeUndefined();
}
});
it("validates large bodies and malformed suffixes without a regexp stack overflow", () => {
const body = Buffer.alloc(24 * 1024 * 1024 - 3, 0x81);
const encoded = body.toString("base64");
expect(paddedBase64ByteLength(encoded)).toBe(body.length);
expect(decodePaddedBase64(encoded)?.equals(body)).toBe(true);
const malformed = `${encoded.slice(0, -1)}!`;
expect(paddedBase64ByteLength(malformed)).toBeUndefined();
});