1use 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#[derive(Debug, Clone, Copy, Default)]
35pub struct DecompressionLimit {
36 pub(crate) max_decoded_body_size: Option<usize>,
38 pub(crate) max_decompression_ratio: Option<f64>,
41}
42
43impl DecompressionLimit {
44 #[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 #[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 #[must_use]
61 pub fn max_decoded_body_size(&self) -> Option<usize> {
62 self.max_decoded_body_size
63 }
64
65 #[must_use]
67 pub fn max_decompression_ratio(&self) -> Option<f64> {
68 self.max_decompression_ratio
69 }
70
71 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 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
113const MAX_NESTING_DEPTH: usize = 4;
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum ContentCoding {
121 Gzip,
123 Deflate,
126 Brotli,
128 Zstd,
130}
131
132impl ContentCoding {
133 #[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 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 fn is_identity(token: &str) -> bool {
158 token.trim().eq_ignore_ascii_case("identity")
159 }
160}
161
162#[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 #[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
203pub(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
232pub 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
257pub 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 limit.validate()?;
295
296 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 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 if outer_counter.is_none() {
336 outer_counter = Some(Arc::clone(&counter));
337 }
338
339 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#[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 limit.validate()?;
389 validate_content_encodings(content_encoding)?;
391
392 let encodings = parse_content_encodings(content_encoding);
393 let Some(encodings) = encodings else {
394 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 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 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(¤t, 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(¤t, 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(¤t, 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 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#[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#[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 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#[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#[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#[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 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
818struct 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
846fn 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#[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 return Err(LimitExceededKind::Ratio);
929 }
930 let compressed = self.compressed_counter.load(Ordering::Acquire);
931 if compressed == 0 {
932 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 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 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 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 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 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 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])); assert!(is_zlib_header(&[0x78, 0x9c, 0x03])); }
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 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 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 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 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 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}