From 7c090d7da48a9e441a8bffab4514791516a3a2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Tue, 24 Oct 2023 11:13:36 +0200 Subject: [PATCH] Don't panic if chunked encoding contains no chunk length --- src/response.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/response.rs b/src/response.rs index a7e36f6..01e0c16 100644 --- a/src/response.rs +++ b/src/response.rs @@ -482,14 +482,13 @@ where return Err(Error::Codec); } } else { + if total_read == 0 || header_buf[total_read - 1] != b'\r' { + return Err(Error::Codec); + } break 'read_size; } } - if header_buf[total_read - 1] != b'\r' { - return Err(Error::Codec); - } - let hex_digits = total_read - 1; // Prepend hex with zeros @@ -687,6 +686,7 @@ mod tests { reader::BufferingReader, request::Method, response::{ChunkState, ChunkedBodyReader, Response}, + Error, }; #[tokio::test] @@ -727,6 +727,21 @@ mod tests { assert_eq!(11, response.body().discard().await.unwrap()); } + #[tokio::test] + async fn incorrect_fragment_length_does_not_panic() { + let mut response = + b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n\n\r\nHELLO WORLD\r\n0\r\n\r\n".as_slice(); + let mut header_buf = [0; 200]; + + let response = Response::read(&mut response, Method::GET, &mut header_buf) + .await + .unwrap(); + + let error = response.body().read_to_end().await.unwrap_err(); + + assert!(matches!(error, Error::Codec)) + } + #[tokio::test] async fn can_read_with_chunked_encoding() { let mut response =