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