diff options
author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2020-02-23 06:45:02 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-23 09:45:02 -0500 |
commit | e9fff02e9681f3eb2edee9f94db66b140e179899 (patch) | |
tree | debb7868de81f91e0f4e67a0bcf8cc592e9c8e2b | |
parent | bf48f5fa5a15e01d6f8b7eb7c3e70f6ecc91fa23 (diff) |
fetch: proper error for unsupported protocol (#4085)
-rw-r--r-- | cli/js/fetch_test.ts | 11 | ||||
-rw-r--r-- | cli/ops/fetch.rs | 15 | ||||
-rw-r--r-- | cli/permissions.rs | 6 |
3 files changed, 31 insertions, 1 deletions
diff --git a/cli/js/fetch_test.ts b/cli/js/fetch_test.ts index 2912d5d2d..54bec46be 100644 --- a/cli/js/fetch_test.ts +++ b/cli/js/fetch_test.ts @@ -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 { diff --git a/cli/ops/fetch.rs b/cli/ops/fetch.rs index 580fd993a..9758732c7 100644 --- a/cli/ops/fetch.rs +++ b/cli/ops/fetch.rs @@ -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; @@ -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_); diff --git a/cli/permissions.rs b/cli/permissions.rs index 868f0e009..950bec400 100644 --- a/cli/permissions.rs +++ b/cli/permissions.rs @@ -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))] @@ -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") } |