| @@ -94,9 +94,13 @@ pub(crate) fn parse_head(buffer: &[u8], at: usize) -> Head<'_> { |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | /// A `Content-Length` value: decimal digits only (no sign), a small `Int`, |
| 97 | | −/// once surrounding whitespace is trimmed. |
| 97 | +/// once surrounding spaces and tabs are trimmed. Only those (RFC 9110's |
| 98 | +/// optional whitespace): trimming Unicode whitespace too would read |
| 99 | +/// `\u{a0}5` as 5, where a proxy in front may not (request smuggling). |
| 98 | 100 | fn decimal_length(value: &[u8]) -> Option<i64> { |
| 99 | | − let value = std::str::from_utf8(value).unwrap_or("").trim(); |
| 101 | + let value = std::str::from_utf8(value) |
| 102 | + .unwrap_or("") |
| 103 | + .trim_matches([' ', '\t']); |
| 100 | 104 | if value.is_empty() || !value.bytes().all(|b| b.is_ascii_digit()) { |
| 101 | 105 | return None; |
| 102 | 106 | } |
| @@ -305,6 +309,9 @@ mod tests { |
| 305 | 309 | "5,5", |
| 306 | 310 | "1e3", |
| 307 | 311 | "٣", |
| 312 | + // Whitespace other than spaces and tabs: a no-break space. |
| 313 | + "\u{a0}5", |
| 314 | + "5\u{a0}", |
| 308 | 315 | // 2^62 and beyond: not a small Int. |
| 309 | 316 | "4611686018427387904", |
| 310 | 317 | "99999999999999999999999", |
| @@ -320,6 +327,15 @@ mod tests { |
| 320 | 327 | assert_eq!(complete(text).content_length, Some(12)); |
| 321 | 328 | let text = b"POST / HTTP/1.1\r\nContent-Length: 1\xff\r\n\r\n"; |
| 322 | 329 | assert!(matches!(parse_head(text, 0), Head::Invalid(_))); |
| 330 | + // Control characters (a form feed, a vertical tab) never get this |
| 331 | + // far: httparse rejects the header value. |
| 332 | + for value in ["\u{c}5", "\u{b}5"] { |
| 333 | + let text = format!("POST / HTTP/1.1\r\nContent-Length: {value}\r\n\r\n"); |
| 334 | + assert!( |
| 335 | + matches!(parse_head(text.as_bytes(), 0), Head::Invalid(_)), |
| 336 | + "{value:?}" |
| 337 | + ); |
| 338 | + } |
| 323 | 339 | } |
| 324 | 340 | |
| 325 | 341 | /// Two lengths that disagree would let the server and a proxy in |