2020-01-02 15:13:47 -05:00
|
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-02-24 19:30:17 -05:00
|
|
|
|
use crate::colors;
|
2020-02-26 05:52:15 -05:00
|
|
|
|
use crate::flags::Flags;
|
2020-05-29 11:27:43 -04:00
|
|
|
|
use crate::fs::resolve_from_cwd;
|
2020-02-23 14:51:29 -05:00
|
|
|
|
use crate::op_error::OpError;
|
2020-05-29 07:00:47 -04:00
|
|
|
|
use serde::de;
|
|
|
|
|
use serde::Deserialize;
|
2019-05-08 19:20:30 -04:00
|
|
|
|
use std::collections::HashSet;
|
2020-05-29 11:27:43 -04:00
|
|
|
|
use std::env::current_dir;
|
2019-03-18 16:46:23 -04:00
|
|
|
|
use std::fmt;
|
2019-11-11 10:33:29 -05:00
|
|
|
|
#[cfg(not(test))]
|
|
|
|
|
use std::io;
|
2020-01-20 09:45:44 -05:00
|
|
|
|
use std::path::{Path, PathBuf};
|
2019-11-11 10:33:29 -05:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
use std::sync::atomic::AtomicBool;
|
2019-11-24 10:42:30 -05:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
use std::sync::atomic::Ordering;
|
2020-02-24 09:13:03 -05:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
use std::sync::Mutex;
|
2019-10-27 11:22:53 -04:00
|
|
|
|
use url::Url;
|
2019-03-18 16:46:23 -04:00
|
|
|
|
|
2019-06-22 12:02:51 -04:00
|
|
|
|
const PERMISSION_EMOJI: &str = "⚠️";
|
|
|
|
|
|
2019-03-18 16:46:23 -04:00
|
|
|
|
/// Tri-state value for storing permission state
|
2019-11-24 10:42:30 -05:00
|
|
|
|
#[derive(PartialEq, Debug, Clone, Copy)]
|
|
|
|
|
pub enum PermissionState {
|
2019-03-18 16:46:23 -04:00
|
|
|
|
Allow = 0,
|
|
|
|
|
Ask = 1,
|
|
|
|
|
Deny = 2,
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
impl PermissionState {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
/// Checks the permission state and returns the result.
|
2020-02-23 14:51:29 -05:00
|
|
|
|
pub fn check(self, msg: &str, flag_name: &str) -> Result<(), OpError> {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
if self == PermissionState::Allow {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
log_perm_access(msg);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2020-01-27 21:13:17 -05:00
|
|
|
|
let m = format!("{}, run again with the {} flag", msg, flag_name);
|
2020-02-23 14:51:29 -05:00
|
|
|
|
Err(OpError::permission_denied(m))
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
2019-11-24 10:42:30 -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
|
|
|
|
|
}
|
2020-06-06 10:56:21 -04:00
|
|
|
|
|
|
|
|
|
pub fn fork(self, value: bool) -> Result<PermissionState, OpError> {
|
|
|
|
|
if value && self == PermissionState::Deny {
|
|
|
|
|
Err(OpError::permission_denied(
|
|
|
|
|
"Arguments escalate parent permissions.".to_string(),
|
|
|
|
|
))
|
|
|
|
|
} else if value {
|
|
|
|
|
Ok(PermissionState::Allow)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(PermissionState::Deny)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
impl From<usize> for PermissionState {
|
2019-03-18 16:46:23 -04:00
|
|
|
|
fn from(val: usize) -> Self {
|
|
|
|
|
match val {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
0 => PermissionState::Allow,
|
|
|
|
|
1 => PermissionState::Ask,
|
|
|
|
|
2 => PermissionState::Deny,
|
2019-03-18 16:46:23 -04:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
impl From<bool> for PermissionState {
|
2019-03-18 16:46:23 -04:00
|
|
|
|
fn from(val: bool) -> Self {
|
2019-03-20 18:55:52 -04:00
|
|
|
|
if val {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Allow
|
2019-03-20 18:55:52 -04:00
|
|
|
|
} else {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Ask
|
2019-03-18 16:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
impl fmt::Display for PermissionState {
|
2019-03-18 16:46:23 -04:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Allow => f.pad("granted"),
|
|
|
|
|
PermissionState::Ask => f.pad("prompt"),
|
|
|
|
|
PermissionState::Deny => f.pad("denied"),
|
2019-03-18 16:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
impl Default for PermissionState {
|
2019-03-18 16:46:23 -04:00
|
|
|
|
fn default() -> Self {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Ask
|
2019-03-18 16:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-27 09:11:39 -04:00
|
|
|
|
|
2020-05-29 07:00:47 -04:00
|
|
|
|
struct BoolPermVisitor;
|
|
|
|
|
|
|
|
|
|
fn deserialize_permission_state<'de, D>(
|
|
|
|
|
d: D,
|
|
|
|
|
) -> Result<PermissionState, D::Error>
|
|
|
|
|
where
|
|
|
|
|
D: de::Deserializer<'de>,
|
|
|
|
|
{
|
|
|
|
|
impl<'de> de::Visitor<'de> for BoolPermVisitor {
|
|
|
|
|
type Value = PermissionState;
|
|
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
formatter.write_str("a boolean value")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
|
|
|
|
|
where
|
|
|
|
|
E: de::Error,
|
|
|
|
|
{
|
|
|
|
|
if value {
|
|
|
|
|
Ok(PermissionState::Allow)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(PermissionState::Deny)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
d.deserialize_bool(BoolPermVisitor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
|
2020-05-04 14:10:59 -04:00
|
|
|
|
pub struct Permissions {
|
2019-10-27 11:22:53 -04:00
|
|
|
|
// Keep in sync with cli/js/permissions.ts
|
2020-05-29 07:00:47 -04:00
|
|
|
|
#[serde(deserialize_with = "deserialize_permission_state")]
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub allow_read: PermissionState,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
pub read_allowlist: HashSet<PathBuf>,
|
2020-05-29 07:00:47 -04:00
|
|
|
|
#[serde(deserialize_with = "deserialize_permission_state")]
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub allow_write: PermissionState,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
pub write_allowlist: HashSet<PathBuf>,
|
2020-05-29 07:00:47 -04:00
|
|
|
|
#[serde(deserialize_with = "deserialize_permission_state")]
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub allow_net: PermissionState,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
pub net_allowlist: HashSet<String>,
|
2020-05-29 07:00:47 -04:00
|
|
|
|
#[serde(deserialize_with = "deserialize_permission_state")]
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub allow_env: PermissionState,
|
2020-05-29 07:00:47 -04:00
|
|
|
|
#[serde(deserialize_with = "deserialize_permission_state")]
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub allow_run: PermissionState,
|
2020-05-29 07:00:47 -04:00
|
|
|
|
#[serde(deserialize_with = "deserialize_permission_state")]
|
2019-12-05 15:30:20 -05:00
|
|
|
|
pub allow_plugin: PermissionState,
|
2020-05-29 07:00:47 -04:00
|
|
|
|
#[serde(deserialize_with = "deserialize_permission_state")]
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub allow_hrtime: PermissionState,
|
2018-10-27 09:11:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 13:09:39 -04:00
|
|
|
|
fn resolve_fs_allowlist(allowlist: &[PathBuf]) -> HashSet<PathBuf> {
|
|
|
|
|
allowlist
|
2020-05-29 11:27:43 -04:00
|
|
|
|
.iter()
|
|
|
|
|
.map(|raw_path| resolve_from_cwd(Path::new(&raw_path)).unwrap())
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
impl Permissions {
|
2020-02-26 05:52:15 -05:00
|
|
|
|
pub fn from_flags(flags: &Flags) -> Self {
|
2018-11-05 01:21:21 -05:00
|
|
|
|
Self {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
allow_read: PermissionState::from(flags.allow_read),
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist: resolve_fs_allowlist(&flags.read_allowlist),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
allow_write: PermissionState::from(flags.allow_write),
|
2020-06-13 13:09:39 -04:00
|
|
|
|
write_allowlist: resolve_fs_allowlist(&flags.write_allowlist),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
allow_net: PermissionState::from(flags.allow_net),
|
2020-06-13 13:09:39 -04:00
|
|
|
|
net_allowlist: flags.net_allowlist.iter().cloned().collect(),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
allow_env: PermissionState::from(flags.allow_env),
|
|
|
|
|
allow_run: PermissionState::from(flags.allow_run),
|
2019-12-05 15:30:20 -05:00
|
|
|
|
allow_plugin: PermissionState::from(flags.allow_plugin),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
allow_hrtime: PermissionState::from(flags.allow_hrtime),
|
2018-10-27 09:11:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 11:27:43 -04:00
|
|
|
|
/// Arbitrary helper. Resolves the path from CWD, and also gets a path that
|
|
|
|
|
/// can be displayed without leaking the CWD when not allowed.
|
|
|
|
|
fn resolved_and_display_path(&self, path: &Path) -> (PathBuf, PathBuf) {
|
|
|
|
|
let resolved_path = resolve_from_cwd(path).unwrap();
|
|
|
|
|
let display_path = if path.is_absolute() {
|
|
|
|
|
path.to_path_buf()
|
|
|
|
|
} else {
|
|
|
|
|
match self
|
|
|
|
|
.get_state_read(&Some(¤t_dir().unwrap()))
|
|
|
|
|
.check("", "")
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => resolved_path.clone(),
|
|
|
|
|
Err(_) => path.to_path_buf(),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
(resolved_path, display_path)
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 07:13:27 -04:00
|
|
|
|
pub fn allow_all() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
allow_read: PermissionState::from(true),
|
|
|
|
|
allow_write: PermissionState::from(true),
|
|
|
|
|
allow_net: PermissionState::from(true),
|
|
|
|
|
allow_env: PermissionState::from(true),
|
|
|
|
|
allow_run: PermissionState::from(true),
|
|
|
|
|
allow_plugin: PermissionState::from(true),
|
|
|
|
|
allow_hrtime: PermissionState::from(true),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
|
pub fn check_run(&self) -> Result<(), OpError> {
|
2020-01-27 21:13:17 -05:00
|
|
|
|
self
|
|
|
|
|
.allow_run
|
|
|
|
|
.check("access to run a subprocess", "--allow-run")
|
2019-10-27 11:22:53 -04:00
|
|
|
|
}
|
2019-06-22 12:02:51 -04:00
|
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
|
fn get_state_read(&self, path: &Option<&Path>) -> PermissionState {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
if path.map_or(false, |f| check_path_white_list(f, &self.read_allowlist)) {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
return PermissionState::Allow;
|
2018-11-15 23:07:40 -05:00
|
|
|
|
}
|
2019-11-24 10:42:30 -05:00
|
|
|
|
self.allow_read
|
2018-11-15 23:07:40 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
|
pub fn check_read(&self, path: &Path) -> Result<(), OpError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
let (resolved_path, display_path) = self.resolved_and_display_path(path);
|
|
|
|
|
self.get_state_read(&Some(&resolved_path)).check(
|
|
|
|
|
&format!("read access to \"{}\"", display_path.display()),
|
2020-01-27 21:13:17 -05:00
|
|
|
|
"--allow-read",
|
2019-10-27 11:22:53 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 11:27:43 -04:00
|
|
|
|
/// As `check_read()`, but permission error messages will anonymize the path
|
|
|
|
|
/// by replacing it with the given `display`.
|
|
|
|
|
pub fn check_read_blind(
|
|
|
|
|
&self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
display: &str,
|
|
|
|
|
) -> Result<(), OpError> {
|
|
|
|
|
let resolved_path = resolve_from_cwd(path).unwrap();
|
|
|
|
|
self
|
|
|
|
|
.get_state_read(&Some(&resolved_path))
|
|
|
|
|
.check(&format!("read access to <{}>", display), "--allow-read")
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
|
fn get_state_write(&self, path: &Option<&Path>) -> PermissionState {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
if path.map_or(false, |f| check_path_white_list(f, &self.write_allowlist)) {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
return PermissionState::Allow;
|
2019-02-08 15:59:38 -05:00
|
|
|
|
}
|
2019-11-24 10:42:30 -05:00
|
|
|
|
self.allow_write
|
2019-02-08 15:59:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
|
pub fn check_write(&self, path: &Path) -> Result<(), OpError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
let (resolved_path, display_path) = self.resolved_and_display_path(path);
|
|
|
|
|
self.get_state_write(&Some(&resolved_path)).check(
|
|
|
|
|
&format!("write access to \"{}\"", display_path.display()),
|
2020-01-27 21:13:17 -05:00
|
|
|
|
"--allow-write",
|
2019-10-27 11:22:53 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
fn get_state_net(&self, host: &str, port: Option<u16>) -> PermissionState {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
if check_host_and_port_allowlist(host, port, &self.net_allowlist) {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
return PermissionState::Allow;
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|
2019-11-24 10:42:30 -05:00
|
|
|
|
self.allow_net
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 10:33:29 -05:00
|
|
|
|
fn get_state_net_url(
|
|
|
|
|
&self,
|
|
|
|
|
url: &Option<&str>,
|
2020-02-23 14:51:29 -05:00
|
|
|
|
) -> Result<PermissionState, OpError> {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
if url.is_none() {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
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.
|
2020-02-23 14:51:29 -05:00
|
|
|
|
let parsed = Url::parse(url).map_err(OpError::from)?;
|
2020-06-26 17:37:03 -04:00
|
|
|
|
// 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(),
|
|
|
|
|
));
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
Ok(
|
|
|
|
|
self.get_state_net(&format!("{}", parsed.host().unwrap()), parsed.port()),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
|
pub fn check_net(&self, hostname: &str, port: u16) -> Result<(), OpError> {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
self.get_state_net(hostname, Some(port)).check(
|
2019-10-27 11:22:53 -04:00
|
|
|
|
&format!("network access to \"{}:{}\"", hostname, port),
|
2020-01-27 21:13:17 -05:00
|
|
|
|
"--allow-net",
|
2019-10-27 11:22:53 -04:00
|
|
|
|
)
|
2018-10-27 09:11:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
|
pub fn check_net_url(&self, url: &url::Url) -> Result<(), OpError> {
|
|
|
|
|
let host = url
|
|
|
|
|
.host_str()
|
|
|
|
|
.ok_or_else(|| OpError::uri_error("missing host".to_owned()))?;
|
2019-11-11 10:33:29 -05:00
|
|
|
|
self
|
2020-02-23 09:45:02 -05:00
|
|
|
|
.get_state_net(host, url.port())
|
2020-01-27 21:13:17 -05:00
|
|
|
|
.check(&format!("network access to \"{}\"", url), "--allow-net")
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
|
pub fn check_env(&self) -> Result<(), OpError> {
|
2020-01-27 21:13:17 -05:00
|
|
|
|
self
|
|
|
|
|
.allow_env
|
|
|
|
|
.check("access to environment variables", "--allow-env")
|
2019-03-13 12:43:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
|
pub fn check_plugin(&self, path: &Path) -> Result<(), OpError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
let (_, display_path) = self.resolved_and_display_path(path);
|
2019-12-05 15:30:20 -05:00
|
|
|
|
self.allow_plugin.check(
|
2020-05-29 11:27:43 -04:00
|
|
|
|
&format!("access to open a plugin: {}", display_path.display()),
|
2020-01-27 21:13:17 -05:00
|
|
|
|
"--allow-plugin",
|
2019-12-05 15:30:20 -05:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub fn request_run(&mut self) -> PermissionState {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
self
|
|
|
|
|
.allow_run
|
2020-03-24 00:54:17 -04:00
|
|
|
|
.request("Deno requests to access to run a subprocess")
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
|
pub fn request_read(&mut self, path: &Option<&Path>) -> PermissionState {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
let paths = path.map(|p| self.resolved_and_display_path(p));
|
|
|
|
|
if let Some((p, _)) = paths.as_ref() {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
if check_path_white_list(&p, &self.read_allowlist) {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
return PermissionState::Allow;
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
};
|
2020-05-29 11:27:43 -04:00
|
|
|
|
self.allow_read.request(&match paths {
|
2020-03-24 00:54:17 -04:00
|
|
|
|
None => "Deno requests read access".to_string(),
|
2020-05-29 11:27:43 -04:00
|
|
|
|
Some((_, display_path)) => format!(
|
|
|
|
|
"Deno requests read access to \"{}\"",
|
|
|
|
|
display_path.display()
|
|
|
|
|
),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
|
pub fn request_write(&mut self, path: &Option<&Path>) -> PermissionState {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
let paths = path.map(|p| self.resolved_and_display_path(p));
|
|
|
|
|
if let Some((p, _)) = paths.as_ref() {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
if check_path_white_list(&p, &self.write_allowlist) {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
return PermissionState::Allow;
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
};
|
2020-05-29 11:27:43 -04:00
|
|
|
|
self.allow_write.request(&match paths {
|
2020-03-24 00:54:17 -04:00
|
|
|
|
None => "Deno requests write access".to_string(),
|
2020-05-29 11:27:43 -04:00
|
|
|
|
Some((_, display_path)) => format!(
|
|
|
|
|
"Deno requests write access to \"{}\"",
|
|
|
|
|
display_path.display()
|
|
|
|
|
),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request_net(
|
2019-11-24 10:42:30 -05:00
|
|
|
|
&mut self,
|
2019-11-11 10:33:29 -05:00
|
|
|
|
url: &Option<&str>,
|
2020-02-23 14:51:29 -05:00
|
|
|
|
) -> Result<PermissionState, OpError> {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
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 {
|
2020-03-24 00:54:17 -04:00
|
|
|
|
None => "Deno requests network access".to_string(),
|
|
|
|
|
Some(url) => format!("Deno requests network access to \"{}\"", url),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
self.get_state_net_url(url)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub fn request_env(&mut self) -> PermissionState {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
self
|
|
|
|
|
.allow_env
|
2020-03-24 00:54:17 -04:00
|
|
|
|
.request("Deno requests to access to environment variables")
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub fn request_hrtime(&mut self) -> PermissionState {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
self
|
|
|
|
|
.allow_hrtime
|
2020-03-24 00:54:17 -04:00
|
|
|
|
.request("Deno requests to access to high precision time")
|
2019-06-22 12:02:51 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 15:30:20 -05:00
|
|
|
|
pub fn request_plugin(&mut self) -> PermissionState {
|
2020-03-24 00:54:17 -04:00
|
|
|
|
self.allow_plugin.request("Deno requests to open plugins")
|
2019-12-05 15:30:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
|
pub fn get_permission_state(
|
|
|
|
|
&self,
|
|
|
|
|
name: &str,
|
|
|
|
|
url: &Option<&str>,
|
2020-01-20 09:45:44 -05:00
|
|
|
|
path: &Option<&Path>,
|
2020-02-23 14:51:29 -05:00
|
|
|
|
) -> Result<PermissionState, OpError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
let path = path.map(|p| resolve_from_cwd(p).unwrap());
|
|
|
|
|
let path = path.as_deref();
|
2019-10-27 11:22:53 -04:00
|
|
|
|
match name {
|
2019-11-24 10:42:30 -05:00
|
|
|
|
"run" => Ok(self.allow_run),
|
2020-05-29 11:27:43 -04:00
|
|
|
|
"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),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
"env" => Ok(self.allow_env),
|
2019-12-05 15:30:20 -05:00
|
|
|
|
"plugin" => Ok(self.allow_plugin),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
"hrtime" => Ok(self.allow_hrtime),
|
2020-02-23 14:51:29 -05:00
|
|
|
|
n => Err(OpError::other(format!("No such permission name: {}", n))),
|
2019-10-27 11:22:53 -04:00
|
|
|
|
}
|
2019-04-08 16:22:40 -04:00
|
|
|
|
}
|
2020-06-06 10:56:21 -04:00
|
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
|
pub fn fork(
|
|
|
|
|
&self,
|
|
|
|
|
allow_read: bool,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist: HashSet<PathBuf>,
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_write: bool,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
write_allowlist: HashSet<PathBuf>,
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_net: bool,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
net_allowlist: HashSet<String>,
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_env: bool,
|
|
|
|
|
allow_run: bool,
|
|
|
|
|
allow_plugin: bool,
|
|
|
|
|
allow_hrtime: bool,
|
|
|
|
|
) -> Result<Permissions, OpError> {
|
|
|
|
|
let allow_read = self.allow_read.fork(allow_read)?;
|
|
|
|
|
let allow_write = self.allow_write.fork(allow_write)?;
|
|
|
|
|
let allow_net = self.allow_net.fork(allow_net)?;
|
|
|
|
|
let allow_env = self.allow_env.fork(allow_env)?;
|
|
|
|
|
let allow_run = self.allow_run.fork(allow_run)?;
|
|
|
|
|
let allow_plugin = self.allow_plugin.fork(allow_plugin)?;
|
|
|
|
|
let allow_hrtime = self.allow_hrtime.fork(allow_hrtime)?;
|
2020-06-13 13:09:39 -04:00
|
|
|
|
if !(read_allowlist.is_subset(&self.read_allowlist)) {
|
2020-06-06 10:56:21 -04:00
|
|
|
|
Err(OpError::permission_denied(format!(
|
2020-06-13 13:09:39 -04:00
|
|
|
|
"Arguments escalate parent permissions. Parent Permissions have only {:?} in `read_allowlist`",
|
|
|
|
|
self.read_allowlist
|
2020-06-06 10:56:21 -04:00
|
|
|
|
)))
|
2020-06-13 13:09:39 -04:00
|
|
|
|
} else if !(write_allowlist.is_subset(&self.write_allowlist)) {
|
2020-06-06 10:56:21 -04:00
|
|
|
|
Err(OpError::permission_denied(format!(
|
2020-06-13 13:09:39 -04:00
|
|
|
|
"Arguments escalate parent permissions. Parent Permissions have only {:?} in `write_allowlist`",
|
|
|
|
|
self.write_allowlist
|
2020-06-06 10:56:21 -04:00
|
|
|
|
)))
|
2020-06-13 13:09:39 -04:00
|
|
|
|
} else if !(net_allowlist.is_subset(&self.net_allowlist)) {
|
2020-06-06 10:56:21 -04:00
|
|
|
|
Err(OpError::permission_denied(format!(
|
2020-06-13 13:09:39 -04:00
|
|
|
|
"Arguments escalate parent permissions. Parent Permissions have only {:?} in `net_allowlist`",
|
|
|
|
|
self.net_allowlist
|
2020-06-06 10:56:21 -04:00
|
|
|
|
)))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Permissions {
|
|
|
|
|
allow_read,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist,
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_write,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
write_allowlist,
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_net,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
net_allowlist,
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_env,
|
|
|
|
|
allow_run,
|
|
|
|
|
allow_plugin,
|
|
|
|
|
allow_hrtime,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-18 16:46:23 -04:00
|
|
|
|
}
|
2019-03-04 11:04:19 -05:00
|
|
|
|
|
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.
|
2020-06-29 08:17:37 -04:00
|
|
|
|
eprint!("{}", colors::bold(&msg));
|
2019-11-11 10:33:29 -05:00
|
|
|
|
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);
|
2020-06-29 08:17:37 -04:00
|
|
|
|
eprint!("{}", colors::bold(&msg_again));
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-24 09:13:03 -05:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
lazy_static! {
|
|
|
|
|
/// Lock this when you use `set_prompt_result` in a test case.
|
|
|
|
|
static ref PERMISSION_PROMPT_GUARD: Mutex<()> = Mutex::new(());
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 10:33:29 -05:00
|
|
|
|
#[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) {
|
2020-03-10 08:26:17 -04:00
|
|
|
|
debug!(
|
|
|
|
|
"{}",
|
2020-06-29 08:17:37 -04:00
|
|
|
|
colors::bold(&format!("{}️ Granted {}", PERMISSION_EMOJI, message))
|
2020-03-10 08:26:17 -04:00
|
|
|
|
);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 04:29:36 -05:00
|
|
|
|
fn check_path_white_list(path: &Path, white_list: &HashSet<PathBuf>) -> bool {
|
2020-01-20 09:45:44 -05:00
|
|
|
|
let mut path_buf = PathBuf::from(path);
|
2019-05-08 19:20:30 -04:00
|
|
|
|
loop {
|
2020-02-11 04:29:36 -05:00
|
|
|
|
if white_list.contains(&path_buf) {
|
2019-05-08 19:20:30 -04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if !path_buf.pop() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 13:09:39 -04:00
|
|
|
|
fn check_host_and_port_allowlist(
|
2019-10-27 11:22:53 -04:00
|
|
|
|
host: &str,
|
|
|
|
|
port: Option<u16>,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
allowlist: &HashSet<String>,
|
2019-10-27 11:22:53 -04:00
|
|
|
|
) -> bool {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
allowlist.contains(host)
|
2019-10-27 11:22:53 -04:00
|
|
|
|
|| (port.is_some()
|
2020-06-13 13:09:39 -04:00
|
|
|
|
&& allowlist.contains(&format!("{}:{}", host, port.unwrap())))
|
2019-10-27 11:22:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
|
#[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() {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
let allowlist = vec![
|
2020-02-11 04:29:36 -05:00
|
|
|
|
PathBuf::from("/a/specific/dir/name"),
|
|
|
|
|
PathBuf::from("/a/specific"),
|
|
|
|
|
PathBuf::from("/b/c"),
|
|
|
|
|
];
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let perms = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist: allowlist.clone(),
|
|
|
|
|
write_allowlist: allowlist,
|
2019-05-08 19:20:30 -04:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Inside of /a/specific and /a/specific/dir/name
|
2020-01-20 09:45:44 -05:00
|
|
|
|
assert!(perms.check_read(Path::new("/a/specific/dir/name")).is_ok());
|
|
|
|
|
assert!(perms.check_write(Path::new("/a/specific/dir/name")).is_ok());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Inside of /a/specific but outside of /a/specific/dir/name
|
2020-01-20 09:45:44 -05:00
|
|
|
|
assert!(perms.check_read(Path::new("/a/specific/dir")).is_ok());
|
|
|
|
|
assert!(perms.check_write(Path::new("/a/specific/dir")).is_ok());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Inside of /a/specific and /a/specific/dir/name
|
2020-01-20 09:45:44 -05:00
|
|
|
|
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());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Inside of /a/specific but outside of /a/specific/dir/name
|
2020-01-20 09:45:44 -05:00
|
|
|
|
assert!(perms.check_read(Path::new("/a/specific/other/dir")).is_ok());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.check_write(Path::new("/a/specific/other/dir"))
|
|
|
|
|
.is_ok());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Exact match with /b/c
|
2020-01-20 09:45:44 -05:00
|
|
|
|
assert!(perms.check_read(Path::new("/b/c")).is_ok());
|
|
|
|
|
assert!(perms.check_write(Path::new("/b/c")).is_ok());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Sub path within /b/c
|
2020-01-20 09:45:44 -05:00
|
|
|
|
assert!(perms.check_read(Path::new("/b/c/sub/path")).is_ok());
|
|
|
|
|
assert!(perms.check_write(Path::new("/b/c/sub/path")).is_ok());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2020-05-29 11:27:43 -04:00
|
|
|
|
// Sub path within /b/c, needs normalizing
|
|
|
|
|
assert!(perms
|
|
|
|
|
.check_read(Path::new("/b/c/sub/path/../path/."))
|
|
|
|
|
.is_ok());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.check_write(Path::new("/b/c/sub/path/../path/."))
|
|
|
|
|
.is_ok());
|
|
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
|
// Inside of /b but outside of /b/c
|
2020-01-20 09:45:44 -05:00
|
|
|
|
assert!(perms.check_read(Path::new("/b/e")).is_err());
|
|
|
|
|
assert!(perms.check_write(Path::new("/b/e")).is_err());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Inside of /a but outside of /a/specific
|
2020-01-20 09:45:44 -05:00
|
|
|
|
assert!(perms.check_read(Path::new("/a/b")).is_err());
|
|
|
|
|
assert!(perms.check_write(Path::new("/a/b")).is_err());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-06-24 19:30:11 -04:00
|
|
|
|
fn test_check_net() {
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let perms = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
net_allowlist: svec![
|
2019-05-08 19:20:30 -04:00
|
|
|
|
"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![
|
2019-10-23 10:19:27 -04:00
|
|
|
|
("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
|
2019-10-23 10:19:27 -04:00
|
|
|
|
("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();
|
2019-08-13 14:51:15 -04:00
|
|
|
|
assert_eq!(*is_ok, perms.check_net_url(&u).is_ok());
|
2019-06-24 19:30:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-23 10:19:27 -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-05-08 19:20:30 -04:00
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_permissions_request_run() {
|
2020-02-24 09:13:03 -05:00
|
|
|
|
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms0 = Permissions::from_flags(&Flags {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(true);
|
2019-11-24 10:42:30 -05:00
|
|
|
|
assert_eq!(perms0.request_run(), PermissionState::Allow);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms1 = Permissions::from_flags(&Flags {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
2019-11-24 10:42:30 -05:00
|
|
|
|
assert_eq!(perms1.request_run(), PermissionState::Deny);
|
2020-02-24 09:13:03 -05:00
|
|
|
|
drop(guard);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_permissions_request_read() {
|
2020-02-24 09:13:03 -05:00
|
|
|
|
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
|
2020-06-13 13:09:39 -04:00
|
|
|
|
let allowlist = vec![PathBuf::from("/foo/bar")];
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms0 = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist: allowlist.clone(),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
2020-06-13 13:09:39 -04:00
|
|
|
|
// If the allowlist contains the path, then the result is `allow`
|
2019-11-11 10:33:29 -05:00
|
|
|
|
// regardless of prompt result
|
|
|
|
|
assert_eq!(
|
2020-01-20 09:45:44 -05:00
|
|
|
|
perms0.request_read(&Some(Path::new("/foo/bar"))),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Allow
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms1 = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist: allowlist.clone(),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(true);
|
|
|
|
|
assert_eq!(
|
2020-01-20 09:45:44 -05:00
|
|
|
|
perms1.request_read(&Some(Path::new("/foo/baz"))),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Allow
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms2 = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist: allowlist,
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
|
|
|
|
assert_eq!(
|
2020-01-20 09:45:44 -05:00
|
|
|
|
perms2.request_read(&Some(Path::new("/foo/baz"))),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Deny
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
2020-02-24 09:13:03 -05:00
|
|
|
|
drop(guard);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_permissions_request_write() {
|
2020-02-24 09:13:03 -05:00
|
|
|
|
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
|
2020-06-13 13:09:39 -04:00
|
|
|
|
let allowlist = vec![PathBuf::from("/foo/bar")];
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms0 = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
write_allowlist: allowlist.clone(),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
2020-06-13 13:09:39 -04:00
|
|
|
|
// If the allowlist contains the path, then the result is `allow`
|
2019-11-11 10:33:29 -05:00
|
|
|
|
// regardless of prompt result
|
|
|
|
|
assert_eq!(
|
2020-01-20 09:45:44 -05:00
|
|
|
|
perms0.request_write(&Some(Path::new("/foo/bar"))),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Allow
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms1 = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
write_allowlist: allowlist.clone(),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(true);
|
|
|
|
|
assert_eq!(
|
2020-01-20 09:45:44 -05:00
|
|
|
|
perms1.request_write(&Some(Path::new("/foo/baz"))),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Allow
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms2 = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
write_allowlist: allowlist,
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
|
|
|
|
assert_eq!(
|
2020-01-20 09:45:44 -05:00
|
|
|
|
perms2.request_write(&Some(Path::new("/foo/baz"))),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Deny
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
2020-02-24 09:13:03 -05:00
|
|
|
|
drop(guard);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_permission_request_net() {
|
2020-02-24 09:13:03 -05:00
|
|
|
|
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
|
2020-06-13 13:09:39 -04:00
|
|
|
|
let allowlist = svec!["localhost:8080"];
|
2019-11-11 10:33:29 -05:00
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms0 = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
net_allowlist: allowlist.clone(),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
2020-06-13 13:09:39 -04:00
|
|
|
|
// If the url matches the allowlist item, then the result is `allow`
|
2019-11-11 10:33:29 -05:00
|
|
|
|
// regardless of prompt result
|
|
|
|
|
assert_eq!(
|
|
|
|
|
perms0
|
|
|
|
|
.request_net(&Some("http://localhost:8080/"))
|
|
|
|
|
.expect("Testing expect"),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Allow
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms1 = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
net_allowlist: allowlist.clone(),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(true);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
perms1
|
|
|
|
|
.request_net(&Some("http://deno.land/"))
|
|
|
|
|
.expect("Testing expect"),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Allow
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms2 = Permissions::from_flags(&Flags {
|
2020-06-13 13:09:39 -04:00
|
|
|
|
net_allowlist: allowlist.clone(),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
perms2
|
|
|
|
|
.request_net(&Some("http://deno.land/"))
|
|
|
|
|
.expect("Testing expect"),
|
2019-11-24 10:42:30 -05:00
|
|
|
|
PermissionState::Deny
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms3 = Permissions::from_flags(&Flags {
|
2020-06-26 17:37:03 -04:00
|
|
|
|
net_allowlist: allowlist.clone(),
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(true);
|
|
|
|
|
assert!(perms3.request_net(&Some(":")).is_err());
|
2020-06-26 17:37:03 -04:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
|
2020-02-24 09:13:03 -05:00
|
|
|
|
drop(guard);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_permissions_request_env() {
|
2020-02-24 09:13:03 -05:00
|
|
|
|
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms0 = Permissions::from_flags(&Flags {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(true);
|
2019-11-24 10:42:30 -05:00
|
|
|
|
assert_eq!(perms0.request_env(), PermissionState::Allow);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms1 = Permissions::from_flags(&Flags {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
2019-11-24 10:42:30 -05:00
|
|
|
|
assert_eq!(perms1.request_env(), PermissionState::Deny);
|
2020-02-24 09:13:03 -05:00
|
|
|
|
drop(guard);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 15:30:20 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_permissions_request_plugin() {
|
2020-02-24 09:13:03 -05:00
|
|
|
|
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms0 = Permissions::from_flags(&Flags {
|
2019-12-05 15:30:20 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(true);
|
|
|
|
|
assert_eq!(perms0.request_plugin(), PermissionState::Allow);
|
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms1 = Permissions::from_flags(&Flags {
|
2019-12-05 15:30:20 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
|
|
|
|
assert_eq!(perms1.request_plugin(), PermissionState::Deny);
|
2020-02-24 09:13:03 -05:00
|
|
|
|
drop(guard);
|
2019-12-05 15:30:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 10:33:29 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_permissions_request_hrtime() {
|
2020-02-24 09:13:03 -05:00
|
|
|
|
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms0 = Permissions::from_flags(&Flags {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(true);
|
2019-11-24 10:42:30 -05:00
|
|
|
|
assert_eq!(perms0.request_hrtime(), PermissionState::Allow);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
|
2020-05-04 14:10:59 -04:00
|
|
|
|
let mut perms1 = Permissions::from_flags(&Flags {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(false);
|
2019-11-24 10:42:30 -05:00
|
|
|
|
assert_eq!(perms1.request_hrtime(), PermissionState::Deny);
|
2020-02-24 09:13:03 -05:00
|
|
|
|
drop(guard);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
2020-05-29 07:00:47 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_deserialize_perms() {
|
|
|
|
|
let json_perms = r#"
|
|
|
|
|
{
|
|
|
|
|
"allow_read": true,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
"read_allowlist": [],
|
2020-05-29 07:00:47 -04:00
|
|
|
|
"allow_write": true,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
"write_allowlist": [],
|
2020-05-29 07:00:47 -04:00
|
|
|
|
"allow_net": true,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
"net_allowlist": [],
|
2020-05-29 07:00:47 -04:00
|
|
|
|
"allow_env": true,
|
|
|
|
|
"allow_run": true,
|
|
|
|
|
"allow_plugin": true,
|
|
|
|
|
"allow_hrtime": true
|
|
|
|
|
}
|
|
|
|
|
"#;
|
|
|
|
|
let perms0 = Permissions {
|
|
|
|
|
allow_read: PermissionState::Allow,
|
|
|
|
|
allow_write: PermissionState::Allow,
|
|
|
|
|
allow_net: PermissionState::Allow,
|
|
|
|
|
allow_hrtime: PermissionState::Allow,
|
|
|
|
|
allow_env: PermissionState::Allow,
|
|
|
|
|
allow_plugin: PermissionState::Allow,
|
|
|
|
|
allow_run: PermissionState::Allow,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist: HashSet::new(),
|
|
|
|
|
write_allowlist: HashSet::new(),
|
|
|
|
|
net_allowlist: HashSet::new(),
|
2020-05-29 07:00:47 -04:00
|
|
|
|
};
|
|
|
|
|
let deserialized_perms: Permissions =
|
|
|
|
|
serde_json::from_str(json_perms).unwrap();
|
|
|
|
|
assert_eq!(perms0, deserialized_perms);
|
|
|
|
|
}
|
2020-06-06 10:56:21 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_fork() {
|
|
|
|
|
let guard = PERMISSION_PROMPT_GUARD.lock().unwrap();
|
|
|
|
|
let perms0 = Permissions::from_flags(&Flags {
|
|
|
|
|
..Default::default()
|
|
|
|
|
});
|
|
|
|
|
set_prompt_result(true);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
perms0
|
|
|
|
|
.fork(
|
|
|
|
|
true,
|
|
|
|
|
HashSet::new(),
|
|
|
|
|
true,
|
|
|
|
|
HashSet::new(),
|
|
|
|
|
true,
|
|
|
|
|
HashSet::new(),
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.expect("Testing expect"),
|
|
|
|
|
Permissions {
|
|
|
|
|
allow_read: PermissionState::Allow,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist: HashSet::new(),
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_write: PermissionState::Allow,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
write_allowlist: HashSet::new(),
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_net: PermissionState::Allow,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
net_allowlist: HashSet::new(),
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_env: PermissionState::Allow,
|
|
|
|
|
allow_run: PermissionState::Allow,
|
|
|
|
|
allow_plugin: PermissionState::Deny,
|
|
|
|
|
allow_hrtime: PermissionState::Deny,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
set_prompt_result(false);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
perms0
|
|
|
|
|
.fork(
|
|
|
|
|
true,
|
|
|
|
|
HashSet::new(),
|
|
|
|
|
true,
|
|
|
|
|
HashSet::new(),
|
|
|
|
|
true,
|
|
|
|
|
HashSet::new(),
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.expect("Testing expect"),
|
|
|
|
|
Permissions {
|
|
|
|
|
allow_read: PermissionState::Allow,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
read_allowlist: HashSet::new(),
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_write: PermissionState::Allow,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
write_allowlist: HashSet::new(),
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_net: PermissionState::Allow,
|
2020-06-13 13:09:39 -04:00
|
|
|
|
net_allowlist: HashSet::new(),
|
2020-06-06 10:56:21 -04:00
|
|
|
|
allow_env: PermissionState::Allow,
|
|
|
|
|
allow_run: PermissionState::Allow,
|
|
|
|
|
allow_plugin: PermissionState::Deny,
|
|
|
|
|
allow_hrtime: PermissionState::Deny,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
drop(guard);
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|