Skip to main content

eggfetch_core/
compression.rs

1//! Response decompression.
2//!
3//! Provides streaming decompression of response bodies based on
4//! `Content-Encoding` headers. Supports gzip, deflate, brotli, and
5//! zstd via feature-gated `async-compression` decoders.
6//!
7//! # Policy
8//!
9//! When automatic decompression is enabled (the default when any
10//! compression feature is compiled in), the client:
11//!
12//! 1. Advertises supported encodings in `Accept-Encoding`.
13//! 2. Decodes compressed response bodies transparently.
14//! 3. Strips `Content-Encoding` and `Content-Length` from decoded
15//!    response headers.
16//!
17//! If the server responds with an unsupported encoding and automatic
18//! decompression is enabled, an `Error::UnsupportedContentEncoding`
19//! is returned. If decompression is disabled, the raw bytes pass
20//! through unchanged.
21
22use std::sync::atomic::{AtomicUsize, Ordering};
23use std::sync::Arc;
24use std::sync::OnceLock;
25
26use crate::body::BoxBytesStream;
27use crate::error::{Error, Result};
28
29/// Limits applied during response body decompression.
30///
31/// Nested content encodings are additionally capped at 4 layers
32/// (`MAX_NESTING_DEPTH`); `max_decompression_ratio` remains the primary
33/// defense against large expansion ratios.
34#[derive(Debug, Clone, Copy, Default)]
35pub struct DecompressionLimit {
36    /// Hard limit on total decoded bytes.
37    pub(crate) max_decoded_body_size: Option<usize>,
38    /// Ratio of decoded bytes to compressed bytes after which
39    /// decompression is rejected.
40    pub(crate) max_decompression_ratio: Option<f64>,
41}
42
43impl DecompressionLimit {
44    /// Create a new limit with both fields set to `None` (unlimited).
45    #[must_use]
46    pub const fn new() -> Self {
47        Self {
48            max_decoded_body_size: None,
49            max_decompression_ratio: None,
50        }
51    }
52
53    /// Returns `true` if neither limit is set.
54    #[must_use]
55    pub fn is_unlimited(&self) -> bool {
56        self.max_decoded_body_size.is_none() && self.max_decompression_ratio.is_none()
57    }
58
59    /// Returns the configured maximum decoded body size, if any.
60    #[must_use]
61    pub fn max_decoded_body_size(&self) -> Option<usize> {
62        self.max_decoded_body_size
63    }
64
65    /// Returns the configured maximum decompression ratio, if any.
66    #[must_use]
67    pub fn max_decompression_ratio(&self) -> Option<f64> {
68        self.max_decompression_ratio
69    }
70
71    /// Validate the decompression ratio.
72    ///
73    /// # Errors
74    ///
75    /// Returns [`Error::Decompression`] if `max_decompression_ratio` is not
76    /// finite and positive. This is the runtime counterpart to
77    /// [`Self::try_new`] for limits constructed via struct literal.
78    pub fn validate(&self) -> Result<()> {
79        if let Some(ratio) = self.max_decompression_ratio {
80            if !ratio.is_finite() || ratio <= 0.0 {
81                return Err(Error::Decompression(
82                    "invalid max_decompression_ratio: must be finite and positive".into(),
83                ));
84            }
85        }
86        Ok(())
87    }
88
89    /// Create a limit with validation for the decompression ratio.
90    ///
91    /// # Errors
92    ///
93    /// Returns an error if `max_decompression_ratio` is not finite and
94    /// positive.
95    pub fn try_new(
96        max_decoded_body_size: Option<usize>,
97        max_decompression_ratio: Option<f64>,
98    ) -> crate::error::Result<Self> {
99        if let Some(ratio) = max_decompression_ratio {
100            if !ratio.is_finite() || ratio <= 0.0 {
101                return Err(crate::error::Error::RequestBuild(
102                    "max_decompression_ratio must be finite and positive".into(),
103                ));
104            }
105        }
106        Ok(Self {
107            max_decoded_body_size,
108            max_decompression_ratio,
109        })
110    }
111}
112
113/// Maximum number of nested content encodings we will attempt to
114/// decode. This prevents stack overflow or excessive resource use
115/// from adversarial multi-layer compression.
116const MAX_NESTING_DEPTH: usize = 4;
117
118/// A content coding identifier.
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum ContentCoding {
121    /// gzip (RFC 1952)
122    Gzip,
123    /// deflate (RFC 9110 §8.4.1.2: the zlib wrapper, RFC 1950; raw
124    /// deflate streams, RFC 1951, are accepted as a lenient fallback)
125    Deflate,
126    /// brotli (RFC 7932)
127    Brotli,
128    /// zstd (RFC 8478)
129    Zstd,
130}
131
132impl ContentCoding {
133    /// Return the wire identifier for this encoding.
134    #[must_use]
135    pub fn as_str(self) -> &'static str {
136        match self {
137            Self::Gzip => "gzip",
138            Self::Deflate => "deflate",
139            Self::Brotli => "br",
140            Self::Zstd => "zstd",
141        }
142    }
143
144    /// Parse a wire identifier into a `ContentCoding`.
145    fn from_wire(s: &str) -> Option<Self> {
146        match s.trim().to_ascii_lowercase().as_str() {
147            "gzip" | "x-gzip" => Some(Self::Gzip),
148            "deflate" => Some(Self::Deflate),
149            "br" => Some(Self::Brotli),
150            "zstd" => Some(Self::Zstd),
151            _ => None,
152        }
153    }
154
155    /// Returns `true` when the token is the RFC 9110 §8.4 `identity`
156    /// content-coding, which means "no encoding" and decodes to itself.
157    fn is_identity(token: &str) -> bool {
158        token.trim().eq_ignore_ascii_case("identity")
159    }
160}
161
162/// Generate the `Accept-Encoding` header value for the compiled-in
163/// features. Returns `None` if no compression features are enabled.
164#[must_use]
165pub fn accept_encoding_value() -> Option<&'static str> {
166    static VALUE: OnceLock<Option<Box<str>>> = OnceLock::new();
167
168    VALUE
169        .get_or_init(|| {
170            // Order matches mainstream client defaults (most common first).
171            //
172            // Only advertise codings whose streaming decoder is compiled
173            // in; advertising an encoding without its decoder makes a
174            // compliant server produce responses we cannot decode.
175            // The pushes are feature-gated, so `vec![]` cannot express
176            // this sequence.
177            #[allow(clippy::vec_init_then_push)]
178            {
179                #[allow(unused_mut)]
180                let mut parts: Vec<&str> = Vec::new();
181
182                #[cfg(feature = "compression-gzip")]
183                parts.push("gzip");
184                #[cfg(feature = "compression-deflate")]
185                parts.push("deflate");
186                #[cfg(feature = "compression-brotli")]
187                parts.push("br");
188                #[cfg(feature = "compression-zstd")]
189                parts.push("zstd");
190
191                (!parts.is_empty()).then(|| parts.join(", ").into_boxed_str())
192            }
193        })
194        .as_deref()
195}
196
197/// Parse a `Content-Encoding` header value into an ordered list of
198/// content codings. The list is in wire order (outermost first).
199///
200/// `identity` tokens (RFC 9110 §8.4, meaning "no encoding") are no-ops
201/// and are skipped.
202///
203/// Unknown tokens are silently dropped here: this function must only be
204/// called after [`validate_content_encodings`] has rejected unsupported
205/// codings (see `decompress_stream` / `finalize.rs`). Keep the two
206/// coupled — calling this alone on unvalidated input would silently
207/// ignore an encoding instead of erroring.
208///
209/// Returns `None` if the header is empty, contains only whitespace, or
210/// contains only no-op codings such as bare `identity`.
211pub(crate) fn parse_content_encodings(header: &str) -> Option<Vec<ContentCoding>> {
212    let encodings: Vec<ContentCoding> = header
213        .split(',')
214        .map(str::trim)
215        .filter(|s| !s.is_empty() && !ContentCoding::is_identity(s))
216        .filter_map(ContentCoding::from_wire)
217        .collect();
218
219    if encodings.is_empty() {
220        None
221    } else {
222        Some(encodings)
223    }
224}
225
226/// Check whether a `Content-Encoding` header value contains only
227/// supported encodings.
228///
229/// `identity` (RFC 9110 §8.4) is a valid no-op coding and is accepted.
230///
231/// Returns `Ok(())` if the header is absent, empty, or all encodings
232/// are supported.
233///
234/// # Errors
235///
236/// Returns [`Error::UnsupportedContentEncoding`] if any encoding is
237/// unsupported.
238pub fn validate_content_encodings(header_value: &str) -> Result<()> {
239    for token in header_value.split(',') {
240        let token = token.trim();
241        if token.is_empty() || ContentCoding::is_identity(token) {
242            continue;
243        }
244        if ContentCoding::from_wire(token).is_none() {
245            return Err(Error::UnsupportedContentEncoding(token.to_string()));
246        }
247    }
248    Ok(())
249}
250
251/// Wrap a byte stream in decompression decoders based on the
252/// `Content-Encoding` header.
253///
254/// Decoders are applied in reverse order (innermost encoding first).
255/// For example, `Content-Encoding: gzip, br` means decode brotli
256/// first, then gzip.
257///
258/// Returns the original stream unchanged if there are no encodings
259/// to decode or if decompression is disabled.
260///
261/// # Errors
262///
263/// Returns [`Error::Decompression`] if the nesting depth exceeds the
264/// maximum or if a decoder fails. Returns
265/// [`Error::UnsupportedContentEncoding`] if any encoding is not
266/// supported by the compiled-in features. Returns
267/// [`Error::DecodedBodyTooLarge`] or
268/// [`Error::DecompressionRatioExceeded`] if the decoded body exceeds
269/// the configured limits.
270pub fn decompress_stream(
271    stream: BoxBytesStream,
272    content_encoding: Option<&str>,
273    decompression_enabled: bool,
274    limit: DecompressionLimit,
275) -> Result<BoxBytesStream> {
276    if !decompression_enabled {
277        return Ok(stream);
278    }
279
280    let header = match content_encoding {
281        Some(h) if !h.trim().is_empty() => h,
282        _ => return Ok(stream),
283    };
284
285    // Centralized validation so buffered and streaming paths handle invalid
286    // ratios identically (previously buffered treated NaN/inf/negative as
287    // unlimited while streaming errored per poll).
288    limit.validate()?;
289
290    // Validate the complete wire value before parsing recognized codings.
291    // This keeps unsupported encodings and nesting-depth errors ordered the
292    // same way for streaming and buffered bodies.
293    validate_content_encodings(header)?;
294
295    let Some(encodings) = parse_content_encodings(header) else {
296        return Ok(stream);
297    };
298
299    if encodings.len() > MAX_NESTING_DEPTH {
300        return Err(Error::Decompression(format!(
301            "content encoding nesting depth {} exceeds maximum {}",
302            encodings.len(),
303            MAX_NESTING_DEPTH
304        )));
305    }
306
307    // Apply decoders in reverse order (innermost encoding first).
308    //
309    // Threat model for stacked encodings (e.g., `gzip, gzip`):
310    // - Each layer's `CountingStream` measures that layer's compressed
311    //   input. The first decoder's counter is the wire size; inner
312    //   decoders' counters measure the outer layer's *decompressed* output.
313    // - Per-layer `LimitingStream`s enforce `max_decompression_ratio`
314    //   against each layer's own input (catches `gzip(gzip(huge))` where
315    //   inner expansion is huge but outer is small).
316    // - The final `LimitingStream` enforces both `max_decoded_body_size`
317    //   and `max_decompression_ratio` against the outermost wire size
318    //   (catches cumulative expansion that stays under per-layer limits).
319    // Both checks are required; removing either would let an attacker
320    // craft sizes that stay under the remaining threshold.
321    let mut current = stream;
322    let mut outer_counter = None;
323    for encoding in encodings.into_iter().rev() {
324        let counting = CountingStream::new(current);
325        let counter = counting.counter();
326        current = make_decoder(Box::pin(counting), encoding)?;
327        // The first decoder consumes the wire stream, so its counter is the
328        // only one that measures the original compressed body size.
329        if outer_counter.is_none() {
330            outer_counter = Some(Arc::clone(&counter));
331        }
332
333        // Check every decoder against the bytes consumed for its own input
334        // layer. Comparing only the final output with the outermost wire
335        // size lets stacked encodings hide a large intermediate expansion.
336        if let Some(max_ratio) = limit.max_decompression_ratio {
337            current = Box::pin(LimitingStream::new(
338                current,
339                DecompressionLimit {
340                    max_decoded_body_size: None,
341                    max_decompression_ratio: Some(max_ratio),
342                },
343                counter,
344            ));
345        }
346    }
347
348    if limit.is_unlimited() {
349        return Ok(current);
350    }
351
352    let Some(outer_counter) = outer_counter else {
353        return Ok(current);
354    };
355    Ok(Box::pin(LimitingStream::new(current, limit, outer_counter)))
356}
357
358/// Decompress a fully buffered byte slice using synchronous decoding.
359///
360/// Used for response bodies that have already been collected into memory.
361/// Supports gzip/deflate via `flate2` and, when the corresponding
362/// features are enabled, brotli (`brotli` crate) and zstd (`zstd` crate).
363///
364/// # Errors
365///
366/// Returns an error if the content encoding is unsupported or
367/// decompression fails. Returns [`Error::DecodedBodyTooLarge`] or
368/// [`Error::DecompressionRatioExceeded`] if the decoded body exceeds
369/// the configured limits.
370#[allow(clippy::too_many_lines)]
371pub fn decompress_buffered(
372    data: &[u8],
373    content_encoding: &str,
374    limit: DecompressionLimit,
375) -> Result<bytes::Bytes> {
376    // Validate decompression limit first so invalid ratios (NaN/inf/negative)
377    // are handled identically to the streaming path (previously buffered
378    // treated them as unlimited).
379    limit.validate()?;
380    // Validate before the pass-through so unsupported codings still fail.
381    validate_content_encodings(content_encoding)?;
382
383    let encodings = parse_content_encodings(content_encoding);
384    let Some(encodings) = encodings else {
385        // Validation accepted every token; reaching this arm means only
386        // no-op codings (e.g. bare `identity`) were present, so the body
387        // is served unchanged.
388        return Ok(bytes::Bytes::copy_from_slice(data));
389    };
390
391    if encodings.len() > MAX_NESTING_DEPTH {
392        return Err(Error::Decompression(format!(
393            "content encoding nesting depth {} exceeds maximum {}",
394            encodings.len(),
395            MAX_NESTING_DEPTH
396        )));
397    }
398
399    // Preserve the existing empty-body behavior while still routing the
400    // body through the same validation path as non-empty buffered bodies.
401    if data.is_empty() {
402        return Ok(bytes::Bytes::new());
403    }
404
405    let compressed_len = data.len();
406    let ratio_limit = limit.max_decompression_ratio.and_then(|ratio| {
407        if ratio.is_finite() && ratio > 0.0 {
408            #[allow(clippy::cast_precision_loss)]
409            let limit = (compressed_len as f64 * ratio).ceil();
410            #[allow(
411                clippy::cast_possible_truncation,
412                clippy::cast_sign_loss,
413                clippy::cast_precision_loss,
414                reason = "the finite non-negative ratio is clamped to the usize allocation limit"
415            )]
416            // `usize::MAX as f64` rounds up to 2^64, and float→int casts
417            // saturate, which would yield `usize::MAX - 1`. Saturate
418            // explicitly so the extreme budget keeps the full limit.
419            Some(if limit >= usize::MAX as f64 {
420                usize::MAX
421            } else {
422                limit.min(usize::MAX as f64) as usize
423            })
424        } else {
425            None
426        }
427    });
428    let output_limit = match (limit.max_decoded_body_size, ratio_limit) {
429        (Some(size), Some(ratio)) => Some(size.min(ratio)),
430        (Some(size), None) | (None, Some(size)) => Some(size),
431        (None, None) => None,
432    };
433    let ratio_is_tighter = ratio_limit.is_some_and(|ratio| Some(ratio) == output_limit);
434
435    #[allow(unused_mut)]
436    let mut current = bytes::Bytes::copy_from_slice(data);
437    #[allow(clippy::never_loop)]
438    for encoding in encodings.into_iter().rev() {
439        match encoding {
440            ContentCoding::Gzip | ContentCoding::Deflate => {
441                #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
442                {
443                    let layer_input_len = current.len();
444                    current =
445                        sync_decode_flate2(&current, encoding, output_limit, ratio_is_tighter)?;
446                    if let Some(max_ratio) = limit.max_decompression_ratio {
447                        if layer_input_len > 0 {
448                            #[allow(clippy::cast_precision_loss)]
449                            let layer_ratio = current.len() as f64 / layer_input_len as f64;
450                            if layer_ratio > max_ratio {
451                                return Err(Error::DecompressionRatioExceeded);
452                            }
453                        }
454                    }
455                }
456                #[cfg(not(any(feature = "compression-gzip", feature = "compression-deflate")))]
457                {
458                    return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
459                }
460            }
461            ContentCoding::Brotli => {
462                #[cfg(feature = "compression-brotli")]
463                {
464                    let layer_input_len = current.len();
465                    current = sync_decode_brotli(&current, output_limit, ratio_is_tighter)?;
466                    if let Some(max_ratio) = limit.max_decompression_ratio {
467                        if layer_input_len > 0 {
468                            #[allow(clippy::cast_precision_loss)]
469                            let layer_ratio = current.len() as f64 / layer_input_len as f64;
470                            if layer_ratio > max_ratio {
471                                return Err(Error::DecompressionRatioExceeded);
472                            }
473                        }
474                    }
475                }
476                #[cfg(not(feature = "compression-brotli"))]
477                {
478                    return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
479                }
480            }
481            ContentCoding::Zstd => {
482                #[cfg(feature = "compression-zstd")]
483                {
484                    let layer_input_len = current.len();
485                    current = sync_decode_zstd(&current, output_limit, ratio_is_tighter)?;
486                    if let Some(max_ratio) = limit.max_decompression_ratio {
487                        if layer_input_len > 0 {
488                            #[allow(clippy::cast_precision_loss)]
489                            let layer_ratio = current.len() as f64 / layer_input_len as f64;
490                            if layer_ratio > max_ratio {
491                                return Err(Error::DecompressionRatioExceeded);
492                            }
493                        }
494                    }
495                }
496                #[cfg(not(feature = "compression-zstd"))]
497                {
498                    return Err(Error::UnsupportedContentEncoding(encoding.as_str().into()));
499                }
500            }
501        }
502    }
503
504    // Final wire-size checks are mutually exclusive with the per-layer
505    // `output_limit` enforcement to keep the error kind stable when both
506    // limits are tight. When `output_limit` was ratio-derived, the size
507    // check is redundant (ratio*compressed < size) and would otherwise
508    // flip the error kind from `DecompressionRatioExceeded` to
509    // `DecodedBodyTooLarge` for the same logical violation.
510    let size_violated = limit
511        .max_decoded_body_size
512        .is_some_and(|max| current.len() > max);
513    let ratio_violated = limit.max_decompression_ratio.is_some_and(|max_ratio| {
514        compressed_len > 0 && {
515            #[allow(clippy::cast_precision_loss)]
516            let ratio = current.len() as f64 / compressed_len as f64;
517            ratio > max_ratio
518        }
519    });
520    if size_violated && ratio_violated {
521        if ratio_is_tighter {
522            return Err(Error::DecompressionRatioExceeded);
523        }
524        return Err(Error::DecodedBodyTooLarge);
525    }
526    if size_violated {
527        return Err(Error::DecodedBodyTooLarge);
528    }
529    if ratio_violated {
530        return Err(Error::DecompressionRatioExceeded);
531    }
532
533    Ok(current)
534}
535
536/// Incrementally drain a sync decoder, enforcing `output_limit` per chunk.
537///
538/// Replaces `take(limit+1)` + `read_to_end` so a large `output_limit` does
539/// not grow the output `Vec` to the full budget in one call: reads use a
540/// fixed 8 KiB stack buffer and fail fast once the cumulative output
541/// exceeds the limit.
542#[cfg(any(
543    feature = "compression-gzip",
544    feature = "compression-deflate",
545    feature = "compression-brotli",
546    feature = "compression-zstd"
547))]
548fn sync_read_limited<R: std::io::Read>(
549    mut reader: R,
550    output_limit: Option<usize>,
551    ratio_is_tighter: bool,
552) -> Result<bytes::Bytes> {
553    let mut output = Vec::new();
554    let mut buf = [0u8; 8 * 1024];
555    loop {
556        let n = reader
557            .read(&mut buf)
558            .map_err(|e| Error::Decompression(e.to_string()))?;
559        if n == 0 {
560            break;
561        }
562        output.extend_from_slice(&buf[..n]);
563        if output_limit.is_some_and(|limit| output.len() > limit) {
564            return Err(if ratio_is_tighter {
565                Error::DecompressionRatioExceeded
566            } else {
567                Error::DecodedBodyTooLarge
568            });
569        }
570    }
571    Ok(bytes::Bytes::from(output))
572}
573
574/// Synchronously decode a single layer of gzip or deflate compression using flate2.
575#[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
576fn sync_decode_flate2(
577    data: &bytes::Bytes,
578    encoding: ContentCoding,
579    output_limit: Option<usize>,
580    ratio_is_tighter: bool,
581) -> Result<bytes::Bytes> {
582    match encoding {
583        ContentCoding::Gzip => {
584            let decoder = flate2::read::GzDecoder::new(&data[..]);
585            sync_read_limited(decoder, output_limit, ratio_is_tighter)
586        }
587        ContentCoding::Deflate => {
588            // HTTP `deflate` (RFC 9110 §8.4.1.2) is the zlib wrapper
589            // (RFC 1950), but some servers send raw deflate (RFC 1951).
590            // Try the wrapper first, then fall back to the raw stream so
591            // lenient receivers decode both framings.
592            let zlib = flate2::read::ZlibDecoder::new(&data[..]);
593            if let ok @ Ok(_) = sync_read_limited(zlib, output_limit, ratio_is_tighter) {
594                ok
595            } else {
596                let raw = flate2::read::DeflateDecoder::new(&data[..]);
597                sync_read_limited(raw, output_limit, ratio_is_tighter)
598            }
599        }
600        _ => Err(Error::UnsupportedContentEncoding(
601            encoding.as_str().to_owned(),
602        )),
603    }
604}
605
606#[cfg(feature = "compression-brotli")]
607fn sync_decode_brotli(
608    data: &bytes::Bytes,
609    output_limit: Option<usize>,
610    ratio_is_tighter: bool,
611) -> Result<bytes::Bytes> {
612    let decoder = brotli::Decompressor::new(&data[..], 4096);
613    sync_read_limited(decoder, output_limit, ratio_is_tighter)
614}
615
616#[cfg(feature = "compression-zstd")]
617fn sync_decode_zstd(
618    data: &bytes::Bytes,
619    output_limit: Option<usize>,
620    ratio_is_tighter: bool,
621) -> Result<bytes::Bytes> {
622    let decoder = zstd::stream::read::Decoder::new(&data[..])
623        .map_err(|e| Error::Decompression(e.to_string()))?;
624    sync_read_limited(decoder, output_limit, ratio_is_tighter)
625}
626
627/// Returns `true` when `prefix` looks like an RFC 1950 zlib wrapper
628/// header: compression method 8 (deflate) with a valid FCHECK checksum.
629/// A raw deflate stream can pass this check by coincidence, so the check
630/// only selects the first decoder tried on the streaming path (the
631/// buffered path tries the wrapper and falls back to raw on failure).
632#[cfg(feature = "compression-deflate")]
633fn is_zlib_header(prefix: &[u8]) -> bool {
634    let [cmf, flg, ..] = prefix else {
635        return false;
636    };
637    let cmf = u16::from(*cmf);
638    let flg = u16::from(*flg);
639    cmf & 0x0F == 8 && (cmf * 256 + flg) % 31 == 0
640}
641
642/// Build the streaming `deflate` decoder with lazy framing selection.
643///
644/// HTTP `deflate` (RFC 9110 §8.4.1.2) is the zlib wrapper (RFC 1950), but
645/// some servers send raw deflate (RFC 1951). The first two bytes are
646/// sniffed once they arrive ([`is_zlib_header`]) and the decoder is
647/// selected then, so streaming never buffers the whole body for a decode
648/// retry. Split out of [`make_decoder`] to keep that function small.
649#[cfg(feature = "compression-deflate")]
650fn make_deflate_decoder(stream: BoxBytesStream) -> BoxBytesStream {
651    use async_compression::tokio::bufread::{DeflateDecoder, ZlibDecoder};
652    use futures_util::StreamExt;
653    use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};
654    use tokio_util::io::{ReaderStream, StreamReader};
655
656    enum DeflateState<R> {
657        Sniffing(R),
658        Decoding(ReaderStream<std::pin::Pin<Box<dyn AsyncRead + Send>>>),
659        Done,
660    }
661
662    let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
663    let reader = StreamReader::new(mapped);
664    let sniffed = futures_util::stream::unfold(
665        DeflateState::Sniffing(BufReader::new(reader)),
666        |mut state| async move {
667            loop {
668                match state {
669                    DeflateState::Sniffing(mut reader) => {
670                        let is_zlib = match reader.fill_buf().await {
671                            Ok(buf) => {
672                                if buf.is_empty() {
673                                    return None;
674                                }
675                                is_zlib_header(buf)
676                            }
677                            Err(e) => return Some((Err(e), DeflateState::Done)),
678                        };
679                        let decoder: std::pin::Pin<Box<dyn AsyncRead + Send>> = if is_zlib {
680                            Box::pin(ZlibDecoder::new(reader))
681                        } else {
682                            Box::pin(DeflateDecoder::new(reader))
683                        };
684                        state = DeflateState::Decoding(ReaderStream::new(decoder));
685                    }
686                    DeflateState::Decoding(mut inner) => {
687                        return inner
688                            .next()
689                            .await
690                            .map(|item| (item, DeflateState::Decoding(inner)));
691                    }
692                    DeflateState::Done => return None,
693                }
694            }
695        },
696    );
697    let stream = sniffed.map(|r| r.map_err(|e| Error::Decompression(e.to_string())));
698    Box::pin(stream)
699}
700
701/// Create a decoder for a single content coding.
702#[allow(
703    clippy::unnecessary_wraps,
704    clippy::needless_pass_by_value,
705    unused_variables
706)]
707fn make_decoder(stream: BoxBytesStream, encoding: ContentCoding) -> Result<BoxBytesStream> {
708    match encoding {
709        ContentCoding::Gzip => {
710            #[cfg(feature = "compression-gzip")]
711            {
712                use async_compression::tokio::bufread::GzipDecoder;
713                use futures_util::StreamExt;
714                use tokio::io::BufReader;
715                use tokio_util::io::{ReaderStream, StreamReader};
716
717                // Map locally to keep the conversion private; the decoder
718                // boundary keeps its explicit `Error::Decompression` text.
719                let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
720                let reader = StreamReader::new(mapped);
721                let decoder = GzipDecoder::new(BufReader::new(reader));
722                let stream = ReaderStream::new(decoder);
723                Ok(Box::pin(stream.map(|r| {
724                    r.map_err(|e| Error::Decompression(e.to_string()))
725                })))
726            }
727            #[cfg(not(feature = "compression-gzip"))]
728            {
729                Err(Error::UnsupportedContentEncoding("gzip".to_string()))
730            }
731        }
732        ContentCoding::Deflate => {
733            #[cfg(feature = "compression-deflate")]
734            {
735                Ok(make_deflate_decoder(stream))
736            }
737            #[cfg(not(feature = "compression-deflate"))]
738            {
739                Err(Error::UnsupportedContentEncoding("deflate".to_string()))
740            }
741        }
742        ContentCoding::Brotli => {
743            #[cfg(feature = "compression-brotli")]
744            {
745                use async_compression::tokio::bufread::BrotliDecoder;
746                use futures_util::StreamExt;
747                use tokio::io::BufReader;
748                use tokio_util::io::{ReaderStream, StreamReader};
749
750                let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
751                let reader = StreamReader::new(mapped);
752                let decoder = BrotliDecoder::new(BufReader::new(reader));
753                let stream = ReaderStream::new(decoder);
754                Ok(Box::pin(stream.map(|r| {
755                    r.map_err(|e| Error::Decompression(e.to_string()))
756                })))
757            }
758            #[cfg(not(feature = "compression-brotli"))]
759            {
760                Err(Error::UnsupportedContentEncoding("br".to_string()))
761            }
762        }
763        ContentCoding::Zstd => {
764            #[cfg(feature = "compression-zstd")]
765            {
766                use async_compression::tokio::bufread::ZstdDecoder;
767                use futures_util::StreamExt;
768                use tokio::io::BufReader;
769                use tokio_util::io::{ReaderStream, StreamReader};
770
771                let mapped = stream.map(|r| r.map_err(|e| std::io::Error::other(e.to_string())));
772                let reader = StreamReader::new(mapped);
773                let decoder = ZstdDecoder::new(BufReader::new(reader));
774                let stream = ReaderStream::new(decoder);
775                Ok(Box::pin(stream.map(|r| {
776                    r.map_err(|e| Error::Decompression(e.to_string()))
777                })))
778            }
779            #[cfg(not(feature = "compression-zstd"))]
780            {
781                Err(Error::UnsupportedContentEncoding("zstd".to_string()))
782            }
783        }
784    }
785}
786
787/// A stream wrapper that counts the compressed bytes yielded by the
788/// underlying stream. The count is exposed via a shared
789/// `Arc<AtomicUsize>` so that downstream limiters can read the
790/// compressed byte count without needing to see the raw stream.
791///
792/// For stacked decoders the *outer* `CountingStream` (first in the chain)
793/// counts wire bytes; inner counters count the outer layer's decompressed
794/// output. The outer counter is used for the final cumulative ratio
795/// check, while each inner counter drives its own per-layer ratio check
796/// (see `decompress_stream` docs).
797struct CountingStream {
798    inner: BoxBytesStream,
799    count: Arc<AtomicUsize>,
800}
801
802impl CountingStream {
803    fn new(inner: BoxBytesStream) -> Self {
804        Self {
805            inner,
806            count: Arc::new(AtomicUsize::new(0)),
807        }
808    }
809
810    fn counter(&self) -> Arc<AtomicUsize> {
811        Arc::clone(&self.count)
812    }
813}
814
815/// Atomically add `amount` to `counter`, saturating at `usize::MAX`
816/// instead of wrapping. A wrapped counter would under-report compressed
817/// bytes and silently disable a configured decompression-ratio limit
818/// mid-stream.
819fn saturating_fetch_add(counter: &AtomicUsize, amount: usize) {
820    let mut current = counter.load(Ordering::Acquire);
821    loop {
822        let Some(next) = current.checked_add(amount) else {
823            counter.store(usize::MAX, Ordering::Release);
824            return;
825        };
826        match counter.compare_exchange_weak(current, next, Ordering::AcqRel, Ordering::Acquire) {
827            Ok(_) => return,
828            Err(actual) => current = actual,
829        }
830    }
831}
832
833impl futures_core::Stream for CountingStream {
834    type Item = <BoxBytesStream as futures_core::Stream>::Item;
835
836    fn poll_next(
837        mut self: std::pin::Pin<&mut Self>,
838        cx: &mut std::task::Context<'_>,
839    ) -> std::task::Poll<Option<Self::Item>> {
840        use std::pin::Pin;
841
842        match Pin::new(&mut self.inner).poll_next(cx) {
843            std::task::Poll::Ready(Some(Ok(chunk))) => {
844                saturating_fetch_add(&self.count, chunk.len());
845                std::task::Poll::Ready(Some(Ok(chunk)))
846            }
847            other => other,
848        }
849    }
850}
851
852struct LimitingStream {
853    inner: BoxBytesStream,
854    limit: DecompressionLimit,
855    decoded_bytes: usize,
856    compressed_counter: Arc<AtomicUsize>,
857    limit_exceeded: Option<LimitExceededKind>,
858}
859
860/// Which limit tripped first; retained so later polls fail fast without
861/// re-polling the inner transport.
862#[derive(Debug, Clone, Copy, PartialEq, Eq)]
863enum LimitExceededKind {
864    Size,
865    Ratio,
866}
867
868impl LimitingStream {
869    fn new(
870        inner: BoxBytesStream,
871        limit: DecompressionLimit,
872        compressed_counter: Arc<AtomicUsize>,
873    ) -> Self {
874        Self {
875            inner,
876            limit,
877            decoded_bytes: 0,
878            compressed_counter,
879            limit_exceeded: None,
880        }
881    }
882
883    #[allow(clippy::cast_precision_loss)]
884    fn check_limit(&self) -> std::result::Result<(), LimitExceededKind> {
885        if let Some(max) = self.limit.max_decoded_body_size {
886            if self.decoded_bytes > max {
887                return Err(LimitExceededKind::Size);
888            }
889        }
890        if let Some(max_ratio) = self.limit.max_decompression_ratio {
891            if !max_ratio.is_finite() || max_ratio <= 0.0 {
892                // Invalid configuration surfaces as a decompression error;
893                // record it as a terminal ratio failure so we still fuse.
894                return Err(LimitExceededKind::Ratio);
895            }
896            let compressed = self.compressed_counter.load(Ordering::Acquire);
897            if compressed == 0 {
898                // No compressed bytes observed yet: any decoded output
899                // already exceeds any finite ratio, so fail closed instead
900                // of emitting an unbounded zip-bomb prefix up to the chunk size.
901                if self.decoded_bytes > 0 {
902                    return Err(LimitExceededKind::Ratio);
903                }
904            } else {
905                let ratio = self.decoded_bytes as f64 / compressed as f64;
906                if ratio > max_ratio {
907                    return Err(LimitExceededKind::Ratio);
908                }
909            }
910        }
911        Ok(())
912    }
913
914    fn kind_to_error(kind: LimitExceededKind, limit: &DecompressionLimit) -> Error {
915        match kind {
916            LimitExceededKind::Size => Error::DecodedBodyTooLarge,
917            LimitExceededKind::Ratio => {
918                // Preserve the invalid-ratio configuration message; all
919                // genuine ratio violations map to `DecompressionRatioExceeded`.
920                if limit
921                    .max_decompression_ratio
922                    .is_some_and(|r| !r.is_finite() || r <= 0.0)
923                {
924                    Error::Decompression(
925                        "invalid max_decompression_ratio: must be finite and positive".into(),
926                    )
927                } else {
928                    Error::DecompressionRatioExceeded
929                }
930            }
931        }
932    }
933}
934
935impl futures_core::Stream for LimitingStream {
936    type Item = Result<bytes::Bytes>;
937
938    fn poll_next(
939        mut self: std::pin::Pin<&mut Self>,
940        cx: &mut std::task::Context<'_>,
941    ) -> std::task::Poll<Option<Self::Item>> {
942        use std::pin::Pin;
943
944        // Once the limit trips, fuse without polling inner again (see
945        // `LimitedResponseStream`: `None` keeps `collect` terminating;
946        // callers already observed the first error).
947        if self.limit_exceeded.is_some() {
948            return std::task::Poll::Ready(None);
949        }
950
951        match Pin::new(&mut self.inner).poll_next(cx) {
952            std::task::Poll::Ready(Some(Ok(chunk))) => {
953                // Saturate instead of wrapping: a wrapped counter would
954                // permanently disable the limit mid-stream on targets
955                // where `usize` is narrower than the body size.
956                self.decoded_bytes = self.decoded_bytes.saturating_add(chunk.len());
957                if let Err(kind) = self.check_limit() {
958                    self.limit_exceeded = Some(kind);
959                    let err = Self::kind_to_error(kind, &self.limit);
960                    return std::task::Poll::Ready(Some(Err(err)));
961                }
962                std::task::Poll::Ready(Some(Ok(chunk)))
963            }
964            std::task::Poll::Ready(Some(Err(e))) => std::task::Poll::Ready(Some(Err(e))),
965            std::task::Poll::Ready(None) => std::task::Poll::Ready(None),
966            std::task::Poll::Pending => std::task::Poll::Pending,
967        }
968    }
969}
970
971#[cfg(test)]
972mod tests {
973    use super::*;
974    use futures_util::FutureExt;
975    #[cfg(feature = "compression-gzip")]
976    use futures_util::StreamExt;
977
978    #[test]
979    fn content_coding_from_wire() {
980        assert_eq!(ContentCoding::from_wire("gzip"), Some(ContentCoding::Gzip));
981        assert_eq!(
982            ContentCoding::from_wire("x-gzip"),
983            Some(ContentCoding::Gzip)
984        );
985        assert_eq!(
986            ContentCoding::from_wire("deflate"),
987            Some(ContentCoding::Deflate)
988        );
989        assert_eq!(ContentCoding::from_wire("br"), Some(ContentCoding::Brotli));
990        assert_eq!(ContentCoding::from_wire("zstd"), Some(ContentCoding::Zstd));
991        assert_eq!(ContentCoding::from_wire("identity"), None);
992        assert_eq!(ContentCoding::from_wire("unknown"), None);
993        assert_eq!(ContentCoding::from_wire("GZIP"), Some(ContentCoding::Gzip));
994        assert_eq!(
995            ContentCoding::from_wire(" Br "),
996            Some(ContentCoding::Brotli)
997        );
998    }
999
1000    #[test]
1001    fn content_coding_as_str() {
1002        assert_eq!(ContentCoding::Gzip.as_str(), "gzip");
1003        assert_eq!(ContentCoding::Deflate.as_str(), "deflate");
1004        assert_eq!(ContentCoding::Brotli.as_str(), "br");
1005        assert_eq!(ContentCoding::Zstd.as_str(), "zstd");
1006    }
1007
1008    #[test]
1009    fn parse_content_encodings_single() {
1010        let encs = parse_content_encodings("gzip").unwrap();
1011        assert_eq!(encs, vec![ContentCoding::Gzip]);
1012    }
1013
1014    #[test]
1015    fn parse_content_encodings_multiple() {
1016        let encs = parse_content_encodings("gzip, br").unwrap();
1017        assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1018    }
1019
1020    #[test]
1021    fn parse_content_encodings_with_whitespace() {
1022        let encs = parse_content_encodings("  gzip , br  ").unwrap();
1023        assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1024    }
1025
1026    #[test]
1027    fn parse_content_encodings_empty() {
1028        assert!(parse_content_encodings("").is_none());
1029        assert!(parse_content_encodings("  ").is_none());
1030    }
1031
1032    #[test]
1033    fn parse_content_encodings_unknown_filtered() {
1034        let encs = parse_content_encodings("gzip, identity, br").unwrap();
1035        assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1036    }
1037
1038    #[test]
1039    fn validate_content_encodings_supported() {
1040        assert!(validate_content_encodings("gzip").is_ok());
1041        assert!(validate_content_encodings("gzip, br").is_ok());
1042        assert!(validate_content_encodings("").is_ok());
1043    }
1044
1045    #[test]
1046    fn validate_content_encodings_accepts_identity() {
1047        // RFC 9110 §8.4: `identity` is a valid no-op content-coding.
1048        assert!(validate_content_encodings("identity").is_ok());
1049        assert!(validate_content_encodings("IDENTITY").is_ok());
1050        assert!(validate_content_encodings("identity, gzip").is_ok());
1051        assert!(validate_content_encodings("gzip, identity").is_ok());
1052    }
1053
1054    #[test]
1055    fn parse_content_encodings_identity_only_is_none() {
1056        assert!(parse_content_encodings("identity").is_none());
1057        let encs = parse_content_encodings("gzip, identity, br").unwrap();
1058        assert_eq!(encs, vec![ContentCoding::Gzip, ContentCoding::Brotli]);
1059    }
1060
1061    #[test]
1062    fn validate_content_encodings_unsupported() {
1063        let err = validate_content_encodings("gzip, weird").unwrap_err();
1064        assert_eq!(err.kind(), "unsupported_content_encoding");
1065        assert!(err.to_string().contains("weird"));
1066    }
1067
1068    #[test]
1069    fn accept_encoding_value_requires_compiled_features() {
1070        let _ = accept_encoding_value();
1071    }
1072
1073    #[cfg(not(any(
1074        feature = "compression-gzip",
1075        feature = "compression-deflate",
1076        feature = "compression-brotli",
1077        feature = "compression-zstd"
1078    )))]
1079    #[test]
1080    fn accept_encoding_value_none_without_compression_features() {
1081        assert!(
1082            accept_encoding_value().is_none(),
1083            "accept_encoding_value() should return None when no compression features are compiled"
1084        );
1085    }
1086
1087    #[cfg(all(
1088        feature = "compression-gzip",
1089        not(feature = "compression-brotli"),
1090        not(feature = "compression-zstd")
1091    ))]
1092    #[test]
1093    fn accept_encoding_value_gzip_only() {
1094        let val = accept_encoding_value().unwrap();
1095        assert!(val.contains("gzip"), "should contain gzip, got: {val}");
1096        assert!(
1097            !val.contains("br"),
1098            "should not contain br without brotli feature, got: {val}"
1099        );
1100        assert!(
1101            !val.contains("zstd"),
1102            "should not contain zstd without zstd feature, got: {val}"
1103        );
1104    }
1105
1106    #[test]
1107    fn accept_encoding_value_deterministic_order() {
1108        let val1 = accept_encoding_value();
1109        let val2 = accept_encoding_value();
1110        assert_eq!(
1111            val1, val2,
1112            "accept_encoding_value() should return the same value across calls"
1113        );
1114    }
1115
1116    #[test]
1117    fn accept_encoding_advertises_only_supported_decoders() {
1118        // Every coding advertised in Accept-Encoding must have a working
1119        // streaming decoder for the compiled-in feature set; otherwise a
1120        // compliant server can produce responses we cannot decode.
1121        let Some(value) = accept_encoding_value() else {
1122            return;
1123        };
1124        for token in value.split(", ") {
1125            let coding =
1126                ContentCoding::from_wire(token).unwrap_or_else(|| panic!("invalid token {token}"));
1127            let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1128            let result = make_decoder(stream, coding);
1129            assert!(
1130                result.is_ok(),
1131                "advertised encoding '{token}' has no compiled-in decoder"
1132            );
1133        }
1134    }
1135
1136    #[test]
1137    fn decompress_stream_no_encoding_returns_original() {
1138        let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1139        let result = decompress_stream(stream, None, true, DecompressionLimit::new()).unwrap();
1140        drop(result);
1141    }
1142
1143    #[test]
1144    fn decompress_stream_disabled_returns_original() {
1145        let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1146        let result =
1147            decompress_stream(stream, Some("gzip"), false, DecompressionLimit::new()).unwrap();
1148        drop(result);
1149    }
1150
1151    #[test]
1152    fn decompress_stream_empty_header_returns_original() {
1153        let stream: BoxBytesStream = Box::pin(futures_util::stream::empty());
1154        let result =
1155            decompress_stream(stream, Some("  "), true, DecompressionLimit::new()).unwrap();
1156        drop(result);
1157    }
1158
1159    #[test]
1160    fn decompress_buffered_identity_passthrough() {
1161        // RFC 9110 §8.4: `identity` means "no encoding"; the body must be
1162        // served unchanged instead of raising UnsupportedContentEncoding.
1163        assert_eq!(
1164            &decompress_buffered(b"plain body", "identity", DecompressionLimit::new()).unwrap()[..],
1165            b"plain body"
1166        );
1167        let err = decompress_buffered(b"x", "weird", DecompressionLimit::new()).unwrap_err();
1168        assert_eq!(err.kind(), "unsupported_content_encoding");
1169    }
1170
1171    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1172    #[test]
1173    fn sync_decode_flate2_rejects_non_flate_encoding() {
1174        let err = sync_decode_flate2(&bytes::Bytes::new(), ContentCoding::Brotli, None, false)
1175            .unwrap_err();
1176        assert_eq!(err.kind(), "unsupported_content_encoding");
1177        assert!(err.to_string().contains("br"));
1178    }
1179
1180    #[test]
1181    fn decompression_limit_new_is_unlimited() {
1182        let limit = DecompressionLimit::new();
1183        assert!(limit.is_unlimited());
1184        assert!(limit.max_decoded_body_size.is_none());
1185        assert!(limit.max_decompression_ratio.is_none());
1186    }
1187
1188    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1189    #[test]
1190    fn decompression_limit_max_size_rejects() {
1191        let data = gzip_compress(b"hello world");
1192        let limit = DecompressionLimit {
1193            max_decoded_body_size: Some(5),
1194            max_decompression_ratio: None,
1195        };
1196        let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
1197        assert_eq!(err.kind(), "decoded_body_too_large");
1198    }
1199
1200    #[cfg(feature = "compression-deflate")]
1201    #[test]
1202    fn deflate_buffered_decodes_zlib_wrapper_and_raw() {
1203        use std::io::Write;
1204        let plain = b"hello deflate world, hello again";
1205
1206        let mut zlib = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
1207        zlib.write_all(plain).unwrap();
1208        let zlib = zlib.finish().unwrap();
1209        assert!(is_zlib_header(&zlib));
1210        let result = decompress_buffered(&zlib, "deflate", DecompressionLimit::new()).unwrap();
1211        assert_eq!(&result[..], &plain[..]);
1212
1213        let mut raw =
1214            flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default());
1215        raw.write_all(plain).unwrap();
1216        let raw = raw.finish().unwrap();
1217        let result = decompress_buffered(&raw, "deflate", DecompressionLimit::new()).unwrap();
1218        assert_eq!(&result[..], &plain[..]);
1219    }
1220
1221    #[cfg(feature = "compression-deflate")]
1222    #[test]
1223    fn zlib_header_sniff_classifies_framing() {
1224        assert!(!is_zlib_header(&[]));
1225        assert!(!is_zlib_header(&[0x78]));
1226        assert!(!is_zlib_header(&[0x1f, 0x8b])); // gzip magic
1227        assert!(is_zlib_header(&[0x78, 0x9c, 0x03])); // default zlib header
1228    }
1229
1230    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1231    #[test]
1232    fn decompression_limit_max_size_allows_within() {
1233        let data = gzip_compress(b"hi");
1234        let limit = DecompressionLimit {
1235            max_decoded_body_size: Some(1024),
1236            max_decompression_ratio: None,
1237        };
1238        let result = decompress_buffered(&data, "gzip", limit).unwrap();
1239        assert_eq!(&result[..], b"hi");
1240    }
1241
1242    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1243    #[test]
1244    fn decompression_limit_ratio_rejects() {
1245        let data = gzip_compress(b"hello world, this is a test of compression");
1246        let limit = DecompressionLimit {
1247            max_decoded_body_size: None,
1248            max_decompression_ratio: Some(0.5),
1249        };
1250        let err = decompress_buffered(&data, "gzip", limit).unwrap_err();
1251        assert_eq!(err.kind(), "decompression_ratio_exceeded");
1252    }
1253
1254    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1255    #[test]
1256    fn decompression_limit_ratio_allows_within() {
1257        let data = gzip_compress(b"hello world, this is a test of compression");
1258        let limit = DecompressionLimit {
1259            max_decoded_body_size: None,
1260            max_decompression_ratio: Some(100.0),
1261        };
1262        let result = decompress_buffered(&data, "gzip", limit).unwrap();
1263        assert_eq!(&result[..], b"hello world, this is a test of compression");
1264    }
1265
1266    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1267    #[test]
1268    fn decompression_limit_unlimited_allows() {
1269        let data = gzip_compress(b"hello world");
1270        let limit = DecompressionLimit::new();
1271        let result = decompress_buffered(&data, "gzip", limit).unwrap();
1272        assert_eq!(&result[..], b"hello world");
1273    }
1274
1275    #[cfg(feature = "compression-gzip")]
1276    #[test]
1277    fn decompression_limit_ratio_enforced_per_layer() {
1278        // Stacked encodings: the middle layer stores near-incompressible
1279        // data, so the cumulative expansion measured against the wire size
1280        // is *smaller* than the inner layer's own expansion. A per-layer
1281        // limit between the two must reject even though the old
1282        // final-size-vs-outermost-length check would have passed.
1283        let payload = vec![b'a'; 5000];
1284        let inner = gzip_compress(&payload);
1285        let outer = gzip_compress(&inner);
1286        #[allow(clippy::cast_precision_loss)]
1287        let (total_ratio, inner_step) = {
1288            (
1289                payload.len() as f64 / outer.len() as f64,
1290                payload.len() as f64 / inner.len() as f64,
1291            )
1292        };
1293        assert!(
1294            inner_step > total_ratio + 1.0,
1295            "expected the inner step to dominate: {inner_step} vs {total_ratio}"
1296        );
1297
1298        #[allow(clippy::cast_precision_loss)]
1299        let limit_value = total_ratio.midpoint(inner_step).ceil();
1300        let limit = DecompressionLimit {
1301            max_decoded_body_size: None,
1302            max_decompression_ratio: Some(limit_value),
1303        };
1304        let err = decompress_buffered(&outer, "gzip, gzip", limit).unwrap_err();
1305        assert_eq!(err.kind(), "decompression_ratio_exceeded");
1306
1307        // With no ratio limit both layers decode fine.
1308        let ok = decompress_buffered(&outer, "gzip, gzip", DecompressionLimit::default()).unwrap();
1309        assert_eq!(&ok[..], &payload[..]);
1310    }
1311
1312    #[cfg(feature = "compression-gzip")]
1313    #[tokio::test]
1314    async fn decompression_stream_enforces_ratio_per_layer() {
1315        let payload = vec![b'a'; 5000];
1316        let inner = gzip_compress(&payload);
1317        let outer = gzip_compress(&inner);
1318        #[allow(clippy::cast_precision_loss)]
1319        let limit_value = (payload.len() as f64 / outer.len() as f64)
1320            .midpoint(payload.len() as f64 / inner.len() as f64);
1321        let limit = DecompressionLimit {
1322            max_decoded_body_size: None,
1323            max_decompression_ratio: Some(limit_value.ceil()),
1324        };
1325        let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
1326            Ok(bytes::Bytes::from(outer))
1327        }));
1328        let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
1329        let err = decoded
1330            .collect::<Vec<_>>()
1331            .await
1332            .into_iter()
1333            .find_map(std::result::Result::err)
1334            .expect("streaming ratio limit must reject the inner layer");
1335        assert_eq!(err.kind(), "decompression_ratio_exceeded");
1336    }
1337
1338    #[cfg(feature = "compression-gzip")]
1339    #[tokio::test]
1340    async fn decompression_stream_enforces_ratio_against_wire_size() {
1341        // Both individual layers stay below the limit, but their cumulative
1342        // expansion exceeds it. The final check must use the original wire
1343        // size rather than the inner layer's size.
1344        let payload: Vec<u8> = (0usize..5000)
1345            .map(|i| u8::try_from((i * 73 + i / 17) % 251).expect("value is below 251"))
1346            .collect();
1347        let inner = gzip_compress(&payload);
1348        let outer = gzip_compress(&inner);
1349        #[allow(clippy::cast_precision_loss)]
1350        let (first_layer, second_layer, total) = (
1351            inner.len() as f64 / outer.len() as f64,
1352            payload.len() as f64 / inner.len() as f64,
1353            payload.len() as f64 / outer.len() as f64,
1354        );
1355        let per_layer_limit = first_layer.max(second_layer);
1356        assert!(total > per_layer_limit);
1357        let limit = DecompressionLimit {
1358            max_decoded_body_size: None,
1359            max_decompression_ratio: Some(per_layer_limit.midpoint(total)),
1360        };
1361        let input: BoxBytesStream = Box::pin(futures_util::stream::once(async move {
1362            Ok(bytes::Bytes::from(outer))
1363        }));
1364        let decoded = decompress_stream(input, Some("gzip, gzip"), true, limit).unwrap();
1365        let err = decoded
1366            .collect::<Vec<_>>()
1367            .await
1368            .into_iter()
1369            .find_map(std::result::Result::err)
1370            .expect("streaming ratio limit must include the wire size");
1371        assert_eq!(err.kind(), "decompression_ratio_exceeded");
1372    }
1373
1374    #[test]
1375    fn limiting_stream_enforces_max_decoded_body_size() {
1376        let limit = DecompressionLimit {
1377            max_decoded_body_size: Some(3),
1378            max_decompression_ratio: None,
1379        };
1380        let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
1381            Ok(bytes::Bytes::from("ab")),
1382            Ok(bytes::Bytes::from("cd")),
1383        ]));
1384        let counter = Arc::new(AtomicUsize::new(0));
1385        let mut limited = LimitingStream::new(stream, limit, counter);
1386
1387        let chunk1 = futures_util::StreamExt::next(&mut limited)
1388            .now_or_never()
1389            .unwrap()
1390            .unwrap()
1391            .unwrap();
1392        assert_eq!(chunk1, "ab");
1393
1394        let err = futures_util::StreamExt::next(&mut limited)
1395            .now_or_never()
1396            .unwrap()
1397            .unwrap()
1398            .unwrap_err();
1399        assert_eq!(err.kind(), "decoded_body_too_large");
1400    }
1401
1402    #[test]
1403    fn limiting_stream_allows_within_limit() {
1404        let limit = DecompressionLimit {
1405            max_decoded_body_size: Some(1024),
1406            max_decompression_ratio: None,
1407        };
1408        let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![
1409            Ok(bytes::Bytes::from("a")),
1410            Ok(bytes::Bytes::from("b")),
1411        ]));
1412        let counter = Arc::new(AtomicUsize::new(0));
1413        let mut limited = LimitingStream::new(stream, limit, counter);
1414
1415        let chunk1 = futures_util::StreamExt::next(&mut limited)
1416            .now_or_never()
1417            .unwrap()
1418            .unwrap()
1419            .unwrap();
1420        assert_eq!(chunk1, "a");
1421        let chunk2 = futures_util::StreamExt::next(&mut limited)
1422            .now_or_never()
1423            .unwrap()
1424            .unwrap()
1425            .unwrap();
1426        assert_eq!(chunk2, "b");
1427    }
1428
1429    #[test]
1430    fn saturating_fetch_add_saturates_instead_of_wrapping() {
1431        let counter = AtomicUsize::new(usize::MAX - 2);
1432        saturating_fetch_add(&counter, 1);
1433        assert_eq!(counter.load(Ordering::Relaxed), usize::MAX - 1);
1434        saturating_fetch_add(&counter, 5);
1435        assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
1436        // Further adds stay pinned at the maximum.
1437        saturating_fetch_add(&counter, 5);
1438        assert_eq!(counter.load(Ordering::Relaxed), usize::MAX);
1439    }
1440
1441    #[test]
1442    fn limiting_stream_saturated_counter_still_enforces_size_limit() {
1443        let limit = DecompressionLimit {
1444            max_decoded_body_size: Some(10),
1445            max_decompression_ratio: None,
1446        };
1447        let stream: BoxBytesStream = Box::pin(futures_util::stream::iter(vec![Ok(
1448            bytes::Bytes::from("x"),
1449        )]));
1450        let counter = Arc::new(AtomicUsize::new(0));
1451        let mut limited = LimitingStream::new(stream, limit, counter);
1452        // Simulate a saturated counter (e.g. >4 GiB decoded on a 32-bit
1453        // target): the size limit must still trip instead of wrapping to a
1454        // small value and passing.
1455        limited.decoded_bytes = usize::MAX;
1456        let err = futures_util::StreamExt::next(&mut limited)
1457            .now_or_never()
1458            .unwrap()
1459            .unwrap()
1460            .unwrap_err();
1461        assert_eq!(err.kind(), "decoded_body_too_large");
1462    }
1463
1464    #[cfg(any(feature = "compression-gzip", feature = "compression-deflate"))]
1465    fn gzip_compress(data: &[u8]) -> Vec<u8> {
1466        use flate2::write::GzEncoder;
1467        use flate2::Compression;
1468        use std::io::Write;
1469
1470        let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
1471        encoder.write_all(data).unwrap();
1472        encoder.finish().unwrap()
1473    }
1474}