forge
Explore
Log in

Don't you dare go Hollow

in-web: ignore syntactically invalid Range headers; back_to() validates Referer

Browse files at this commit

62387971625d701d3f438f13a6d5868a5cf93a0bAhmet Kaan GümüşParent: dcf889a
- 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.rsmodified+13 -6
@@ -126,8 +126,9 @@ fn disposition_of(file_name: &str, inline: bool) -> String {
126 /// as `(start, end)` inclusive. `None` for anything this does not parse as126 /// as `(start, end)` inclusive. `None` for anything this does not parse as
127 /// exactly one `bytes=` range (a multi-range header included) — the caller127 /// exactly one `bytes=` range (a multi-range header included) — the caller
128 /// falls back to a full `200`, which is always a valid answer to a `Range`128 /// falls back to a full `200`, which is always a valid answer to a `Range`
129-/// request. `Err(())` for an unsatisfiable range, so the caller can answer129+/// request. Syntactically invalid ranges (`bytes=5-2`, `bytes=-0`) are not
130-/// `416` instead of serving nonsense bytes.130+/// range sets at all, so the header is ignored per RFC 7233; `Err(())` is
131+/// kept for a well-formed range that simply doesn't fit, answered `416`.
131 fn parse_range(header: &str, total: u64) -> Option<std::result::Result<(u64, u64), ()>> {132 fn parse_range(header: &str, total: u64) -> Option<std::result::Result<(u64, u64), ()>> {
132 let spec = header.strip_prefix("bytes=")?;133 let spec = header.strip_prefix("bytes=")?;
133 if spec.contains(',') {134 if spec.contains(',') {
@@ -138,7 +139,8 @@ fn parse_range(header: &str, total: u64) -> Option<std::result::Result<(u64, u64
138 // A suffix range: the last N bytes.139 // A suffix range: the last N bytes.
139 let suffix: u64 = end.parse().ok()?;140 let suffix: u64 = end.parse().ok()?;
140 if suffix == 0 || total == 0 {141 if suffix == 0 || total == 0 {
141- return Some(Err(()));142+ // `bytes=-0` names no bytes: not a range set, header ignored.
143+ return None;
142 }144 }
143 let start = total.saturating_sub(suffix);145 let start = total.saturating_sub(suffix);
144 return Some(Ok((start, total - 1)));146 return Some(Ok((start, total - 1)));
@@ -151,9 +153,14 @@ fn parse_range(header: &str, total: u64) -> Option<std::result::Result<(u64, u64
151 return Some(Ok((start, total - 1)));153 return Some(Ok((start, total - 1)));
152 }154 }
153 let end: u64 = end.parse().ok()?;155 let end: u64 = end.parse().ok()?;
154- if start > end || start >= total {156+ // `start > end` is not a range set: ignore. A well-formed range that
155- return Some(Err(()));157+ // starts past the body is the one true `416`.
156- }158+ if start > end {
159+ return None;
160+ }
161+ if start >= total {
162+ return Some(Err(()));
163+ }
157 Some(Ok((start, end.min(total - 1))))164 Some(Ok((start, end.min(total - 1))))
158 }165 }
159 166
crates/in-web/src/server.rsmodified+8 -0
@@ -612,6 +612,14 @@ pub fn back_to(cx: &Cx, nowhere: &str) -> String {
612 .get(header::REFERER)612 .get(header::REFERER)
613 .and_then(|value| value.to_str().ok())613 .and_then(|value| value.to_str().ok())
614 .unwrap_or(nowhere);614 .unwrap_or(nowhere);
615+ // The Referer is attacker-influenceable input on its way into a
616+ // Location header: only a local path rides it — an absolute URL, a
617+ // protocol-relative `//`, or a backslash (browsers normalize it to a
618+ // slash) all fall back to the caller's safe target.
619+ let referer = match referer {
620+ r if r.starts_with('/') && !r.starts_with("//") && !r.contains('\\') => r,
621+ _ => nowhere,
622+ };
615 let (path, query) = referer.split_once('?').unwrap_or((referer, ""));623 let (path, query) = referer.split_once('?').unwrap_or((referer, ""));
616 let pairs: Vec<&str> = query624 let pairs: Vec<&str> = query
617 .split('&')625 .split('&')