|
| 1 | +import { gunzipSync, inflateRawSync, inflateSync, zstdDecompressSync } from "node:zlib"; |
| 2 | + |
1 | 3 | /** |
2 | 4 | * Request-body decompression for the /v1/responses data plane. |
3 | 5 | * |
@@ -25,27 +27,54 @@ export class UnsupportedContentEncodingError extends Error { |
25 | 27 | } |
26 | 28 |
|
27 | 29 | 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`); |
30 | 32 | } |
31 | 33 | } |
32 | 34 |
|
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); |
35 | 37 | return body; |
36 | 38 | } |
37 | 39 |
|
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 { |
39 | 57 | const encoding = (contentEncoding ?? "").trim().toLowerCase(); |
40 | | - if (encoding === "" || encoding === "identity") return assertBodySizeWithinLimit(raw); |
| 58 | + if (encoding === "" || encoding === "identity") return assertBodySizeWithinLimit(raw, maxBytes); |
41 | 59 | 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 }; |
42 | 64 | 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); |
49 | 78 | } |
50 | 79 |
|
51 | 80 | /** Parse a JSON request body, transparently decoding compressed payloads. */ |
|
0 commit comments