From b8473aa45e30494899c932120130e5ea6eec6bae Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Tue, 27 Jun 2023 23:16:07 +0200 Subject: [PATCH] fix: potential panic when invalid content range it handles a "potential" panic when content rages are invalid replying with a `416 Range Not Satisfiable` --- src/static_files.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/static_files.rs b/src/static_files.rs index ca9e602..9cb04e4 100644 --- a/src/static_files.rs +++ b/src/static_files.rs @@ -571,7 +571,17 @@ async fn response_body( if sub_len != len { *resp.status_mut() = StatusCode::PARTIAL_CONTENT; resp.headers_mut().typed_insert( - ContentRange::bytes(start..end, len).expect("valid ContentRange"), + match ContentRange::bytes(start..end, len) { + Ok(range) => range, + Err(err) => { + tracing::error!("invalid content range error: {:?}", err); + let mut resp = Response::new(Body::empty()); + *resp.status_mut() = StatusCode::RANGE_NOT_SATISFIABLE; + resp.headers_mut() + .typed_insert(ContentRange::unsatisfied_bytes(len)); + return Ok(resp); + } + }, ); len = sub_len; -- libgit2 1.7.2