From 62387971625d701d3f438f13a6d5868a5cf93a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 11 Sep 2026 10:21:56 +0300 Subject: [PATCH] in-web: ignore syntactically invalid Range headers; back_to() validates Referer - parse_range: bytes=5-2 / bytes=-0 are not range sets -> header ignored (200), only well-formed-but-unsatisfiable keeps 416 (RFC 7233) - back_to: absolute/protocol-relative/backslash Referers fall back to the safe target instead of riding into Location --- crates/in-web/src/files.rs | 19 +++++++++++++------ crates/in-web/src/server.rs | 8 ++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/crates/in-web/src/files.rs b/crates/in-web/src/files.rs index 01eae7b..c1d6021 100644 --- a/crates/in-web/src/files.rs +++ b/crates/in-web/src/files.rs @@ -126,8 +126,9 @@ fn disposition_of(file_name: &str, inline: bool) -> String { /// as `(start, end)` inclusive. `None` for anything this does not parse as /// exactly one `bytes=` range (a multi-range header included) — the caller /// falls back to a full `200`, which is always a valid answer to a `Range` -/// request. `Err(())` for an unsatisfiable range, so the caller can answer -/// `416` instead of serving nonsense bytes. +/// request. Syntactically invalid ranges (`bytes=5-2`, `bytes=-0`) are not +/// range sets at all, so the header is ignored per RFC 7233; `Err(())` is +/// kept for a well-formed range that simply doesn't fit, answered `416`. fn parse_range(header: &str, total: u64) -> Option> { let spec = header.strip_prefix("bytes=")?; if spec.contains(',') { @@ -138,7 +139,8 @@ fn parse_range(header: &str, total: u64) -> Option Option end || start >= total { - return Some(Err(())); - } + // `start > end` is not a range set: ignore. A well-formed range that + // starts past the body is the one true `416`. + if start > end { + return None; + } + if start >= total { + return Some(Err(())); + } Some(Ok((start, end.min(total - 1)))) } diff --git a/crates/in-web/src/server.rs b/crates/in-web/src/server.rs index 1d1de38..9633af7 100644 --- a/crates/in-web/src/server.rs +++ b/crates/in-web/src/server.rs @@ -612,6 +612,14 @@ pub fn back_to(cx: &Cx, nowhere: &str) -> String { .get(header::REFERER) .and_then(|value| value.to_str().ok()) .unwrap_or(nowhere); + // The Referer is attacker-influenceable input on its way into a + // Location header: only a local path rides it — an absolute URL, a + // protocol-relative `//`, or a backslash (browsers normalize it to a + // slash) all fall back to the caller's safe target. + let referer = match referer { + r if r.starts_with('/') && !r.starts_with("//") && !r.contains('\\') => r, + _ => nowhere, + }; let (path, query) = referer.split_once('?').unwrap_or((referer, "")); let pairs: Vec<&str> = query .split('&')