Skip to content

Commit 4a51b2a

Browse files
committed
fix(http1): reject final chunked if missing 0
If a chunked body had valid chunks, but ended without a `0` in the final chunk (so, just `\r\n\r\n`), it would be parsed as a valid end. Now it will be rejected as the final chunk MUST be `0\r\n\r\n`. This was partially done before, but only if there were no chunks before the final. This fixes both paths.
1 parent c38437d commit 4a51b2a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/proto/h1/decode.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ impl ChunkedState {
431431
rdr: &mut R,
432432
) -> Poll<Result<ChunkedState, io::Error>> {
433433
match byte!(rdr, cx) {
434-
b'\n' => Poll::Ready(Ok(ChunkedState::Size)),
434+
b'\n' => Poll::Ready(Ok(ChunkedState::Start)),
435435
_ => Poll::Ready(Err(io::Error::new(
436436
io::ErrorKind::InvalidInput,
437437
"Invalid chunk body LF",
@@ -666,6 +666,21 @@ mod tests {
666666
assert_eq!("1234567890abcdef", &result);
667667
}
668668

669+
#[tokio::test]
670+
async fn test_read_chunked_with_missing_zero_digit() {
671+
// After reading a valid chunk, the ending is missing a zero.
672+
let mut mock_buf = &b"1\r\nZ\r\n\r\n\r\n"[..];
673+
let mut decoder = Decoder::chunked();
674+
let buf = decoder.decode_fut(&mut mock_buf).await.expect("decode");
675+
assert_eq!("Z", buf);
676+
677+
let err = decoder
678+
.decode_fut(&mut mock_buf)
679+
.await
680+
.expect_err("decode 2");
681+
assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
682+
}
683+
669684
#[tokio::test]
670685
async fn test_read_chunked_extensions_over_limit() {
671686
// construct a chunked body where each individual chunked extension

tests/server.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,29 @@ fn post_with_incomplete_body() {
585585
req.read(&mut [0; 256]).expect("read");
586586
}
587587

588+
#[test]
589+
fn post_with_chunked_missing_final_digit() {
590+
let _ = pretty_env_logger::try_init();
591+
let server = serve();
592+
let mut req = connect(server.addr());
593+
req.write_all(
594+
b"\
595+
POST / HTTP/1.1\r\n\
596+
Host: example.domain\r\n\
597+
transfer-encoding: chunked\r\n\
598+
\r\n\
599+
1\r\n\
600+
Z\r\n\
601+
\r\n\r\n\
602+
",
603+
)
604+
.expect("write");
605+
606+
server.body_err();
607+
608+
req.read(&mut [0; 256]).expect("read");
609+
}
610+
588611
#[test]
589612
fn head_response_can_send_content_length() {
590613
let _ = pretty_env_logger::try_init();

0 commit comments

Comments
 (0)