mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
fetch: proper error for unsupported protocol (#4085)
This commit is contained in:
parent
bf48f5fa5a
commit
e9fff02e96
3 changed files with 31 additions and 1 deletions
|
@ -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 {
|
||||||
|
|
|
@ -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_);
|
||||||
|
|
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue