zlib: validate flush kind for brotli streams#63746
Open
Ic3b3rg wants to merge 1 commit into
Open
Conversation
BrotliCompress/Decompress.flush(kind) forwarded any value to the native layer, causing a 100% CPU hang for kinds outside the brotli operation range (e.g. Z_FINISH). Validate against [0, 3] and throw ERR_OUT_OF_RANGE on invalid input. Fixes: nodejs#63701 Signed-off-by: Ic3b3rg <s.ceccarini94@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BrotliCompress.flush(kind)andBrotliDecompress.flush(kind)forwarded anykindvalue to the native encoder/decoder without validating it. PassingZ_FINISH(4) orZ_BLOCK(5) caused the process to spin at 100% CPUindefinitely, because brotli has no matching operation and the loop never makes progress.
This PR validates
kindagainst the brotli operation range[0, 3]before the fake-chunk is queued, and throwsERR_OUT_OF_RANGEon invalid input. The check mirrors the validation already done in theZlibBaseconstructor foroptions.flushandoptions.finishFlush.Scope
Validation is intentionally limited to brotli streams. Zlib and zstd
flush()calls are unchanged. Internal callers offlush()were audited and the only one (Zlib.prototype.paramscallingflush(Z_SYNC_FLUSH=2)) is in range for brotli — no regression.Observable behaviour changes (brotli streams only)
flush(4)(Z_FINISH)ERR_OUT_OF_RANGEflush(5)(Z_BLOCK)ERR_OUT_OF_RANGEflush(6)(Z_TREES)ERR_INVALID_ARG_TYPE(incidental, fromwrite(undefined))ERR_OUT_OF_RANGEflush(0..3)(valid brotli ops)flush(),flush(cb)Cross-runtime note
Bun is fixing the same bug on their side (oven-sh/bun#31505) but plans to throw
ERR_INVALID_ARG_TYPEto match the current incidental behaviour offlush(6)on Node. This PR choosesERR_OUT_OF_RANGEfor internal consistency with thecheckRangesOrGetDefaultvalidation already used elsewhere inlib/zlib.js.Fixes: #63701