Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch: proper error for unsupported protocol #4085

Merged
merged 1 commit into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cli/js/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import {
fail
} from "./test_util.ts";

testPerm({ net: true }, async function fetchProtocolError(): Promise<void> {
let err;
try {
await fetch("file:///");
} catch (err_) {
err = err_;
}
assert(err instanceof TypeError);
assertStrContains(err.message, "not supported");
});

testPerm({ net: true }, async function fetchConnectionError(): Promise<void> {
let err;
try {
Expand Down
15 changes: 15 additions & 0 deletions cli/ops/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
use super::io::StreamResource;
use crate::deno_error::DenoError;
use crate::deno_error::ErrorKind;
use crate::http_util::{create_http_client, HttpBody};
use crate::ops::json_op;
use crate::state::State;
Expand Down Expand Up @@ -40,6 +42,19 @@ pub fn op_fetch(
};

let url_ = url::Url::parse(&url).map_err(ErrBox::from)?;

// Check scheme before asking for net permission
let scheme = url_.scheme();
if scheme != "http" && scheme != "https" {
return Err(
DenoError::new(
ErrorKind::TypeError,
format!("scheme '{}' not supported", scheme),
)
.into(),
);
}

state.check_net_url(&url_)?;

let mut request = client.request(method, url_);
Expand Down
6 changes: 5 additions & 1 deletion cli/permissions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::deno_error::{other_error, permission_denied_msg};
use crate::deno_error::{DenoError, ErrorKind};
use crate::flags::DenoFlags;
use ansi_term::Style;
#[cfg(not(test))]
Expand Down Expand Up @@ -193,8 +194,11 @@ impl DenoPermissions {
}

pub fn check_net_url(&self, url: &url::Url) -> Result<(), ErrBox> {
let host = url.host_str().ok_or_else(|| {
DenoError::new(ErrorKind::URIError, "missing host".to_owned())
})?;
self
.get_state_net(&format!("{}", url.host().unwrap()), url.port())
.get_state_net(host, url.port())
.check(&format!("network access to \"{}\"", url), "--allow-net")
}

Expand Down