2022-01-07 22:09:52 -05:00
|
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2020-02-24 19:30:17 -05:00
|
|
|
|
use crate::colors;
|
2020-11-16 14:48:50 -05:00
|
|
|
|
use crate::fs_util::resolve_from_cwd;
|
2020-09-14 12:48:57 -04:00
|
|
|
|
use deno_core::error::custom_error;
|
2022-09-29 06:34:16 -04:00
|
|
|
|
use deno_core::error::type_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
|
use deno_core::error::uri_error;
|
|
|
|
|
use deno_core::error::AnyError;
|
2021-07-06 23:48:01 -04:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
use deno_core::parking_lot::Mutex;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
use deno_core::serde::de;
|
2021-02-25 15:18:35 -05:00
|
|
|
|
use deno_core::serde::Deserialize;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
use deno_core::serde::Deserializer;
|
2021-02-25 15:18:35 -05:00
|
|
|
|
use deno_core::serde::Serialize;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
use deno_core::serde_json;
|
2020-09-16 14:28:07 -04:00
|
|
|
|
use deno_core::url;
|
2020-10-26 15:56:00 -04:00
|
|
|
|
use deno_core::ModuleSpecifier;
|
2021-04-14 15:10:48 -04:00
|
|
|
|
use deno_core::OpState;
|
2021-09-22 18:45:58 -04:00
|
|
|
|
use log;
|
2021-12-18 16:14:42 -05:00
|
|
|
|
use once_cell::sync::Lazy;
|
2019-05-08 19:20:30 -04:00
|
|
|
|
use std::collections::HashSet;
|
2019-03-18 16:46:23 -04:00
|
|
|
|
use std::fmt;
|
2020-08-18 16:29:32 -04:00
|
|
|
|
use std::hash::Hash;
|
2020-01-20 09:45:44 -05:00
|
|
|
|
use std::path::{Path, PathBuf};
|
2021-10-13 13:04:44 -04:00
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
use std::string::ToString;
|
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;
|
2019-03-18 16:46:23 -04:00
|
|
|
|
|
2019-06-22 12:02:51 -04:00
|
|
|
|
const PERMISSION_EMOJI: &str = "⚠️";
|
|
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
|
static DEBUG_LOG_ENABLED: Lazy<bool> =
|
|
|
|
|
Lazy::new(|| log::log_enabled!(log::Level::Debug));
|
2021-09-22 18:45:58 -04:00
|
|
|
|
|
2019-03-18 16:46:23 -04:00
|
|
|
|
/// Tri-state value for storing permission state
|
2022-09-19 04:25:03 -04:00
|
|
|
|
#[derive(Eq, PartialEq, Debug, Clone, Copy, Deserialize, PartialOrd)]
|
2019-11-24 10:42:30 -05:00
|
|
|
|
pub enum PermissionState {
|
2020-08-18 16:29:32 -04:00
|
|
|
|
Granted = 0,
|
|
|
|
|
Prompt = 1,
|
|
|
|
|
Denied = 2,
|
2019-03-18 16:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 10:42:30 -05:00
|
|
|
|
impl PermissionState {
|
2021-04-12 07:24:41 -04:00
|
|
|
|
#[inline(always)]
|
2022-08-27 11:20:05 -04:00
|
|
|
|
fn log_perm_access(name: &str, info: impl FnOnce() -> Option<String>) {
|
2021-09-22 18:45:58 -04:00
|
|
|
|
// Eliminates log overhead (when logging is disabled),
|
|
|
|
|
// log_enabled!(Debug) check in a hot path still has overhead
|
|
|
|
|
// TODO(AaronO): generalize or upstream this optimization
|
|
|
|
|
if *DEBUG_LOG_ENABLED {
|
|
|
|
|
log::debug!(
|
|
|
|
|
"{}",
|
|
|
|
|
colors::bold(&format!(
|
|
|
|
|
"{}️ Granted {}",
|
|
|
|
|
PERMISSION_EMOJI,
|
|
|
|
|
Self::fmt_access(name, info)
|
|
|
|
|
))
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-04-12 07:24:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 11:20:05 -04:00
|
|
|
|
fn fmt_access(name: &str, info: impl FnOnce() -> Option<String>) -> String {
|
2021-04-12 07:24:41 -04:00
|
|
|
|
format!(
|
|
|
|
|
"{} access{}",
|
|
|
|
|
name,
|
2022-08-27 11:20:05 -04:00
|
|
|
|
info().map_or(String::new(), |info| { format!(" to {}", info) }),
|
2021-04-12 07:24:41 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 11:20:05 -04:00
|
|
|
|
fn error(name: &str, info: impl FnOnce() -> Option<String>) -> AnyError {
|
2021-04-12 07:24:41 -04:00
|
|
|
|
custom_error(
|
|
|
|
|
"PermissionDenied",
|
|
|
|
|
format!(
|
|
|
|
|
"Requires {}, run again with the --allow-{} flag",
|
|
|
|
|
Self::fmt_access(name, info),
|
|
|
|
|
name
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 22:15:43 -04:00
|
|
|
|
/// Check the permission state. bool is whether a prompt was issued.
|
2022-08-27 11:20:05 -04:00
|
|
|
|
#[inline]
|
2021-04-11 22:15:43 -04:00
|
|
|
|
fn check(
|
|
|
|
|
self,
|
|
|
|
|
name: &str,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: Option<&str>,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
info: Option<&str>,
|
|
|
|
|
prompt: bool,
|
2022-08-27 11:20:05 -04:00
|
|
|
|
) -> (Result<(), AnyError>, bool) {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self.check2(name, api_name, || info.map(|s| s.to_string()), prompt)
|
2022-08-27 11:20:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn check2(
|
|
|
|
|
self,
|
|
|
|
|
name: &str,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: Option<&str>,
|
2022-08-27 11:20:05 -04:00
|
|
|
|
info: impl Fn() -> Option<String>,
|
|
|
|
|
prompt: bool,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
) -> (Result<(), AnyError>, bool) {
|
2021-04-12 07:24:41 -04:00
|
|
|
|
match self {
|
|
|
|
|
PermissionState::Granted => {
|
|
|
|
|
Self::log_perm_access(name, info);
|
|
|
|
|
(Ok(()), false)
|
|
|
|
|
}
|
|
|
|
|
PermissionState::Prompt if prompt => {
|
2022-08-27 11:20:05 -04:00
|
|
|
|
let msg = format!(
|
|
|
|
|
"{} access{}",
|
|
|
|
|
name,
|
|
|
|
|
info().map_or(String::new(), |info| { format!(" to {}", info) }),
|
|
|
|
|
);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if permission_prompt(&msg, name, api_name) {
|
2021-04-12 07:24:41 -04:00
|
|
|
|
Self::log_perm_access(name, info);
|
|
|
|
|
(Ok(()), true)
|
|
|
|
|
} else {
|
|
|
|
|
(Err(Self::error(name, info)), true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => (Err(Self::error(name, info)), false),
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05: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 {
|
2020-08-18 16:29:32 -04:00
|
|
|
|
PermissionState::Granted => f.pad("granted"),
|
|
|
|
|
PermissionState::Prompt => f.pad("prompt"),
|
|
|
|
|
PermissionState::Denied => 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 {
|
2020-08-18 16:29:32 -04:00
|
|
|
|
PermissionState::Prompt
|
2019-03-18 16:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-27 09:11:39 -04:00
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2021-03-21 08:49:58 -04:00
|
|
|
|
pub struct UnitPermission {
|
|
|
|
|
pub name: &'static str,
|
|
|
|
|
pub description: &'static str,
|
|
|
|
|
pub state: PermissionState,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
pub prompt: bool,
|
2021-03-21 08:49:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UnitPermission {
|
|
|
|
|
pub fn query(&self) -> PermissionState {
|
|
|
|
|
self.state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request(&mut self) -> PermissionState {
|
|
|
|
|
if self.state == PermissionState::Prompt {
|
2022-02-16 18:14:46 -05:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
&format!("access to {}", self.description),
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
Some("Deno.permissions.query()"),
|
2022-02-16 18:14:46 -05:00
|
|
|
|
) {
|
2021-03-21 08:49:58 -04:00
|
|
|
|
self.state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self) -> PermissionState {
|
|
|
|
|
if self.state == PermissionState::Granted {
|
|
|
|
|
self.state = PermissionState::Prompt;
|
|
|
|
|
}
|
|
|
|
|
self.state
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 22:15:43 -04:00
|
|
|
|
pub fn check(&mut self) -> Result<(), AnyError> {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
let (result, prompted) =
|
|
|
|
|
self.state.check(self.name, None, None, self.prompt);
|
2021-04-11 22:15:43 -04:00
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
2021-03-21 08:49:58 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 15:50:59 -04:00
|
|
|
|
/// A normalized environment variable name. On Windows this will
|
|
|
|
|
/// be uppercase and on other platforms it will stay as-is.
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2021-09-30 15:50:59 -04:00
|
|
|
|
struct EnvVarName {
|
|
|
|
|
inner: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl EnvVarName {
|
|
|
|
|
pub fn new(env: impl AsRef<str>) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
inner: if cfg!(windows) {
|
|
|
|
|
env.as_ref().to_uppercase()
|
|
|
|
|
} else {
|
|
|
|
|
env.as_ref().to_string()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsRef<str> for EnvVarName {
|
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
|
self.inner.as_str()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2020-08-18 16:29:32 -04:00
|
|
|
|
pub struct UnaryPermission<T: Eq + Hash> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub name: &'static str,
|
|
|
|
|
pub description: &'static str,
|
2020-10-31 18:44:42 -04:00
|
|
|
|
pub global_state: PermissionState,
|
2020-08-18 16:29:32 -04:00
|
|
|
|
pub granted_list: HashSet<T>,
|
|
|
|
|
pub denied_list: HashSet<T>,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
pub prompt: bool,
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2020-05-29 07:00:47 -04:00
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2021-03-21 08:49:58 -04:00
|
|
|
|
pub struct ReadDescriptor(pub PathBuf);
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2021-03-21 08:49:58 -04:00
|
|
|
|
pub struct WriteDescriptor(pub PathBuf);
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2021-03-21 08:49:58 -04:00
|
|
|
|
pub struct NetDescriptor(pub String, pub Option<u16>);
|
|
|
|
|
|
|
|
|
|
impl NetDescriptor {
|
|
|
|
|
fn new<T: AsRef<str>>(host: &&(T, Option<u16>)) -> Self {
|
|
|
|
|
NetDescriptor(host.0.as_ref().to_string(), host.1)
|
|
|
|
|
}
|
2022-08-10 15:13:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for NetDescriptor {
|
|
|
|
|
type Err = AnyError;
|
2021-03-21 08:49:58 -04:00
|
|
|
|
|
2022-08-10 15:13:53 -04:00
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
let url = url::Url::parse(&format!("http://{s}"))?;
|
2021-03-21 08:49:58 -04:00
|
|
|
|
let hostname = url.host_str().unwrap().to_string();
|
|
|
|
|
|
2022-08-10 15:13:53 -04:00
|
|
|
|
Ok(NetDescriptor(hostname, url.port()))
|
2021-03-21 08:49:58 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for NetDescriptor {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.write_str(&match self.1 {
|
|
|
|
|
None => self.0.clone(),
|
|
|
|
|
Some(port) => format!("{}:{}", self.0, port),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-27 09:11:39 -04:00
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2021-09-30 15:50:59 -04:00
|
|
|
|
pub struct EnvDescriptor(EnvVarName);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
|
2021-09-30 15:50:59 -04:00
|
|
|
|
impl EnvDescriptor {
|
|
|
|
|
pub fn new(env: impl AsRef<str>) -> Self {
|
|
|
|
|
Self(EnvVarName::new(env))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsRef<str> for EnvDescriptor {
|
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
|
self.0.as_ref()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub enum RunDescriptor {
|
|
|
|
|
Name(String),
|
|
|
|
|
Path(PathBuf),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for RunDescriptor {
|
|
|
|
|
type Err = ();
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
let is_path = s.contains('/');
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
let is_path = is_path || s.contains('\\') || Path::new(s).is_absolute();
|
|
|
|
|
if is_path {
|
|
|
|
|
Ok(Self::Path(resolve_from_cwd(Path::new(s)).unwrap()))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Self::Name(s.to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ToString for RunDescriptor {
|
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
|
match self {
|
|
|
|
|
RunDescriptor::Name(s) => s.clone(),
|
|
|
|
|
RunDescriptor::Path(p) => p.to_string_lossy().to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-09 18:12:00 -04:00
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub struct SysDescriptor(pub String);
|
|
|
|
|
|
2022-09-29 06:34:16 -04:00
|
|
|
|
pub fn parse_sys_kind(kind: &str) -> Result<&str, AnyError> {
|
|
|
|
|
match kind {
|
|
|
|
|
"hostname" | "osRelease" | "loadavg" | "networkInterfaces"
|
2022-10-26 16:37:48 -04:00
|
|
|
|
| "systemMemoryInfo" | "uid" | "gid" => Ok(kind),
|
2022-09-29 06:34:16 -04:00
|
|
|
|
_ => Err(type_error(format!("unknown system info kind \"{}\"", kind))),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub struct FfiDescriptor(pub PathBuf);
|
2021-08-06 17:28:10 -04:00
|
|
|
|
|
2021-03-21 08:49:58 -04:00
|
|
|
|
impl UnaryPermission<ReadDescriptor> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn query(&self, path: Option<&Path>) -> PermissionState {
|
2022-09-22 05:09:25 -04:00
|
|
|
|
if self.global_state == PermissionState::Granted {
|
|
|
|
|
return PermissionState::Granted;
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let path = path.map(|p| resolve_from_cwd(p).unwrap());
|
|
|
|
|
if self.global_state == PermissionState::Denied
|
|
|
|
|
&& match path.as_ref() {
|
|
|
|
|
None => true,
|
|
|
|
|
Some(path) => self
|
|
|
|
|
.denied_list
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|path_| path_.0.starts_with(path)),
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else if self.global_state == PermissionState::Granted
|
|
|
|
|
|| match path.as_ref() {
|
|
|
|
|
None => false,
|
|
|
|
|
Some(path) => self
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|path_| path.starts_with(&path_.0)),
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
|
|
|
|
}
|
2020-12-29 13:34:35 -05:00
|
|
|
|
}
|
2020-05-29 11:27:43 -04:00
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn request(&mut self, path: Option<&Path>) -> PermissionState {
|
|
|
|
|
if let Some(path) = path {
|
|
|
|
|
let (resolved_path, display_path) = resolved_and_display_path(path);
|
|
|
|
|
let state = self.query(Some(&resolved_path));
|
|
|
|
|
if state == PermissionState::Prompt {
|
2022-02-16 18:14:46 -05:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
&format!("read access to \"{}\"", display_path.display()),
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
Some("Deno.permissions.query()"),
|
2022-02-16 18:14:46 -05:00
|
|
|
|
) {
|
2021-03-21 08:49:58 -04:00
|
|
|
|
self.granted_list.insert(ReadDescriptor(resolved_path));
|
2021-03-17 17:45:12 -04:00
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
2021-03-21 08:49:58 -04:00
|
|
|
|
self.denied_list.insert(ReadDescriptor(resolved_path));
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
2021-09-24 01:48:15 -04:00
|
|
|
|
} else if state == PermissionState::Granted {
|
|
|
|
|
self.granted_list.insert(ReadDescriptor(resolved_path));
|
|
|
|
|
PermissionState::Granted
|
2020-10-31 18:44:42 -04:00
|
|
|
|
} else {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
state
|
2020-10-31 18:44:42 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
} else {
|
|
|
|
|
let state = self.query(None);
|
|
|
|
|
if state == PermissionState::Prompt {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
"read access",
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.granted_list.clear();
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
2020-12-29 13:34:35 -05:00
|
|
|
|
} else {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
state
|
2020-12-29 13:34:35 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-27 09:11:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn revoke(&mut self, path: Option<&Path>) -> PermissionState {
|
|
|
|
|
if let Some(path) = path {
|
|
|
|
|
let path = resolve_from_cwd(path).unwrap();
|
|
|
|
|
self
|
|
|
|
|
.granted_list
|
2021-09-24 01:48:15 -04:00
|
|
|
|
.retain(|path_| !path.starts_with(&path_.0));
|
2020-05-29 11:27:43 -04:00
|
|
|
|
} else {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.granted_list.clear();
|
2021-09-24 01:48:15 -04:00
|
|
|
|
}
|
|
|
|
|
if self.global_state == PermissionState::Granted {
|
|
|
|
|
self.global_state = PermissionState::Prompt;
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
self.query(path)
|
2020-05-29 11:27:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 11:20:05 -04:00
|
|
|
|
#[inline]
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-27 11:20:05 -04:00
|
|
|
|
let (result, prompted) = self.query(Some(path)).check2(
|
2021-04-11 22:15:43 -04:00
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name,
|
2022-08-27 11:20:05 -04:00
|
|
|
|
|| Some(format!("\"{}\"", path.to_path_buf().display())),
|
2021-04-11 22:15:43 -04:00
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
2022-08-27 11:20:05 -04:00
|
|
|
|
let resolved_path = resolve_from_cwd(path).unwrap();
|
2021-04-11 22:15:43 -04:00
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.granted_list.insert(ReadDescriptor(resolved_path));
|
|
|
|
|
} else {
|
|
|
|
|
self.denied_list.insert(ReadDescriptor(resolved_path));
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
2020-05-11 07:13:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
/// As `check()`, but permission error messages will anonymize the path
|
|
|
|
|
/// by replacing it with the given `display`.
|
|
|
|
|
pub fn check_blind(
|
2021-04-11 22:15:43 -04:00
|
|
|
|
&mut self,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
path: &Path,
|
|
|
|
|
display: &str,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: &str,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
let resolved_path = resolve_from_cwd(path).unwrap();
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let (result, prompted) = self.query(Some(&resolved_path)).check(
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
Some(api_name),
|
2021-04-11 22:15:43 -04:00
|
|
|
|
Some(&format!("<{}>", display)),
|
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.granted_list.insert(ReadDescriptor(resolved_path));
|
|
|
|
|
} else {
|
|
|
|
|
self.denied_list.insert(ReadDescriptor(resolved_path));
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
2019-02-08 15:59:38 -05:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check_all(&mut self, api_name: Option<&str>) -> Result<(), AnyError> {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
let (result, prompted) =
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self
|
|
|
|
|
.query(None)
|
|
|
|
|
.check(self.name, api_name, Some("all"), self.prompt);
|
2021-10-13 13:04:44 -04:00
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for UnaryPermission<ReadDescriptor> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
UnaryPermission::<ReadDescriptor> {
|
|
|
|
|
name: "read",
|
|
|
|
|
description: "read the file system",
|
|
|
|
|
global_state: Default::default(),
|
|
|
|
|
granted_list: Default::default(),
|
|
|
|
|
denied_list: Default::default(),
|
|
|
|
|
prompt: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-21 08:49:58 -04:00
|
|
|
|
impl UnaryPermission<WriteDescriptor> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn query(&self, path: Option<&Path>) -> PermissionState {
|
2022-09-22 05:09:25 -04:00
|
|
|
|
if self.global_state == PermissionState::Granted {
|
|
|
|
|
return PermissionState::Granted;
|
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
|
let path = path.map(|p| resolve_from_cwd(p).unwrap());
|
2021-03-17 17:45:12 -04:00
|
|
|
|
if self.global_state == PermissionState::Denied
|
2020-08-18 16:29:32 -04:00
|
|
|
|
&& match path.as_ref() {
|
|
|
|
|
None => true,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
Some(path) => self
|
|
|
|
|
.denied_list
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|path_| path_.0.starts_with(path)),
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
{
|
2021-03-17 17:45:12 -04:00
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else if self.global_state == PermissionState::Granted
|
2020-08-18 16:29:32 -04:00
|
|
|
|
|| match path.as_ref() {
|
|
|
|
|
None => false,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
Some(path) => self
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|path_| path.starts_with(&path_.0)),
|
2020-12-30 17:35:28 -05:00
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
|
{
|
2021-03-17 17:45:12 -04:00
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn request(&mut self, path: Option<&Path>) -> PermissionState {
|
2020-08-18 16:29:32 -04:00
|
|
|
|
if let Some(path) = path {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let (resolved_path, display_path) = resolved_and_display_path(path);
|
|
|
|
|
let state = self.query(Some(&resolved_path));
|
2020-08-18 16:29:32 -04:00
|
|
|
|
if state == PermissionState::Prompt {
|
2022-02-16 18:14:46 -05:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
&format!("write access to \"{}\"", display_path.display()),
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
Some("Deno.permissions.query()"),
|
2022-02-16 18:14:46 -05:00
|
|
|
|
) {
|
2021-03-21 08:49:58 -04:00
|
|
|
|
self.granted_list.insert(WriteDescriptor(resolved_path));
|
2021-03-17 17:45:12 -04:00
|
|
|
|
PermissionState::Granted
|
2020-08-18 16:29:32 -04:00
|
|
|
|
} else {
|
2021-03-21 08:49:58 -04:00
|
|
|
|
self.denied_list.insert(WriteDescriptor(resolved_path));
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2021-09-24 01:48:15 -04:00
|
|
|
|
} else if state == PermissionState::Granted {
|
|
|
|
|
self.granted_list.insert(WriteDescriptor(resolved_path));
|
|
|
|
|
PermissionState::Granted
|
2021-03-17 17:45:12 -04:00
|
|
|
|
} else {
|
|
|
|
|
state
|
2020-05-29 11:27:43 -04:00
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
|
} else {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let state = self.query(None);
|
2020-08-18 16:29:32 -04:00
|
|
|
|
if state == PermissionState::Prompt {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
"write access",
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.granted_list.clear();
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
PermissionState::Granted
|
2020-08-18 16:29:32 -04:00
|
|
|
|
} else {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
} else {
|
|
|
|
|
state
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn revoke(&mut self, path: Option<&Path>) -> PermissionState {
|
2020-08-18 16:29:32 -04:00
|
|
|
|
if let Some(path) = path {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let path = resolve_from_cwd(path).unwrap();
|
|
|
|
|
self
|
|
|
|
|
.granted_list
|
2021-09-24 01:48:15 -04:00
|
|
|
|
.retain(|path_| !path.starts_with(&path_.0));
|
2021-03-17 17:45:12 -04:00
|
|
|
|
} else {
|
|
|
|
|
self.granted_list.clear();
|
2021-09-24 01:48:15 -04:00
|
|
|
|
}
|
|
|
|
|
if self.global_state == PermissionState::Granted {
|
|
|
|
|
self.global_state = PermissionState::Prompt;
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
self.query(path)
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 11:20:05 -04:00
|
|
|
|
#[inline]
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2022-08-27 11:20:05 -04:00
|
|
|
|
let (result, prompted) = self.query(Some(path)).check2(
|
2021-04-11 22:15:43 -04:00
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name,
|
2022-08-27 11:20:05 -04:00
|
|
|
|
|| Some(format!("\"{}\"", path.to_path_buf().display())),
|
2021-04-11 22:15:43 -04:00
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
2022-08-27 11:20:05 -04:00
|
|
|
|
let resolved_path = resolve_from_cwd(path).unwrap();
|
2021-04-11 22:15:43 -04:00
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.granted_list.insert(WriteDescriptor(resolved_path));
|
|
|
|
|
} else {
|
|
|
|
|
self.denied_list.insert(WriteDescriptor(resolved_path));
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check_all(&mut self, api_name: Option<&str>) -> Result<(), AnyError> {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
let (result, prompted) =
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self
|
|
|
|
|
.query(None)
|
|
|
|
|
.check(self.name, api_name, Some("all"), self.prompt);
|
2021-10-13 13:04:44 -04:00
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for UnaryPermission<WriteDescriptor> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
UnaryPermission::<WriteDescriptor> {
|
|
|
|
|
name: "write",
|
|
|
|
|
description: "write to the file system",
|
|
|
|
|
global_state: Default::default(),
|
|
|
|
|
granted_list: Default::default(),
|
|
|
|
|
denied_list: Default::default(),
|
|
|
|
|
prompt: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-21 08:49:58 -04:00
|
|
|
|
impl UnaryPermission<NetDescriptor> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn query<T: AsRef<str>>(
|
|
|
|
|
&self,
|
|
|
|
|
host: Option<&(T, Option<u16>)>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
if self.global_state == PermissionState::Denied
|
|
|
|
|
&& match host.as_ref() {
|
|
|
|
|
None => true,
|
|
|
|
|
Some(host) => match host.1 {
|
|
|
|
|
None => self
|
2020-08-18 16:29:32 -04:00
|
|
|
|
.denied_list
|
2021-03-17 17:45:12 -04:00
|
|
|
|
.iter()
|
|
|
|
|
.any(|host_| host.0.as_ref() == host_.0),
|
2021-03-21 08:49:58 -04:00
|
|
|
|
Some(_) => self.denied_list.contains(&NetDescriptor::new(host)),
|
2021-03-17 17:45:12 -04:00
|
|
|
|
},
|
2020-05-29 11:27:43 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
{
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else if self.global_state == PermissionState::Granted
|
|
|
|
|
|| match host.as_ref() {
|
|
|
|
|
None => false,
|
|
|
|
|
Some(host) => {
|
2021-03-21 08:49:58 -04:00
|
|
|
|
self.granted_list.contains(&NetDescriptor::new(&&(
|
2021-03-17 17:45:12 -04:00
|
|
|
|
host.0.as_ref().to_string(),
|
|
|
|
|
None,
|
|
|
|
|
)))
|
2021-03-21 08:49:58 -04:00
|
|
|
|
|| self.granted_list.contains(&NetDescriptor::new(host))
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
{
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn request<T: AsRef<str>>(
|
2019-11-24 10:42:30 -05:00
|
|
|
|
&mut self,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
host: Option<&(T, Option<u16>)>,
|
2020-12-30 17:35:28 -05:00
|
|
|
|
) -> PermissionState {
|
|
|
|
|
if let Some(host) = host {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let state = self.query(Some(host));
|
2021-09-24 01:48:15 -04:00
|
|
|
|
let host = NetDescriptor::new(&host);
|
2020-08-18 16:29:32 -04:00
|
|
|
|
if state == PermissionState::Prompt {
|
2022-02-16 18:14:46 -05:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
&format!("network access to \"{}\"", host),
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
Some("Deno.permissions.query()"),
|
2022-02-16 18:14:46 -05:00
|
|
|
|
) {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.granted_list.insert(host);
|
|
|
|
|
PermissionState::Granted
|
2020-08-18 16:29:32 -04:00
|
|
|
|
} else {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.denied_list.insert(host);
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2021-09-24 01:48:15 -04:00
|
|
|
|
} else if state == PermissionState::Granted {
|
|
|
|
|
self.granted_list.insert(host);
|
|
|
|
|
PermissionState::Granted
|
2021-03-17 17:45:12 -04:00
|
|
|
|
} else {
|
|
|
|
|
state
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let state = self.query::<&str>(None);
|
2020-08-18 16:29:32 -04:00
|
|
|
|
if state == PermissionState::Prompt {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
"network access",
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.granted_list.clear();
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
PermissionState::Granted
|
2020-08-18 16:29:32 -04:00
|
|
|
|
} else {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
} else {
|
|
|
|
|
state
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn revoke<T: AsRef<str>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
host: Option<&(T, Option<u16>)>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
if let Some(host) = host {
|
2021-09-24 01:48:15 -04:00
|
|
|
|
if host.1.is_some() {
|
|
|
|
|
self
|
|
|
|
|
.granted_list
|
|
|
|
|
.remove(&NetDescriptor(host.0.as_ref().to_string(), host.1));
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2021-09-24 01:48:15 -04:00
|
|
|
|
self
|
|
|
|
|
.granted_list
|
|
|
|
|
.remove(&NetDescriptor(host.0.as_ref().to_string(), None));
|
2021-03-17 17:45:12 -04:00
|
|
|
|
} else {
|
|
|
|
|
self.granted_list.clear();
|
2021-09-24 01:48:15 -04:00
|
|
|
|
}
|
|
|
|
|
if self.global_state == PermissionState::Granted {
|
|
|
|
|
self.global_state = PermissionState::Prompt;
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.query(host)
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn check<T: AsRef<str>>(
|
2021-04-11 22:15:43 -04:00
|
|
|
|
&mut self,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
host: &(T, Option<u16>),
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: Option<&str>,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
) -> Result<(), AnyError> {
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let new_host = NetDescriptor::new(&host);
|
|
|
|
|
let (result, prompted) = self.query(Some(host)).check(
|
2021-03-17 17:45:12 -04:00
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
Some(&format!("\"{}\"", new_host)),
|
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.granted_list.insert(new_host);
|
|
|
|
|
} else {
|
|
|
|
|
self.denied_list.insert(new_host);
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
2019-06-22 12:02:51 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check_url(
|
|
|
|
|
&mut self,
|
|
|
|
|
url: &url::Url,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let hostname = url
|
|
|
|
|
.host_str()
|
|
|
|
|
.ok_or_else(|| uri_error("Missing host"))?
|
|
|
|
|
.to_string();
|
|
|
|
|
let display_host = match url.port() {
|
|
|
|
|
None => hostname.clone(),
|
|
|
|
|
Some(port) => format!("{}:{}", hostname, port),
|
|
|
|
|
};
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let host = &(&hostname, url.port_or_known_default());
|
|
|
|
|
let (result, prompted) = self.query(Some(host)).check(
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
Some(&format!("\"{}\"", display_host)),
|
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.granted_list.insert(NetDescriptor::new(&host));
|
|
|
|
|
} else {
|
|
|
|
|
self.denied_list.insert(NetDescriptor::new(&host));
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
2019-12-05 15:30:20 -05:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
|
|
|
|
pub fn check_all(&mut self) -> Result<(), AnyError> {
|
2022-02-17 09:15:29 -05:00
|
|
|
|
let (result, prompted) =
|
|
|
|
|
self
|
|
|
|
|
.query::<&str>(None)
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.check(self.name, None, Some("all"), self.prompt);
|
2021-10-13 13:04:44 -04:00
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for UnaryPermission<NetDescriptor> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
UnaryPermission::<NetDescriptor> {
|
|
|
|
|
name: "net",
|
|
|
|
|
description: "network",
|
|
|
|
|
global_state: Default::default(),
|
|
|
|
|
granted_list: Default::default(),
|
|
|
|
|
denied_list: Default::default(),
|
|
|
|
|
prompt: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2019-12-05 15:30:20 -05:00
|
|
|
|
|
2021-04-13 07:25:21 -04:00
|
|
|
|
impl UnaryPermission<EnvDescriptor> {
|
|
|
|
|
pub fn query(&self, env: Option<&str>) -> PermissionState {
|
2021-09-30 15:50:59 -04:00
|
|
|
|
let env = env.map(EnvVarName::new);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
if self.global_state == PermissionState::Denied
|
2021-09-30 15:50:59 -04:00
|
|
|
|
&& match env.as_ref() {
|
2021-04-13 07:25:21 -04:00
|
|
|
|
None => true,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
Some(env) => self.denied_list.contains(&EnvDescriptor::new(env)),
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else if self.global_state == PermissionState::Granted
|
2021-09-30 15:50:59 -04:00
|
|
|
|
|| match env.as_ref() {
|
2021-04-13 07:25:21 -04:00
|
|
|
|
None => false,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
Some(env) => self.granted_list.contains(&EnvDescriptor::new(env)),
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request(&mut self, env: Option<&str>) -> PermissionState {
|
|
|
|
|
if let Some(env) = env {
|
2021-09-30 15:50:59 -04:00
|
|
|
|
let state = self.query(Some(env));
|
2021-04-13 07:25:21 -04:00
|
|
|
|
if state == PermissionState::Prompt {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
&format!("env access to \"{}\"", env),
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
2021-09-30 15:50:59 -04:00
|
|
|
|
self.granted_list.insert(EnvDescriptor::new(env));
|
2021-04-13 07:25:21 -04:00
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
2021-09-30 15:50:59 -04:00
|
|
|
|
self.denied_list.insert(EnvDescriptor::new(env));
|
2021-04-13 07:25:21 -04:00
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
2021-09-24 01:48:15 -04:00
|
|
|
|
} else if state == PermissionState::Granted {
|
2021-09-30 15:50:59 -04:00
|
|
|
|
self.granted_list.insert(EnvDescriptor::new(env));
|
2021-09-24 01:48:15 -04:00
|
|
|
|
PermissionState::Granted
|
2021-04-13 07:25:21 -04:00
|
|
|
|
} else {
|
|
|
|
|
state
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let state = self.query(None);
|
|
|
|
|
if state == PermissionState::Prompt {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
"env access",
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
2021-04-13 07:25:21 -04:00
|
|
|
|
self.granted_list.clear();
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
state
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self, env: Option<&str>) -> PermissionState {
|
|
|
|
|
if let Some(env) = env {
|
2021-09-30 15:50:59 -04:00
|
|
|
|
self.granted_list.remove(&EnvDescriptor::new(env));
|
2021-04-13 07:25:21 -04:00
|
|
|
|
} else {
|
|
|
|
|
self.granted_list.clear();
|
2021-09-24 01:48:15 -04:00
|
|
|
|
}
|
|
|
|
|
if self.global_state == PermissionState::Granted {
|
|
|
|
|
self.global_state = PermissionState::Prompt;
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
self.query(env)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check(&mut self, env: &str) -> Result<(), AnyError> {
|
|
|
|
|
let (result, prompted) = self.query(Some(env)).check(
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
None,
|
2021-04-13 07:25:21 -04:00
|
|
|
|
Some(&format!("\"{}\"", env)),
|
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
2021-09-30 15:50:59 -04:00
|
|
|
|
self.granted_list.insert(EnvDescriptor::new(env));
|
2021-04-13 07:25:21 -04:00
|
|
|
|
} else {
|
2021-09-30 15:50:59 -04:00
|
|
|
|
self.denied_list.insert(EnvDescriptor::new(env));
|
2021-04-13 07:25:21 -04:00
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check_all(&mut self) -> Result<(), AnyError> {
|
|
|
|
|
let (result, prompted) =
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self
|
|
|
|
|
.query(None)
|
|
|
|
|
.check(self.name, None, Some("all"), self.prompt);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
impl Default for UnaryPermission<EnvDescriptor> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
UnaryPermission::<EnvDescriptor> {
|
|
|
|
|
name: "env",
|
|
|
|
|
description: "environment variables",
|
|
|
|
|
global_state: Default::default(),
|
|
|
|
|
granted_list: Default::default(),
|
|
|
|
|
denied_list: Default::default(),
|
|
|
|
|
prompt: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
impl UnaryPermission<SysDescriptor> {
|
|
|
|
|
pub fn query(&self, kind: Option<&str>) -> PermissionState {
|
|
|
|
|
if self.global_state == PermissionState::Denied
|
|
|
|
|
&& match kind {
|
|
|
|
|
None => true,
|
|
|
|
|
Some(kind) => {
|
|
|
|
|
self.denied_list.contains(&SysDescriptor(kind.to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else if self.global_state == PermissionState::Granted
|
|
|
|
|
|| match kind {
|
|
|
|
|
None => false,
|
|
|
|
|
Some(kind) => {
|
|
|
|
|
self.granted_list.contains(&SysDescriptor(kind.to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request(&mut self, kind: Option<&str>) -> PermissionState {
|
|
|
|
|
let state = self.query(kind);
|
|
|
|
|
if state != PermissionState::Prompt {
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
if let Some(kind) = kind {
|
|
|
|
|
let desc = SysDescriptor(kind.to_string());
|
|
|
|
|
if permission_prompt(
|
|
|
|
|
&format!("sys access to \"{}\"", kind),
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
|
|
|
|
self.granted_list.insert(desc);
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
self.denied_list.insert(desc);
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if permission_prompt(
|
|
|
|
|
"sys access",
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.granted_list.clear();
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
self.global_state
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self, kind: Option<&str>) -> PermissionState {
|
|
|
|
|
if let Some(kind) = kind {
|
|
|
|
|
self.granted_list.remove(&SysDescriptor(kind.to_string()));
|
|
|
|
|
} else {
|
|
|
|
|
self.granted_list.clear();
|
|
|
|
|
}
|
|
|
|
|
if self.global_state == PermissionState::Granted {
|
|
|
|
|
self.global_state = PermissionState::Prompt;
|
|
|
|
|
}
|
|
|
|
|
self.query(kind)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
kind: &str,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
let (result, prompted) = self.query(Some(kind)).check(
|
|
|
|
|
self.name,
|
|
|
|
|
api_name,
|
|
|
|
|
Some(&format!("\"{}\"", kind)),
|
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.granted_list.insert(SysDescriptor(kind.to_string()));
|
|
|
|
|
} else {
|
|
|
|
|
self.denied_list.insert(SysDescriptor(kind.to_string()));
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check_all(&mut self) -> Result<(), AnyError> {
|
|
|
|
|
let (result, prompted) =
|
|
|
|
|
self
|
|
|
|
|
.query(None)
|
|
|
|
|
.check(self.name, None, Some("all"), self.prompt);
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for UnaryPermission<SysDescriptor> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
UnaryPermission::<SysDescriptor> {
|
|
|
|
|
name: "sys",
|
|
|
|
|
description: "system information",
|
|
|
|
|
global_state: Default::default(),
|
|
|
|
|
granted_list: Default::default(),
|
|
|
|
|
denied_list: Default::default(),
|
|
|
|
|
prompt: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 18:12:00 -04:00
|
|
|
|
impl UnaryPermission<RunDescriptor> {
|
|
|
|
|
pub fn query(&self, cmd: Option<&str>) -> PermissionState {
|
|
|
|
|
if self.global_state == PermissionState::Denied
|
|
|
|
|
&& match cmd {
|
|
|
|
|
None => true,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
Some(cmd) => self
|
|
|
|
|
.denied_list
|
|
|
|
|
.contains(&RunDescriptor::from_str(cmd).unwrap()),
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else if self.global_state == PermissionState::Granted
|
|
|
|
|
|| match cmd {
|
|
|
|
|
None => false,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
Some(cmd) => self
|
|
|
|
|
.granted_list
|
|
|
|
|
.contains(&RunDescriptor::from_str(cmd).unwrap()),
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request(&mut self, cmd: Option<&str>) -> PermissionState {
|
|
|
|
|
if let Some(cmd) = cmd {
|
2021-07-30 09:03:41 -04:00
|
|
|
|
let state = self.query(Some(cmd));
|
2021-04-09 18:12:00 -04:00
|
|
|
|
if state == PermissionState::Prompt {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
&format!("run access to \"{}\"", cmd),
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self
|
|
|
|
|
.granted_list
|
|
|
|
|
.insert(RunDescriptor::from_str(cmd).unwrap());
|
2021-04-09 18:12:00 -04:00
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self
|
|
|
|
|
.denied_list
|
|
|
|
|
.insert(RunDescriptor::from_str(cmd).unwrap());
|
2021-04-09 18:12:00 -04:00
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
2021-09-24 01:48:15 -04:00
|
|
|
|
} else if state == PermissionState::Granted {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self
|
|
|
|
|
.granted_list
|
|
|
|
|
.insert(RunDescriptor::from_str(cmd).unwrap());
|
2021-09-24 01:48:15 -04:00
|
|
|
|
PermissionState::Granted
|
2021-04-09 18:12:00 -04:00
|
|
|
|
} else {
|
|
|
|
|
state
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let state = self.query(None);
|
|
|
|
|
if state == PermissionState::Prompt {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
"run access",
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
2021-04-09 18:12:00 -04:00
|
|
|
|
self.granted_list.clear();
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
state
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self, cmd: Option<&str>) -> PermissionState {
|
|
|
|
|
if let Some(cmd) = cmd {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self
|
|
|
|
|
.granted_list
|
|
|
|
|
.remove(&RunDescriptor::from_str(cmd).unwrap());
|
2021-04-09 18:12:00 -04:00
|
|
|
|
} else {
|
|
|
|
|
self.granted_list.clear();
|
2021-09-24 01:48:15 -04:00
|
|
|
|
}
|
|
|
|
|
if self.global_state == PermissionState::Granted {
|
|
|
|
|
self.global_state = PermissionState::Prompt;
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
self.query(cmd)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
cmd: &str,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let (result, prompted) = self.query(Some(cmd)).check(
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
Some(&format!("\"{}\"", cmd)),
|
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self
|
|
|
|
|
.granted_list
|
|
|
|
|
.insert(RunDescriptor::from_str(cmd).unwrap());
|
2021-04-09 18:12:00 -04:00
|
|
|
|
} else {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self
|
|
|
|
|
.denied_list
|
|
|
|
|
.insert(RunDescriptor::from_str(cmd).unwrap());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
self.global_state = PermissionState::Denied;
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-11 22:15:43 -04:00
|
|
|
|
result
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check_all(&mut self, api_name: Option<&str>) -> Result<(), AnyError> {
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let (result, prompted) =
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self
|
|
|
|
|
.query(None)
|
|
|
|
|
.check(self.name, api_name, Some("all"), self.prompt);
|
2021-04-11 22:15:43 -04:00
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
2021-04-11 22:15:43 -04:00
|
|
|
|
result
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
impl Default for UnaryPermission<RunDescriptor> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
UnaryPermission::<RunDescriptor> {
|
|
|
|
|
name: "run",
|
|
|
|
|
description: "run a subprocess",
|
|
|
|
|
global_state: Default::default(),
|
|
|
|
|
granted_list: Default::default(),
|
|
|
|
|
denied_list: Default::default(),
|
|
|
|
|
prompt: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
|
impl UnaryPermission<FfiDescriptor> {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub fn query(&self, path: Option<&Path>) -> PermissionState {
|
|
|
|
|
let path = path.map(|p| resolve_from_cwd(p).unwrap());
|
2021-08-06 17:28:10 -04:00
|
|
|
|
if self.global_state == PermissionState::Denied
|
2021-10-13 13:04:44 -04:00
|
|
|
|
&& match path.as_ref() {
|
2021-08-06 17:28:10 -04:00
|
|
|
|
None => true,
|
2022-12-14 15:38:53 -05:00
|
|
|
|
Some(path) => self
|
|
|
|
|
.denied_list
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|path_| path_.0.starts_with(path)),
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else if self.global_state == PermissionState::Granted
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|| match path.as_ref() {
|
2021-08-06 17:28:10 -04:00
|
|
|
|
None => false,
|
2022-12-14 15:38:53 -05:00
|
|
|
|
Some(path) => self
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|path_| path.starts_with(&path_.0)),
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub fn request(&mut self, path: Option<&Path>) -> PermissionState {
|
|
|
|
|
if let Some(path) = path {
|
|
|
|
|
let (resolved_path, display_path) = resolved_and_display_path(path);
|
|
|
|
|
let state = self.query(Some(&resolved_path));
|
2021-08-06 17:28:10 -04:00
|
|
|
|
if state == PermissionState::Prompt {
|
2022-02-16 18:14:46 -05:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
&format!("ffi access to \"{}\"", display_path.display()),
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
Some("Deno.permissions.query()"),
|
2022-02-16 18:14:46 -05:00
|
|
|
|
) {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self.granted_list.insert(FfiDescriptor(resolved_path));
|
2021-08-06 17:28:10 -04:00
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self.denied_list.insert(FfiDescriptor(resolved_path));
|
2021-08-06 17:28:10 -04:00
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
2021-09-24 01:48:15 -04:00
|
|
|
|
} else if state == PermissionState::Granted {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self.granted_list.insert(FfiDescriptor(resolved_path));
|
2021-09-24 01:48:15 -04:00
|
|
|
|
PermissionState::Granted
|
2021-08-06 17:28:10 -04:00
|
|
|
|
} else {
|
|
|
|
|
state
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let state = self.query(None);
|
|
|
|
|
if state == PermissionState::Prompt {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if permission_prompt(
|
|
|
|
|
"ffi access",
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
|
|
|
|
) {
|
2021-08-06 17:28:10 -04:00
|
|
|
|
self.granted_list.clear();
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
state
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub fn revoke(&mut self, path: Option<&Path>) -> PermissionState {
|
|
|
|
|
if let Some(path) = path {
|
|
|
|
|
let path = resolve_from_cwd(path).unwrap();
|
2022-12-14 15:38:53 -05:00
|
|
|
|
self
|
|
|
|
|
.granted_list
|
|
|
|
|
.retain(|path_| !path.starts_with(&path_.0));
|
2021-08-06 17:28:10 -04:00
|
|
|
|
} else {
|
|
|
|
|
self.granted_list.clear();
|
2021-09-24 01:48:15 -04:00
|
|
|
|
}
|
|
|
|
|
if self.global_state == PermissionState::Granted {
|
|
|
|
|
self.global_state = PermissionState::Prompt;
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
self.query(path)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 09:41:49 -05:00
|
|
|
|
pub fn check(&mut self, path: Option<&Path>) -> Result<(), AnyError> {
|
|
|
|
|
if let Some(path) = path {
|
|
|
|
|
let (resolved_path, display_path) = resolved_and_display_path(path);
|
|
|
|
|
let (result, prompted) = self.query(Some(&resolved_path)).check(
|
|
|
|
|
self.name,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
None,
|
2021-12-15 09:41:49 -05:00
|
|
|
|
Some(&format!("\"{}\"", display_path.display())),
|
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.granted_list.insert(FfiDescriptor(resolved_path));
|
|
|
|
|
} else {
|
|
|
|
|
self.denied_list.insert(FfiDescriptor(resolved_path));
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
2021-12-15 09:41:49 -05:00
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
} else {
|
|
|
|
|
let (result, prompted) =
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self.query(None).check(self.name, None, None, self.prompt);
|
2021-12-15 09:41:49 -05:00
|
|
|
|
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check_all(&mut self) -> Result<(), AnyError> {
|
|
|
|
|
let (result, prompted) =
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self
|
|
|
|
|
.query(None)
|
|
|
|
|
.check(self.name, None, Some("all"), self.prompt);
|
2021-08-06 17:28:10 -04:00
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
self.global_state = PermissionState::Granted;
|
|
|
|
|
} else {
|
|
|
|
|
self.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
impl Default for UnaryPermission<FfiDescriptor> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
UnaryPermission::<FfiDescriptor> {
|
|
|
|
|
name: "ffi",
|
|
|
|
|
description: "load a dynamic library",
|
|
|
|
|
global_state: Default::default(),
|
|
|
|
|
granted_list: Default::default(),
|
|
|
|
|
denied_list: Default::default(),
|
|
|
|
|
prompt: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub struct Permissions {
|
2021-03-21 08:49:58 -04:00
|
|
|
|
pub read: UnaryPermission<ReadDescriptor>,
|
|
|
|
|
pub write: UnaryPermission<WriteDescriptor>,
|
|
|
|
|
pub net: UnaryPermission<NetDescriptor>,
|
2021-04-13 07:25:21 -04:00
|
|
|
|
pub env: UnaryPermission<EnvDescriptor>,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
pub sys: UnaryPermission<SysDescriptor>,
|
2021-04-09 18:12:00 -04:00
|
|
|
|
pub run: UnaryPermission<RunDescriptor>,
|
2021-08-06 17:28:10 -04:00
|
|
|
|
pub ffi: UnaryPermission<FfiDescriptor>,
|
2021-03-21 08:49:58 -04:00
|
|
|
|
pub hrtime: UnitPermission,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
impl Default for Permissions {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
read: Permissions::new_read(&None, false).unwrap(),
|
|
|
|
|
write: Permissions::new_write(&None, false).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&None, false).unwrap(),
|
|
|
|
|
env: Permissions::new_env(&None, false).unwrap(),
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: Permissions::new_sys(&None, false).unwrap(),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
run: Permissions::new_run(&None, false).unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(&None, false).unwrap(),
|
2022-02-16 18:17:24 -05:00
|
|
|
|
hrtime: Permissions::new_hrtime(false),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub struct PermissionsOptions {
|
2021-04-13 07:25:21 -04:00
|
|
|
|
pub allow_env: Option<Vec<String>>,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub allow_hrtime: bool,
|
|
|
|
|
pub allow_net: Option<Vec<String>>,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub allow_ffi: Option<Vec<PathBuf>>,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub allow_read: Option<Vec<PathBuf>>,
|
2021-04-09 18:12:00 -04:00
|
|
|
|
pub allow_run: Option<Vec<String>>,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
pub allow_sys: Option<Vec<String>>,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub allow_write: Option<Vec<PathBuf>>,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
pub prompt: bool,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Permissions {
|
|
|
|
|
pub fn new_read(
|
|
|
|
|
state: &Option<Vec<PathBuf>>,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt: bool,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<UnaryPermission<ReadDescriptor>, AnyError> {
|
|
|
|
|
Ok(UnaryPermission::<ReadDescriptor> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
global_state: global_state_from_option(state),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
granted_list: resolve_read_allowlist(state)?,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn new_write(
|
|
|
|
|
state: &Option<Vec<PathBuf>>,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt: bool,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<UnaryPermission<WriteDescriptor>, AnyError> {
|
|
|
|
|
Ok(UnaryPermission::<WriteDescriptor> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
global_state: global_state_from_option(state),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
granted_list: resolve_write_allowlist(state)?,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn new_net(
|
|
|
|
|
state: &Option<Vec<String>>,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt: bool,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<UnaryPermission<NetDescriptor>, AnyError> {
|
|
|
|
|
Ok(UnaryPermission::<NetDescriptor> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
global_state: global_state_from_option(state),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
granted_list: state.as_ref().map_or_else(
|
|
|
|
|
|| Ok(HashSet::new()),
|
|
|
|
|
|v| {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
v.iter()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.map(|x| NetDescriptor::from_str(x))
|
|
|
|
|
.collect::<Result<HashSet<NetDescriptor>, AnyError>>()
|
|
|
|
|
},
|
|
|
|
|
)?,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 07:25:21 -04:00
|
|
|
|
pub fn new_env(
|
|
|
|
|
state: &Option<Vec<String>>,
|
|
|
|
|
prompt: bool,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<UnaryPermission<EnvDescriptor>, AnyError> {
|
|
|
|
|
Ok(UnaryPermission::<EnvDescriptor> {
|
2021-04-13 07:25:21 -04:00
|
|
|
|
global_state: global_state_from_option(state),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
granted_list: state.as_ref().map_or_else(
|
|
|
|
|
|| Ok(HashSet::new()),
|
|
|
|
|
|v| {
|
|
|
|
|
v.iter()
|
|
|
|
|
.map(|x| {
|
|
|
|
|
if x.is_empty() {
|
|
|
|
|
Err(AnyError::msg("Empty path is not allowed"))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(EnvDescriptor::new(x))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
},
|
|
|
|
|
)?,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
pub fn new_sys(
|
|
|
|
|
state: &Option<Vec<String>>,
|
|
|
|
|
prompt: bool,
|
|
|
|
|
) -> Result<UnaryPermission<SysDescriptor>, AnyError> {
|
|
|
|
|
Ok(UnaryPermission::<SysDescriptor> {
|
|
|
|
|
global_state: global_state_from_option(state),
|
|
|
|
|
granted_list: state.as_ref().map_or_else(
|
|
|
|
|
|| Ok(HashSet::new()),
|
|
|
|
|
|v| {
|
|
|
|
|
v.iter()
|
|
|
|
|
.map(|x| {
|
|
|
|
|
if x.is_empty() {
|
|
|
|
|
Err(AnyError::msg("emtpy"))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(SysDescriptor(x.to_string()))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
},
|
|
|
|
|
)?,
|
|
|
|
|
prompt,
|
|
|
|
|
..Default::default()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 18:12:00 -04:00
|
|
|
|
pub fn new_run(
|
|
|
|
|
state: &Option<Vec<String>>,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt: bool,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<UnaryPermission<RunDescriptor>, AnyError> {
|
|
|
|
|
Ok(UnaryPermission::<RunDescriptor> {
|
2021-04-09 18:12:00 -04:00
|
|
|
|
global_state: global_state_from_option(state),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
granted_list: state.as_ref().map_or_else(
|
|
|
|
|
|| Ok(HashSet::new()),
|
|
|
|
|
|v| {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
v.iter()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.map(|x| {
|
|
|
|
|
if x.is_empty() {
|
|
|
|
|
Err(AnyError::msg("Empty path is not allowed"))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(RunDescriptor::from_str(x).unwrap())
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-10-13 13:04:44 -04:00
|
|
|
|
.collect()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
},
|
|
|
|
|
)?,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
|
pub fn new_ffi(
|
2021-10-13 13:04:44 -04:00
|
|
|
|
state: &Option<Vec<PathBuf>>,
|
2021-08-06 17:28:10 -04:00
|
|
|
|
prompt: bool,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<UnaryPermission<FfiDescriptor>, AnyError> {
|
|
|
|
|
Ok(UnaryPermission::<FfiDescriptor> {
|
2021-08-06 17:28:10 -04:00
|
|
|
|
global_state: global_state_from_option(state),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
granted_list: resolve_ffi_allowlist(state)?,
|
2021-08-06 17:28:10 -04:00
|
|
|
|
prompt,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 18:17:24 -05:00
|
|
|
|
pub fn new_hrtime(state: bool) -> UnitPermission {
|
2021-04-13 07:25:21 -04:00
|
|
|
|
unit_permission_from_flag_bool(
|
2021-04-11 22:15:43 -04:00
|
|
|
|
state,
|
|
|
|
|
"hrtime",
|
|
|
|
|
"high precision time",
|
2022-02-16 18:17:24 -05:00
|
|
|
|
false, // never prompt for hrtime
|
2021-04-11 22:15:43 -04:00
|
|
|
|
)
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 15:13:53 -04:00
|
|
|
|
pub fn from_options(opts: &PermissionsOptions) -> Result<Self, AnyError> {
|
|
|
|
|
Ok(Self {
|
|
|
|
|
read: Permissions::new_read(&opts.allow_read, opts.prompt)?,
|
|
|
|
|
write: Permissions::new_write(&opts.allow_write, opts.prompt)?,
|
|
|
|
|
net: Permissions::new_net(&opts.allow_net, opts.prompt)?,
|
|
|
|
|
env: Permissions::new_env(&opts.allow_env, opts.prompt)?,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: Permissions::new_sys(&opts.allow_sys, opts.prompt)?,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
run: Permissions::new_run(&opts.allow_run, opts.prompt)?,
|
|
|
|
|
ffi: Permissions::new_ffi(&opts.allow_ffi, opts.prompt)?,
|
2022-02-16 18:17:24 -05:00
|
|
|
|
hrtime: Permissions::new_hrtime(opts.allow_hrtime),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn allow_all() -> Self {
|
|
|
|
|
Self {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
read: Permissions::new_read(&Some(vec![]), false).unwrap(),
|
|
|
|
|
write: Permissions::new_write(&Some(vec![]), false).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&Some(vec![]), false).unwrap(),
|
|
|
|
|
env: Permissions::new_env(&Some(vec![]), false).unwrap(),
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: Permissions::new_sys(&Some(vec![]), false).unwrap(),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
run: Permissions::new_run(&Some(vec![]), false).unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(&Some(vec![]), false).unwrap(),
|
2022-02-16 18:17:24 -05:00
|
|
|
|
hrtime: Permissions::new_hrtime(true),
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-26 15:56:00 -04:00
|
|
|
|
/// A helper function that determines if the module specifier is a local or
|
|
|
|
|
/// remote, and performs a read or net check for the specifier.
|
|
|
|
|
pub fn check_specifier(
|
2021-04-11 22:15:43 -04:00
|
|
|
|
&mut self,
|
2020-10-26 15:56:00 -04:00
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2021-02-17 13:47:18 -05:00
|
|
|
|
match specifier.scheme() {
|
|
|
|
|
"file" => match specifier.to_file_path() {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
Ok(path) => self.read.check(&path, None),
|
2021-01-04 04:33:20 -05:00
|
|
|
|
Err(_) => Err(uri_error(format!(
|
|
|
|
|
"Invalid file path.\n Specifier: {}",
|
|
|
|
|
specifier
|
|
|
|
|
))),
|
2021-01-05 21:22:38 -05:00
|
|
|
|
},
|
|
|
|
|
"data" => Ok(()),
|
2021-04-07 09:22:14 -04:00
|
|
|
|
"blob" => Ok(()),
|
2022-09-27 16:36:33 -04:00
|
|
|
|
_ => self.net.check_url(specifier, None),
|
2020-10-26 15:56:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2020-10-26 15:56:00 -04:00
|
|
|
|
|
2022-08-18 08:05:02 -04:00
|
|
|
|
impl deno_flash::FlashPermissions for Permissions {
|
|
|
|
|
fn check_net<T: AsRef<str>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
host: &(T, Option<u16>),
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: &str,
|
2022-08-18 08:05:02 -04:00
|
|
|
|
) -> Result<(), AnyError> {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self.net.check(host, Some(api_name))
|
2022-08-18 08:05:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-24 12:07:49 -04:00
|
|
|
|
impl deno_node::NodePermissions for Permissions {
|
|
|
|
|
fn check_read(&mut self, path: &Path) -> Result<(), AnyError> {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self.read.check(path, None)
|
2022-08-24 12:07:49 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 19:43:03 -04:00
|
|
|
|
impl deno_net::NetPermissions for Permissions {
|
|
|
|
|
fn check_net<T: AsRef<str>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
host: &(T, Option<u16>),
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: &str,
|
2021-06-28 19:43:03 -04:00
|
|
|
|
) -> Result<(), AnyError> {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
self.net.check(host, Some(api_name))
|
2021-06-28 19:43:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_read(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.read.check(path, Some(api_name))
|
2021-06-28 19:43:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_write(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.write.check(path, Some(api_name))
|
2021-06-28 19:43:03 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
impl deno_fetch::FetchPermissions for Permissions {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_net_url(
|
|
|
|
|
&mut self,
|
|
|
|
|
url: &url::Url,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.net.check_url(url, Some(api_name))
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_read(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.read.check(path, Some(api_name))
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
|
|
2022-02-15 06:17:30 -05:00
|
|
|
|
impl deno_web::TimersPermission for Permissions {
|
2021-04-14 15:10:48 -04:00
|
|
|
|
fn allow_hrtime(&mut self) -> bool {
|
|
|
|
|
self.hrtime.check().is_ok()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_unstable(&self, state: &OpState, api_name: &'static str) {
|
|
|
|
|
crate::ops::check_unstable(state, api_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
impl deno_websocket::WebSocketPermissions for Permissions {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_net_url(
|
|
|
|
|
&mut self,
|
|
|
|
|
url: &url::Url,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.net.check_url(url, Some(api_name))
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
|
// NOTE(bartlomieju): for now, NAPI uses `--allow-ffi` flag, but that might
|
|
|
|
|
// change in the future.
|
|
|
|
|
impl deno_napi::NapiPermissions for Permissions {
|
|
|
|
|
fn check(&mut self, path: Option<&Path>) -> Result<(), AnyError> {
|
|
|
|
|
self.ffi.check(path)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
|
impl deno_ffi::FfiPermissions for Permissions {
|
2021-12-15 09:41:49 -05:00
|
|
|
|
fn check(&mut self, path: Option<&Path>) -> Result<(), AnyError> {
|
2021-08-06 17:28:10 -04:00
|
|
|
|
self.ffi.check(path)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 07:25:21 -04:00
|
|
|
|
fn unit_permission_from_flag_bool(
|
2021-03-17 17:45:12 -04:00
|
|
|
|
flag: bool,
|
|
|
|
|
name: &'static str,
|
|
|
|
|
description: &'static str,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt: bool,
|
2021-03-21 08:49:58 -04:00
|
|
|
|
) -> UnitPermission {
|
|
|
|
|
UnitPermission {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
state: if flag {
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
|
|
|
|
},
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt,
|
2019-04-08 16:22:40 -04:00
|
|
|
|
}
|
2019-03-18 16:46:23 -04:00
|
|
|
|
}
|
2019-03-04 11:04:19 -05:00
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
fn global_state_from_option<T>(flag: &Option<Vec<T>>) -> PermissionState {
|
|
|
|
|
if matches!(flag, Some(v) if v.is_empty()) {
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
2020-09-19 19:17:35 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2020-09-19 19:17:35 -04:00
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn resolve_read_allowlist(
|
|
|
|
|
allow: &Option<Vec<PathBuf>>,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<HashSet<ReadDescriptor>, AnyError> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
if let Some(v) = allow {
|
|
|
|
|
v.iter()
|
|
|
|
|
.map(|raw_path| {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
if raw_path.as_os_str().is_empty() {
|
|
|
|
|
Err(AnyError::msg("Empty path is not allowed"))
|
|
|
|
|
} else {
|
|
|
|
|
resolve_from_cwd(Path::new(&raw_path)).map(ReadDescriptor)
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
} else {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
Ok(HashSet::new())
|
2020-09-19 19:17:35 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn resolve_write_allowlist(
|
|
|
|
|
allow: &Option<Vec<PathBuf>>,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<HashSet<WriteDescriptor>, AnyError> {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
if let Some(v) = allow {
|
|
|
|
|
v.iter()
|
|
|
|
|
.map(|raw_path| {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
if raw_path.as_os_str().is_empty() {
|
|
|
|
|
Err(AnyError::msg("Empty path is not allowed"))
|
|
|
|
|
} else {
|
|
|
|
|
resolve_from_cwd(Path::new(&raw_path)).map(WriteDescriptor)
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
} else {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
Ok(HashSet::new())
|
2021-01-06 10:57:28 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub fn resolve_ffi_allowlist(
|
|
|
|
|
allow: &Option<Vec<PathBuf>>,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<HashSet<FfiDescriptor>, AnyError> {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
if let Some(v) = allow {
|
|
|
|
|
v.iter()
|
|
|
|
|
.map(|raw_path| {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
if raw_path.as_os_str().is_empty() {
|
|
|
|
|
Err(AnyError::msg("Empty path is not allowed"))
|
|
|
|
|
} else {
|
|
|
|
|
resolve_from_cwd(Path::new(&raw_path)).map(FfiDescriptor)
|
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
} else {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
Ok(HashSet::new())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -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.
|
2022-08-27 11:20:05 -04:00
|
|
|
|
#[inline]
|
2021-03-17 17:45:12 -04:00
|
|
|
|
fn resolved_and_display_path(path: &Path) -> (PathBuf, PathBuf) {
|
|
|
|
|
let resolved_path = resolve_from_cwd(path).unwrap();
|
|
|
|
|
let display_path = path.to_path_buf();
|
|
|
|
|
(resolved_path, display_path)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
fn escalation_error() -> AnyError {
|
|
|
|
|
custom_error(
|
|
|
|
|
"PermissionDenied",
|
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub enum ChildUnitPermissionArg {
|
|
|
|
|
Inherit,
|
|
|
|
|
Granted,
|
|
|
|
|
NotGranted,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for ChildUnitPermissionArg {
|
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
|
where
|
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
|
{
|
|
|
|
|
struct ChildUnitPermissionArgVisitor;
|
|
|
|
|
impl<'de> de::Visitor<'de> for ChildUnitPermissionArgVisitor {
|
|
|
|
|
type Value = ChildUnitPermissionArg;
|
|
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
formatter.write_str("\"inherit\" or boolean")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_unit<E>(self) -> Result<ChildUnitPermissionArg, E>
|
|
|
|
|
where
|
|
|
|
|
E: de::Error,
|
|
|
|
|
{
|
2022-03-15 20:43:14 -04:00
|
|
|
|
Ok(ChildUnitPermissionArg::NotGranted)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<ChildUnitPermissionArg, E>
|
|
|
|
|
where
|
|
|
|
|
E: de::Error,
|
|
|
|
|
{
|
|
|
|
|
if v == "inherit" {
|
|
|
|
|
Ok(ChildUnitPermissionArg::Inherit)
|
|
|
|
|
} else {
|
|
|
|
|
Err(de::Error::invalid_value(de::Unexpected::Str(v), &self))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_bool<E>(self, v: bool) -> Result<ChildUnitPermissionArg, E>
|
|
|
|
|
where
|
|
|
|
|
E: de::Error,
|
|
|
|
|
{
|
|
|
|
|
match v {
|
|
|
|
|
true => Ok(ChildUnitPermissionArg::Granted),
|
|
|
|
|
false => Ok(ChildUnitPermissionArg::NotGranted),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
deserializer.deserialize_any(ChildUnitPermissionArgVisitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub enum ChildUnaryPermissionArg {
|
|
|
|
|
Inherit,
|
|
|
|
|
Granted,
|
|
|
|
|
NotGranted,
|
|
|
|
|
GrantedList(Vec<String>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for ChildUnaryPermissionArg {
|
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
|
where
|
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
|
{
|
|
|
|
|
struct ChildUnaryPermissionArgVisitor;
|
|
|
|
|
impl<'de> de::Visitor<'de> for ChildUnaryPermissionArgVisitor {
|
|
|
|
|
type Value = ChildUnaryPermissionArg;
|
|
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
formatter.write_str("\"inherit\" or boolean or string[]")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_unit<E>(self) -> Result<ChildUnaryPermissionArg, E>
|
|
|
|
|
where
|
|
|
|
|
E: de::Error,
|
|
|
|
|
{
|
2022-03-15 20:43:14 -04:00
|
|
|
|
Ok(ChildUnaryPermissionArg::NotGranted)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<ChildUnaryPermissionArg, E>
|
|
|
|
|
where
|
|
|
|
|
E: de::Error,
|
|
|
|
|
{
|
|
|
|
|
if v == "inherit" {
|
|
|
|
|
Ok(ChildUnaryPermissionArg::Inherit)
|
|
|
|
|
} else {
|
|
|
|
|
Err(de::Error::invalid_value(de::Unexpected::Str(v), &self))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_bool<E>(self, v: bool) -> Result<ChildUnaryPermissionArg, E>
|
|
|
|
|
where
|
|
|
|
|
E: de::Error,
|
|
|
|
|
{
|
|
|
|
|
match v {
|
|
|
|
|
true => Ok(ChildUnaryPermissionArg::Granted),
|
|
|
|
|
false => Ok(ChildUnaryPermissionArg::NotGranted),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_seq<V>(
|
|
|
|
|
self,
|
|
|
|
|
mut v: V,
|
|
|
|
|
) -> Result<ChildUnaryPermissionArg, V::Error>
|
|
|
|
|
where
|
|
|
|
|
V: de::SeqAccess<'de>,
|
|
|
|
|
{
|
|
|
|
|
let mut granted_list = vec![];
|
|
|
|
|
while let Some(value) = v.next_element::<String>()? {
|
|
|
|
|
granted_list.push(value);
|
|
|
|
|
}
|
|
|
|
|
Ok(ChildUnaryPermissionArg::GrantedList(granted_list))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
deserializer.deserialize_any(ChildUnaryPermissionArgVisitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Directly deserializable from JS worker and test permission options.
|
2022-09-19 04:25:03 -04:00
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub struct ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg,
|
|
|
|
|
hrtime: ChildUnitPermissionArg,
|
|
|
|
|
net: ChildUnaryPermissionArg,
|
|
|
|
|
ffi: ChildUnaryPermissionArg,
|
|
|
|
|
read: ChildUnaryPermissionArg,
|
|
|
|
|
run: ChildUnaryPermissionArg,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: ChildUnaryPermissionArg,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
write: ChildUnaryPermissionArg,
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 17:46:39 -04:00
|
|
|
|
impl ChildPermissionsArg {
|
|
|
|
|
pub fn inherit() -> Self {
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
hrtime: ChildUnitPermissionArg::Inherit,
|
|
|
|
|
net: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
read: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
run: ChildUnaryPermissionArg::Inherit,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: ChildUnaryPermissionArg::Inherit,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
write: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn none() -> Self {
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
hrtime: ChildUnitPermissionArg::NotGranted,
|
|
|
|
|
net: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
read: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
run: ChildUnaryPermissionArg::NotGranted,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: ChildUnaryPermissionArg::NotGranted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
write: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
impl<'de> Deserialize<'de> for ChildPermissionsArg {
|
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
|
where
|
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
|
{
|
|
|
|
|
struct ChildPermissionsArgVisitor;
|
|
|
|
|
impl<'de> de::Visitor<'de> for ChildPermissionsArgVisitor {
|
|
|
|
|
type Value = ChildPermissionsArg;
|
|
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
formatter.write_str("\"inherit\" or \"none\" or object")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_unit<E>(self) -> Result<ChildPermissionsArg, E>
|
|
|
|
|
where
|
|
|
|
|
E: de::Error,
|
|
|
|
|
{
|
2022-03-20 17:46:39 -04:00
|
|
|
|
Ok(ChildPermissionsArg::inherit())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_str<E>(self, v: &str) -> Result<ChildPermissionsArg, E>
|
|
|
|
|
where
|
|
|
|
|
E: de::Error,
|
|
|
|
|
{
|
|
|
|
|
if v == "inherit" {
|
2022-03-20 17:46:39 -04:00
|
|
|
|
Ok(ChildPermissionsArg::inherit())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
} else if v == "none" {
|
2022-03-20 17:46:39 -04:00
|
|
|
|
Ok(ChildPermissionsArg::none())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
} else {
|
|
|
|
|
Err(de::Error::invalid_value(de::Unexpected::Str(v), &self))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_map<V>(self, mut v: V) -> Result<ChildPermissionsArg, V::Error>
|
|
|
|
|
where
|
|
|
|
|
V: de::MapAccess<'de>,
|
|
|
|
|
{
|
2022-03-20 17:46:39 -04:00
|
|
|
|
let mut child_permissions_arg = ChildPermissionsArg::none();
|
2021-10-13 13:04:44 -04:00
|
|
|
|
while let Some((key, value)) =
|
|
|
|
|
v.next_entry::<String, serde_json::Value>()?
|
|
|
|
|
{
|
|
|
|
|
if key == "env" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.env = arg.map_err(|e| {
|
|
|
|
|
de::Error::custom(format!("(deno.permissions.env) {}", e))
|
|
|
|
|
})?;
|
|
|
|
|
} else if key == "hrtime" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnitPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.hrtime = arg.map_err(|e| {
|
|
|
|
|
de::Error::custom(format!("(deno.permissions.hrtime) {}", e))
|
|
|
|
|
})?;
|
|
|
|
|
} else if key == "net" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.net = arg.map_err(|e| {
|
|
|
|
|
de::Error::custom(format!("(deno.permissions.net) {}", e))
|
|
|
|
|
})?;
|
|
|
|
|
} else if key == "ffi" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.ffi = arg.map_err(|e| {
|
|
|
|
|
de::Error::custom(format!("(deno.permissions.ffi) {}", e))
|
|
|
|
|
})?;
|
|
|
|
|
} else if key == "read" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.read = arg.map_err(|e| {
|
|
|
|
|
de::Error::custom(format!("(deno.permissions.read) {}", e))
|
|
|
|
|
})?;
|
|
|
|
|
} else if key == "run" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.run = arg.map_err(|e| {
|
|
|
|
|
de::Error::custom(format!("(deno.permissions.run) {}", e))
|
|
|
|
|
})?;
|
2022-09-28 08:46:50 -04:00
|
|
|
|
} else if key == "sys" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.sys = arg.map_err(|e| {
|
|
|
|
|
de::Error::custom(format!("(deno.permissions.sys) {}", e))
|
|
|
|
|
})?;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
} else if key == "write" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.write = arg.map_err(|e| {
|
|
|
|
|
de::Error::custom(format!("(deno.permissions.write) {}", e))
|
|
|
|
|
})?;
|
|
|
|
|
} else {
|
|
|
|
|
return Err(de::Error::custom("unknown permission name"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(child_permissions_arg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
deserializer.deserialize_any(ChildPermissionsArgVisitor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_child_permissions(
|
|
|
|
|
main_perms: &mut Permissions,
|
|
|
|
|
child_permissions_arg: ChildPermissionsArg,
|
|
|
|
|
) -> Result<Permissions, AnyError> {
|
|
|
|
|
let mut worker_perms = Permissions::default();
|
|
|
|
|
match child_permissions_arg.env {
|
|
|
|
|
ChildUnaryPermissionArg::Inherit => {
|
|
|
|
|
worker_perms.env = main_perms.env.clone();
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::Granted => {
|
|
|
|
|
if main_perms.env.check_all().is_err() {
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
worker_perms.env.global_state = PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
|
|
|
|
worker_perms.env.granted_list =
|
2022-08-10 15:13:53 -04:00
|
|
|
|
Permissions::new_env(&Some(granted_list), false)?.granted_list;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
if !worker_perms
|
|
|
|
|
.env
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
|
|
|
|
.all(|desc| main_perms.env.check(desc.as_ref()).is_ok())
|
|
|
|
|
{
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
worker_perms.env.denied_list = main_perms.env.denied_list.clone();
|
|
|
|
|
if main_perms.env.global_state == PermissionState::Denied {
|
|
|
|
|
worker_perms.env.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
worker_perms.env.prompt = main_perms.env.prompt;
|
2022-09-28 08:46:50 -04:00
|
|
|
|
match child_permissions_arg.sys {
|
|
|
|
|
ChildUnaryPermissionArg::Inherit => {
|
|
|
|
|
worker_perms.sys = main_perms.sys.clone();
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::Granted => {
|
|
|
|
|
if main_perms.sys.check_all().is_err() {
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
worker_perms.sys.global_state = PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
|
|
|
|
worker_perms.sys.granted_list =
|
|
|
|
|
Permissions::new_sys(&Some(granted_list), false)?.granted_list;
|
|
|
|
|
if !worker_perms
|
|
|
|
|
.sys
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
|
|
|
|
.all(|desc| main_perms.sys.check(&desc.0, None).is_ok())
|
|
|
|
|
{
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
worker_perms.sys.denied_list = main_perms.sys.denied_list.clone();
|
|
|
|
|
if main_perms.sys.global_state == PermissionState::Denied {
|
|
|
|
|
worker_perms.sys.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
worker_perms.sys.prompt = main_perms.sys.prompt;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
match child_permissions_arg.hrtime {
|
|
|
|
|
ChildUnitPermissionArg::Inherit => {
|
|
|
|
|
worker_perms.hrtime = main_perms.hrtime.clone();
|
|
|
|
|
}
|
|
|
|
|
ChildUnitPermissionArg::Granted => {
|
|
|
|
|
if main_perms.hrtime.check().is_err() {
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
worker_perms.hrtime.state = PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
ChildUnitPermissionArg::NotGranted => {}
|
|
|
|
|
}
|
|
|
|
|
if main_perms.hrtime.state == PermissionState::Denied {
|
|
|
|
|
worker_perms.hrtime.state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
worker_perms.hrtime.prompt = main_perms.hrtime.prompt;
|
|
|
|
|
match child_permissions_arg.net {
|
|
|
|
|
ChildUnaryPermissionArg::Inherit => {
|
|
|
|
|
worker_perms.net = main_perms.net.clone();
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::Granted => {
|
|
|
|
|
if main_perms.net.check_all().is_err() {
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
worker_perms.net.global_state = PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
|
|
|
|
worker_perms.net.granted_list =
|
2022-08-10 15:13:53 -04:00
|
|
|
|
Permissions::new_net(&Some(granted_list), false)?.granted_list;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
if !worker_perms
|
|
|
|
|
.net
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.all(|desc| main_perms.net.check(&(&desc.0, desc.1), None).is_ok())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
{
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
worker_perms.net.denied_list = main_perms.net.denied_list.clone();
|
|
|
|
|
if main_perms.net.global_state == PermissionState::Denied {
|
|
|
|
|
worker_perms.net.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
worker_perms.net.prompt = main_perms.net.prompt;
|
|
|
|
|
match child_permissions_arg.ffi {
|
|
|
|
|
ChildUnaryPermissionArg::Inherit => {
|
|
|
|
|
worker_perms.ffi = main_perms.ffi.clone();
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::Granted => {
|
|
|
|
|
if main_perms.ffi.check_all().is_err() {
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
worker_perms.ffi.global_state = PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
|
|
|
|
worker_perms.ffi.granted_list = Permissions::new_ffi(
|
|
|
|
|
&Some(granted_list.iter().map(PathBuf::from).collect()),
|
|
|
|
|
false,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
)?
|
2021-10-13 13:04:44 -04:00
|
|
|
|
.granted_list;
|
|
|
|
|
if !worker_perms
|
|
|
|
|
.ffi
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
2021-12-15 09:41:49 -05:00
|
|
|
|
.all(|desc| main_perms.ffi.check(Some(&desc.0)).is_ok())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
{
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
worker_perms.ffi.denied_list = main_perms.ffi.denied_list.clone();
|
|
|
|
|
if main_perms.ffi.global_state == PermissionState::Denied {
|
|
|
|
|
worker_perms.ffi.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
worker_perms.ffi.prompt = main_perms.ffi.prompt;
|
|
|
|
|
match child_permissions_arg.read {
|
|
|
|
|
ChildUnaryPermissionArg::Inherit => {
|
|
|
|
|
worker_perms.read = main_perms.read.clone();
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::Granted => {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if main_perms.read.check_all(None).is_err() {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
worker_perms.read.global_state = PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
|
|
|
|
worker_perms.read.granted_list = Permissions::new_read(
|
|
|
|
|
&Some(granted_list.iter().map(PathBuf::from).collect()),
|
|
|
|
|
false,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
)?
|
2021-10-13 13:04:44 -04:00
|
|
|
|
.granted_list;
|
|
|
|
|
if !worker_perms
|
|
|
|
|
.read
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.all(|desc| main_perms.read.check(&desc.0, None).is_ok())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
{
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
worker_perms.read.denied_list = main_perms.read.denied_list.clone();
|
|
|
|
|
if main_perms.read.global_state == PermissionState::Denied {
|
|
|
|
|
worker_perms.read.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
worker_perms.read.prompt = main_perms.read.prompt;
|
|
|
|
|
match child_permissions_arg.run {
|
|
|
|
|
ChildUnaryPermissionArg::Inherit => {
|
|
|
|
|
worker_perms.run = main_perms.run.clone();
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::Granted => {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if main_perms.run.check_all(None).is_err() {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
worker_perms.run.global_state = PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
|
|
|
|
worker_perms.run.granted_list =
|
2022-08-10 15:13:53 -04:00
|
|
|
|
Permissions::new_run(&Some(granted_list), false)?.granted_list;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
if !worker_perms
|
|
|
|
|
.run
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.all(|desc| main_perms.run.check(&desc.to_string(), None).is_ok())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
{
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
worker_perms.run.denied_list = main_perms.run.denied_list.clone();
|
|
|
|
|
if main_perms.run.global_state == PermissionState::Denied {
|
|
|
|
|
worker_perms.run.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
worker_perms.run.prompt = main_perms.run.prompt;
|
|
|
|
|
match child_permissions_arg.write {
|
|
|
|
|
ChildUnaryPermissionArg::Inherit => {
|
|
|
|
|
worker_perms.write = main_perms.write.clone();
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::Granted => {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if main_perms.write.check_all(None).is_err() {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
worker_perms.write.global_state = PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
|
|
|
|
worker_perms.write.granted_list = Permissions::new_write(
|
|
|
|
|
&Some(granted_list.iter().map(PathBuf::from).collect()),
|
|
|
|
|
false,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
)?
|
2021-10-13 13:04:44 -04:00
|
|
|
|
.granted_list;
|
|
|
|
|
if !worker_perms
|
|
|
|
|
.write
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.all(|desc| main_perms.write.check(&desc.0, None).is_ok())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
{
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
worker_perms.write.denied_list = main_perms.write.denied_list.clone();
|
|
|
|
|
if main_perms.write.global_state == PermissionState::Denied {
|
|
|
|
|
worker_perms.write.global_state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
worker_perms.write.prompt = main_perms.write.prompt;
|
|
|
|
|
Ok(worker_perms)
|
|
|
|
|
}
|
|
|
|
|
|
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))]
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn permission_prompt(
|
|
|
|
|
message: &str,
|
|
|
|
|
name: &str,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> bool {
|
2019-11-11 10:33:29 -05:00
|
|
|
|
if !atty::is(atty::Stream::Stdin) || !atty::is(atty::Stream::Stderr) {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2021-09-09 08:38:47 -04:00
|
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
2022-04-18 09:00:38 -04:00
|
|
|
|
fn clear_stdin() -> Result<(), AnyError> {
|
2022-06-25 18:13:24 -04:00
|
|
|
|
// TODO(bartlomieju):
|
|
|
|
|
#[allow(clippy::undocumented_unsafe_blocks)]
|
2021-09-09 08:38:47 -04:00
|
|
|
|
let r = unsafe { libc::tcflush(0, libc::TCIFLUSH) };
|
|
|
|
|
assert_eq!(r, 0);
|
2022-04-18 09:00:38 -04:00
|
|
|
|
Ok(())
|
2021-09-09 08:38:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
2022-04-18 09:00:38 -04:00
|
|
|
|
fn clear_stdin() -> Result<(), AnyError> {
|
|
|
|
|
use deno_core::anyhow::bail;
|
2021-09-14 08:37:27 -04:00
|
|
|
|
use winapi::shared::minwindef::TRUE;
|
|
|
|
|
use winapi::shared::minwindef::UINT;
|
|
|
|
|
use winapi::shared::minwindef::WORD;
|
|
|
|
|
use winapi::shared::ntdef::WCHAR;
|
|
|
|
|
use winapi::um::processenv::GetStdHandle;
|
|
|
|
|
use winapi::um::winbase::STD_INPUT_HANDLE;
|
|
|
|
|
use winapi::um::wincon::FlushConsoleInputBuffer;
|
|
|
|
|
use winapi::um::wincon::PeekConsoleInputW;
|
|
|
|
|
use winapi::um::wincon::WriteConsoleInputW;
|
|
|
|
|
use winapi::um::wincontypes::INPUT_RECORD;
|
|
|
|
|
use winapi::um::wincontypes::KEY_EVENT;
|
|
|
|
|
use winapi::um::winnt::HANDLE;
|
|
|
|
|
use winapi::um::winuser::MapVirtualKeyW;
|
|
|
|
|
use winapi::um::winuser::MAPVK_VK_TO_VSC;
|
|
|
|
|
use winapi::um::winuser::VK_RETURN;
|
|
|
|
|
|
2022-07-15 12:30:25 -04:00
|
|
|
|
// SAFETY: winapi calls
|
2021-09-09 08:38:47 -04:00
|
|
|
|
unsafe {
|
2021-09-14 08:37:27 -04:00
|
|
|
|
let stdin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
|
// emulate an enter key press to clear any line buffered console characters
|
2022-04-18 09:00:38 -04:00
|
|
|
|
emulate_enter_key_press(stdin)?;
|
2021-09-14 08:37:27 -04:00
|
|
|
|
// read the buffered line or enter key press
|
2022-04-18 09:00:38 -04:00
|
|
|
|
read_stdin_line()?;
|
2021-09-14 08:37:27 -04:00
|
|
|
|
// check if our emulated key press was executed
|
2022-04-18 09:00:38 -04:00
|
|
|
|
if is_input_buffer_empty(stdin)? {
|
2021-09-14 08:37:27 -04:00
|
|
|
|
// if so, move the cursor up to prevent a blank line
|
2022-04-18 09:00:38 -04:00
|
|
|
|
move_cursor_up()?;
|
2021-09-14 08:37:27 -04:00
|
|
|
|
} else {
|
|
|
|
|
// the emulated key press is still pending, so a buffered line was read
|
|
|
|
|
// and we can flush the emulated key press
|
2022-04-18 09:00:38 -04:00
|
|
|
|
flush_input_buffer(stdin)?;
|
2021-09-14 08:37:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-18 09:00:38 -04:00
|
|
|
|
return Ok(());
|
|
|
|
|
|
|
|
|
|
unsafe fn flush_input_buffer(stdin: HANDLE) -> Result<(), AnyError> {
|
2021-09-14 08:37:27 -04:00
|
|
|
|
let success = FlushConsoleInputBuffer(stdin);
|
|
|
|
|
if success != TRUE {
|
2022-04-18 09:00:38 -04:00
|
|
|
|
bail!(
|
|
|
|
|
"Could not flush the console input buffer: {}",
|
2022-01-15 01:10:12 -05:00
|
|
|
|
std::io::Error::last_os_error()
|
2021-09-14 08:37:27 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
2022-04-18 09:00:38 -04:00
|
|
|
|
Ok(())
|
2021-09-14 08:37:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-18 09:00:38 -04:00
|
|
|
|
unsafe fn emulate_enter_key_press(stdin: HANDLE) -> Result<(), AnyError> {
|
2021-09-14 08:37:27 -04:00
|
|
|
|
// https://github.com/libuv/libuv/blob/a39009a5a9252a566ca0704d02df8dabc4ce328f/src/win/tty.c#L1121-L1131
|
|
|
|
|
let mut input_record: INPUT_RECORD = std::mem::zeroed();
|
|
|
|
|
input_record.EventType = KEY_EVENT;
|
|
|
|
|
input_record.Event.KeyEvent_mut().bKeyDown = TRUE;
|
|
|
|
|
input_record.Event.KeyEvent_mut().wRepeatCount = 1;
|
|
|
|
|
input_record.Event.KeyEvent_mut().wVirtualKeyCode = VK_RETURN as WORD;
|
|
|
|
|
input_record.Event.KeyEvent_mut().wVirtualScanCode =
|
|
|
|
|
MapVirtualKeyW(VK_RETURN as UINT, MAPVK_VK_TO_VSC) as WORD;
|
|
|
|
|
*input_record.Event.KeyEvent_mut().uChar.UnicodeChar_mut() =
|
|
|
|
|
'\r' as WCHAR;
|
|
|
|
|
|
|
|
|
|
let mut record_written = 0;
|
|
|
|
|
let success =
|
|
|
|
|
WriteConsoleInputW(stdin, &input_record, 1, &mut record_written);
|
|
|
|
|
if success != TRUE {
|
2022-04-18 09:00:38 -04:00
|
|
|
|
bail!(
|
|
|
|
|
"Could not emulate enter key press: {}",
|
2022-01-15 01:10:12 -05:00
|
|
|
|
std::io::Error::last_os_error()
|
2022-04-18 09:00:38 -04:00
|
|
|
|
);
|
2021-09-14 08:37:27 -04:00
|
|
|
|
}
|
2022-04-18 09:00:38 -04:00
|
|
|
|
Ok(())
|
2021-09-14 08:37:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-18 09:00:38 -04:00
|
|
|
|
unsafe fn is_input_buffer_empty(stdin: HANDLE) -> Result<bool, AnyError> {
|
2021-09-14 08:37:27 -04:00
|
|
|
|
let mut buffer = Vec::with_capacity(1);
|
|
|
|
|
let mut events_read = 0;
|
|
|
|
|
let success =
|
|
|
|
|
PeekConsoleInputW(stdin, buffer.as_mut_ptr(), 1, &mut events_read);
|
|
|
|
|
if success != TRUE {
|
2022-04-18 09:00:38 -04:00
|
|
|
|
bail!(
|
|
|
|
|
"Could not peek the console input buffer: {}",
|
2022-01-15 01:10:12 -05:00
|
|
|
|
std::io::Error::last_os_error()
|
2021-09-14 08:37:27 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
2022-04-18 09:00:38 -04:00
|
|
|
|
Ok(events_read == 0)
|
2021-09-14 08:37:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-18 09:00:38 -04:00
|
|
|
|
fn move_cursor_up() -> Result<(), AnyError> {
|
2021-09-14 08:37:27 -04:00
|
|
|
|
use std::io::Write;
|
2022-04-18 09:00:38 -04:00
|
|
|
|
write!(std::io::stderr(), "\x1B[1A")?;
|
|
|
|
|
Ok(())
|
2021-09-14 08:37:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-18 09:00:38 -04:00
|
|
|
|
fn read_stdin_line() -> Result<(), AnyError> {
|
2021-09-14 08:37:27 -04:00
|
|
|
|
let mut input = String::new();
|
|
|
|
|
let stdin = std::io::stdin();
|
2022-04-18 09:00:38 -04:00
|
|
|
|
stdin.read_line(&mut input)?;
|
|
|
|
|
Ok(())
|
2021-09-09 08:38:47 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 08:16:06 -04:00
|
|
|
|
// Clear n-lines in terminal and move cursor to the beginning of the line.
|
|
|
|
|
fn clear_n_lines(n: usize) {
|
|
|
|
|
eprint!("\x1B[{}A\x1B[0J", n);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 08:38:47 -04:00
|
|
|
|
// For security reasons we must consume everything in stdin so that previously
|
|
|
|
|
// buffered data cannot effect the prompt.
|
2022-04-18 09:00:38 -04:00
|
|
|
|
if let Err(err) = clear_stdin() {
|
|
|
|
|
eprintln!("Error clearing stdin for permission prompt. {:#}", err);
|
|
|
|
|
return false; // don't grant permission if this fails
|
|
|
|
|
}
|
2021-09-09 08:38:47 -04:00
|
|
|
|
|
2022-09-22 08:16:06 -04:00
|
|
|
|
// print to stderr so that if stdout is piped this is still displayed.
|
|
|
|
|
const OPTS: &str = "[y/n] (y = yes, allow; n = no, deny)";
|
|
|
|
|
eprint!("{} ┌ ", PERMISSION_EMOJI);
|
|
|
|
|
eprint!("{}", colors::bold("Deno requests "));
|
|
|
|
|
eprint!("{}", colors::bold(message));
|
|
|
|
|
eprintln!("{}", colors::bold("."));
|
2022-09-27 16:36:33 -04:00
|
|
|
|
if let Some(api_name) = api_name {
|
|
|
|
|
eprintln!(" ├ Requested by `{}` API", api_name);
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
let msg = format!(
|
2022-09-22 08:16:06 -04:00
|
|
|
|
" ├ Run again with --allow-{} to bypass this prompt.",
|
|
|
|
|
name
|
2019-11-11 10:33:29 -05:00
|
|
|
|
);
|
2022-09-22 08:16:06 -04:00
|
|
|
|
eprintln!("{}", colors::italic(&msg));
|
|
|
|
|
eprint!(" └ {}", colors::bold("Allow?"));
|
|
|
|
|
eprint!(" {} > ", OPTS);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
loop {
|
|
|
|
|
let mut input = String::new();
|
2021-09-14 08:37:27 -04:00
|
|
|
|
let stdin = std::io::stdin();
|
2019-11-11 10:33:29 -05:00
|
|
|
|
let result = stdin.read_line(&mut input);
|
|
|
|
|
if result.is_err() {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2021-04-02 16:49:51 -04:00
|
|
|
|
let ch = match input.chars().next() {
|
|
|
|
|
None => return false,
|
|
|
|
|
Some(v) => v,
|
|
|
|
|
};
|
2019-11-11 10:33:29 -05:00
|
|
|
|
match ch.to_ascii_lowercase() {
|
2022-09-22 08:16:06 -04:00
|
|
|
|
'y' => {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
clear_n_lines(if api_name.is_some() { 4 } else { 3 });
|
2022-09-22 08:16:06 -04:00
|
|
|
|
let msg = format!("Granted {}.", message);
|
|
|
|
|
eprintln!("✅ {}", colors::bold(&msg));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
'n' => {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
clear_n_lines(if api_name.is_some() { 4 } else { 3 });
|
2022-09-22 08:16:06 -04:00
|
|
|
|
let msg = format!("Denied {}.", message);
|
|
|
|
|
eprintln!("❌ {}", colors::bold(&msg));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
_ => {
|
|
|
|
|
// If we don't get a recognized option try again.
|
2022-09-22 08:16:06 -04:00
|
|
|
|
clear_n_lines(1);
|
|
|
|
|
eprint!(" └ {}", colors::bold("Unrecognized option. Allow?"));
|
|
|
|
|
eprint!(" {} > ", OPTS);
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
// When testing, permission prompt returns the value of STUB_PROMPT_VALUE
|
|
|
|
|
// which we set from the test functions.
|
|
|
|
|
#[cfg(test)]
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn permission_prompt(
|
|
|
|
|
_message: &str,
|
|
|
|
|
_flag: &str,
|
|
|
|
|
_api_name: Option<&str>,
|
|
|
|
|
) -> bool {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
STUB_PROMPT_VALUE.load(Ordering::SeqCst)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
static STUB_PROMPT_VALUE: AtomicBool = AtomicBool::new(true);
|
|
|
|
|
|
2020-02-24 09:13:03 -05:00
|
|
|
|
#[cfg(test)]
|
2021-12-18 16:14:42 -05:00
|
|
|
|
static PERMISSION_PROMPT_STUB_VALUE_SETTER: Lazy<
|
|
|
|
|
Mutex<PermissionPromptStubValueSetter>,
|
|
|
|
|
> = Lazy::new(|| Mutex::new(PermissionPromptStubValueSetter));
|
2020-02-24 09:13:03 -05:00
|
|
|
|
|
2019-11-11 10:33:29 -05:00
|
|
|
|
#[cfg(test)]
|
2021-10-26 11:49:07 -04:00
|
|
|
|
struct PermissionPromptStubValueSetter;
|
2019-11-11 10:33:29 -05:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2021-10-26 11:49:07 -04:00
|
|
|
|
impl PermissionPromptStubValueSetter {
|
|
|
|
|
pub fn set(&self, value: bool) {
|
|
|
|
|
STUB_PROMPT_VALUE.store(value, Ordering::SeqCst);
|
|
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2021-02-17 13:47:18 -05:00
|
|
|
|
use deno_core::resolve_url_or_path;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
use deno_core::serde_json::json;
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let mut perms = Permissions::from_options(&PermissionsOptions {
|
2020-12-29 13:34:35 -05:00
|
|
|
|
allow_read: Some(allowlist.clone()),
|
2022-12-14 15:38:53 -05:00
|
|
|
|
allow_write: Some(allowlist.clone()),
|
|
|
|
|
allow_ffi: Some(allowlist),
|
2019-05-08 19:20:30 -04:00
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Inside of /a/specific and /a/specific/dir/name
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.read
|
|
|
|
|
.check(Path::new("/a/specific/dir/name"), None)
|
|
|
|
|
.is_ok());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.write
|
|
|
|
|
.check(Path::new("/a/specific/dir/name"), None)
|
|
|
|
|
.is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert!(perms
|
|
|
|
|
.ffi
|
|
|
|
|
.check(Some(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
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/a/specific/dir"), None).is_ok());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.write
|
|
|
|
|
.check(Path::new("/a/specific/dir"), None)
|
|
|
|
|
.is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert!(perms.ffi.check(Some(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
|
2021-03-17 17:45:12 -04:00
|
|
|
|
.read
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.check(Path::new("/a/specific/dir/name/inner"), None)
|
2020-01-20 09:45:44 -05:00
|
|
|
|
.is_ok());
|
|
|
|
|
assert!(perms
|
2021-03-17 17:45:12 -04:00
|
|
|
|
.write
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.check(Path::new("/a/specific/dir/name/inner"), None)
|
2020-01-20 09:45:44 -05:00
|
|
|
|
.is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert!(perms
|
|
|
|
|
.ffi
|
|
|
|
|
.check(Some(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
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.read
|
|
|
|
|
.check(Path::new("/a/specific/other/dir"), None)
|
|
|
|
|
.is_ok());
|
2020-01-20 09:45:44 -05:00
|
|
|
|
assert!(perms
|
2021-03-17 17:45:12 -04:00
|
|
|
|
.write
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.check(Path::new("/a/specific/other/dir"), None)
|
2020-01-20 09:45:44 -05:00
|
|
|
|
.is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert!(perms
|
|
|
|
|
.ffi
|
|
|
|
|
.check(Some(Path::new("/a/specific/other/dir")))
|
|
|
|
|
.is_ok());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Exact match with /b/c
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/b/c"), None).is_ok());
|
|
|
|
|
assert!(perms.write.check(Path::new("/b/c"), None).is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/b/c"))).is_ok());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Sub path within /b/c
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/b/c/sub/path"), None).is_ok());
|
|
|
|
|
assert!(perms.write.check(Path::new("/b/c/sub/path"), None).is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert!(perms.ffi.check(Some(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
|
2021-03-17 17:45:12 -04:00
|
|
|
|
.read
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.check(Path::new("/b/c/sub/path/../path/."), None)
|
2020-05-29 11:27:43 -04:00
|
|
|
|
.is_ok());
|
|
|
|
|
assert!(perms
|
2021-03-17 17:45:12 -04:00
|
|
|
|
.write
|
2022-09-27 16:36:33 -04:00
|
|
|
|
.check(Path::new("/b/c/sub/path/../path/."), None)
|
2020-05-29 11:27:43 -04:00
|
|
|
|
.is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert!(perms
|
|
|
|
|
.ffi
|
|
|
|
|
.check(Some(Path::new("/b/c/sub/path/../path/.")))
|
|
|
|
|
.is_ok());
|
2020-05-29 11:27:43 -04:00
|
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
|
// Inside of /b but outside of /b/c
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/b/e"), None).is_err());
|
|
|
|
|
assert!(perms.write.check(Path::new("/b/e"), None).is_err());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/b/e"))).is_err());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Inside of /a but outside of /a/specific
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/a/b"), None).is_err());
|
|
|
|
|
assert!(perms.write.check(Path::new("/a/b"), None).is_err());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/a/b"))).is_err());
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-01-19 09:39:04 -05:00
|
|
|
|
fn test_check_net_with_values() {
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let mut perms = Permissions::from_options(&PermissionsOptions {
|
2020-12-29 13:34:35 -05:00
|
|
|
|
allow_net: Some(svec![
|
2019-05-08 19:20:30 -04:00
|
|
|
|
"localhost",
|
|
|
|
|
"deno.land",
|
|
|
|
|
"github.com:3000",
|
|
|
|
|
"127.0.0.1",
|
2020-07-02 10:16:41 -04:00
|
|
|
|
"172.16.0.2:8000",
|
|
|
|
|
"www.github.com:443"
|
2020-12-29 13:34:35 -05:00
|
|
|
|
]),
|
2019-05-08 19:20:30 -04:00
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
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
|
|
|
|
];
|
|
|
|
|
|
2021-01-19 09:39:04 -05:00
|
|
|
|
for (host, port, is_ok) in domain_tests {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert_eq!(is_ok, perms.net.check(&(host, Some(port)), None).is_ok());
|
2021-01-19 09:39:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_check_net_only_flag() {
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let mut perms = Permissions::from_options(&PermissionsOptions {
|
2021-01-19 09:39:04 -05:00
|
|
|
|
allow_net: Some(svec![]), // this means `--allow-net` is present without values following `=` sign
|
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2021-01-19 09:39:04 -05:00
|
|
|
|
|
|
|
|
|
let domain_tests = vec![
|
|
|
|
|
("localhost", 1234),
|
|
|
|
|
("deno.land", 0),
|
|
|
|
|
("deno.land", 3000),
|
|
|
|
|
("deno.lands", 0),
|
|
|
|
|
("deno.lands", 3000),
|
|
|
|
|
("github.com", 3000),
|
|
|
|
|
("github.com", 0),
|
|
|
|
|
("github.com", 2000),
|
|
|
|
|
("github.net", 3000),
|
|
|
|
|
("127.0.0.1", 0),
|
|
|
|
|
("127.0.0.1", 3000),
|
|
|
|
|
("127.0.0.2", 0),
|
|
|
|
|
("127.0.0.2", 3000),
|
|
|
|
|
("172.16.0.2", 8000),
|
|
|
|
|
("172.16.0.2", 0),
|
|
|
|
|
("172.16.0.2", 6000),
|
|
|
|
|
("172.16.0.1", 8000),
|
|
|
|
|
("somedomain", 0),
|
|
|
|
|
("192.168.0.1", 0),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (host, port) in domain_tests {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.net.check(&(host, Some(port)), None).is_ok());
|
2021-01-19 09:39:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_check_net_no_flag() {
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let mut perms = Permissions::from_options(&PermissionsOptions {
|
2021-01-19 09:39:04 -05:00
|
|
|
|
allow_net: None,
|
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2021-01-19 09:39:04 -05:00
|
|
|
|
|
|
|
|
|
let domain_tests = vec![
|
|
|
|
|
("localhost", 1234),
|
|
|
|
|
("deno.land", 0),
|
|
|
|
|
("deno.land", 3000),
|
|
|
|
|
("deno.lands", 0),
|
|
|
|
|
("deno.lands", 3000),
|
|
|
|
|
("github.com", 3000),
|
|
|
|
|
("github.com", 0),
|
|
|
|
|
("github.com", 2000),
|
|
|
|
|
("github.net", 3000),
|
|
|
|
|
("127.0.0.1", 0),
|
|
|
|
|
("127.0.0.1", 3000),
|
|
|
|
|
("127.0.0.2", 0),
|
|
|
|
|
("127.0.0.2", 3000),
|
|
|
|
|
("172.16.0.2", 8000),
|
|
|
|
|
("172.16.0.2", 0),
|
|
|
|
|
("172.16.0.2", 6000),
|
|
|
|
|
("172.16.0.1", 8000),
|
|
|
|
|
("somedomain", 0),
|
|
|
|
|
("192.168.0.1", 0),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (host, port) in domain_tests {
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.net.check(&(host, Some(port)), None).is_err());
|
2021-01-19 09:39:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_check_net_url() {
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let mut perms = Permissions::from_options(&PermissionsOptions {
|
2021-01-19 09:39:04 -05:00
|
|
|
|
allow_net: Some(svec![
|
|
|
|
|
"localhost",
|
|
|
|
|
"deno.land",
|
|
|
|
|
"github.com:3000",
|
|
|
|
|
"127.0.0.1",
|
|
|
|
|
"172.16.0.2:8000",
|
|
|
|
|
"www.github.com:443"
|
|
|
|
|
]),
|
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2021-01-19 09:39:04 -05:00
|
|
|
|
|
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),
|
2020-07-02 10:16:41 -04:00
|
|
|
|
// Testing issue #6531 (Network permissions check doesn't account for well-known default ports) so we dont regress
|
|
|
|
|
("https://www.github.com:443/robots.txt", true),
|
2019-06-24 19:30:11 -04:00
|
|
|
|
];
|
|
|
|
|
|
2021-01-19 09:39:04 -05:00
|
|
|
|
for (url_str, is_ok) in url_tests {
|
2019-06-24 19:30:11 -04:00
|
|
|
|
let u = url::Url::parse(url_str).unwrap();
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert_eq!(is_ok, perms.net.check_url(&u, None).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
|
|
|
|
|
2020-10-26 15:56:00 -04:00
|
|
|
|
#[test]
|
|
|
|
|
fn check_specifiers() {
|
|
|
|
|
let read_allowlist = if cfg!(target_os = "windows") {
|
|
|
|
|
vec![PathBuf::from("C:\\a")]
|
|
|
|
|
} else {
|
|
|
|
|
vec![PathBuf::from("/a")]
|
|
|
|
|
};
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let mut perms = Permissions::from_options(&PermissionsOptions {
|
2020-12-29 13:34:35 -05:00
|
|
|
|
allow_read: Some(read_allowlist),
|
|
|
|
|
allow_net: Some(svec!["localhost"]),
|
2020-10-26 15:56:00 -04:00
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2020-10-26 15:56:00 -04:00
|
|
|
|
|
|
|
|
|
let mut fixtures = vec![
|
|
|
|
|
(
|
2021-02-17 13:47:18 -05:00
|
|
|
|
resolve_url_or_path("http://localhost:4545/mod.ts").unwrap(),
|
2020-10-26 15:56:00 -04:00
|
|
|
|
true,
|
|
|
|
|
),
|
|
|
|
|
(
|
2021-02-17 13:47:18 -05:00
|
|
|
|
resolve_url_or_path("http://deno.land/x/mod.ts").unwrap(),
|
2020-10-26 15:56:00 -04:00
|
|
|
|
false,
|
|
|
|
|
),
|
2021-01-05 21:22:38 -05:00
|
|
|
|
(
|
2021-02-17 13:47:18 -05:00
|
|
|
|
resolve_url_or_path("data:text/plain,Hello%2C%20Deno!").unwrap(),
|
2021-01-05 21:22:38 -05:00
|
|
|
|
true,
|
|
|
|
|
),
|
2020-10-26 15:56:00 -04:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if cfg!(target_os = "windows") {
|
2021-02-17 13:47:18 -05:00
|
|
|
|
fixtures
|
|
|
|
|
.push((resolve_url_or_path("file:///C:/a/mod.ts").unwrap(), true));
|
|
|
|
|
fixtures
|
|
|
|
|
.push((resolve_url_or_path("file:///C:/b/mod.ts").unwrap(), false));
|
2020-10-26 15:56:00 -04:00
|
|
|
|
} else {
|
2021-02-17 13:47:18 -05:00
|
|
|
|
fixtures.push((resolve_url_or_path("file:///a/mod.ts").unwrap(), true));
|
|
|
|
|
fixtures.push((resolve_url_or_path("file:///b/mod.ts").unwrap(), false));
|
2020-10-26 15:56:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (specifier, expected) in fixtures {
|
|
|
|
|
assert_eq!(perms.check_specifier(&specifier).is_ok(), expected);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 04:33:20 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn check_invalid_specifiers() {
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let mut perms = Permissions::allow_all();
|
2021-01-04 04:33:20 -05:00
|
|
|
|
|
|
|
|
|
let mut test_cases = vec![];
|
|
|
|
|
|
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
|
test_cases.push("file://");
|
|
|
|
|
test_cases.push("file:///");
|
|
|
|
|
} else {
|
|
|
|
|
test_cases.push("file://remotehost/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for url in test_cases {
|
|
|
|
|
assert!(perms
|
2021-02-17 13:47:18 -05:00
|
|
|
|
.check_specifier(&resolve_url_or_path(url).unwrap())
|
2021-01-04 04:33:20 -05:00
|
|
|
|
.is_err());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 16:29:32 -04:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_query() {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let perms1 = Permissions::allow_all();
|
|
|
|
|
let perms2 = Permissions {
|
2020-08-18 16:29:32 -04:00
|
|
|
|
read: UnaryPermission {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
global_state: PermissionState::Prompt,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
..Permissions::new_read(&Some(vec![PathBuf::from("/foo")]), false)
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.unwrap()
|
2020-08-18 16:29:32 -04:00
|
|
|
|
},
|
|
|
|
|
write: UnaryPermission {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
global_state: PermissionState::Prompt,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
..Permissions::new_write(&Some(vec![PathBuf::from("/foo")]), false)
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.unwrap()
|
2020-08-18 16:29:32 -04:00
|
|
|
|
},
|
2022-12-14 15:38:53 -05:00
|
|
|
|
ffi: UnaryPermission {
|
|
|
|
|
global_state: PermissionState::Prompt,
|
|
|
|
|
..Permissions::new_ffi(&Some(vec![PathBuf::from("/foo")]), false)
|
|
|
|
|
.unwrap()
|
|
|
|
|
},
|
|
|
|
|
|
2020-08-18 16:29:32 -04:00
|
|
|
|
net: UnaryPermission {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
global_state: PermissionState::Prompt,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
..Permissions::new_net(&Some(svec!["127.0.0.1:8000"]), false).unwrap()
|
2021-03-17 17:45:12 -04:00
|
|
|
|
},
|
2021-04-13 07:25:21 -04:00
|
|
|
|
env: UnaryPermission {
|
|
|
|
|
global_state: PermissionState::Prompt,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
..Permissions::new_env(&Some(svec!["HOME"]), false).unwrap()
|
2020-08-18 16:29:32 -04:00
|
|
|
|
},
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: UnaryPermission {
|
|
|
|
|
global_state: PermissionState::Prompt,
|
|
|
|
|
..Permissions::new_sys(&Some(svec!["hostname"]), false).unwrap()
|
|
|
|
|
},
|
2021-04-09 18:12:00 -04:00
|
|
|
|
run: UnaryPermission {
|
|
|
|
|
global_state: PermissionState::Prompt,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
..Permissions::new_run(&Some(svec!["deno"]), false).unwrap()
|
2020-08-18 16:29:32 -04:00
|
|
|
|
},
|
2021-03-21 08:49:58 -04:00
|
|
|
|
hrtime: UnitPermission {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
state: PermissionState::Prompt,
|
2022-02-16 18:17:24 -05:00
|
|
|
|
..Permissions::new_hrtime(false)
|
2020-08-18 16:29:32 -04:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
{
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms1.read.query(None), PermissionState::Granted);
|
2021-07-30 09:03:41 -04:00
|
|
|
|
assert_eq!(perms1.read.query(Some(Path::new("/foo"))), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms2.read.query(None), PermissionState::Prompt);
|
2021-07-30 09:03:41 -04:00
|
|
|
|
assert_eq!(perms2.read.query(Some(Path::new("/foo"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.read.query(Some(Path::new("/foo/bar"))), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms1.write.query(None), PermissionState::Granted);
|
2021-07-30 09:03:41 -04:00
|
|
|
|
assert_eq!(perms1.write.query(Some(Path::new("/foo"))), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms2.write.query(None), PermissionState::Prompt);
|
2021-07-30 09:03:41 -04:00
|
|
|
|
assert_eq!(perms2.write.query(Some(Path::new("/foo"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.write.query(Some(Path::new("/foo/bar"))), PermissionState::Granted);
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert_eq!(perms1.ffi.query(None), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms1.ffi.query(Some(Path::new("/foo"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.ffi.query(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms2.ffi.query(Some(Path::new("/foo"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.ffi.query(Some(Path::new("/foo/bar"))), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms1.net.query::<&str>(None), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms1.net.query(Some(&("127.0.0.1", None))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.net.query::<&str>(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms2.net.query(Some(&("127.0.0.1", Some(8000)))), PermissionState::Granted);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert_eq!(perms1.env.query(None), PermissionState::Granted);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms1.env.query(Some("HOME")), PermissionState::Granted);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert_eq!(perms2.env.query(None), PermissionState::Prompt);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms2.env.query(Some("HOME")), PermissionState::Granted);
|
2022-09-28 08:46:50 -04:00
|
|
|
|
assert_eq!(perms1.sys.query(None), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms1.sys.query(Some("HOME")), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.env.query(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms2.sys.query(Some("hostname")), PermissionState::Granted);
|
2021-04-09 18:12:00 -04:00
|
|
|
|
assert_eq!(perms1.run.query(None), PermissionState::Granted);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms1.run.query(Some("deno")), PermissionState::Granted);
|
2021-04-09 18:12:00 -04:00
|
|
|
|
assert_eq!(perms2.run.query(None), PermissionState::Prompt);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms2.run.query(Some("deno")), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms1.hrtime.query(), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.hrtime.query(), PermissionState::Prompt);
|
2020-08-18 16:29:32 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_request() {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let mut perms: Permissions = Default::default();
|
2020-08-18 16:29:32 -04:00
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
{
|
2021-10-26 11:49:07 -04:00
|
|
|
|
let prompt_value = PERMISSION_PROMPT_STUB_VALUE_SETTER.lock();
|
|
|
|
|
prompt_value.set(true);
|
2021-07-30 09:03:41 -04:00
|
|
|
|
assert_eq!(perms.read.request(Some(Path::new("/foo"))), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms.read.query(None), PermissionState::Prompt);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2021-07-30 09:03:41 -04:00
|
|
|
|
assert_eq!(perms.read.request(Some(Path::new("/foo/bar"))), PermissionState::Granted);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2021-07-30 09:03:41 -04:00
|
|
|
|
assert_eq!(perms.write.request(Some(Path::new("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms.write.query(Some(Path::new("/foo/bar"))), PermissionState::Prompt);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms.write.request(None), PermissionState::Denied);
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(false);
|
|
|
|
|
assert_eq!(perms.ffi.request(Some(Path::new("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms.ffi.query(Some(Path::new("/foo/bar"))), PermissionState::Prompt);
|
|
|
|
|
prompt_value.set(true);
|
|
|
|
|
assert_eq!(perms.ffi.request(None), PermissionState::Denied);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms.net.request(Some(&("127.0.0.1", None))), PermissionState::Granted);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms.net.request(Some(&("127.0.0.1", Some(8000)))), PermissionState::Granted);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms.env.request(Some("HOME")), PermissionState::Granted);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert_eq!(perms.env.query(None), PermissionState::Prompt);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms.env.request(Some("HOME")), PermissionState::Granted);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-09-28 08:46:50 -04:00
|
|
|
|
assert_eq!(perms.sys.request(Some("hostname")), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms.sys.query(None), PermissionState::Prompt);
|
|
|
|
|
prompt_value.set(false);
|
|
|
|
|
assert_eq!(perms.sys.request(Some("hostname")), PermissionState::Granted);
|
|
|
|
|
prompt_value.set(true);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms.run.request(Some("deno")), PermissionState::Granted);
|
2021-04-09 18:12:00 -04:00
|
|
|
|
assert_eq!(perms.run.query(None), PermissionState::Prompt);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms.run.request(Some("deno")), PermissionState::Granted);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms.hrtime.request(), PermissionState::Denied);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms.hrtime.request(), PermissionState::Denied);
|
2020-08-18 16:29:32 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_revoke() {
|
|
|
|
|
let mut perms = Permissions {
|
|
|
|
|
read: UnaryPermission {
|
2020-10-31 18:44:42 -04:00
|
|
|
|
global_state: PermissionState::Prompt,
|
2021-09-24 01:48:15 -04:00
|
|
|
|
..Permissions::new_read(
|
|
|
|
|
&Some(vec![PathBuf::from("/foo"), PathBuf::from("/foo/baz")]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.unwrap()
|
2020-08-18 16:29:32 -04:00
|
|
|
|
},
|
|
|
|
|
write: UnaryPermission {
|
2020-10-31 18:44:42 -04:00
|
|
|
|
global_state: PermissionState::Prompt,
|
2021-09-24 01:48:15 -04:00
|
|
|
|
..Permissions::new_write(
|
|
|
|
|
&Some(vec![PathBuf::from("/foo"), PathBuf::from("/foo/baz")]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.unwrap()
|
2020-08-18 16:29:32 -04:00
|
|
|
|
},
|
2022-12-14 15:38:53 -05:00
|
|
|
|
ffi: UnaryPermission {
|
|
|
|
|
global_state: PermissionState::Prompt,
|
|
|
|
|
..Permissions::new_ffi(
|
|
|
|
|
&Some(vec![PathBuf::from("/foo"), PathBuf::from("/foo/baz")]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
},
|
2020-08-18 16:29:32 -04:00
|
|
|
|
net: UnaryPermission {
|
2020-12-30 17:35:28 -05:00
|
|
|
|
global_state: PermissionState::Prompt,
|
2021-09-24 01:48:15 -04:00
|
|
|
|
..Permissions::new_net(
|
|
|
|
|
&Some(svec!["127.0.0.1", "127.0.0.1:8000"]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.unwrap()
|
2021-03-17 17:45:12 -04:00
|
|
|
|
},
|
2021-04-13 07:25:21 -04:00
|
|
|
|
env: UnaryPermission {
|
|
|
|
|
global_state: PermissionState::Prompt,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
..Permissions::new_env(&Some(svec!["HOME"]), false).unwrap()
|
2021-03-17 17:45:12 -04:00
|
|
|
|
},
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: UnaryPermission {
|
|
|
|
|
global_state: PermissionState::Prompt,
|
|
|
|
|
..Permissions::new_sys(&Some(svec!["hostname"]), false).unwrap()
|
|
|
|
|
},
|
2021-04-09 18:12:00 -04:00
|
|
|
|
run: UnaryPermission {
|
|
|
|
|
global_state: PermissionState::Prompt,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
..Permissions::new_run(&Some(svec!["deno"]), false).unwrap()
|
2021-03-17 17:45:12 -04:00
|
|
|
|
},
|
2021-03-21 08:49:58 -04:00
|
|
|
|
hrtime: UnitPermission {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
state: PermissionState::Denied,
|
2022-02-16 18:17:24 -05:00
|
|
|
|
..Permissions::new_hrtime(false)
|
2020-08-18 16:29:32 -04:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
{
|
2021-09-24 01:48:15 -04:00
|
|
|
|
assert_eq!(perms.read.revoke(Some(Path::new("/foo/bar"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.read.query(Some(Path::new("/foo"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.read.query(Some(Path::new("/foo/baz"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms.write.revoke(Some(Path::new("/foo/bar"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.write.query(Some(Path::new("/foo"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.write.query(Some(Path::new("/foo/baz"))), PermissionState::Granted);
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert_eq!(perms.ffi.revoke(Some(Path::new("/foo/bar"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.ffi.query(Some(Path::new("/foo"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.ffi.query(Some(Path::new("/foo/baz"))), PermissionState::Granted);
|
2021-09-24 01:48:15 -04:00
|
|
|
|
assert_eq!(perms.net.revoke(Some(&("127.0.0.1", Some(9000)))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.net.query(Some(&("127.0.0.1", None))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.net.query(Some(&("127.0.0.1", Some(8000)))), PermissionState::Granted);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms.env.revoke(Some("HOME")), PermissionState::Prompt);
|
2022-09-28 08:46:50 -04:00
|
|
|
|
assert_eq!(perms.env.revoke(Some("hostname")), PermissionState::Prompt);
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms.run.revoke(Some("deno")), PermissionState::Prompt);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms.hrtime.revoke(), PermissionState::Denied);
|
2020-08-18 16:29:32 -04:00
|
|
|
|
};
|
2020-06-06 10:56:21 -04:00
|
|
|
|
}
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_check() {
|
|
|
|
|
let mut perms = Permissions {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
read: Permissions::new_read(&None, true).unwrap(),
|
|
|
|
|
write: Permissions::new_write(&None, true).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&None, true).unwrap(),
|
|
|
|
|
env: Permissions::new_env(&None, true).unwrap(),
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: Permissions::new_sys(&None, true).unwrap(),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
run: Permissions::new_run(&None, true).unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(&None, true).unwrap(),
|
2022-02-16 18:17:24 -05:00
|
|
|
|
hrtime: Permissions::new_hrtime(false),
|
2021-04-11 22:15:43 -04:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
let prompt_value = PERMISSION_PROMPT_STUB_VALUE_SETTER.lock();
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/foo"), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/foo"), None).is_ok());
|
|
|
|
|
assert!(perms.read.check(Path::new("/bar"), None).is_err());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.write.check(Path::new("/foo"), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.write.check(Path::new("/foo"), None).is_ok());
|
|
|
|
|
assert!(perms.write.check(Path::new("/bar"), None).is_err());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(true);
|
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/foo"))).is_ok());
|
|
|
|
|
prompt_value.set(false);
|
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/foo"))).is_ok());
|
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/bar"))).is_err());
|
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.net.check(&("127.0.0.1", Some(8000)), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.net.check(&("127.0.0.1", Some(8000)), None).is_ok());
|
|
|
|
|
assert!(perms.net.check(&("127.0.0.1", Some(8001)), None).is_err());
|
|
|
|
|
assert!(perms.net.check(&("127.0.0.1", None), None).is_err());
|
|
|
|
|
assert!(perms.net.check(&("deno.land", Some(8000)), None).is_err());
|
|
|
|
|
assert!(perms.net.check(&("deno.land", None), None).is_err());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.run.check("cat", None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.run.check("cat", None).is_ok());
|
|
|
|
|
assert!(perms.run.check("ls", None).is_err());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert!(perms.env.check("HOME").is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert!(perms.env.check("HOME").is_ok());
|
|
|
|
|
assert!(perms.env.check("PATH").is_err());
|
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt_value.set(true);
|
|
|
|
|
assert!(perms.env.check("hostname").is_ok());
|
|
|
|
|
prompt_value.set(false);
|
|
|
|
|
assert!(perms.env.check("hostname").is_ok());
|
|
|
|
|
assert!(perms.env.check("osRelease").is_err());
|
|
|
|
|
|
2022-02-16 18:17:24 -05:00
|
|
|
|
assert!(perms.hrtime.check().is_err());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_check_fail() {
|
|
|
|
|
let mut perms = Permissions {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
read: Permissions::new_read(&None, true).unwrap(),
|
|
|
|
|
write: Permissions::new_write(&None, true).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&None, true).unwrap(),
|
|
|
|
|
env: Permissions::new_env(&None, true).unwrap(),
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: Permissions::new_sys(&None, true).unwrap(),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
run: Permissions::new_run(&None, true).unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(&None, true).unwrap(),
|
2022-02-16 18:17:24 -05:00
|
|
|
|
hrtime: Permissions::new_hrtime(false),
|
2021-04-11 22:15:43 -04:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
let prompt_value = PERMISSION_PROMPT_STUB_VALUE_SETTER.lock();
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/foo"), None).is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/foo"), None).is_err());
|
|
|
|
|
assert!(perms.read.check(Path::new("/bar"), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.read.check(Path::new("/bar"), None).is_ok());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.write.check(Path::new("/foo"), None).is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.write.check(Path::new("/foo"), None).is_err());
|
|
|
|
|
assert!(perms.write.check(Path::new("/bar"), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.write.check(Path::new("/bar"), None).is_ok());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(false);
|
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/foo"))).is_err());
|
|
|
|
|
prompt_value.set(true);
|
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/foo"))).is_err());
|
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/bar"))).is_ok());
|
|
|
|
|
prompt_value.set(false);
|
|
|
|
|
assert!(perms.ffi.check(Some(Path::new("/bar"))).is_ok());
|
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.net.check(&("127.0.0.1", Some(8000)), None).is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.net.check(&("127.0.0.1", Some(8000)), None).is_err());
|
|
|
|
|
assert!(perms.net.check(&("127.0.0.1", Some(8001)), None).is_ok());
|
|
|
|
|
assert!(perms.net.check(&("deno.land", Some(8000)), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.net.check(&("127.0.0.1", Some(8001)), None).is_ok());
|
|
|
|
|
assert!(perms.net.check(&("deno.land", Some(8000)), None).is_ok());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.run.check("cat", None).is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.run.check("cat", None).is_err());
|
|
|
|
|
assert!(perms.run.check("ls", None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(perms.run.check("ls", None).is_ok());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert!(perms.env.check("HOME").is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert!(perms.env.check("HOME").is_err());
|
|
|
|
|
assert!(perms.env.check("PATH").is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert!(perms.env.check("PATH").is_ok());
|
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt_value.set(false);
|
|
|
|
|
assert!(perms.sys.check("hostname", None).is_err());
|
|
|
|
|
prompt_value.set(true);
|
|
|
|
|
assert!(perms.sys.check("hostname", None).is_err());
|
|
|
|
|
assert!(perms.sys.check("osRelease", None).is_ok());
|
|
|
|
|
prompt_value.set(false);
|
|
|
|
|
assert!(perms.sys.check("osRelease", None).is_ok());
|
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2021-04-11 22:15:43 -04:00
|
|
|
|
assert!(perms.hrtime.check().is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2021-04-11 22:15:43 -04:00
|
|
|
|
assert!(perms.hrtime.check().is_err());
|
|
|
|
|
}
|
2021-04-13 07:25:21 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
fn test_env_windows() {
|
2021-10-26 11:49:07 -04:00
|
|
|
|
let prompt_value = PERMISSION_PROMPT_STUB_VALUE_SETTER.lock();
|
2021-04-13 07:25:21 -04:00
|
|
|
|
let mut perms = Permissions::allow_all();
|
|
|
|
|
perms.env = UnaryPermission {
|
|
|
|
|
global_state: PermissionState::Prompt,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
..Permissions::new_env(&Some(svec!["HOME"]), false).unwrap()
|
2021-04-13 07:25:21 -04:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert!(perms.env.check("HOME").is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
assert!(perms.env.check("HOME").is_ok());
|
|
|
|
|
assert!(perms.env.check("hOmE").is_ok());
|
|
|
|
|
|
2022-02-24 20:03:12 -05:00
|
|
|
|
assert_eq!(perms.env.revoke(Some("HomE")), PermissionState::Prompt);
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_deserialize_child_permissions_arg() {
|
|
|
|
|
assert_eq!(
|
2022-03-20 17:46:39 -04:00
|
|
|
|
ChildPermissionsArg::inherit(),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
hrtime: ChildUnitPermissionArg::Inherit,
|
|
|
|
|
net: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
read: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
run: ChildUnaryPermissionArg::Inherit,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: ChildUnaryPermissionArg::Inherit,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
write: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
2022-03-20 17:46:39 -04:00
|
|
|
|
ChildPermissionsArg::none(),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
hrtime: ChildUnitPermissionArg::NotGranted,
|
|
|
|
|
net: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
read: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
run: ChildUnaryPermissionArg::NotGranted,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: ChildUnaryPermissionArg::NotGranted,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
write: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-03-20 17:46:39 -04:00
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!("inherit")).unwrap(),
|
|
|
|
|
ChildPermissionsArg::inherit()
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!("none")).unwrap(),
|
|
|
|
|
ChildPermissionsArg::none()
|
|
|
|
|
);
|
2021-10-13 13:04:44 -04:00
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!({})).unwrap(),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!({
|
|
|
|
|
"env": ["foo", "bar"],
|
|
|
|
|
}))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg::GrantedList(svec!["foo", "bar"]),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!({
|
|
|
|
|
"hrtime": true,
|
|
|
|
|
}))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
hrtime: ChildUnitPermissionArg::Granted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!({
|
|
|
|
|
"hrtime": false,
|
|
|
|
|
}))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
hrtime: ChildUnitPermissionArg::NotGranted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!({
|
|
|
|
|
"env": true,
|
|
|
|
|
"net": true,
|
|
|
|
|
"ffi": true,
|
|
|
|
|
"read": true,
|
|
|
|
|
"run": true,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
"sys": true,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
"write": true,
|
|
|
|
|
}))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg::Granted,
|
|
|
|
|
net: ChildUnaryPermissionArg::Granted,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::Granted,
|
|
|
|
|
read: ChildUnaryPermissionArg::Granted,
|
|
|
|
|
run: ChildUnaryPermissionArg::Granted,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: ChildUnaryPermissionArg::Granted,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
write: ChildUnaryPermissionArg::Granted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!({
|
|
|
|
|
"env": false,
|
|
|
|
|
"net": false,
|
|
|
|
|
"ffi": false,
|
|
|
|
|
"read": false,
|
|
|
|
|
"run": false,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
"sys": false,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
"write": false,
|
|
|
|
|
}))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
net: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
read: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
run: ChildUnaryPermissionArg::NotGranted,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: ChildUnaryPermissionArg::NotGranted,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
write: ChildUnaryPermissionArg::NotGranted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!({
|
|
|
|
|
"env": ["foo", "bar"],
|
|
|
|
|
"net": ["foo", "bar:8000"],
|
|
|
|
|
"ffi": ["foo", "file:///bar/baz"],
|
|
|
|
|
"read": ["foo", "file:///bar/baz"],
|
|
|
|
|
"run": ["foo", "file:///bar/baz", "./qux"],
|
2022-09-28 08:46:50 -04:00
|
|
|
|
"sys": ["hostname", "osRelease"],
|
2021-10-13 13:04:44 -04:00
|
|
|
|
"write": ["foo", "file:///bar/baz"],
|
|
|
|
|
}))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg::GrantedList(svec!["foo", "bar"]),
|
|
|
|
|
net: ChildUnaryPermissionArg::GrantedList(svec!["foo", "bar:8000"]),
|
|
|
|
|
ffi: ChildUnaryPermissionArg::GrantedList(svec![
|
|
|
|
|
"foo",
|
|
|
|
|
"file:///bar/baz"
|
|
|
|
|
]),
|
|
|
|
|
read: ChildUnaryPermissionArg::GrantedList(svec![
|
|
|
|
|
"foo",
|
|
|
|
|
"file:///bar/baz"
|
|
|
|
|
]),
|
|
|
|
|
run: ChildUnaryPermissionArg::GrantedList(svec![
|
|
|
|
|
"foo",
|
|
|
|
|
"file:///bar/baz",
|
|
|
|
|
"./qux"
|
|
|
|
|
]),
|
2022-09-28 08:46:50 -04:00
|
|
|
|
sys: ChildUnaryPermissionArg::GrantedList(svec![
|
|
|
|
|
"hostname",
|
|
|
|
|
"osRelease"
|
|
|
|
|
]),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
write: ChildUnaryPermissionArg::GrantedList(svec![
|
|
|
|
|
"foo",
|
|
|
|
|
"file:///bar/baz"
|
|
|
|
|
]),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_create_child_permissions() {
|
|
|
|
|
let mut main_perms = Permissions {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
env: Permissions::new_env(&Some(vec![]), false).unwrap(),
|
2022-02-16 18:17:24 -05:00
|
|
|
|
hrtime: Permissions::new_hrtime(true),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
net: Permissions::new_net(&Some(svec!["foo", "bar"]), false).unwrap(),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(
|
|
|
|
|
create_child_permissions(
|
|
|
|
|
&mut main_perms.clone(),
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
env: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
hrtime: ChildUnitPermissionArg::NotGranted,
|
|
|
|
|
net: ChildUnaryPermissionArg::GrantedList(svec!["foo"]),
|
|
|
|
|
ffi: ChildUnaryPermissionArg::NotGranted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
Permissions {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
env: Permissions::new_env(&Some(vec![]), false).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&Some(svec!["foo"]), false).unwrap(),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert!(create_child_permissions(
|
|
|
|
|
&mut main_perms.clone(),
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
net: ChildUnaryPermissionArg::Granted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
|
|
|
|
assert!(create_child_permissions(
|
|
|
|
|
&mut main_perms.clone(),
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
net: ChildUnaryPermissionArg::GrantedList(svec!["foo", "bar", "baz"]),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
|
|
|
|
assert!(create_child_permissions(
|
|
|
|
|
&mut main_perms,
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
ffi: ChildUnaryPermissionArg::GrantedList(svec!["foo"]),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_create_child_permissions_with_prompt() {
|
2021-10-26 11:49:07 -04:00
|
|
|
|
let prompt_value = PERMISSION_PROMPT_STUB_VALUE_SETTER.lock();
|
2021-10-13 13:04:44 -04:00
|
|
|
|
let mut main_perms = Permissions::from_options(&PermissionsOptions {
|
|
|
|
|
prompt: true,
|
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2021-10-13 13:04:44 -04:00
|
|
|
|
let worker_perms = create_child_permissions(
|
|
|
|
|
&mut main_perms,
|
|
|
|
|
ChildPermissionsArg {
|
|
|
|
|
read: ChildUnaryPermissionArg::Granted,
|
|
|
|
|
run: ChildUnaryPermissionArg::GrantedList(svec!["foo", "bar"]),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(main_perms, worker_perms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_create_child_permissions_with_inherited_denied_list() {
|
2021-10-26 11:49:07 -04:00
|
|
|
|
let prompt_value = PERMISSION_PROMPT_STUB_VALUE_SETTER.lock();
|
2021-10-13 13:04:44 -04:00
|
|
|
|
let mut main_perms = Permissions::from_options(&PermissionsOptions {
|
|
|
|
|
prompt: true,
|
|
|
|
|
..Default::default()
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2022-09-27 16:36:33 -04:00
|
|
|
|
assert!(main_perms.write.check(&PathBuf::from("foo"), None).is_err());
|
2021-10-13 13:04:44 -04:00
|
|
|
|
let worker_perms = create_child_permissions(
|
|
|
|
|
&mut main_perms.clone(),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
ChildPermissionsArg::none(),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert_eq!(worker_perms.write.denied_list, main_perms.write.denied_list);
|
|
|
|
|
}
|
2022-08-10 15:13:53 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_handle_empty_value() {
|
|
|
|
|
assert!(Permissions::new_read(&Some(vec![PathBuf::new()]), false).is_err());
|
|
|
|
|
assert!(Permissions::new_env(&Some(vec![String::new()]), false).is_err());
|
2022-09-28 08:46:50 -04:00
|
|
|
|
assert!(Permissions::new_sys(&Some(vec![String::new()]), false).is_err());
|
2022-08-10 15:13:53 -04:00
|
|
|
|
assert!(Permissions::new_run(&Some(vec![String::new()]), false).is_err());
|
|
|
|
|
assert!(Permissions::new_ffi(&Some(vec![PathBuf::new()]), false).is_err());
|
|
|
|
|
assert!(Permissions::new_net(&Some(svec![String::new()]), false).is_err());
|
|
|
|
|
assert!(Permissions::new_write(&Some(vec![PathBuf::new()]), false).is_err());
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|