Skip to content

Commit

Permalink
fix(cli/permissions): panic on hostless URLs (denoland#6500)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spoonbender committed Jun 26, 2020
1 parent 42464e9 commit 598a7dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cli/js/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,12 @@ declare namespace Deno {

export interface NetPermissionDescriptor {
name: "net";
/** Optional url associated with this descriptor.
*
* If specified: must be a valid url. Expected format: <scheme>://<host_or_ip>[:port][/path]
* If the scheme is unknown, callers should specify some scheme, such as x:// na:// unknown://
*
* See: https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml */
url?: string;
}

Expand Down
34 changes: 33 additions & 1 deletion cli/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@ impl Permissions {
let url: &str = url.unwrap();
// If url is invalid, then throw a TypeError.
let parsed = Url::parse(url).map_err(OpError::from)?;
// The url may be parsed correctly but still lack a host, i.e. "localhost:235" or "mailto:someone@somewhere.com" or "file:/1.txt"
// Note that host:port combos are parsed as scheme:path
if parsed.host().is_none() {
return Err(OpError::uri_error(
"invalid url, expected format: <scheme>://<host>[:port][/subpath]"
.to_owned(),
));
}
Ok(
self.get_state_net(&format!("{}", parsed.host().unwrap()), parsed.port()),
)
Expand Down Expand Up @@ -833,11 +841,35 @@ mod tests {
);

let mut perms3 = Permissions::from_flags(&Flags {
net_allowlist: allowlist,
net_allowlist: allowlist.clone(),
..Default::default()
});
set_prompt_result(true);
assert!(perms3.request_net(&Some(":")).is_err());

let mut perms4 = Permissions::from_flags(&Flags {
net_allowlist: allowlist.clone(),
..Default::default()
});
set_prompt_result(false);
assert_eq!(
perms4
.request_net(&Some("localhost:8080"))
.unwrap_err()
.kind,
crate::op_error::ErrorKind::URIError
);

let mut perms5 = Permissions::from_flags(&Flags {
net_allowlist: allowlist,
..Default::default()
});
set_prompt_result(false);
assert_eq!(
perms5.request_net(&Some("file:/1.txt")).unwrap_err().kind,
crate::op_error::ErrorKind::URIError
);

drop(guard);
}

Expand Down

0 comments on commit 598a7dc

Please sign in to comment.