Skip to content

Commit

Permalink
Avoid fetch segfault on empty Uri (denoland#1394)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo authored and ry committed Dec 21, 2018
1 parent 105a519 commit e4be120
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
11 changes: 11 additions & 0 deletions js/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ testPerm({ net: true }, async function responseClone() {
}
});

testPerm({ net: true }, async function fetchEmptyInvalid() {
let err;
try {
await fetch("");
} catch (err_) {
err = err_;
}
assertEqual(err.kind, deno.ErrorKind.InvalidUri);
assertEqual(err.name, "InvalidUri");
});

// TODO(ry) The following tests work but are flaky. There's a race condition
// somewhere. Here is what one of these flaky failures looks like:
//
Expand Down
3 changes: 3 additions & 0 deletions src/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ enum ErrorKind: byte {
HttpParse,
HttpOther,
TooLarge,

// custom errors
InvalidUri,
}

table Cwd {}
Expand Down
9 changes: 6 additions & 3 deletions src/msg_util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
// Helpers for serialization.
use errors;
use errors::DenoResult;
use flatbuffers;
use http::header::HeaderName;
use http::uri::Uri;
Expand Down Expand Up @@ -94,13 +96,14 @@ pub fn serialize_http_response<'bldr>(
pub fn deserialize_request(
header_msg: msg::HttpHeader,
body: Body,
) -> Request<Body> {
) -> DenoResult<Request<Body>> {
let mut r = Request::new(body);

assert!(header_msg.is_request());

let u = header_msg.url().unwrap();
let u = Uri::from_str(u).unwrap();
let u = Uri::from_str(u)
.map_err(|e| errors::new(msg::ErrorKind::InvalidUri, e.to_string()))?;
*r.uri_mut() = u;

if let Some(method) = header_msg.method() {
Expand All @@ -119,5 +122,5 @@ pub fn deserialize_request(
headers.insert(name, v);
}
}
r
Ok(r)
}
6 changes: 5 additions & 1 deletion src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ fn op_fetch(
hyper::Body::from(Vec::from(&*data))
};

let req = msg_util::deserialize_request(header, body);
let maybe_req = msg_util::deserialize_request(header, body);
if let Err(e) = maybe_req {
return odd_future(e);
}
let req = maybe_req.unwrap();

if let Err(e) = state.check_net(url) {
return odd_future(e);
Expand Down

0 comments on commit e4be120

Please sign in to comment.