-
Notifications
You must be signed in to change notification settings - Fork 5.1k
node:zlib: reject invalid brotli flush values at validation #31505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alii
wants to merge
14
commits into
main
Choose a base branch
from
ali/brotli-flush-no-abort
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3ca345a
node:zlib: don't abort on brotli .flush() with a zlib flush value
alii cb6608c
node:zlib: filter ASAN stderr warning in brotli flush tests
robobun b5a90e2
node:zlib: give brotli/zstd streaming tests more time under debug/ASAN
robobun 961645b
[autofix.ci] apply automated fixes
autofix-ci[bot] bd65571
ci: retrigger
robobun 3785f54
node:zlib: reject invalid brotli flush values at the write boundary
robobun 062591b
node:zlib: throw ERR_INVALID_ARG_TYPE for an out-of-range flush
robobun 430c072
node:zlib: make the flush op type-safe per codec
robobun a8089bf
Merge remote-tracking branch 'origin/main' into ali/brotli-flush-no-a…
robobun f0557ff
Merge remote-tracking branch 'origin/main' into ali/brotli-flush-no-a…
robobun d1a67dc
node:zlib: match nodejs/node#63746 — validate .flush(kind) per codec …
robobun ea0d900
node:zlib: reword stale native flush-validation comments
robobun 611d473
node:zlib: reword stale test comment for the deflate flush sanity case
robobun 980eaa0
Merge remote-tracking branch 'origin/main' into ali/brotli-flush-no-a…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -182,9 +182,20 @@ | |
| /// Backing-stream surface used by [`CompressionStream`] (zlib / brotli / zstd | ||
| /// `Context` types). | ||
| pub(crate) trait CompressionContext { | ||
| /// The codec's flush-operation type (brotli `BrotliEncoderOperation`, zlib | ||
| /// `FlushValue`, …). Holding a value of this type is proof the flush mode | ||
| /// is valid for this codec — invalid ints can't be converted, so they | ||
| /// can't reach the encoder. | ||
| type FlushOp: Copy; | ||
|
|
||
| fn set_buffers(&mut self, in_: Option<&[u8]>, out: Option<&mut [u8]>); | ||
| fn set_flush(&mut self, flush: i32); | ||
| fn flush_value_is_valid(flush: u32) -> bool; | ||
| /// Convert a raw flush int (from JS) into this codec's flush op, or `None` | ||
| /// if it isn't a valid op for this codec. This is the single validation | ||
| /// point: brotli only has `BrotliEncoderOperation` 0..=3, so a zlib-only | ||
| /// mode like Z_FINISH(4)/Z_BLOCK(5) fails here instead of reaching the | ||
| /// encoder (where it would spin — see nodejs/node#63701). | ||
| fn flush_op_from_u32(flush: u32) -> Option<Self::FlushOp>; | ||
| fn set_flush(&mut self, op: Self::FlushOp); | ||
| fn do_work(&mut self); | ||
| fn reset(&mut self) -> Error; | ||
| fn close(&mut self); | ||
|
|
@@ -332,14 +343,19 @@ | |
| .throw()); | ||
| } | ||
| let flush: u32 = jsv_to_u32(arguments[0]); | ||
| if !<T::Stream as CompressionContext>::flush_value_is_valid(flush) { | ||
| // Convert to the codec's typed flush op up front — failure means the | ||
| // value isn't a flush mode this codec defines. zlib.ts's `.flush()` | ||
| // already rejects out-of-range kinds (ERR_OUT_OF_RANGE) before queuing | ||
| // the flush chunk; this is defense-in-depth for direct handle.write() | ||
| // and `_processChunk()` callers that bypass `.flush()`. | ||
| let Some(flush_op) = <T::Stream as CompressionContext>::flush_op_from_u32(flush) else { | ||
| return Err(global_this | ||
| .err( | ||
| ErrorCode::INVALID_ARG_VALUE, | ||
| format_args!("Invalid flush value"), | ||
| ) | ||
| .throw()); | ||
| } | ||
| }; | ||
|
Check warning on line 358 in src/runtime/node/node_zlib_binding.rs
|
||
|
robobun marked this conversation as resolved.
|
||
|
|
||
| if arguments[1].is_null() { | ||
| // just a flush | ||
|
|
@@ -451,7 +467,7 @@ | |
|
|
||
| this.stream().with_mut(|s| { | ||
| s.set_buffers(in_, out); | ||
| s.set_flush(i32::try_from(flush).expect("int cast")); | ||
| s.set_flush(flush_op); | ||
| }); | ||
|
|
||
| // Only create the strong handle when we have a pending write | ||
|
|
@@ -648,14 +664,19 @@ | |
| .throw()); | ||
| } | ||
| let flush: u32 = jsv_to_u32(arguments[0]); | ||
| if !<T::Stream as CompressionContext>::flush_value_is_valid(flush) { | ||
| // Convert to the codec's typed flush op up front — failure means the | ||
| // value isn't a flush mode this codec defines. zlib.ts's `.flush()` | ||
| // already rejects out-of-range kinds (ERR_OUT_OF_RANGE) before queuing | ||
| // the flush chunk; this is defense-in-depth for direct handle.write() | ||
| // and `_processChunk()` callers that bypass `.flush()`. | ||
| let Some(flush_op) = <T::Stream as CompressionContext>::flush_op_from_u32(flush) else { | ||
| return Err(global_this | ||
| .err( | ||
| ErrorCode::INVALID_ARG_VALUE, | ||
| format_args!("Invalid flush value"), | ||
| ) | ||
| .throw()); | ||
| } | ||
| }; | ||
|
|
||
| // Hoisted so `in_` can borrow it past the `else` arm (mirrors `out_buf`). | ||
| let in_buf: jsc::ArrayBuffer; | ||
|
|
@@ -730,7 +751,7 @@ | |
|
|
||
| this.stream().with_mut(|s| { | ||
| s.set_buffers(in_, out); | ||
| s.set_flush(i32::try_from(flush).expect("int cast")); | ||
| s.set_flush(flush_op); | ||
| }); | ||
| let this_value = callframe.this(); | ||
|
|
||
|
|
@@ -1014,10 +1035,14 @@ | |
| /// emits a `pub mod js { … }` with the cached-property accessors | ||
| /// (`writeCallback` / `errorCallback` / `dictionary`) wired to the | ||
| /// `${TypeName}Prototype__${prop}{Get,Set}CachedValue` extern symbols. | ||
| /// | ||
| /// `$flush_op` is the codec's [`CompressionContext::FlushOp`] — the typed | ||
| /// flush operation its fallible `flush_op_from_u32` produces and its | ||
| /// `set_flush` consumes. | ||
| #[macro_export] | ||
| #[doc(hidden)] | ||
| macro_rules! __impl_compression_stream { | ||
| ($native:ident, $ctx:ty, $type_name:literal) => { | ||
| ($native:ident, $ctx:ty, $type_name:literal, $flush_op:ty) => { | ||
| impl ::bun_event_loop::Taskable for $native { | ||
| const TAG: ::bun_event_loop::TaskTag = ::bun_event_loop::task_tag::$native; | ||
| /// An async write whose completion will not run: unpin, unref, drop | ||
|
|
@@ -1036,9 +1061,11 @@ | |
| } | ||
|
|
||
| impl $crate::node::node_zlib_binding::CompressionContext for $ctx { | ||
| type FlushOp = $flush_op; | ||
|
|
||
| #[inline] fn set_buffers(&mut self, in_: Option<&[u8]>, out: Option<&mut [u8]>) { Self::set_buffers(self, in_, out) } | ||
| #[inline] fn set_flush(&mut self, flush: i32) { Self::set_flush(self, flush) } | ||
| #[inline] fn flush_value_is_valid(flush: u32) -> bool { Self::flush_value_is_valid(flush) } | ||
| #[inline] fn flush_op_from_u32(flush: u32) -> Option<$flush_op> { Self::flush_op_from_u32(flush) } | ||
| #[inline] fn set_flush(&mut self, op: $flush_op) { Self::set_flush(self, op) } | ||
| #[inline] fn do_work(&mut self) { Self::do_work(self) } | ||
| #[inline] fn reset(&mut self) -> $crate::node::node_zlib_binding::Error { Self::reset(self) } | ||
| #[inline] fn close(&mut self) { Self::close(self) } | ||
|
|
||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.