Skip to content

Commit f6a3983

Browse files
authored
fix(network): enforce consistent HTTP framing (#4414)
* fix(network): validate chunked message framing Signed-off-by: Adrien Langou <alangou@nvidia.com> * fix(network): preserve chunk extension whitespace Signed-off-by: Adrien Langou <alangou@nvidia.com> --------- Signed-off-by: Adrien Langou <alangou@nvidia.com>
1 parent e326c10 commit f6a3983

4 files changed

Lines changed: 367 additions & 140 deletions

File tree

‎crates/openshell-supervisor-network/src/l7/http.rs‎

Lines changed: 133 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,88 @@ use tokio::io::{AsyncRead, AsyncReadExt};
99

1010
const READ_BUF_SIZE: usize = 8192;
1111

12+
pub fn parse_chunk_size_line(line: &[u8]) -> Result<usize> {
13+
let line =
14+
std::str::from_utf8(line).map_err(|_| miette!("Invalid UTF-8 in HTTP chunk-size line"))?;
15+
if line
16+
.bytes()
17+
.any(|byte| matches!(byte, b'\r' | b'\n' | 0x7f) || (byte < b' ' && byte != b'\t'))
18+
{
19+
return Err(miette!(
20+
"Invalid HTTP chunk-size line: contains a control byte"
21+
));
22+
}
23+
24+
// RFC 9112 permits bad whitespace immediately before a chunk-extension
25+
// semicolon. Normalize only that suffix: leading whitespace, whitespace
26+
// inside the size, and trailing whitespace without an extension remain
27+
// invalid at this trust boundary.
28+
let size_token = line
29+
.split_once(';')
30+
.map_or(line, |(size, _)| size.trim_end_matches([' ', '\t']));
31+
if size_token.is_empty() || !size_token.bytes().all(|byte| byte.is_ascii_hexdigit()) {
32+
return Err(miette!("Invalid HTTP chunk size token"));
33+
}
34+
35+
usize::from_str_radix(size_token, 16)
36+
.map_err(|_| miette!("HTTP chunk size exceeds platform limit"))
37+
}
38+
39+
pub fn validate_chunk_trailer_line(line: &[u8]) -> Result<()> {
40+
if line.is_empty() {
41+
return Ok(());
42+
}
43+
if line
44+
.iter()
45+
.any(|byte| matches!(*byte, b'\r' | b'\n' | 0x7f))
46+
{
47+
return Err(miette!(
48+
"Invalid HTTP chunk trailer: contains a control byte"
49+
));
50+
}
51+
52+
let colon = line
53+
.iter()
54+
.position(|byte| *byte == b':')
55+
.ok_or_else(|| miette!("Invalid HTTP chunk trailer: missing colon"))?;
56+
let (name, value) = line.split_at(colon);
57+
if name.is_empty() || !name.iter().all(|byte| is_http_token_byte(*byte)) {
58+
return Err(miette!("Invalid HTTP chunk trailer field name"));
59+
}
60+
if !value[1..]
61+
.iter()
62+
.all(|byte| is_http_field_value_byte(*byte))
63+
{
64+
return Err(miette!("Invalid HTTP chunk trailer field value"));
65+
}
66+
Ok(())
67+
}
68+
69+
fn is_http_token_byte(byte: u8) -> bool {
70+
byte.is_ascii_alphanumeric()
71+
|| matches!(
72+
byte,
73+
b'!' | b'#'
74+
| b'$'
75+
| b'%'
76+
| b'&'
77+
| b'\''
78+
| b'*'
79+
| b'+'
80+
| b'-'
81+
| b'.'
82+
| b'^'
83+
| b'_'
84+
| b'`'
85+
| b'|'
86+
| b'~'
87+
)
88+
}
89+
90+
fn is_http_field_value_byte(byte: u8) -> bool {
91+
byte == b'\t' || (b' '..=b'~').contains(&byte) || byte >= 0x80
92+
}
93+
1294
pub async fn read_body_for_inspection<C: AsyncRead + Unpin>(
1395
client: &mut C,
1496
request: &mut L7Request,
@@ -113,17 +195,7 @@ async fn read_chunked_body_for_inspection<C: AsyncRead + Unpin>(
113195

114196
loop {
115197
let size_line_end = read_chunked_line(client, &mut raw, pos, max_body_bytes).await?;
116-
let size_line = std::str::from_utf8(&raw[pos..size_line_end])
117-
.into_diagnostic()
118-
.map_err(|_| miette!("Invalid UTF-8 in HTTP chunk-size line"))?;
119-
let size_token = size_line
120-
.split(';')
121-
.next()
122-
.map(str::trim)
123-
.unwrap_or_default();
124-
let chunk_size = usize::from_str_radix(size_token, 16)
125-
.into_diagnostic()
126-
.map_err(|_| miette!("Invalid HTTP chunk size token: {size_token:?}"))?;
198+
let chunk_size = parse_chunk_size_line(&raw[pos..size_line_end])?;
127199
pos = size_line_end + 2;
128200

129201
if decoded.len().saturating_add(chunk_size) > max_body_bytes {
@@ -136,6 +208,7 @@ async fn read_chunked_body_for_inspection<C: AsyncRead + Unpin>(
136208
loop {
137209
let trailer_end = read_chunked_line(client, &mut raw, pos, max_body_bytes).await?;
138210
let trailer_line = &raw[pos..trailer_end];
211+
validate_chunk_trailer_line(trailer_line)?;
139212
pos = trailer_end + 2;
140213
if trailer_line.is_empty() {
141214
request.raw_header.truncate(header_end);
@@ -230,6 +303,48 @@ mod tests {
230303
}
231304
}
232305

306+
#[test]
307+
fn chunk_framing_helpers_reject_ambiguous_lines() {
308+
assert_eq!(parse_chunk_size_line(b"a;ext=value").unwrap(), 10);
309+
for line in [
310+
b"".as_slice(),
311+
b"+0",
312+
b"+0 ;ext=value",
313+
b" 0",
314+
b" 0;ext=value",
315+
b"\t0;ext=value",
316+
b"0 ",
317+
b"0\t",
318+
b"0 \t1;ext=value",
319+
b"0\n",
320+
b"0\rX",
321+
b"0;ext=bad\nvalue",
322+
] {
323+
assert!(parse_chunk_size_line(line).is_err(), "accepted {line:?}");
324+
}
325+
326+
for line in [
327+
b"Malformed-Trailer".as_slice(),
328+
b"Bad Name: value",
329+
b"X-Test: value\nGET /hidden",
330+
b"X-Test: value\0",
331+
] {
332+
assert!(
333+
validate_chunk_trailer_line(line).is_err(),
334+
"accepted {line:?}"
335+
);
336+
}
337+
validate_chunk_trailer_line(b"").unwrap();
338+
validate_chunk_trailer_line(b"X-Test: value").unwrap();
339+
}
340+
341+
#[test]
342+
fn chunk_size_allows_bws_before_extension() {
343+
for line in [b"a ;ext=value".as_slice(), b"a\t;ext=value"] {
344+
assert_eq!(parse_chunk_size_line(line).unwrap(), 10, "line {line:?}");
345+
}
346+
}
347+
233348
#[tokio::test]
234349
async fn chunked_inspection_preserves_pipelined_request() {
235350
let next = b"DELETE /next HTTP/1.1\r\nHost: example.test\r\n\r\n";
@@ -300,9 +415,16 @@ mod tests {
300415
for (encoded, expected) in [
301416
(b"z\r\n".as_slice(), "Invalid HTTP chunk size token"),
302417
(b"\xff\r\n", "Invalid UTF-8 in HTTP chunk-size line"),
418+
(b"0\n\r\n\r\n", "chunk-size line"),
419+
(b"+0\r\n\r\n", "chunk size token"),
303420
(b"1\r\nx!!", "HTTP chunk payload missing terminating CRLF"),
304421
(b"1\r", "HTTP chunked body ended before terminator"),
305422
(b"5\r\nhe", "HTTP chunked body ended before terminator"),
423+
(
424+
b"0\r\nA: b\n\r\nGET /hidden HTTP/1.1\r\nHost: hidden.example\r\n\r\n",
425+
"chunk trailer",
426+
),
427+
(b"0\r\nMalformed-Trailer\r\n\r\n", "chunk trailer"),
306428
(
307429
b"0\r\nX-Trailer: unfinished",
308430
"HTTP chunked body ended before terminator",

0 commit comments

Comments
 (0)