Skip to content

Commit 8eef32e

Browse files
committed
fix a panic on untrusted input
1 parent 02807a3 commit 8eef32e

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/http/response.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use wasi::http::types::{IncomingBody as WasiIncomingBody, IncomingResponse};
22
use wasi::io::streams::{InputStream, StreamError};
33

4-
use super::{fields::header_map_from_wasi, Body, HeaderMap, StatusCode};
4+
use super::{fields::header_map_from_wasi, Body, Error, HeaderMap, Result, StatusCode};
55
use crate::io::AsyncRead;
66
use crate::runtime::Reactor;
77

@@ -23,27 +23,29 @@ enum BodyKind {
2323
}
2424

2525
impl BodyKind {
26-
fn from_headers(headers: &HeaderMap) -> BodyKind {
26+
fn from_headers(headers: &HeaderMap) -> Result<BodyKind> {
2727
if let Some(value) = headers.get("content-length") {
2828
let content_length = std::str::from_utf8(value.as_ref())
2929
.unwrap()
3030
.parse::<u64>()
31-
.expect("content-length should be a u64; violates HTTP/1.1");
32-
BodyKind::Fixed(content_length)
31+
.map_err(|_| {
32+
Error::other("incoming content-length should be a u64; violates HTTP/1.1")
33+
})?;
34+
Ok(BodyKind::Fixed(content_length))
3335
} else if headers.contains_key("transfer-encoding") {
34-
BodyKind::Chunked
36+
Ok(BodyKind::Chunked)
3537
} else {
36-
BodyKind::Chunked
38+
Ok(BodyKind::Chunked)
3739
}
3840
}
3941
}
4042

4143
impl Response<IncomingBody> {
42-
pub(crate) fn try_from_incoming_response(incoming: IncomingResponse) -> super::Result<Self> {
44+
pub(crate) fn try_from_incoming_response(incoming: IncomingResponse) -> Result<Self> {
4345
let headers: HeaderMap = header_map_from_wasi(incoming.headers())?;
4446
let status = incoming.status().into();
4547

46-
let kind = BodyKind::from_headers(&headers);
48+
let kind = BodyKind::from_headers(&headers)?;
4749
// `body_stream` is a child of `incoming_body` which means we cannot
4850
// drop the parent before we drop the child
4951
let incoming_body = incoming

0 commit comments

Comments
 (0)