Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ turnloop-zstd-decoder = { path = "protocols/turnloop-zstd-decoder", version = "0
http = "1"
url = "2"
httparse = "1"
brotli = "8"
brotli-decompressor = "6.2"
brotli = "9.1.1"
percent-encoding = "2"
webpki-roots = "1"
ring = "0.17"
Expand Down
3 changes: 2 additions & 1 deletion protocols/turnloop-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ http.workspace = true
url.workspace = true
httparse.workspace = true
flate2.workspace = true
brotli.workspace = true
brotli-decompressor.workspace = true
base64.workspace = true
percent-encoding.workspace = true
# Not the workspace entry, so that the host decides whether ring is linked.
turnloop-tls = { path = "../turnloop-tls", version = "0.1.0-alpha.10", default-features = false }

[dev-dependencies]
brotli.workspace = true
rcgen.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
22 changes: 14 additions & 8 deletions protocols/turnloop-http/src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ enum Stage {
}
type BytePool = crate::recycling::Pool<u8>;
type IntPool = crate::recycling::Pool<u32>;
type CodePool = crate::recycling::Pool<brotli::HuffmanCode>;
type BrotliState = brotli::BrotliState<BytePool, IntPool, CodePool>;
type CodePool = crate::recycling::Pool<brotli_decompressor::HuffmanCode>;
type BrotliState = brotli_decompressor::BrotliState<BytePool, IntPool, CodePool>;
// Keep the WASM decoder inline: 480 bytes avoids another per-response allocation.
#[allow(clippy::large_enum_variant)]
enum Engine {
Expand Down Expand Up @@ -153,7 +153,11 @@ impl Codec {
let bytes = BytePool::default();
let ints = IntPool::default();
let codes = CodePool::default();
let state = Box::new(BrotliState::new(bytes.clone(), ints.clone(), codes.clone()));
let state = Box::new(BrotliState::new_strict(
bytes.clone(),
ints.clone(),
codes.clone(),
));
Engine::Brotli {
state,
bytes,
Expand Down Expand Up @@ -209,7 +213,7 @@ impl Codec {
ints,
codes,
} => {
**state = BrotliState::new(bytes.clone(), ints.clone(), codes.clone());
**state = BrotliState::new_strict(bytes.clone(), ints.clone(), codes.clone());
}
#[cfg(not(any(target_arch = "wasm32", feature = "pure-rust-zstd")))]
Engine::Zstd { decoder, boundary } => {
Expand Down Expand Up @@ -371,7 +375,7 @@ impl Codec {
let mut available_in = input.len();
let mut available_out = output.len();
let mut total = self.total;
let result = brotli::BrotliDecompressStream(
let result = brotli_decompressor::BrotliDecompressStream(
&mut available_in,
&mut consumed,
input,
Expand All @@ -382,14 +386,16 @@ impl Codec {
state,
);
match result {
brotli::BrotliResult::ResultSuccess => {
brotli_decompressor::BrotliResult::ResultSuccess => {
if consumed != input.len() {
return Err(corrupt());
}
finished = true;
}
brotli::BrotliResult::ResultFailure => return Err(corrupt()),
brotli::BrotliResult::NeedsMoreInput if end => return Err(corrupt()),
brotli_decompressor::BrotliResult::ResultFailure => return Err(corrupt()),
brotli_decompressor::BrotliResult::NeedsMoreInput if end => {
return Err(corrupt());
}
_ => {}
}
}
Expand Down
6 changes: 3 additions & 3 deletions protocols/turnloop-http/src/recycling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ impl<T> Default for Memory<T> {
Self(Vec::new())
}
}
impl<T> brotli::SliceWrapper<T> for Memory<T> {
impl<T> brotli_decompressor::SliceWrapper<T> for Memory<T> {
fn slice(&self) -> &[T] {
&self.0
}
}
impl<T> brotli::SliceWrapperMut<T> for Memory<T> {
impl<T> brotli_decompressor::SliceWrapperMut<T> for Memory<T> {
fn slice_mut(&mut self) -> &mut [T] {
&mut self.0
}
Expand All @@ -28,7 +28,7 @@ impl<T> Default for Pool<T> {
Self(Arc::new(Mutex::new(Vec::new())))
}
}
impl<T: Default + Clone> brotli::Allocator<T> for Pool<T> {
impl<T: Default + Clone> brotli_decompressor::Allocator<T> for Pool<T> {
type AllocatedMemory = Memory<T>;
fn alloc_cell(&mut self, len: usize) -> Memory<T> {
let mut pool = self.0.lock().expect("scratch pool poisoned");
Expand Down
30 changes: 30 additions & 0 deletions protocols/turnloop-http/tests/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,36 @@ fn gzip_deflate_br_zstd_decoding() {
}
}

/// Large-window Brotli is not RFC 7932 and not `Content-Encoding: br`: its
/// window can reach 1 GiB, which a tiny body could make the decoder allocate
/// before the decoded-size limit applies (the actix-http RUSTSEC-2026-0455
/// shape). Node's decoder rejects it by default, and so does this one.
#[test]
fn large_window_brotli_is_rejected() {
use std::io::{Read, Write};
let body = b"large window brotli is not content-encoding br";
let params = brotli::enc::BrotliEncoderParams {
large_window: true,
lgwin: 30,
quality: 5,
..Default::default()
};
let mut encoded = Vec::new();
{
let mut encoder = brotli::CompressorWriter::with_params(&mut encoded, 4096, &params);
encoder.write_all(body).unwrap();
}
// The stream is valid large-window Brotli: a permissive decoder reads it.
let mut permissive = Vec::new();
brotli::Decompressor::new(encoded.as_slice(), 4096)
.read_to_end(&mut permissive)
.unwrap();
assert_eq!(permissive, body);

let mut out = Vec::new();
assert!(turnloop_http::compression::decode("br", &encoded, &mut out, 1 << 20).is_err());
}

#[test]
fn vendored_hpack_interop_corpus() {
use std::io::Read;
Expand Down
Loading