Skip to content

Commit 94a1ce7

Browse files
committed
fix(decompress): enforce body cap during inflation via zlib maxOutputLength
Review finding (PR #96): the previous cap checked size AFTER full allocation, so a compressed bomb still forced the whole decode into memory. node:zlib's maxOutputLength aborts inside inflation (ERR_BUFFER_TOO_LARGE), which we map to DecompressedBodyTooLargeError. decodeRequestBody gains an injectable maxBytes param so activation tests stay cheap; HTTP deflate accepts both zlib-wrapped and raw streams (Bun.inflateSync previously accepted raw).
1 parent 6818c61 commit 94a1ce7

2 files changed

Lines changed: 58 additions & 12 deletions

File tree

‎src/server/request-decompress.ts‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { gunzipSync, inflateRawSync, inflateSync, zstdDecompressSync } from "node:zlib";
2+
13
/**
24
* Request-body decompression for the /v1/responses data plane.
35
*
@@ -25,27 +27,54 @@ export class UnsupportedContentEncodingError extends Error {
2527
}
2628

2729
export class DecompressedBodyTooLargeError extends Error {
28-
constructor(readonly bytes: number) {
29-
super(`Decompressed request body exceeds ${MAX_DECOMPRESSED_BODY_BYTES} bytes`);
30+
constructor(readonly bytes: number, limit: number = MAX_DECOMPRESSED_BODY_BYTES) {
31+
super(`Decompressed request body exceeds ${limit} bytes`);
3032
}
3133
}
3234

33-
function assertBodySizeWithinLimit(body: Uint8Array): Uint8Array {
34-
if (body.byteLength > MAX_DECOMPRESSED_BODY_BYTES) throw new DecompressedBodyTooLargeError(body.byteLength);
35+
function assertBodySizeWithinLimit(body: Uint8Array, maxBytes: number): Uint8Array {
36+
if (body.byteLength > maxBytes) throw new DecompressedBodyTooLargeError(body.byteLength, maxBytes);
3537
return body;
3638
}
3739

38-
export function decodeRequestBody(raw: Uint8Array, contentEncoding: string | null): Uint8Array {
40+
function inflateDeflateBody(compressed: Uint8Array<ArrayBuffer>, opts: { maxOutputLength: number }): Uint8Array {
41+
// HTTP "deflate" appears both zlib-wrapped and raw in the wild (Bun.deflateSync emits raw,
42+
// which the previous Bun.inflateSync accepted). Try zlib-wrapped first, fall back to raw —
43+
// but never swallow the size-cap abort.
44+
try {
45+
return inflateSync(compressed, opts);
46+
} catch (err) {
47+
if ((err as NodeJS.ErrnoException | null)?.code === "ERR_BUFFER_TOO_LARGE") throw err;
48+
return inflateRawSync(compressed, opts);
49+
}
50+
}
51+
52+
export function decodeRequestBody(
53+
raw: Uint8Array,
54+
contentEncoding: string | null,
55+
maxBytes: number = MAX_DECOMPRESSED_BODY_BYTES,
56+
): Uint8Array {
3957
const encoding = (contentEncoding ?? "").trim().toLowerCase();
40-
if (encoding === "" || encoding === "identity") return assertBodySizeWithinLimit(raw);
58+
if (encoding === "" || encoding === "identity") return assertBodySizeWithinLimit(raw, maxBytes);
4159
const compressed = raw as Uint8Array<ArrayBuffer>;
60+
// `maxOutputLength` makes zlib abort DURING inflation (ERR_BUFFER_TOO_LARGE), so a
61+
// decompression bomb never allocates beyond the cap — checking after the fact would
62+
// already have paid the full allocation (review finding, PR #96).
63+
const opts = { maxOutputLength: maxBytes };
4264
let decoded: Uint8Array;
43-
if (encoding === "zstd") decoded = Bun.zstdDecompressSync(compressed);
44-
else if (encoding === "gzip" || encoding === "x-gzip") decoded = Bun.gunzipSync(compressed);
45-
else if (encoding === "deflate") decoded = Bun.inflateSync(compressed);
46-
// Multi-codings ("zstd, gzip") and unknown tokens are rejected rather than guessed.
47-
else throw new UnsupportedContentEncodingError(encoding);
48-
return assertBodySizeWithinLimit(decoded);
65+
try {
66+
if (encoding === "zstd") decoded = zstdDecompressSync(compressed, opts);
67+
else if (encoding === "gzip" || encoding === "x-gzip") decoded = gunzipSync(compressed, opts);
68+
else if (encoding === "deflate") decoded = inflateDeflateBody(compressed, opts);
69+
// Multi-codings ("zstd, gzip") and unknown tokens are rejected rather than guessed.
70+
else throw new UnsupportedContentEncodingError(encoding);
71+
} catch (err) {
72+
if ((err as NodeJS.ErrnoException | null)?.code === "ERR_BUFFER_TOO_LARGE") {
73+
throw new DecompressedBodyTooLargeError(maxBytes + 1, maxBytes);
74+
}
75+
throw err;
76+
}
77+
return assertBodySizeWithinLimit(decoded, maxBytes);
4978
}
5079

5180
/** Parse a JSON request body, transparently decoding compressed payloads. */

‎tests/request-decompress.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ describe("decodeRequestBody", () => {
6666
expect(() => decodeRequestBody(compressed, "zstd")).toThrow(DecompressedBodyTooLargeError);
6767
});
6868

69+
test("aborts DURING inflation via maxOutputLength — activation per codec (injected cap)", () => {
70+
// Review finding (PR #96): the cap must fire inside zlib, not after full allocation.
71+
// A small injected cap keeps the test cheap while exercising the exact
72+
// ERR_BUFFER_TOO_LARGE -> DecompressedBodyTooLargeError path.
73+
const CAP = 1024;
74+
const inflates64k = new Uint8Array(64 * 1024);
75+
expect(() => decodeRequestBody(Bun.zstdCompressSync(inflates64k), "zstd", CAP)).toThrow(DecompressedBodyTooLargeError);
76+
expect(() => decodeRequestBody(Bun.gzipSync(inflates64k), "gzip", CAP)).toThrow(DecompressedBodyTooLargeError);
77+
expect(() => decodeRequestBody(Bun.deflateSync(inflates64k), "deflate", CAP)).toThrow(DecompressedBodyTooLargeError);
78+
});
79+
80+
test("injected cap still admits bodies within the limit", () => {
81+
const CAP = 1024 * 1024;
82+
const compressed = Bun.zstdCompressSync(PAYLOAD_BYTES);
83+
expect(new TextDecoder().decode(decodeRequestBody(compressed, "zstd", CAP))).toBe(JSON.stringify(PAYLOAD));
84+
});
85+
6986
test("decodes image-heavy bodies that exceed the old 64MB cap (regression)", () => {
7087
// The reported "Invalid JSON body" failure: ~12 screenshots inflate past the former 64MB cap.
7188
// 100MB is over the old limit and under the current one, so it must now decode.

0 commit comments

Comments
 (0)