Repository navigation
Let one-shot node:zlib functions decompress up to maxOutputLength - #8642
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
|
This pull request's reviews are posted as pull request reviews: see the review of |
|
I have read the CLA Document and I hereby sign the CLA |
|
clanker review |
There was a problem hiding this comment.
⚠️ Out of date: this review is of8ee3ed09; the pull request is now at048185a0. Someone with write access can commentclanky review(orclanker reviewor@clanky-the-clanker review) to review the new commits.
✅ Approved — The output request is now capped at maxOutputLength, which fixes the false overflow error for one-shot zlib, brotli and zstd calls, and the tests cover it. No issues found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
Output is still bounded by maxOutputLength, because grow() clamps to maxCapacity.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
📋 Code Quality: ✅ LGTM
Capping addChunk() at maxCapacity stops grow() from rejecting output that still fits within the limit. grow() still clamps the capacity to maxCapacity, so the buffer can never exceed the limit. Overflow is now reported only by the existing full-buffer check in syncProcessBuffer, and that check now uses Node's error message. The new tests cover the sizes and limits that failed before.
⚡ Performance: ✅ LGTM
No performance concerns.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: 8ee3ed09
8ee3ed0 to
984bfaa
Compare
zlib.inflateRawSync(data, { maxOutputLength: n }) threw "Memory limit
exceeded" for output of n bytes whenever n was above 40960, unless n
happened to land on the output buffer's growth steps: 50000-, 100000-
and 200000-byte entries all failed, while 81920 worked. adm-zip passes
an entry's exact size this way, so it fails on most entries over 40 KiB.
Any maxOutputLength that leaves less than 40 KiB of room after a full
buffer has the same problem, for every sync and callback zlib, brotli
and zstd function.
The sync loop asks its GrowableBuffer for another 40 KiB chunk each
time the codec fills the buffer, and the buffer refuses to grow past
maxOutputLength at all, rather than grow up to it. cloudflare#6294 fixed the case
where the whole output fits in the first chunk. The chunk the loop asks
for is now capped at maxOutputLength, so the codec gets whatever room is
left, and the existing check after a full buffer at maxOutputLength
decides whether the output fits.
maxOutputLengthAfterBufferGrowth and
zstdMaxOutputLengthAfterBufferGrowthTest decompress 40961 to 200000
bytes with maxOutputLength set to the output size and above it, and
check that one byte less still fails. They fail without this change.
The one-shot zlib, brotli and zstd functions report output that would exceed maxOutputLength as RangeError ERR_BUFFER_TOO_LARGE, as Node.js does, but with the message "Memory limit exceeded". Node's message is "Cannot create a Buffer larger than <maxOutputLength> bytes", and the test ported from Node's test-zlib-maxOutputLength.js had been changed to expect ours. Use Node's message, and go back to Node's assertion. Without a maxOutputLength the limit is Z_MAX_CHUNK (128 MiB), so a zip bomb now reports "Cannot create a Buffer larger than 134217728 bytes".
984bfaa to
194ab93
Compare
# Conflicts: # src/workerd/api/node/tests/zlib-zstd-nodejs-test.js
|
Most of the time probably goes to compressing the inputs, Brotli at its default quality of 11 especially. The test only needs each input compressed once, at any setting, so the fastest settings should bring it well under a second without losing coverage: for (const [compress, decompress, options] of [
[zlib.deflateRawSync, zlib.inflateRawSync, { level: 1 }],
[zlib.deflateSync, zlib.inflateSync, { level: 1 }],
[zlib.gzipSync, zlib.gunzipSync, { level: 1 }],
[
zlib.brotliCompressSync,
zlib.brotliDecompressSync,
{ params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 1 } },
],
]) {
const compressed = compress(original, options);If it's still slow after that, dropping the 200000 size should be enough: 40961, 50000 and 100000 already cover output that crosses one, two and several 40 KiB chunks. |
|
switched |
one-shot zlib, brotli and zstd functions can throw "Memory limit exceeded" for output over 40960 bytes that fits in
maxOutputLengthwhen the limit is less than 40 KiB above the output size: 50000 bytes with a limit of 50000 or 81919 fails. adm-zip passes each entry's exact size as the limit, so it fails on most entries over 40 KiB. the sync loop asksGrowableBufferfor 40 KiB chunks, and the buffer refuses one that would passmaxOutputLengthinstead of growing up to the limit. #6294 only fixed output that fits in the first chunkthe requested chunk is now capped at
maxOutputLength, and the existing full-buffer check decides whether the output fits. a second commit switches the overflow error to node's "Cannot create a Buffer larger than N bytes" and restores the portedtest-zlib-maxOutputLength.jsassertion. new tests decompress 40961 to 200000 bytes with zlib, gzip, deflate-raw, brotli and zstd and fail without the fix