fetch: proper error for unsupported protocol (#4085)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2020-02-23 06:45:02 -08:00 committed by GitHub
parent bf48f5fa5a
commit e9fff02e96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View file

@ -9,6 +9,17 @@ import {
fail fail
} from "./test_util.ts"; } 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> { testPerm({ net: true }, async function fetchConnectionError(): Promise<void> {
let err; let err;
try { try {

View file

@ -1,6 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value}; use super::dispatch_json::{Deserialize, JsonOp, Value};
use super::io::StreamResource; use super::io::StreamResource;
use crate::deno_error::DenoError;
use crate::deno_error::ErrorKind;
use crate::http_util::{create_http_client, HttpBody}; use crate::http_util::{create_http_client, HttpBody};
use crate::ops::json_op; use crate::ops::json_op;
use crate::state::State; use crate::state::State;
@ -40,6 +42,19 @@ pub fn op_fetch(
}; };
let url_ = url::Url::parse(&url).map_err(ErrBox::from)?; 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_)?; state.check_net_url(&url_)?;
let mut request = client.request(method, url_); let mut request = client.request(method, url_);

View file

@ -1,5 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::deno_error::{other_error, permission_denied_msg}; use crate::deno_error::{other_error, permission_denied_msg};
use crate::deno_error::{DenoError, ErrorKind};
use crate::flags::DenoFlags; use crate::flags::DenoFlags;
use ansi_term::Style; use ansi_term::Style;
#[cfg(not(test))] #[cfg(not(test))]
@ -193,8 +194,11 @@ impl DenoPermissions {
} }
pub fn check_net_url(&self, url: &url::Url) -> Result<(), ErrBox> { 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 self
.get_state_net(&format!("{}", url.host().unwrap()), url.port()) .get_state_net(host, url.port())
.check(&format!("network access to \"{}\"", url), "--allow-net") .check(&format!("network access to \"{}\"", url), "--allow-net")
} }