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

fix(flags): properly error out for urls #25770

Merged
merged 2 commits into from
Sep 20, 2024
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
18 changes: 16 additions & 2 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5084,12 +5084,12 @@ fn permission_args_parse(
}

if let Some(net_wl) = matches.remove_many::<String>("allow-net") {
let net_allowlist = flags_net::parse(net_wl.collect()).unwrap();
let net_allowlist = flags_net::parse(net_wl.collect())?;
flags.permissions.allow_net = Some(net_allowlist);
}

if let Some(net_wl) = matches.remove_many::<String>("deny-net") {
let net_denylist = flags_net::parse(net_wl.collect()).unwrap();
let net_denylist = flags_net::parse(net_wl.collect())?;
flags.permissions.deny_net = Some(net_denylist);
}

Expand Down Expand Up @@ -10801,4 +10801,18 @@ mod tests {
["foo,", "bar"]
);
}

#[test]
fn net_flag_with_url() {
let r = flags_from_vec(svec![
"deno",
"run",
"--allow-net=https://example.com",
"script.ts"
]);
assert_eq!(
r.unwrap_err().to_string(),
"error: invalid value 'https://example.com': URLs are not supported, only domains and ips"
);
}
}
4 changes: 4 additions & 0 deletions runtime/permissions/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,10 @@ impl QueryDescriptor for NetDescriptor {
// TODO(bartlomieju): rewrite to not use `AnyError` but a specific error implementations
impl NetDescriptor {
pub fn parse(hostname: &str) -> Result<Self, AnyError> {
if hostname.starts_with("http://") || hostname.starts_with("https://") {
return Err(uri_error(format!("invalid value '{hostname}': URLs are not supported, only domains and ips")));
}

// If this is a IPv6 address enclosed in square brackets, parse it as such.
if hostname.starts_with('[') {
if let Some((ip, after)) = hostname.split_once(']') {
Expand Down