Skip to content

Commit c0b2ea0

Browse files
committed
Fix panic with invalid trailer
1 parent e798b22 commit c0b2ea0

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

src/chunked.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,9 @@ fn decode_trailer(buffer: Block<'static>, pos: &Range<usize>) -> io::Result<Deco
482482
let mut trailers = Trailers::new();
483483
for header in headers {
484484
let value = std::string::String::from_utf8_lossy(header.value).to_string();
485-
trailers.insert(header.name, value).unwrap();
485+
if let Err(err) = trailers.insert(header.name, value) {
486+
return Err(io::Error::new(io::ErrorKind::Other, err.to_string()));
487+
}
486488
}
487489

488490
Ok(DecodeResult::Some {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
GET / HTTP/1.1
2+
content-type: application/octet-stream
3+
transfer-encoding: chunked
4+
trailer: x-invalid
5+
6+
0
7+
x-invalid: å
8+

tests/fixtures/response-invalid-trailer.txt

Whitespace-only changes.

tests/server.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,29 @@ async fn test_unexpected_eof() {
9999

100100
case.assert().await;
101101
}
102+
103+
#[async_std::test]
104+
async fn test_invalid_trailer() {
105+
let case = TestCase::new_server(
106+
"fixtures/request-invalid-trailer.txt",
107+
"fixtures/response-invalid-trailer.txt",
108+
)
109+
.await;
110+
let addr = "http://example.com";
111+
112+
async_h1::accept(addr, case.clone(), |req| async {
113+
let mut resp = Response::new(StatusCode::Ok);
114+
let ct = req.content_type();
115+
let body: Body = req.into();
116+
resp.set_body(body);
117+
if let Some(ct) = ct {
118+
resp.set_content_type(ct);
119+
}
120+
121+
Ok(resp)
122+
})
123+
.await
124+
.unwrap_err();
125+
126+
assert!(case.read_result().await.is_empty());
127+
}

0 commit comments

Comments
 (0)