Skip to content

Let one-shot node:zlib functions decompress up to maxOutputLength - #8642

Merged
bartlomieju merged 5 commits into
cloudflare:mainfrom
maschwenk:zlib-max-output-length-growth
Nov 28, 2026
Merged

bartlomieju merged 5 commits into
cloudflare:mainfrom
maschwenk:zlib-max-output-length-growth

Conversation

@maschwenk

Copy link
Copy Markdown
Contributor

one-shot zlib, brotli and zstd functions can throw "Memory limit exceeded" for output over 40960 bytes that fits in maxOutputLength when 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 asks GrowableBuffer for 40 KiB chunks, and the buffer refuses one that would pass maxOutputLength instead of growing up to the limit. #6294 only fixed output that fits in the first chunk

the 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 ported test-zlib-maxOutputLength.js assertion. new tests decompress 40961 to 200000 bytes with zlib, gzip, deflate-raw, brotli and zstd and fail without the fix

@maschwenk
maschwenk requested review from a team as code owners November 19, 2026 03:38
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@clanky-the-clanker

clanky-the-clanker Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

This pull request's reviews are posted as pull request reviews: see the review of 8ee3ed09.

@maschwenk

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@jasnell

jasnell commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

clanker review

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Out of date: this review is of 8ee3ed09; the pull request is now at 048185a0. Someone with write access can comment clanky review (or clanker review or @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

@maschwenk
maschwenk force-pushed the zlib-max-output-length-growth branch from 8ee3ed0 to 984bfaa Compare November 20, 2026 18:22
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".
@maschwenk
maschwenk force-pushed the zlib-max-output-length-growth branch from 984bfaa to 194ab93 Compare November 21, 2026 01:52
@bartlomieju

Copy link
Copy Markdown
Collaborator

test-tsan fails on this PR: //src/workerd/api/node/tests:zlib-nodejs-test@ times out at 60s partway through the new maxOutputLengthAfterBufferGrowth test. It isn't a flake. The test takes about 5.6s in a regular build, while every other test in the file takes milliseconds, and the tsan build is slow enough to push that past the target's limit.

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.

@maschwenk

Copy link
Copy Markdown
Contributor Author

switched maxOutputLengthAfterBufferGrowth to { level: 1 } for deflate/gzip and BROTLI_PARAM_QUALITY: 1 for brotli, kept 200000 for now. haven't run test-tsan, happy to drop 200000 if it's still close to the timeout

@bartlomieju
bartlomieju merged commit c495230 into cloudflare:main Nov 28, 2026
58 of 66 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants