LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Reject signed numbers in Content-Range parsing
NetterSilver73f0d2540512
diff --git a/crates/fetch/src/lib.rs b/crates/fetch/src/lib.rsindex 12daff541..07552e4bb 100644--- a/crates/fetch/src/lib.rs+++ b/crates/fetch/src/lib.rs@@ -466,16 +466,25 @@ fn parse_content_range(v: &str) -> Option<(u64, Option<u64>)> {let v = v.trim().strip_prefix("bytes")?.trim();let (range, total) = v.split_once('/')?;let (a, b) = range.split_once('-')?;- let (a, b) = (a.trim().parse::<u64>().ok()?, b.trim().parse::<u64>().ok()?);+ let (a, b) = (digits(a)?, digits(b)?);if b < a {return None;}let total = match total.trim() {"*" => None,- t => Some(t.parse::<u64>().ok()?),+ t => Some(digits(t)?),};Some((a, total))}+/// A non-empty run of ASCII digits as a `u64` (no sign, unlike `str::parse`).+fn digits(s: &str) -> Option<u64> {+ let s = s.trim();+ if s.is_empty() || !s.bytes().all(|b| b.is_ascii_digit()) {+ return None;+ }+ s.parse().ok()+}+#[cfg(test)]mod tests;diff --git a/crates/fetch/src/tests.rs b/crates/fetch/src/tests.rsindex 08f869dcc..2a9c91f4f 100644--- a/crates/fetch/src/tests.rs+++ b/crates/fetch/src/tests.rs@@ -335,6 +335,10 @@ fn content_ranges() {assert_eq!(parse_content_range("bytes 0-0/*"), Some((0, None)));assert_eq!(parse_content_range("bytes 9-1/300"), None);assert_eq!(parse_content_range("items 1-2/3"), None);+ assert_eq!(parse_content_range("bytes +100-+299/+300"), None);+ assert_eq!(parse_content_range("bytes 100-+299/300"), None);+ assert_eq!(parse_content_range("bytes 100-299/+300"), None);+ assert_eq!(parse_content_range("bytes 1-/3"), None);}/// A bearer token goes only to https, only to the host the download began at, and only when there is one.