1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/cli/permissions.rs

698 lines
20 KiB
Rust
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::deno_error::{other_error, permission_denied_msg};
use crate::flags::DenoFlags;
use ansi_term::Style;
2019-11-11 10:33:29 -05:00
#[cfg(not(test))]
use atty;
use deno_core::ErrBox;
use log;
use std::collections::HashSet;
use std::fmt;
2019-11-11 10:33:29 -05:00
#[cfg(not(test))]
use std::io;
use std::path::{Path, PathBuf};
2019-11-11 10:33:29 -05:00
#[cfg(test)]
use std::sync::atomic::AtomicBool;
#[cfg(test)]
use std::sync::atomic::Ordering;
use url::Url;
const PERMISSION_EMOJI: &str = "⚠️";
/// Tri-state value for storing permission state
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum PermissionState {
Allow = 0,
Ask = 1,
Deny = 2,
}
impl PermissionState {
2019-11-11 10:33:29 -05:00
/// Checks the permission state and returns the result.
dx: descriptive permission errors (#3808) Before: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ``` After: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ```
2020-01-27 21:13:17 -05:00
pub fn check(self, msg: &str, flag_name: &str) -> Result<(), ErrBox> {
if self == PermissionState::Allow {
2019-11-11 10:33:29 -05:00
log_perm_access(msg);
return Ok(());
}
dx: descriptive permission errors (#3808) Before: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ``` After: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ```
2020-01-27 21:13:17 -05:00
let m = format!("{}, run again with the {} flag", msg, flag_name);
Err(permission_denied_msg(m))
2019-11-11 10:33:29 -05:00
}
pub fn is_allow(self) -> bool {
self == PermissionState::Allow
}
/// If the state is "Allow" walk it back to the default "Ask"
/// Don't do anything if state is "Deny"
pub fn revoke(&mut self) {
if *self == PermissionState::Allow {
*self = PermissionState::Ask;
}
}
/// Requests the permission.
pub fn request(&mut self, msg: &str) -> PermissionState {
if *self != PermissionState::Ask {
return *self;
}
if permission_prompt(msg) {
*self = PermissionState::Allow;
} else {
*self = PermissionState::Deny;
}
*self
}
2019-11-11 10:33:29 -05:00
}
impl From<usize> for PermissionState {
fn from(val: usize) -> Self {
match val {
0 => PermissionState::Allow,
1 => PermissionState::Ask,
2 => PermissionState::Deny,
_ => unreachable!(),
}
}
}
impl From<bool> for PermissionState {
fn from(val: bool) -> Self {
if val {
PermissionState::Allow
} else {
PermissionState::Ask
}
}
}
impl fmt::Display for PermissionState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PermissionState::Allow => f.pad("granted"),
PermissionState::Ask => f.pad("prompt"),
PermissionState::Deny => f.pad("denied"),
}
}
}
impl Default for PermissionState {
fn default() -> Self {
PermissionState::Ask
}
}
#[derive(Clone, Debug, Default)]
pub struct DenoPermissions {
// Keep in sync with cli/js/permissions.ts
pub allow_read: PermissionState,
pub read_whitelist: HashSet<String>,
pub allow_write: PermissionState,
pub write_whitelist: HashSet<String>,
pub allow_net: PermissionState,
pub net_whitelist: HashSet<String>,
pub allow_env: PermissionState,
pub allow_run: PermissionState,
pub allow_plugin: PermissionState,
pub allow_hrtime: PermissionState,
}
impl DenoPermissions {
pub fn from_flags(flags: &DenoFlags) -> Self {
Self {
allow_read: PermissionState::from(flags.allow_read),
read_whitelist: flags.read_whitelist.iter().cloned().collect(),
allow_write: PermissionState::from(flags.allow_write),
write_whitelist: flags.write_whitelist.iter().cloned().collect(),
allow_net: PermissionState::from(flags.allow_net),
net_whitelist: flags.net_whitelist.iter().cloned().collect(),
allow_env: PermissionState::from(flags.allow_env),
allow_run: PermissionState::from(flags.allow_run),
allow_plugin: PermissionState::from(flags.allow_plugin),
allow_hrtime: PermissionState::from(flags.allow_hrtime),
}
}
pub fn check_run(&self) -> Result<(), ErrBox> {
dx: descriptive permission errors (#3808) Before: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ``` After: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ```
2020-01-27 21:13:17 -05:00
self
.allow_run
.check("access to run a subprocess", "--allow-run")
}
fn get_state_read(&self, path: &Option<&Path>) -> PermissionState {
if path.map_or(false, |f| check_path_white_list(f, &self.read_whitelist)) {
return PermissionState::Allow;
}
self.allow_read
}
pub fn check_read(&self, path: &Path) -> Result<(), ErrBox> {
self.get_state_read(&Some(path)).check(
&format!("read access to \"{}\"", path.display()),
dx: descriptive permission errors (#3808) Before: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ``` After: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ```
2020-01-27 21:13:17 -05:00
"--allow-read",
)
}
fn get_state_write(&self, path: &Option<&Path>) -> PermissionState {
if path.map_or(false, |f| check_path_white_list(f, &self.write_whitelist)) {
return PermissionState::Allow;
}
self.allow_write
}
pub fn check_write(&self, path: &Path) -> Result<(), ErrBox> {
self.get_state_write(&Some(path)).check(
&format!("write access to \"{}\"", path.display()),
dx: descriptive permission errors (#3808) Before: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ``` After: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ```
2020-01-27 21:13:17 -05:00
"--allow-write",
)
}
fn get_state_net(&self, host: &str, port: Option<u16>) -> PermissionState {
if check_host_and_port_whitelist(host, port, &self.net_whitelist) {
return PermissionState::Allow;
}
self.allow_net
}
2019-11-11 10:33:29 -05:00
fn get_state_net_url(
&self,
url: &Option<&str>,
) -> Result<PermissionState, ErrBox> {
2019-11-11 10:33:29 -05:00
if url.is_none() {
return Ok(self.allow_net);
2019-11-11 10:33:29 -05:00
}
let url: &str = url.unwrap();
// If url is invalid, then throw a TypeError.
let parsed = Url::parse(url).map_err(ErrBox::from)?;
2019-11-11 10:33:29 -05:00
Ok(
self.get_state_net(&format!("{}", parsed.host().unwrap()), parsed.port()),
)
}
pub fn check_net(&self, hostname: &str, port: u16) -> Result<(), ErrBox> {
2019-11-11 10:33:29 -05:00
self.get_state_net(hostname, Some(port)).check(
&format!("network access to \"{}:{}\"", hostname, port),
dx: descriptive permission errors (#3808) Before: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ``` After: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ```
2020-01-27 21:13:17 -05:00
"--allow-net",
)
}
pub fn check_net_url(&self, url: &url::Url) -> Result<(), ErrBox> {
2019-11-11 10:33:29 -05:00
self
.get_state_net(&format!("{}", url.host().unwrap()), url.port())
dx: descriptive permission errors (#3808) Before: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ``` After: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ```
2020-01-27 21:13:17 -05:00
.check(&format!("network access to \"{}\"", url), "--allow-net")
}
pub fn check_env(&self) -> Result<(), ErrBox> {
dx: descriptive permission errors (#3808) Before: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ``` After: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ```
2020-01-27 21:13:17 -05:00
self
.allow_env
.check("access to environment variables", "--allow-env")
}
pub fn check_plugin(&self, path: &Path) -> Result<(), ErrBox> {
self.allow_plugin.check(
&format!("access to open a plugin: {}", path.display()),
dx: descriptive permission errors (#3808) Before: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ``` After: ``` ▶ target/debug/deno https://deno.land/std/examples/echo_server.ts error: Uncaught PermissionDenied: network access to "0.0.0.0:8080", run again with the --allow-net flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendSync ($deno$/dispatch_json.ts:67:10) at listen ($deno$/net.ts:170:15) at https://deno.land/std/examples/echo_server.ts:4:23 ``` ``` ▶ target/debug/deno --allow-read=/usr https://deno.land/std/examples/cat.ts /etc/passwd error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flag ► $deno$/dispatch_json.ts:40:11 at DenoError ($deno$/errors.ts:20:5) at unwrapResponse ($deno$/dispatch_json.ts:40:11) at sendAsync ($deno$/dispatch_json.ts:91:10) ```
2020-01-27 21:13:17 -05:00
"--allow-plugin",
)
}
pub fn request_run(&mut self) -> PermissionState {
2019-11-11 10:33:29 -05:00
self
.allow_run
.request("Deno requests to access to run a subprocess.")
}
pub fn request_read(&mut self, path: &Option<&Path>) -> PermissionState {
if path.map_or(false, |f| check_path_white_list(f, &self.read_whitelist)) {
return PermissionState::Allow;
2019-11-11 10:33:29 -05:00
};
2019-12-03 12:22:51 -05:00
self.allow_read.request(&match path {
2019-11-11 10:33:29 -05:00
None => "Deno requests read access.".to_string(),
Some(path) => {
format!("Deno requests read access to \"{}\".", path.display())
}
2019-11-11 10:33:29 -05:00
})
}
pub fn request_write(&mut self, path: &Option<&Path>) -> PermissionState {
if path.map_or(false, |f| check_path_white_list(f, &self.write_whitelist)) {
return PermissionState::Allow;
2019-11-11 10:33:29 -05:00
};
self.allow_write.request(&match path {
None => "Deno requests write access.".to_string(),
Some(path) => {
format!("Deno requests write access to \"{}\".", path.display())
}
2019-11-11 10:33:29 -05:00
})
}
pub fn request_net(
&mut self,
2019-11-11 10:33:29 -05:00
url: &Option<&str>,
) -> Result<PermissionState, ErrBox> {
if self.get_state_net_url(url)? == PermissionState::Ask {
2019-12-03 12:22:51 -05:00
return Ok(self.allow_net.request(&match url {
2019-11-11 10:33:29 -05:00
None => "Deno requests network access.".to_string(),
Some(url) => format!("Deno requests network access to \"{}\".", url),
}));
};
self.get_state_net_url(url)
}
pub fn request_env(&mut self) -> PermissionState {
2019-11-11 10:33:29 -05:00
self
.allow_env
.request("Deno requests to access to environment variables.")
}
pub fn request_hrtime(&mut self) -> PermissionState {
2019-11-11 10:33:29 -05:00
self
.allow_hrtime
.request("Deno requests to access to high precision time.")
}
pub fn request_plugin(&mut self) -> PermissionState {
self.allow_plugin.request("Deno requests to open plugins.")
}
pub fn get_permission_state(
&self,
name: &str,
url: &Option<&str>,
path: &Option<&Path>,
) -> Result<PermissionState, ErrBox> {
match name {
"run" => Ok(self.allow_run),
"read" => Ok(self.get_state_read(path)),
"write" => Ok(self.get_state_write(path)),
2019-11-11 10:33:29 -05:00
"net" => self.get_state_net_url(url),
"env" => Ok(self.allow_env),
"plugin" => Ok(self.allow_plugin),
"hrtime" => Ok(self.allow_hrtime),
n => Err(other_error(format!("No such permission name: {}", n))),
}
}
}
2019-11-11 10:33:29 -05:00
/// Shows the permission prompt and returns the answer according to the user input.
/// This loops until the user gives the proper input.
#[cfg(not(test))]
fn permission_prompt(message: &str) -> bool {
if !atty::is(atty::Stream::Stdin) || !atty::is(atty::Stream::Stderr) {
return false;
};
let msg = format!(
"{} {}. Grant? [g/d (g = grant, d = deny)] ",
PERMISSION_EMOJI, message
);
// print to stderr so that if deno is > to a file this is still displayed.
eprint!("{}", Style::new().bold().paint(msg));
loop {
let mut input = String::new();
let stdin = io::stdin();
let result = stdin.read_line(&mut input);
if result.is_err() {
return false;
};
let ch = input.chars().next().unwrap();
match ch.to_ascii_lowercase() {
'g' => return true,
'd' => return false,
_ => {
// If we don't get a recognized option try again.
let msg_again =
format!("Unrecognized option '{}' [g/d (g = grant, d = deny)] ", ch);
eprint!("{}", Style::new().bold().paint(msg_again));
}
};
}
}
#[cfg(test)]
static STUB_PROMPT_VALUE: AtomicBool = AtomicBool::new(true);
#[cfg(test)]
fn set_prompt_result(value: bool) {
STUB_PROMPT_VALUE.store(value, Ordering::SeqCst);
}
// When testing, permission prompt returns the value of STUB_PROMPT_VALUE
// which we set from the test functions.
#[cfg(test)]
fn permission_prompt(_message: &str) -> bool {
STUB_PROMPT_VALUE.load(Ordering::SeqCst)
}
fn log_perm_access(message: &str) {
if log_enabled!(log::Level::Info) {
eprintln!(
"{}",
Style::new()
.bold()
.paint(format!("{} Granted {}", PERMISSION_EMOJI, message))
);
}
}
fn check_path_white_list(path: &Path, white_list: &HashSet<String>) -> bool {
let mut path_buf = PathBuf::from(path);
loop {
if white_list.contains(path_buf.to_str().unwrap()) {
return true;
}
if !path_buf.pop() {
break;
}
}
false
}
fn check_host_and_port_whitelist(
host: &str,
port: Option<u16>,
whitelist: &HashSet<String>,
) -> bool {
whitelist.contains(host)
|| (port.is_some()
&& whitelist.contains(&format!("{}:{}", host, port.unwrap())))
}
#[cfg(test)]
mod tests {
use super::*;
// Creates vector of strings, Vec<String>
macro_rules! svec {
($($x:expr),*) => (vec![$($x.to_string()),*]);
}
#[test]
fn check_paths() {
let whitelist = svec!["/a/specific/dir/name", "/a/specific", "/b/c"];
let perms = DenoPermissions::from_flags(&DenoFlags {
read_whitelist: whitelist.clone(),
2019-12-23 09:59:44 -05:00
write_whitelist: whitelist,
..Default::default()
});
// Inside of /a/specific and /a/specific/dir/name
assert!(perms.check_read(Path::new("/a/specific/dir/name")).is_ok());
assert!(perms.check_write(Path::new("/a/specific/dir/name")).is_ok());
// Inside of /a/specific but outside of /a/specific/dir/name
assert!(perms.check_read(Path::new("/a/specific/dir")).is_ok());
assert!(perms.check_write(Path::new("/a/specific/dir")).is_ok());
// Inside of /a/specific and /a/specific/dir/name
assert!(perms
.check_read(Path::new("/a/specific/dir/name/inner"))
.is_ok());
assert!(perms
.check_write(Path::new("/a/specific/dir/name/inner"))
.is_ok());
// Inside of /a/specific but outside of /a/specific/dir/name
assert!(perms.check_read(Path::new("/a/specific/other/dir")).is_ok());
assert!(perms
.check_write(Path::new("/a/specific/other/dir"))
.is_ok());
// Exact match with /b/c
assert!(perms.check_read(Path::new("/b/c")).is_ok());
assert!(perms.check_write(Path::new("/b/c")).is_ok());
// Sub path within /b/c
assert!(perms.check_read(Path::new("/b/c/sub/path")).is_ok());
assert!(perms.check_write(Path::new("/b/c/sub/path")).is_ok());
// Inside of /b but outside of /b/c
assert!(perms.check_read(Path::new("/b/e")).is_err());
assert!(perms.check_write(Path::new("/b/e")).is_err());
// Inside of /a but outside of /a/specific
assert!(perms.check_read(Path::new("/a/b")).is_err());
assert!(perms.check_write(Path::new("/a/b")).is_err());
}
#[test]
2019-06-24 19:30:11 -04:00
fn test_check_net() {
let perms = DenoPermissions::from_flags(&DenoFlags {
net_whitelist: svec![
"localhost",
"deno.land",
"github.com:3000",
"127.0.0.1",
"172.16.0.2:8000"
],
..Default::default()
});
2019-06-24 19:30:11 -04:00
let domain_tests = vec![
("localhost", 1234, true),
("deno.land", 0, true),
("deno.land", 3000, true),
("deno.lands", 0, false),
("deno.lands", 3000, false),
("github.com", 3000, true),
("github.com", 0, false),
("github.com", 2000, false),
("github.net", 3000, false),
("127.0.0.1", 0, true),
("127.0.0.1", 3000, true),
("127.0.0.2", 0, false),
("127.0.0.2", 3000, false),
("172.16.0.2", 8000, true),
("172.16.0.2", 0, false),
("172.16.0.2", 6000, false),
("172.16.0.1", 8000, false),
2019-06-24 19:30:11 -04:00
// Just some random hosts that should err
("somedomain", 0, false),
("192.168.0.1", 0, false),
2019-06-24 19:30:11 -04:00
];
let url_tests = vec![
// Any protocol + port for localhost should be ok, since we don't specify
("http://localhost", true),
("https://localhost", true),
("https://localhost:4443", true),
("tcp://localhost:5000", true),
("udp://localhost:6000", true),
// Correct domain + any port and protocol should be ok incorrect shouldn't
("https://deno.land/std/example/welcome.ts", true),
("https://deno.land:3000/std/example/welcome.ts", true),
("https://deno.lands/std/example/welcome.ts", false),
("https://deno.lands:3000/std/example/welcome.ts", false),
// Correct domain + port should be ok all other combinations should err
("https://github.com:3000/denoland/deno", true),
("https://github.com/denoland/deno", false),
("https://github.com:2000/denoland/deno", false),
("https://github.net:3000/denoland/deno", false),
// Correct ipv4 address + any port should be ok others should err
("tcp://127.0.0.1", true),
("https://127.0.0.1", true),
("tcp://127.0.0.1:3000", true),
("https://127.0.0.1:3000", true),
("tcp://127.0.0.2", false),
("https://127.0.0.2", false),
("tcp://127.0.0.2:3000", false),
("https://127.0.0.2:3000", false),
// Correct address + port should be ok all other combinations should err
("tcp://172.16.0.2:8000", true),
("https://172.16.0.2:8000", true),
("tcp://172.16.0.2", false),
("https://172.16.0.2", false),
("tcp://172.16.0.2:6000", false),
("https://172.16.0.2:6000", false),
("tcp://172.16.0.1:8000", false),
("https://172.16.0.1:8000", false),
];
for (url_str, is_ok) in url_tests.iter() {
let u = url::Url::parse(url_str).unwrap();
assert_eq!(*is_ok, perms.check_net_url(&u).is_ok());
2019-06-24 19:30:11 -04:00
}
for (host, port, is_ok) in domain_tests.iter() {
assert_eq!(*is_ok, perms.check_net(host, *port).is_ok());
2019-06-24 19:30:11 -04:00
}
}
2019-11-11 10:33:29 -05:00
#[test]
fn test_permissions_request_run() {
let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
..Default::default()
});
set_prompt_result(true);
assert_eq!(perms0.request_run(), PermissionState::Allow);
2019-11-11 10:33:29 -05:00
let mut perms1 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
..Default::default()
});
set_prompt_result(false);
assert_eq!(perms1.request_run(), PermissionState::Deny);
2019-11-11 10:33:29 -05:00
}
#[test]
fn test_permissions_request_read() {
let whitelist = svec!["/foo/bar"];
let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
read_whitelist: whitelist.clone(),
..Default::default()
});
set_prompt_result(false);
// If the whitelist contains the path, then the result is `allow`
// regardless of prompt result
assert_eq!(
perms0.request_read(&Some(Path::new("/foo/bar"))),
PermissionState::Allow
2019-11-11 10:33:29 -05:00
);
let mut perms1 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
read_whitelist: whitelist.clone(),
..Default::default()
});
set_prompt_result(true);
assert_eq!(
perms1.request_read(&Some(Path::new("/foo/baz"))),
PermissionState::Allow
2019-11-11 10:33:29 -05:00
);
let mut perms2 = DenoPermissions::from_flags(&DenoFlags {
2019-12-23 09:59:44 -05:00
read_whitelist: whitelist,
2019-11-11 10:33:29 -05:00
..Default::default()
});
set_prompt_result(false);
assert_eq!(
perms2.request_read(&Some(Path::new("/foo/baz"))),
PermissionState::Deny
2019-11-11 10:33:29 -05:00
);
}
#[test]
fn test_permissions_request_write() {
let whitelist = svec!["/foo/bar"];
let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
write_whitelist: whitelist.clone(),
..Default::default()
});
set_prompt_result(false);
// If the whitelist contains the path, then the result is `allow`
// regardless of prompt result
assert_eq!(
perms0.request_write(&Some(Path::new("/foo/bar"))),
PermissionState::Allow
2019-11-11 10:33:29 -05:00
);
let mut perms1 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
write_whitelist: whitelist.clone(),
..Default::default()
});
set_prompt_result(true);
assert_eq!(
perms1.request_write(&Some(Path::new("/foo/baz"))),
PermissionState::Allow
2019-11-11 10:33:29 -05:00
);
let mut perms2 = DenoPermissions::from_flags(&DenoFlags {
2019-12-23 09:59:44 -05:00
write_whitelist: whitelist,
2019-11-11 10:33:29 -05:00
..Default::default()
});
set_prompt_result(false);
assert_eq!(
perms2.request_write(&Some(Path::new("/foo/baz"))),
PermissionState::Deny
2019-11-11 10:33:29 -05:00
);
}
#[test]
fn test_permission_request_net() {
let whitelist = svec!["localhost:8080"];
let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
net_whitelist: whitelist.clone(),
..Default::default()
});
set_prompt_result(false);
// If the url matches the whitelist item, then the result is `allow`
// regardless of prompt result
assert_eq!(
perms0
.request_net(&Some("http://localhost:8080/"))
.expect("Testing expect"),
PermissionState::Allow
2019-11-11 10:33:29 -05:00
);
let mut perms1 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
net_whitelist: whitelist.clone(),
..Default::default()
});
set_prompt_result(true);
assert_eq!(
perms1
.request_net(&Some("http://deno.land/"))
.expect("Testing expect"),
PermissionState::Allow
2019-11-11 10:33:29 -05:00
);
let mut perms2 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
net_whitelist: whitelist.clone(),
..Default::default()
});
set_prompt_result(false);
assert_eq!(
perms2
.request_net(&Some("http://deno.land/"))
.expect("Testing expect"),
PermissionState::Deny
2019-11-11 10:33:29 -05:00
);
let mut perms3 = DenoPermissions::from_flags(&DenoFlags {
2019-12-23 09:59:44 -05:00
net_whitelist: whitelist,
2019-11-11 10:33:29 -05:00
..Default::default()
});
set_prompt_result(true);
assert!(perms3.request_net(&Some(":")).is_err());
}
#[test]
fn test_permissions_request_env() {
let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
..Default::default()
});
set_prompt_result(true);
assert_eq!(perms0.request_env(), PermissionState::Allow);
2019-11-11 10:33:29 -05:00
let mut perms1 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
..Default::default()
});
set_prompt_result(false);
assert_eq!(perms1.request_env(), PermissionState::Deny);
2019-11-11 10:33:29 -05:00
}
#[test]
fn test_permissions_request_plugin() {
let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
..Default::default()
});
set_prompt_result(true);
assert_eq!(perms0.request_plugin(), PermissionState::Allow);
let mut perms1 = DenoPermissions::from_flags(&DenoFlags {
..Default::default()
});
set_prompt_result(false);
assert_eq!(perms1.request_plugin(), PermissionState::Deny);
}
2019-11-11 10:33:29 -05:00
#[test]
fn test_permissions_request_hrtime() {
let mut perms0 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
..Default::default()
});
set_prompt_result(true);
assert_eq!(perms0.request_hrtime(), PermissionState::Allow);
2019-11-11 10:33:29 -05:00
let mut perms1 = DenoPermissions::from_flags(&DenoFlags {
2019-11-11 10:33:29 -05:00
..Default::default()
});
set_prompt_result(false);
assert_eq!(perms1.request_hrtime(), PermissionState::Deny);
2019-11-11 10:33:29 -05:00
}
}