2023-01-02 16:00:42 -05:00
|
|
|
|
// Copyright 2018-2023 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;
|
2023-01-07 11:25:34 -05:00
|
|
|
|
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;
|
2023-05-27 09:42:20 -04:00
|
|
|
|
use deno_core::url::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;
|
2023-06-05 20:35:39 -04:00
|
|
|
|
use std::borrow::Cow;
|
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;
|
2023-01-14 23:18:58 -05:00
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::path::PathBuf;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
use std::string::ToString;
|
2023-01-07 11:25:34 -05:00
|
|
|
|
use std::sync::Arc;
|
2019-03-18 16:46:23 -04:00
|
|
|
|
|
2022-12-17 19:12:28 -05:00
|
|
|
|
mod prompter;
|
|
|
|
|
use prompter::permission_prompt;
|
|
|
|
|
use prompter::PromptResponse;
|
|
|
|
|
use prompter::PERMISSION_EMOJI;
|
|
|
|
|
|
|
|
|
|
pub use prompter::set_prompt_callbacks;
|
|
|
|
|
pub use prompter::PromptCallback;
|
2019-06-22 12:02:51 -04:00
|
|
|
|
|
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
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
/// Quadri-state value for storing permission state
|
2023-03-09 14:18:00 -05:00
|
|
|
|
#[derive(
|
|
|
|
|
Eq, PartialEq, Default, 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,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
GrantedPartial = 1,
|
2023-03-09 14:18:00 -05:00
|
|
|
|
#[default]
|
2023-08-03 07:19:19 -04:00
|
|
|
|
Prompt = 2,
|
|
|
|
|
Denied = 3,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// `AllowPartial` prescribes how to treat a permission which is partially
|
|
|
|
|
/// denied due to a `--deny-*` flag affecting a subscope of the queried
|
|
|
|
|
/// permission.
|
|
|
|
|
///
|
|
|
|
|
/// `TreatAsGranted` is used in place of `TreatAsPartialGranted` when we don't
|
|
|
|
|
/// want to wastefully check for partial denials when, say, checking read
|
|
|
|
|
/// access for a file.
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
|
#[allow(clippy::enum_variant_names)]
|
|
|
|
|
enum AllowPartial {
|
|
|
|
|
TreatAsGranted,
|
|
|
|
|
TreatAsDenied,
|
|
|
|
|
TreatAsPartialGranted,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<bool> for AllowPartial {
|
|
|
|
|
fn from(value: bool) -> Self {
|
|
|
|
|
if value {
|
|
|
|
|
Self::TreatAsGranted
|
|
|
|
|
} else {
|
|
|
|
|
Self::TreatAsDenied
|
|
|
|
|
}
|
|
|
|
|
}
|
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,
|
2023-03-15 17:46:36 -04:00
|
|
|
|
info()
|
|
|
|
|
.map(|info| { format!(" to {info}") })
|
|
|
|
|
.unwrap_or_default(),
|
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,
|
2023-02-22 17:02:10 -05:00
|
|
|
|
) -> (Result<(), AnyError>, bool, 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,
|
2023-02-22 17:02:10 -05:00
|
|
|
|
) -> (Result<(), AnyError>, bool, bool) {
|
2021-04-12 07:24:41 -04:00
|
|
|
|
match self {
|
|
|
|
|
PermissionState::Granted => {
|
|
|
|
|
Self::log_perm_access(name, info);
|
2023-02-22 17:02:10 -05:00
|
|
|
|
(Ok(()), false, false)
|
2021-04-12 07:24:41 -04:00
|
|
|
|
}
|
|
|
|
|
PermissionState::Prompt if prompt => {
|
2022-08-27 11:20:05 -04:00
|
|
|
|
let msg = format!(
|
|
|
|
|
"{} access{}",
|
|
|
|
|
name,
|
2023-03-15 17:46:36 -04:00
|
|
|
|
info()
|
|
|
|
|
.map(|info| { format!(" to {info}") })
|
|
|
|
|
.unwrap_or_default(),
|
2022-08-27 11:20:05 -04:00
|
|
|
|
);
|
2023-02-22 17:02:10 -05:00
|
|
|
|
match permission_prompt(&msg, name, api_name, true) {
|
|
|
|
|
PromptResponse::Allow => {
|
|
|
|
|
Self::log_perm_access(name, info);
|
|
|
|
|
(Ok(()), true, false)
|
|
|
|
|
}
|
|
|
|
|
PromptResponse::AllowAll => {
|
|
|
|
|
Self::log_perm_access(name, info);
|
|
|
|
|
(Ok(()), true, true)
|
|
|
|
|
}
|
|
|
|
|
PromptResponse::Deny => (Err(Self::error(name, info)), true, false),
|
2021-04-12 07:24:41 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-22 17:02:10 -05:00
|
|
|
|
_ => (Err(Self::error(name, info)), false, false),
|
2021-04-12 07:24:41 -04:00
|
|
|
|
}
|
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"),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
PermissionState::GrantedPartial => f.pad("granted-partial"),
|
2020-08-18 16:29:32 -04:00
|
|
|
|
PermissionState::Prompt => f.pad("prompt"),
|
|
|
|
|
PermissionState::Denied => f.pad("denied"),
|
2019-03-18 16:46:23 -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-12-17 19:12:28 -05:00
|
|
|
|
if PromptResponse::Allow
|
|
|
|
|
== permission_prompt(
|
|
|
|
|
&format!("access to {}", self.description),
|
|
|
|
|
self.name,
|
|
|
|
|
Some("Deno.permissions.query()"),
|
2023-02-22 17:02:10 -05:00
|
|
|
|
false,
|
2022-12-17 19:12:28 -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> {
|
2023-02-22 17:02:10 -05:00
|
|
|
|
let (result, prompted, _is_allow_all) =
|
2022-09-27 16:36:33 -04:00
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub trait Descriptor: Eq + Clone {
|
|
|
|
|
fn flag_name() -> &'static str;
|
|
|
|
|
fn name(&self) -> Cow<str>;
|
|
|
|
|
// By default, specifies no-stronger-than relationship.
|
|
|
|
|
// As this is not strict, it's only true when descriptors are the same.
|
|
|
|
|
fn stronger_than(&self, other: &Self) -> bool {
|
|
|
|
|
self == other
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub struct UnaryPermission<T: Descriptor + Hash> {
|
|
|
|
|
pub granted_global: bool,
|
2020-08-18 16:29:32 -04:00
|
|
|
|
pub granted_list: HashSet<T>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub flag_denied_global: bool,
|
|
|
|
|
pub flag_denied_list: HashSet<T>,
|
|
|
|
|
pub prompt_denied_global: bool,
|
|
|
|
|
pub prompt_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
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
impl<T: Descriptor + Hash> Default for UnaryPermission<T> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
UnaryPermission {
|
|
|
|
|
granted_global: Default::default(),
|
|
|
|
|
granted_list: Default::default(),
|
|
|
|
|
flag_denied_global: Default::default(),
|
|
|
|
|
flag_denied_list: Default::default(),
|
|
|
|
|
prompt_denied_global: Default::default(),
|
|
|
|
|
prompt_denied_list: Default::default(),
|
|
|
|
|
prompt: Default::default(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Descriptor + Hash> UnaryPermission<T> {
|
|
|
|
|
fn check_desc(
|
|
|
|
|
&mut self,
|
|
|
|
|
desc: &Option<T>,
|
|
|
|
|
assert_non_partial: bool,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
get_display_name: impl Fn() -> Option<String>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
let (result, prompted, is_allow_all) = self
|
|
|
|
|
.query_desc(desc, AllowPartial::from(assert_non_partial))
|
|
|
|
|
.check2(
|
|
|
|
|
T::flag_name(),
|
|
|
|
|
api_name,
|
|
|
|
|
|| match get_display_name() {
|
|
|
|
|
Some(display_name) => Some(display_name),
|
|
|
|
|
None => desc.as_ref().map(|d| format!("\"{}\"", d.name())),
|
|
|
|
|
},
|
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
if is_allow_all {
|
|
|
|
|
self.insert_granted(None);
|
|
|
|
|
} else {
|
|
|
|
|
self.insert_granted(desc.clone());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
self.insert_prompt_denied(desc.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn query_desc(
|
|
|
|
|
&self,
|
|
|
|
|
desc: &Option<T>,
|
|
|
|
|
allow_partial: AllowPartial,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
if self.is_flag_denied(desc) || self.is_prompt_denied(desc) {
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else if self.is_granted(desc) {
|
|
|
|
|
match allow_partial {
|
|
|
|
|
AllowPartial::TreatAsGranted => PermissionState::Granted,
|
|
|
|
|
AllowPartial::TreatAsDenied => {
|
|
|
|
|
if self.is_partial_flag_denied(desc) {
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AllowPartial::TreatAsPartialGranted => {
|
|
|
|
|
if self.is_partial_flag_denied(desc) {
|
|
|
|
|
PermissionState::GrantedPartial
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if matches!(allow_partial, AllowPartial::TreatAsDenied)
|
|
|
|
|
&& self.is_partial_flag_denied(desc)
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn request_desc(
|
|
|
|
|
&mut self,
|
|
|
|
|
desc: &Option<T>,
|
|
|
|
|
get_display_name: impl Fn() -> Option<String>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
let state = self.query_desc(desc, AllowPartial::TreatAsPartialGranted);
|
|
|
|
|
if state == PermissionState::Granted {
|
|
|
|
|
self.insert_granted(desc.clone());
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
if state != PermissionState::Prompt {
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
let mut message = String::with_capacity(40);
|
|
|
|
|
message.push_str(&format!("{} access", T::flag_name()));
|
|
|
|
|
match get_display_name() {
|
|
|
|
|
Some(display_name) => {
|
|
|
|
|
message.push_str(&format!(" to \"{}\"", display_name))
|
|
|
|
|
}
|
|
|
|
|
None => match desc {
|
|
|
|
|
Some(desc) => message.push_str(&format!(" to \"{}\"", desc.name())),
|
|
|
|
|
None => {}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
match permission_prompt(
|
|
|
|
|
&message,
|
|
|
|
|
T::flag_name(),
|
|
|
|
|
Some("Deno.permissions.request()"),
|
|
|
|
|
true,
|
|
|
|
|
) {
|
|
|
|
|
PromptResponse::Allow => {
|
|
|
|
|
self.insert_granted(desc.clone());
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
}
|
|
|
|
|
PromptResponse::Deny => {
|
|
|
|
|
self.insert_prompt_denied(desc.clone());
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
|
|
|
|
PromptResponse::AllowAll => {
|
|
|
|
|
self.insert_granted(None);
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn revoke_desc(&mut self, desc: &Option<T>) -> PermissionState {
|
|
|
|
|
match desc.as_ref() {
|
|
|
|
|
Some(desc) => self.granted_list.retain(|v| !v.stronger_than(desc)),
|
|
|
|
|
None => {
|
|
|
|
|
self.granted_global = false;
|
|
|
|
|
// Revoke global is a special case where the entire granted list is
|
|
|
|
|
// cleared. It's inconsistent with the granular case where only
|
|
|
|
|
// descriptors stronger than the revoked one are purged.
|
|
|
|
|
self.granted_list.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.query_desc(desc, AllowPartial::TreatAsPartialGranted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_granted(&self, desc: &Option<T>) -> bool {
|
|
|
|
|
Self::list_contains(desc, self.granted_global, &self.granted_list)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_flag_denied(&self, desc: &Option<T>) -> bool {
|
|
|
|
|
Self::list_contains(desc, self.flag_denied_global, &self.flag_denied_list)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_prompt_denied(&self, desc: &Option<T>) -> bool {
|
|
|
|
|
match desc.as_ref() {
|
|
|
|
|
Some(desc) => self
|
|
|
|
|
.prompt_denied_list
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|v| desc.stronger_than(v)),
|
|
|
|
|
None => self.prompt_denied_global || !self.prompt_denied_list.is_empty(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_partial_flag_denied(&self, desc: &Option<T>) -> bool {
|
|
|
|
|
match desc {
|
|
|
|
|
None => !self.flag_denied_list.is_empty(),
|
|
|
|
|
Some(desc) => self.flag_denied_list.iter().any(|v| desc.stronger_than(v)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn list_contains(
|
|
|
|
|
desc: &Option<T>,
|
|
|
|
|
list_global: bool,
|
|
|
|
|
list: &HashSet<T>,
|
|
|
|
|
) -> bool {
|
|
|
|
|
match desc.as_ref() {
|
|
|
|
|
Some(desc) => list_global || list.iter().any(|v| v.stronger_than(desc)),
|
|
|
|
|
None => list_global,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn insert_granted(&mut self, desc: Option<T>) {
|
|
|
|
|
Self::list_insert(desc, &mut self.granted_global, &mut self.granted_list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn insert_prompt_denied(&mut self, desc: Option<T>) {
|
|
|
|
|
Self::list_insert(
|
|
|
|
|
desc,
|
|
|
|
|
&mut self.prompt_denied_global,
|
|
|
|
|
&mut self.prompt_denied_list,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn list_insert(
|
|
|
|
|
desc: Option<T>,
|
|
|
|
|
list_global: &mut bool,
|
|
|
|
|
list: &mut HashSet<T>,
|
|
|
|
|
) {
|
|
|
|
|
match desc {
|
|
|
|
|
Some(desc) => {
|
|
|
|
|
list.insert(desc);
|
|
|
|
|
}
|
|
|
|
|
None => *list_global = true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
impl Descriptor for ReadDescriptor {
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"read"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(self.0.display().to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stronger_than(&self, other: &Self) -> bool {
|
|
|
|
|
other.0.starts_with(&self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
impl Descriptor for WriteDescriptor {
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"write"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(self.0.display().to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stronger_than(&self, other: &Self) -> bool {
|
|
|
|
|
other.0.starts_with(&self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
impl Descriptor for NetDescriptor {
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"net"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(format!("{}", self))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stronger_than(&self, other: &Self) -> bool {
|
|
|
|
|
self.0 == other.0 && (self.1.is_none() || self.1 == other.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))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
impl Descriptor for EnvDescriptor {
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"env"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(self.0.as_ref())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 15:50:59 -04:00
|
|
|
|
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),
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
impl Descriptor for RunDescriptor {
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"run"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(self.to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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);
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
impl Descriptor for SysDescriptor {
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"sys"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(self.0.to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 06:34:16 -04:00
|
|
|
|
pub fn parse_sys_kind(kind: &str) -> Result<&str, AnyError> {
|
|
|
|
|
match kind {
|
2022-12-26 18:16:12 -05:00
|
|
|
|
"hostname" | "osRelease" | "osUptime" | "loadavg" | "networkInterfaces"
|
2022-10-26 16:37:48 -04:00
|
|
|
|
| "systemMemoryInfo" | "uid" | "gid" => Ok(kind),
|
2023-01-27 10:43:16 -05:00
|
|
|
|
_ => Err(type_error(format!("unknown system info kind \"{kind}\""))),
|
2022-09-29 06:34:16 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
impl Descriptor for FfiDescriptor {
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"ffi"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(self.0.display().to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stronger_than(&self, other: &Self) -> bool {
|
|
|
|
|
other.0.starts_with(&self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.query_desc(
|
|
|
|
|
&path.map(|p| ReadDescriptor(resolve_from_cwd(p).unwrap())),
|
|
|
|
|
AllowPartial::TreatAsPartialGranted,
|
|
|
|
|
)
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.request_desc(
|
|
|
|
|
&path.map(|p| ReadDescriptor(resolve_from_cwd(p).unwrap())),
|
|
|
|
|
|| Some(path?.display().to_string()),
|
|
|
|
|
)
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self
|
|
|
|
|
.revoke_desc(&path.map(|p| ReadDescriptor(resolve_from_cwd(p).unwrap())))
|
2020-05-29 11:27:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(
|
|
|
|
|
&Some(ReadDescriptor(resolve_from_cwd(path)?)),
|
|
|
|
|
true,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
|| Some(format!("\"{}\"", path.display())),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn check_partial(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
let desc = ReadDescriptor(resolve_from_cwd(path)?);
|
|
|
|
|
self.check_desc(&Some(desc), false, api_name, || {
|
|
|
|
|
Some(format!("\"{}\"", path.display()))
|
|
|
|
|
})
|
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> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
let desc = ReadDescriptor(resolve_from_cwd(path)?);
|
|
|
|
|
self.check_desc(&Some(desc), false, Some(api_name), || {
|
|
|
|
|
Some(format!("<{display}>"))
|
|
|
|
|
})
|
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> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(&None, false, api_name, || None)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.query_desc(
|
|
|
|
|
&path.map(|p| WriteDescriptor(resolve_from_cwd(p).unwrap())),
|
|
|
|
|
AllowPartial::TreatAsPartialGranted,
|
|
|
|
|
)
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.request_desc(
|
|
|
|
|
&path.map(|p| WriteDescriptor(resolve_from_cwd(p).unwrap())),
|
|
|
|
|
|| Some(path?.display().to_string()),
|
|
|
|
|
)
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self
|
|
|
|
|
.revoke_desc(&path.map(|p| WriteDescriptor(resolve_from_cwd(p).unwrap())))
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(
|
|
|
|
|
&Some(WriteDescriptor(resolve_from_cwd(path)?)),
|
|
|
|
|
true,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
|| Some(format!("\"{}\"", path.display())),
|
|
|
|
|
)
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn check_partial(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.check_desc(
|
|
|
|
|
&Some(WriteDescriptor(resolve_from_cwd(path)?)),
|
|
|
|
|
false,
|
|
|
|
|
api_name,
|
|
|
|
|
|| Some(format!("\"{}\"", path.display())),
|
|
|
|
|
)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
2023-04-12 09:13:32 -04:00
|
|
|
|
|
|
|
|
|
/// As `check()`, but permission error messages will anonymize the path
|
|
|
|
|
/// by replacing it with the given `display`.
|
|
|
|
|
pub fn check_blind(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
display: &str,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
let desc = WriteDescriptor(resolve_from_cwd(path)?);
|
|
|
|
|
self.check_desc(&Some(desc), false, Some(api_name), || {
|
|
|
|
|
Some(format!("<{display}>"))
|
|
|
|
|
})
|
2023-04-12 09:13:32 -04:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub fn check_all(&mut self, api_name: Option<&str>) -> Result<(), AnyError> {
|
|
|
|
|
self.check_desc(&None, false, api_name, || None)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.query_desc(
|
|
|
|
|
&host.map(|h| NetDescriptor::new(&h)),
|
|
|
|
|
AllowPartial::TreatAsPartialGranted,
|
|
|
|
|
)
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.request_desc(&host.map(|h| NetDescriptor::new(&h)), || None)
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.revoke_desc(&Some(NetDescriptor::new(&host.unwrap())))
|
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> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(&Some(NetDescriptor::new(&host)), false, api_name, || None)
|
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();
|
2023-08-03 07:19:19 -04:00
|
|
|
|
let host = &(&hostname, url.port_or_known_default());
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let display_host = match url.port() {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
None => hostname.clone(),
|
|
|
|
|
Some(port) => format!("{hostname}:{port}"),
|
2021-03-17 17:45:12 -04:00
|
|
|
|
};
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(&Some(NetDescriptor::new(&host)), false, api_name, || {
|
|
|
|
|
Some(format!("\"{}\"", display_host))
|
|
|
|
|
})
|
2019-12-05 15:30:20 -05:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
|
|
|
|
pub fn check_all(&mut self) -> Result<(), AnyError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(&None, false, None, || None)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.query_desc(
|
|
|
|
|
&env.map(EnvDescriptor::new),
|
|
|
|
|
AllowPartial::TreatAsPartialGranted,
|
|
|
|
|
)
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request(&mut self, env: Option<&str>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.request_desc(&env.map(EnvDescriptor::new), || None)
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self, env: Option<&str>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.revoke_desc(&env.map(EnvDescriptor::new))
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check(&mut self, env: &str) -> Result<(), AnyError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(&Some(EnvDescriptor::new(env)), false, None, || None)
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check_all(&mut self) -> Result<(), AnyError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(&None, false, None, || None)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
impl UnaryPermission<SysDescriptor> {
|
|
|
|
|
pub fn query(&self, kind: Option<&str>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.query_desc(
|
|
|
|
|
&kind.map(|k| SysDescriptor(k.to_string())),
|
|
|
|
|
AllowPartial::TreatAsPartialGranted,
|
|
|
|
|
)
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request(&mut self, kind: Option<&str>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.request_desc(&kind.map(|k| SysDescriptor(k.to_string())), || None)
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self, kind: Option<&str>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.revoke_desc(&kind.map(|k| SysDescriptor(k.to_string())))
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
kind: &str,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(
|
|
|
|
|
&Some(SysDescriptor(kind.to_string())),
|
|
|
|
|
false,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
api_name,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
|| None,
|
|
|
|
|
)
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
|
|
|
|
|
pub fn check_all(&mut self) -> Result<(), AnyError> {
|
|
|
|
|
self.check_desc(&None, false, None, || None)
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 18:12:00 -04:00
|
|
|
|
impl UnaryPermission<RunDescriptor> {
|
|
|
|
|
pub fn query(&self, cmd: Option<&str>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.query_desc(
|
|
|
|
|
&cmd.map(|c| RunDescriptor::from_str(c).unwrap()),
|
|
|
|
|
AllowPartial::TreatAsPartialGranted,
|
|
|
|
|
)
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request(&mut self, cmd: Option<&str>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.request_desc(&cmd.map(|c| RunDescriptor::from_str(c).unwrap()), || {
|
|
|
|
|
Some(cmd?.to_string())
|
|
|
|
|
})
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self, cmd: Option<&str>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.revoke_desc(&cmd.map(|c| RunDescriptor::from_str(c).unwrap()))
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
cmd: &str,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(
|
|
|
|
|
&Some(RunDescriptor::from_str(cmd).unwrap()),
|
|
|
|
|
false,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
|| Some(format!("\"{}\"", cmd)),
|
|
|
|
|
)
|
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> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.check_desc(&None, false, api_name, || None)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.query_desc(
|
|
|
|
|
&path.map(|p| FfiDescriptor(resolve_from_cwd(p).unwrap())),
|
|
|
|
|
AllowPartial::TreatAsPartialGranted,
|
|
|
|
|
)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub fn request(&mut self, path: Option<&Path>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.request_desc(
|
|
|
|
|
&path.map(|p| FfiDescriptor(resolve_from_cwd(p).unwrap())),
|
|
|
|
|
|| Some(path?.display().to_string()),
|
|
|
|
|
)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub fn revoke(&mut self, path: Option<&Path>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.revoke_desc(&path.map(|p| FfiDescriptor(resolve_from_cwd(p).unwrap())))
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.check_desc(
|
|
|
|
|
&Some(FfiDescriptor(resolve_from_cwd(path)?)),
|
|
|
|
|
true,
|
|
|
|
|
api_name,
|
|
|
|
|
|| Some(format!("\"{}\"", path.display())),
|
|
|
|
|
)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub fn check_partial(&mut self, path: Option<&Path>) -> Result<(), AnyError> {
|
|
|
|
|
let desc = match path {
|
|
|
|
|
Some(path) => Some(FfiDescriptor(resolve_from_cwd(path)?)),
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
|
|
|
|
self.check_desc(&desc, false, None, || {
|
|
|
|
|
Some(format!("\"{}\"", path?.display()))
|
|
|
|
|
})
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub fn check_all(&mut self) -> Result<(), AnyError> {
|
|
|
|
|
self.check_desc(&None, false, Some("all"), || None)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
read: Permissions::new_read(&None, &None, false).unwrap(),
|
|
|
|
|
write: Permissions::new_write(&None, &None, false).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&None, &None, false).unwrap(),
|
|
|
|
|
env: Permissions::new_env(&None, &None, false).unwrap(),
|
|
|
|
|
sys: Permissions::new_sys(&None, &None, false).unwrap(),
|
|
|
|
|
run: Permissions::new_run(&None, &None, false).unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(&None, &None, false).unwrap(),
|
|
|
|
|
hrtime: Permissions::new_hrtime(false, 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>>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub deny_env: Option<Vec<String>>,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub allow_hrtime: bool,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub deny_hrtime: bool,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub allow_net: Option<Vec<String>>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub deny_net: Option<Vec<String>>,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
pub allow_ffi: Option<Vec<PathBuf>>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub deny_ffi: Option<Vec<PathBuf>>,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub allow_read: Option<Vec<PathBuf>>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub deny_read: Option<Vec<PathBuf>>,
|
2021-04-09 18:12:00 -04:00
|
|
|
|
pub allow_run: Option<Vec<String>>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub deny_run: Option<Vec<String>>,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
pub allow_sys: Option<Vec<String>>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub deny_sys: Option<Vec<String>>,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub allow_write: Option<Vec<PathBuf>>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub deny_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(
|
2023-08-03 07:19:19 -04:00
|
|
|
|
allow_list: &Option<Vec<PathBuf>>,
|
|
|
|
|
deny_list: &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> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
granted_global: global_from_option(allow_list),
|
|
|
|
|
granted_list: parse_path_list(allow_list, ReadDescriptor)?,
|
|
|
|
|
flag_denied_global: global_from_option(deny_list),
|
|
|
|
|
flag_denied_list: parse_path_list(deny_list, ReadDescriptor)?,
|
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(
|
2023-08-03 07:19:19 -04:00
|
|
|
|
allow_list: &Option<Vec<PathBuf>>,
|
|
|
|
|
deny_list: &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> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
Ok(UnaryPermission {
|
|
|
|
|
granted_global: global_from_option(allow_list),
|
|
|
|
|
granted_list: parse_path_list(allow_list, WriteDescriptor)?,
|
|
|
|
|
flag_denied_global: global_from_option(deny_list),
|
|
|
|
|
flag_denied_list: parse_path_list(deny_list, WriteDescriptor)?,
|
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(
|
2023-08-03 07:19:19 -04:00
|
|
|
|
allow_list: &Option<Vec<String>>,
|
|
|
|
|
deny_list: &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> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
granted_global: global_from_option(allow_list),
|
|
|
|
|
granted_list: parse_net_list(allow_list)?,
|
|
|
|
|
flag_denied_global: global_from_option(deny_list),
|
|
|
|
|
flag_denied_list: parse_net_list(deny_list)?,
|
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(
|
2023-08-03 07:19:19 -04:00
|
|
|
|
allow_list: &Option<Vec<String>>,
|
|
|
|
|
deny_list: &Option<Vec<String>>,
|
2021-04-13 07:25:21 -04:00
|
|
|
|
prompt: bool,
|
2022-08-10 15:13:53 -04:00
|
|
|
|
) -> Result<UnaryPermission<EnvDescriptor>, AnyError> {
|
|
|
|
|
Ok(UnaryPermission::<EnvDescriptor> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
granted_global: global_from_option(allow_list),
|
|
|
|
|
granted_list: parse_env_list(allow_list)?,
|
|
|
|
|
flag_denied_global: global_from_option(deny_list),
|
|
|
|
|
flag_denied_list: parse_env_list(deny_list)?,
|
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(
|
2023-08-03 07:19:19 -04:00
|
|
|
|
allow_list: &Option<Vec<String>>,
|
|
|
|
|
deny_list: &Option<Vec<String>>,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt: bool,
|
|
|
|
|
) -> Result<UnaryPermission<SysDescriptor>, AnyError> {
|
|
|
|
|
Ok(UnaryPermission::<SysDescriptor> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
granted_global: global_from_option(allow_list),
|
|
|
|
|
granted_list: parse_sys_list(allow_list)?,
|
|
|
|
|
flag_denied_global: global_from_option(deny_list),
|
|
|
|
|
flag_denied_list: parse_sys_list(deny_list)?,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt,
|
|
|
|
|
..Default::default()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 18:12:00 -04:00
|
|
|
|
pub fn new_run(
|
2023-08-03 07:19:19 -04:00
|
|
|
|
allow_list: &Option<Vec<String>>,
|
|
|
|
|
deny_list: &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> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
granted_global: global_from_option(allow_list),
|
|
|
|
|
granted_list: parse_run_list(allow_list)?,
|
|
|
|
|
flag_denied_global: global_from_option(deny_list),
|
|
|
|
|
flag_denied_list: parse_run_list(deny_list)?,
|
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(
|
2023-08-03 07:19:19 -04:00
|
|
|
|
allow_list: &Option<Vec<PathBuf>>,
|
|
|
|
|
deny_list: &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> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
granted_global: global_from_option(allow_list),
|
|
|
|
|
granted_list: parse_path_list(allow_list, FfiDescriptor)?,
|
|
|
|
|
flag_denied_global: global_from_option(deny_list),
|
|
|
|
|
flag_denied_list: parse_path_list(deny_list, FfiDescriptor)?,
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub fn new_hrtime(allow_state: bool, deny_state: bool) -> UnitPermission {
|
|
|
|
|
unit_permission_from_flag_bools(
|
|
|
|
|
allow_state,
|
|
|
|
|
deny_state,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
"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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
read: Permissions::new_read(
|
|
|
|
|
&opts.allow_read,
|
|
|
|
|
&opts.deny_read,
|
|
|
|
|
opts.prompt,
|
|
|
|
|
)?,
|
|
|
|
|
write: Permissions::new_write(
|
|
|
|
|
&opts.allow_write,
|
|
|
|
|
&opts.deny_write,
|
|
|
|
|
opts.prompt,
|
|
|
|
|
)?,
|
|
|
|
|
net: Permissions::new_net(&opts.allow_net, &opts.deny_net, opts.prompt)?,
|
|
|
|
|
env: Permissions::new_env(&opts.allow_env, &opts.deny_env, opts.prompt)?,
|
|
|
|
|
sys: Permissions::new_sys(&opts.allow_sys, &opts.deny_sys, opts.prompt)?,
|
|
|
|
|
run: Permissions::new_run(&opts.allow_run, &opts.deny_run, opts.prompt)?,
|
|
|
|
|
ffi: Permissions::new_ffi(&opts.allow_ffi, &opts.deny_ffi, opts.prompt)?,
|
|
|
|
|
hrtime: Permissions::new_hrtime(opts.allow_hrtime, opts.deny_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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
read: Permissions::new_read(&Some(vec![]), &None, false).unwrap(),
|
|
|
|
|
write: Permissions::new_write(&Some(vec![]), &None, false).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&Some(vec![]), &None, false).unwrap(),
|
|
|
|
|
env: Permissions::new_env(&Some(vec![]), &None, false).unwrap(),
|
|
|
|
|
sys: Permissions::new_sys(&Some(vec![]), &None, false).unwrap(),
|
|
|
|
|
run: Permissions::new_run(&Some(vec![]), &None, false).unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(&Some(vec![]), &None, false).unwrap(),
|
|
|
|
|
hrtime: Permissions::new_hrtime(true, false),
|
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-12-21 08:24:39 -05:00
|
|
|
|
Ok(path) => self.read.check(&path, Some("import()")),
|
2021-01-04 04:33:20 -05:00
|
|
|
|
Err(_) => Err(uri_error(format!(
|
2023-01-27 10:43:16 -05:00
|
|
|
|
"Invalid file path.\n Specifier: {specifier}"
|
2021-01-04 04:33:20 -05:00
|
|
|
|
))),
|
2021-01-05 21:22:38 -05:00
|
|
|
|
},
|
|
|
|
|
"data" => Ok(()),
|
2021-04-07 09:22:14 -04:00
|
|
|
|
"blob" => Ok(()),
|
2022-12-21 08:24:39 -05:00
|
|
|
|
_ => self.net.check_url(specifier, Some("import()")),
|
2020-10-26 15:56:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2020-10-26 15:56:00 -04:00
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
/// Wrapper struct for `Permissions` that can be shared across threads.
|
|
|
|
|
///
|
|
|
|
|
/// We need a way to have internal mutability for permissions as they might get
|
|
|
|
|
/// passed to a future that will prompt the user for permission (and in such
|
|
|
|
|
/// case might need to be mutated). Also for the Web Worker API we need a way
|
|
|
|
|
/// to send permissions to a new thread.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct PermissionsContainer(pub Arc<Mutex<Permissions>>);
|
|
|
|
|
|
|
|
|
|
impl PermissionsContainer {
|
|
|
|
|
pub fn new(perms: Permissions) -> Self {
|
|
|
|
|
Self(Arc::new(Mutex::new(perms)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn allow_all() -> Self {
|
|
|
|
|
Self::new(Permissions::allow_all())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_specifier(
|
|
|
|
|
&self,
|
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().check_specifier(specifier)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_read(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().read.check(path, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_read_blind(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
display: &str,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().read.check_blind(path, display, api_name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_read_all(&mut self, api_name: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().read.check_all(Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_write(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().write.check(path, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_write_all(&mut self, api_name: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().write.check_all(Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 09:13:32 -04:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_write_blind(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
display: &str,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().write.check_blind(path, display, api_name)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_run(
|
|
|
|
|
&mut self,
|
|
|
|
|
cmd: &str,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().run.check(cmd, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_run_all(&mut self, api_name: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().run.check_all(Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_sys(
|
|
|
|
|
&mut self,
|
|
|
|
|
kind: &str,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().sys.check(kind, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_env(&mut self, var: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().env.check(var)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_env_all(&mut self) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().env.check_all()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl deno_node::NodePermissions for PermissionsContainer {
|
2023-05-27 09:42:20 -04:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn check_net_url(
|
|
|
|
|
&mut self,
|
|
|
|
|
url: &Url,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().net.check_url(url, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
2023-04-24 21:07:48 -04:00
|
|
|
|
fn check_read(&self, path: &Path) -> Result<(), AnyError> {
|
2023-01-07 11:25:34 -05:00
|
|
|
|
self.0.lock().read.check(path, None)
|
2022-08-24 12:07:49 -04:00
|
|
|
|
}
|
2023-07-31 16:29:09 -04:00
|
|
|
|
|
|
|
|
|
fn check_sys(&self, kind: &str, api_name: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().sys.check(kind, Some(api_name))
|
|
|
|
|
}
|
2022-08-24 12:07:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
impl deno_net::NetPermissions for PermissionsContainer {
|
|
|
|
|
#[inline(always)]
|
2021-06-28 19:43:03 -04:00
|
|
|
|
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> {
|
2023-01-07 11:25:34 -05:00
|
|
|
|
self.0.lock().net.check(host, Some(api_name))
|
2021-06-28 19:43:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_read(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-01-07 11:25:34 -05:00
|
|
|
|
self.0.lock().read.check(path, Some(api_name))
|
2021-06-28 19:43:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_write(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-01-07 11:25:34 -05:00
|
|
|
|
self.0.lock().write.check(path, Some(api_name))
|
2021-06-28 19:43:03 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
impl deno_fetch::FetchPermissions for PermissionsContainer {
|
|
|
|
|
#[inline(always)]
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_net_url(
|
|
|
|
|
&mut self,
|
|
|
|
|
url: &url::Url,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-01-07 11:25:34 -05:00
|
|
|
|
self.0.lock().net.check_url(url, Some(api_name))
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_read(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-01-07 11:25:34 -05:00
|
|
|
|
self.0.lock().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
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
impl deno_web::TimersPermission for PermissionsContainer {
|
|
|
|
|
#[inline(always)]
|
2021-04-14 15:10:48 -04:00
|
|
|
|
fn allow_hrtime(&mut self) -> bool {
|
2023-01-07 11:25:34 -05:00
|
|
|
|
self.0.lock().hrtime.check().is_ok()
|
2021-04-14 15:10:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
2021-04-14 15:10:48 -04:00
|
|
|
|
fn check_unstable(&self, state: &OpState, api_name: &'static str) {
|
|
|
|
|
crate::ops::check_unstable(state, api_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
impl deno_websocket::WebSocketPermissions for PermissionsContainer {
|
|
|
|
|
#[inline(always)]
|
2022-09-27 16:36:33 -04:00
|
|
|
|
fn check_net_url(
|
|
|
|
|
&mut self,
|
|
|
|
|
url: &url::Url,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
2023-01-07 11:25:34 -05:00
|
|
|
|
self.0.lock().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
|
|
|
|
|
2023-03-07 04:13:44 -05:00
|
|
|
|
impl deno_fs::FsPermissions for PermissionsContainer {
|
2023-03-04 18:44:59 -05:00
|
|
|
|
fn check_read(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().read.check(path, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_read_blind(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
display: &str,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().read.check_blind(path, display, api_name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_write(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().write.check(path, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
fn check_write_partial(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().write.check_partial(path, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 09:13:32 -04:00
|
|
|
|
fn check_write_blind(
|
|
|
|
|
&mut self,
|
|
|
|
|
p: &Path,
|
|
|
|
|
display: &str,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().write.check_blind(p, display, api_name)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-04 18:44:59 -05:00
|
|
|
|
fn check_read_all(&mut self, api_name: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().read.check_all(Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_write_all(&mut self, api_name: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().write.check_all(Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 10:06:44 -04:00
|
|
|
|
// NOTE(bartlomieju): for now, NAPI uses `--allow-ffi` flag, but that might
|
|
|
|
|
// change in the future.
|
2023-01-07 11:25:34 -05:00
|
|
|
|
impl deno_napi::NapiPermissions for PermissionsContainer {
|
|
|
|
|
#[inline(always)]
|
2022-10-05 10:06:44 -04:00
|
|
|
|
fn check(&mut self, path: Option<&Path>) -> Result<(), AnyError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.0.lock().ffi.check(path.unwrap(), None)
|
2022-10-05 10:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
impl deno_ffi::FfiPermissions for PermissionsContainer {
|
|
|
|
|
#[inline(always)]
|
2023-08-03 07:19:19 -04:00
|
|
|
|
fn check_partial(&mut self, path: Option<&Path>) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().ffi.check_partial(path)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
|
impl deno_kv::sqlite::SqliteDbHandlerPermissions for PermissionsContainer {
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn check_read(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().read.check(p, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn check_write(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().write.check(p, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-22 01:56:00 -04:00
|
|
|
|
impl deno_kv::remote::RemoteDbHandlerPermissions for PermissionsContainer {
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn check_env(&mut self, var: &str) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().env.check(var)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn check_net_url(
|
|
|
|
|
&mut self,
|
|
|
|
|
url: &url::Url,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
|
self.0.lock().net.check_url(url, Some(api_name))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
fn unit_permission_from_flag_bools(
|
|
|
|
|
allow_flag: bool,
|
|
|
|
|
deny_flag: bool,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
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,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
state: if deny_flag {
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else if allow_flag {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
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
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
fn global_from_option<T>(flag: &Option<Vec<T>>) -> bool {
|
|
|
|
|
matches!(flag, Some(v) if v.is_empty())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_net_list(
|
|
|
|
|
list: &Option<Vec<String>>,
|
|
|
|
|
) -> Result<HashSet<NetDescriptor>, AnyError> {
|
|
|
|
|
if let Some(v) = list {
|
|
|
|
|
v.iter()
|
|
|
|
|
.map(|x| NetDescriptor::from_str(x))
|
|
|
|
|
.collect::<Result<HashSet<NetDescriptor>, AnyError>>()
|
2021-03-17 17:45:12 -04:00
|
|
|
|
} else {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
Ok(HashSet::new())
|
2020-09-19 19:17:35 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2020-09-19 19:17:35 -04:00
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
fn parse_env_list(
|
|
|
|
|
list: &Option<Vec<String>>,
|
|
|
|
|
) -> Result<HashSet<EnvDescriptor>, AnyError> {
|
|
|
|
|
if let Some(v) = list {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
v.iter()
|
2023-08-03 07:19:19 -04:00
|
|
|
|
.map(|x| {
|
|
|
|
|
if x.is_empty() {
|
2022-08-10 15:13:53 -04:00
|
|
|
|
Err(AnyError::msg("Empty path is not allowed"))
|
|
|
|
|
} else {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
Ok(EnvDescriptor::new(x))
|
2022-08-10 15:13:53 -04:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
fn parse_path_list<T: Descriptor + Hash>(
|
|
|
|
|
list: &Option<Vec<PathBuf>>,
|
|
|
|
|
f: fn(PathBuf) -> T,
|
|
|
|
|
) -> Result<HashSet<T>, AnyError> {
|
|
|
|
|
if let Some(v) = list {
|
2021-03-17 17:45:12 -04:00
|
|
|
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
resolve_from_cwd(Path::new(&raw_path)).map(f)
|
2022-08-10 15:13:53 -04:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
fn parse_sys_list(
|
|
|
|
|
list: &Option<Vec<String>>,
|
|
|
|
|
) -> Result<HashSet<SysDescriptor>, AnyError> {
|
|
|
|
|
if let Some(v) = list {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
v.iter()
|
2023-08-03 07:19:19 -04:00
|
|
|
|
.map(|x| {
|
|
|
|
|
if x.is_empty() {
|
|
|
|
|
Err(AnyError::msg("empty"))
|
2022-08-10 15:13:53 -04:00
|
|
|
|
} else {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
Ok(SysDescriptor(x.to_string()))
|
2022-08-10 15:13:53 -04:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
fn parse_run_list(
|
|
|
|
|
list: &Option<Vec<String>>,
|
|
|
|
|
) -> Result<HashSet<RunDescriptor>, AnyError> {
|
|
|
|
|
if let Some(v) = list {
|
|
|
|
|
v.iter()
|
|
|
|
|
.map(|x| {
|
|
|
|
|
if x.is_empty() {
|
|
|
|
|
Err(AnyError::msg("Empty path is not allowed"))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(RunDescriptor::from_str(x).unwrap())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
} else {
|
|
|
|
|
Ok(HashSet::new())
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
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| {
|
2023-01-27 10:43:16 -05:00
|
|
|
|
de::Error::custom(format!("(deno.permissions.env) {e}"))
|
2021-10-13 13:04:44 -04:00
|
|
|
|
})?;
|
|
|
|
|
} else if key == "hrtime" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnitPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.hrtime = arg.map_err(|e| {
|
2023-01-27 10:43:16 -05:00
|
|
|
|
de::Error::custom(format!("(deno.permissions.hrtime) {e}"))
|
2021-10-13 13:04:44 -04:00
|
|
|
|
})?;
|
|
|
|
|
} else if key == "net" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.net = arg.map_err(|e| {
|
2023-01-27 10:43:16 -05:00
|
|
|
|
de::Error::custom(format!("(deno.permissions.net) {e}"))
|
2021-10-13 13:04:44 -04:00
|
|
|
|
})?;
|
|
|
|
|
} else if key == "ffi" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.ffi = arg.map_err(|e| {
|
2023-01-27 10:43:16 -05:00
|
|
|
|
de::Error::custom(format!("(deno.permissions.ffi) {e}"))
|
2021-10-13 13:04:44 -04:00
|
|
|
|
})?;
|
|
|
|
|
} else if key == "read" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.read = arg.map_err(|e| {
|
2023-01-27 10:43:16 -05:00
|
|
|
|
de::Error::custom(format!("(deno.permissions.read) {e}"))
|
2021-10-13 13:04:44 -04:00
|
|
|
|
})?;
|
|
|
|
|
} else if key == "run" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.run = arg.map_err(|e| {
|
2023-01-27 10:43:16 -05:00
|
|
|
|
de::Error::custom(format!("(deno.permissions.run) {e}"))
|
2021-10-13 13:04:44 -04:00
|
|
|
|
})?;
|
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| {
|
2023-01-27 10:43:16 -05:00
|
|
|
|
de::Error::custom(format!("(deno.permissions.sys) {e}"))
|
2022-09-28 08:46:50 -04:00
|
|
|
|
})?;
|
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| {
|
2023-01-27 10:43:16 -05:00
|
|
|
|
de::Error::custom(format!("(deno.permissions.write) {e}"))
|
2021-10-13 13:04:44 -04:00
|
|
|
|
})?;
|
|
|
|
|
} 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());
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.env.granted_global = true;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.env.granted_list = parse_env_list(&Some(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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.env.flag_denied_global = main_perms.env.flag_denied_global;
|
|
|
|
|
worker_perms.env.flag_denied_list = main_perms.env.flag_denied_list.clone();
|
|
|
|
|
worker_perms.env.prompt_denied_global = main_perms.env.prompt_denied_global;
|
|
|
|
|
worker_perms.env.prompt_denied_list =
|
|
|
|
|
main_perms.env.prompt_denied_list.clone();
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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());
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.sys.granted_global = true;
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.sys.granted_list = parse_sys_list(&Some(granted_list))?;
|
2022-09-28 08:46:50 -04:00
|
|
|
|
if !worker_perms
|
|
|
|
|
.sys
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
|
|
|
|
.all(|desc| main_perms.sys.check(&desc.0, None).is_ok())
|
|
|
|
|
{
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.sys.flag_denied_global = main_perms.sys.flag_denied_global;
|
|
|
|
|
worker_perms.sys.flag_denied_list = main_perms.sys.flag_denied_list.clone();
|
|
|
|
|
worker_perms.sys.prompt_denied_global = main_perms.sys.prompt_denied_global;
|
|
|
|
|
worker_perms.sys.prompt_denied_list =
|
|
|
|
|
main_perms.sys.prompt_denied_list.clone();
|
2022-09-28 08:46:50 -04:00
|
|
|
|
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());
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.net.granted_global = true;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.net.granted_list = parse_net_list(&Some(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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.net.flag_denied_global = main_perms.net.flag_denied_global;
|
|
|
|
|
worker_perms.net.flag_denied_list = main_perms.net.flag_denied_list.clone();
|
|
|
|
|
worker_perms.net.prompt_denied_global = main_perms.net.prompt_denied_global;
|
|
|
|
|
worker_perms.net.prompt_denied_list =
|
|
|
|
|
main_perms.net.prompt_denied_list.clone();
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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());
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.ffi.granted_global = true;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.ffi.granted_list = parse_path_list(
|
2021-10-13 13:04:44 -04:00
|
|
|
|
&Some(granted_list.iter().map(PathBuf::from).collect()),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
FfiDescriptor,
|
|
|
|
|
)?;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
if !worker_perms
|
|
|
|
|
.ffi
|
|
|
|
|
.granted_list
|
|
|
|
|
.iter()
|
2023-08-03 07:19:19 -04:00
|
|
|
|
.all(|desc| main_perms.ffi.check(&desc.0, None).is_ok())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
{
|
|
|
|
|
return Err(escalation_error());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.ffi.flag_denied_global = main_perms.env.flag_denied_global;
|
|
|
|
|
worker_perms.ffi.flag_denied_list = main_perms.ffi.flag_denied_list.clone();
|
|
|
|
|
worker_perms.ffi.prompt_denied_global = main_perms.ffi.prompt_denied_global;
|
|
|
|
|
worker_perms.ffi.prompt_denied_list =
|
|
|
|
|
main_perms.ffi.prompt_denied_list.clone();
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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());
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.read.granted_global = true;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.read.granted_list = parse_path_list(
|
2021-10-13 13:04:44 -04:00
|
|
|
|
&Some(granted_list.iter().map(PathBuf::from).collect()),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
ReadDescriptor,
|
|
|
|
|
)?;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.read.flag_denied_global = main_perms.read.flag_denied_global;
|
|
|
|
|
worker_perms.read.flag_denied_list = main_perms.read.flag_denied_list.clone();
|
|
|
|
|
worker_perms.read.prompt_denied_global = main_perms.read.prompt_denied_global;
|
|
|
|
|
worker_perms.read.prompt_denied_list =
|
|
|
|
|
main_perms.read.prompt_denied_list.clone();
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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());
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.run.granted_global = true;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.run.granted_list = parse_run_list(&Some(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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.run.flag_denied_global = main_perms.run.flag_denied_global;
|
|
|
|
|
worker_perms.run.flag_denied_list = main_perms.run.flag_denied_list.clone();
|
|
|
|
|
worker_perms.run.prompt_denied_global = main_perms.run.prompt_denied_global;
|
|
|
|
|
worker_perms.run.prompt_denied_list =
|
|
|
|
|
main_perms.run.prompt_denied_list.clone();
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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());
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.write.granted_global = true;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.write.granted_list = parse_path_list(
|
2021-10-13 13:04:44 -04:00
|
|
|
|
&Some(granted_list.iter().map(PathBuf::from).collect()),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
WriteDescriptor,
|
|
|
|
|
)?;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
worker_perms.write.flag_denied_global = main_perms.write.flag_denied_global;
|
|
|
|
|
worker_perms.write.flag_denied_list =
|
|
|
|
|
main_perms.write.flag_denied_list.clone();
|
|
|
|
|
worker_perms.write.prompt_denied_global =
|
|
|
|
|
main_perms.write.prompt_denied_global;
|
|
|
|
|
worker_perms.write.prompt_denied_list =
|
|
|
|
|
main_perms.write.prompt_denied_list.clone();
|
2021-10-13 13:04:44 -04:00
|
|
|
|
worker_perms.write.prompt = main_perms.write.prompt;
|
|
|
|
|
Ok(worker_perms)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2021-10-13 13:04:44 -04:00
|
|
|
|
use deno_core::serde_json::json;
|
2022-12-17 19:12:28 -05:00
|
|
|
|
use prompter::tests::*;
|
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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
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
|
2023-08-03 07:19:19 -04:00
|
|
|
|
.check(Path::new("/a/specific/dir/name"), None)
|
2022-12-14 15:38:53 -05:00
|
|
|
|
.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());
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/a/specific/dir"), None).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
|
2023-08-03 07:19:19 -04:00
|
|
|
|
.check(Path::new("/a/specific/dir/name/inner"), None)
|
2022-12-14 15:38:53 -05:00
|
|
|
|
.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
|
2023-08-03 07:19:19 -04:00
|
|
|
|
.check(Path::new("/a/specific/other/dir"), None)
|
2022-12-14 15:38:53 -05:00
|
|
|
|
.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());
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/b/c"), None).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());
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/b/c/sub/path"), None).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
|
2023-08-03 07:19:19 -04:00
|
|
|
|
.check(Path::new("/b/c/sub/path/../path/."), None)
|
2022-12-14 15:38:53 -05:00
|
|
|
|
.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());
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/b/e"), None).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());
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/a/b"), None).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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(
|
|
|
|
|
is_ok,
|
|
|
|
|
perms.net.check(&(host, Some(port)), None).is_ok(),
|
|
|
|
|
"{}:{}",
|
|
|
|
|
host,
|
|
|
|
|
port,
|
|
|
|
|
);
|
2021-01-19 09:39:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_check_net_only_flag() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
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();
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(is_ok, perms.net.check_url(&u, None).is_ok(), "{}", u);
|
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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
2020-10-26 15:56:00 -04:00
|
|
|
|
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![
|
|
|
|
|
(
|
2023-03-13 19:31:03 -04:00
|
|
|
|
ModuleSpecifier::parse("http://localhost:4545/mod.ts").unwrap(),
|
2020-10-26 15:56:00 -04:00
|
|
|
|
true,
|
|
|
|
|
),
|
|
|
|
|
(
|
2023-03-13 19:31:03 -04:00
|
|
|
|
ModuleSpecifier::parse("http://deno.land/x/mod.ts").unwrap(),
|
2020-10-26 15:56:00 -04:00
|
|
|
|
false,
|
|
|
|
|
),
|
2021-01-05 21:22:38 -05:00
|
|
|
|
(
|
2023-03-13 19:31:03 -04:00
|
|
|
|
ModuleSpecifier::parse("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
|
2023-03-13 19:31:03 -04:00
|
|
|
|
.push((ModuleSpecifier::parse("file:///C:/a/mod.ts").unwrap(), true));
|
|
|
|
|
fixtures.push((
|
|
|
|
|
ModuleSpecifier::parse("file:///C:/b/mod.ts").unwrap(),
|
|
|
|
|
false,
|
|
|
|
|
));
|
2020-10-26 15:56:00 -04:00
|
|
|
|
} else {
|
2023-03-13 19:31:03 -04:00
|
|
|
|
fixtures
|
|
|
|
|
.push((ModuleSpecifier::parse("file:///a/mod.ts").unwrap(), true));
|
|
|
|
|
fixtures
|
|
|
|
|
.push((ModuleSpecifier::parse("file:///b/mod.ts").unwrap(), false));
|
2020-10-26 15:56:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (specifier, expected) in fixtures {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(
|
|
|
|
|
perms.check_specifier(&specifier).is_ok(),
|
|
|
|
|
expected,
|
|
|
|
|
"{}",
|
|
|
|
|
specifier,
|
|
|
|
|
);
|
2020-10-26 15:56:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 04:33:20 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn check_invalid_specifiers() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
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
|
2023-03-13 19:31:03 -04:00
|
|
|
|
.check_specifier(&ModuleSpecifier::parse(url).unwrap())
|
2021-01-04 04:33:20 -05:00
|
|
|
|
.is_err());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 16:29:32 -04:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_query() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let perms1 = Permissions::allow_all();
|
|
|
|
|
let perms2 = Permissions {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
read: Permissions::new_read(
|
|
|
|
|
&Some(vec![PathBuf::from("/foo")]),
|
|
|
|
|
&None,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
write: Permissions::new_write(
|
|
|
|
|
&Some(vec![PathBuf::from("/foo")]),
|
|
|
|
|
&None,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(
|
|
|
|
|
&Some(vec![PathBuf::from("/foo")]),
|
|
|
|
|
&None,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
net: Permissions::new_net(&Some(svec!["127.0.0.1:8000"]), &None, false)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
env: Permissions::new_env(&Some(svec!["HOME"]), &None, false).unwrap(),
|
|
|
|
|
sys: Permissions::new_sys(&Some(svec!["hostname"]), &None, false)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
run: Permissions::new_run(&Some(svec!["deno"]), &None, false).unwrap(),
|
|
|
|
|
hrtime: Permissions::new_hrtime(false, false),
|
|
|
|
|
};
|
|
|
|
|
let perms3 = Permissions {
|
|
|
|
|
read: Permissions::new_read(
|
|
|
|
|
&None,
|
|
|
|
|
&Some(vec![PathBuf::from("/foo")]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
write: Permissions::new_write(
|
|
|
|
|
&None,
|
|
|
|
|
&Some(vec![PathBuf::from("/foo")]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(
|
|
|
|
|
&None,
|
|
|
|
|
&Some(vec![PathBuf::from("/foo")]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
net: Permissions::new_net(&None, &Some(svec!["127.0.0.1:8000"]), false)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
env: Permissions::new_env(&None, &Some(svec!["HOME"]), false).unwrap(),
|
|
|
|
|
sys: Permissions::new_sys(&None, &Some(svec!["hostname"]), false)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
run: Permissions::new_run(&None, &Some(svec!["deno"]), false).unwrap(),
|
|
|
|
|
hrtime: Permissions::new_hrtime(false, true),
|
|
|
|
|
};
|
|
|
|
|
let perms4 = Permissions {
|
|
|
|
|
read: Permissions::new_read(
|
|
|
|
|
&Some(vec![]),
|
|
|
|
|
&Some(vec![PathBuf::from("/foo")]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
write: Permissions::new_write(
|
|
|
|
|
&Some(vec![]),
|
|
|
|
|
&Some(vec![PathBuf::from("/foo")]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(
|
|
|
|
|
&Some(vec![]),
|
|
|
|
|
&Some(vec![PathBuf::from("/foo")]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
net: Permissions::new_net(
|
|
|
|
|
&Some(vec![]),
|
|
|
|
|
&Some(svec!["127.0.0.1:8000"]),
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
env: Permissions::new_env(&Some(vec![]), &Some(svec!["HOME"]), false)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
sys: Permissions::new_sys(&Some(vec![]), &Some(svec!["hostname"]), false)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
run: Permissions::new_run(&Some(vec![]), &Some(svec!["deno"]), false)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
hrtime: Permissions::new_hrtime(true, true),
|
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);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.read.query(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms3.read.query(Some(Path::new("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms3.read.query(Some(Path::new("/foo/bar"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.read.query(None), PermissionState::GrantedPartial);
|
|
|
|
|
assert_eq!(perms4.read.query(Some(Path::new("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.read.query(Some(Path::new("/foo/bar"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.read.query(Some(Path::new("/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);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.write.query(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms3.write.query(Some(Path::new("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms3.write.query(Some(Path::new("/foo/bar"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.write.query(None), PermissionState::GrantedPartial);
|
|
|
|
|
assert_eq!(perms4.write.query(Some(Path::new("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.write.query(Some(Path::new("/foo/bar"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.write.query(Some(Path::new("/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);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.ffi.query(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms3.ffi.query(Some(Path::new("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms3.ffi.query(Some(Path::new("/foo/bar"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.ffi.query(None), PermissionState::GrantedPartial);
|
|
|
|
|
assert_eq!(perms4.ffi.query(Some(Path::new("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.ffi.query(Some(Path::new("/foo/bar"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.ffi.query(Some(Path::new("/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);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.net.query::<&str>(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms3.net.query(Some(&("127.0.0.1", Some(8000)))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.net.query::<&str>(None), PermissionState::GrantedPartial);
|
|
|
|
|
assert_eq!(perms4.net.query(Some(&("127.0.0.1", Some(8000)))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.net.query(Some(&("192.168.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);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.env.query(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms3.env.query(Some("HOME")), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.env.query(None), PermissionState::GrantedPartial);
|
|
|
|
|
assert_eq!(perms4.env.query(Some("HOME")), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.env.query(Some("AWAY")), 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);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms2.sys.query(None), PermissionState::Prompt);
|
2022-09-28 08:46:50 -04:00
|
|
|
|
assert_eq!(perms2.sys.query(Some("hostname")), PermissionState::Granted);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.sys.query(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms3.sys.query(Some("hostname")), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.sys.query(None), PermissionState::GrantedPartial);
|
|
|
|
|
assert_eq!(perms4.sys.query(Some("hostname")), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.sys.query(Some("uid")), 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);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.run.query(None), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms3.run.query(Some("deno")), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.run.query(None), PermissionState::GrantedPartial);
|
|
|
|
|
assert_eq!(perms4.run.query(Some("deno")), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.run.query(Some("node")), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms1.hrtime.query(), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.hrtime.query(), PermissionState::Prompt);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.hrtime.query(), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.hrtime.query(), PermissionState::Denied);
|
2020-08-18 16:29:32 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_request() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
2020-08-18 16:29:32 -04:00
|
|
|
|
let mut perms = Permissions {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
read: Permissions::new_read(
|
|
|
|
|
&Some(vec![PathBuf::from("/foo"), PathBuf::from("/foo/baz")]),
|
|
|
|
|
&None,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
write: Permissions::new_write(
|
|
|
|
|
&Some(vec![PathBuf::from("/foo"), PathBuf::from("/foo/baz")]),
|
|
|
|
|
&None,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(
|
|
|
|
|
&Some(vec![PathBuf::from("/foo"), PathBuf::from("/foo/baz")]),
|
|
|
|
|
&None,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
net: Permissions::new_net(
|
|
|
|
|
&Some(svec!["127.0.0.1", "127.0.0.1:8000"]),
|
|
|
|
|
&None,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
env: Permissions::new_env(&Some(svec!["HOME"]), &None, false).unwrap(),
|
|
|
|
|
sys: Permissions::new_sys(&Some(svec!["hostname"]), &None, false)
|
|
|
|
|
.unwrap(),
|
|
|
|
|
run: Permissions::new_run(&Some(svec!["deno"]), &None, false).unwrap(),
|
|
|
|
|
hrtime: Permissions::new_hrtime(false, true),
|
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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let mut perms = Permissions {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
read: Permissions::new_read(&None, &None, true).unwrap(),
|
|
|
|
|
write: Permissions::new_write(&None, &None, true).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&None, &None, true).unwrap(),
|
|
|
|
|
env: Permissions::new_env(&None, &None, true).unwrap(),
|
|
|
|
|
sys: Permissions::new_sys(&None, &None, true).unwrap(),
|
|
|
|
|
run: Permissions::new_run(&None, &None, true).unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(&None, &None, true).unwrap(),
|
|
|
|
|
hrtime: Permissions::new_hrtime(false, 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);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/foo"), None).is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(false);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/foo"), None).is_ok());
|
|
|
|
|
assert!(perms.ffi.check(Path::new("/bar"), None).is_err());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
|
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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
2021-04-11 22:15:43 -04:00
|
|
|
|
let mut perms = Permissions {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
read: Permissions::new_read(&None, &None, true).unwrap(),
|
|
|
|
|
write: Permissions::new_write(&None, &None, true).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&None, &None, true).unwrap(),
|
|
|
|
|
env: Permissions::new_env(&None, &None, true).unwrap(),
|
|
|
|
|
sys: Permissions::new_sys(&None, &None, true).unwrap(),
|
|
|
|
|
run: Permissions::new_run(&None, &None, true).unwrap(),
|
|
|
|
|
ffi: Permissions::new_ffi(&None, &None, true).unwrap(),
|
|
|
|
|
hrtime: Permissions::new_hrtime(false, 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);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/foo"), None).is_err());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(true);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/foo"), None).is_err());
|
|
|
|
|
assert!(perms.ffi.check(Path::new("/bar"), None).is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(false);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(perms.ffi.check(Path::new("/bar"), None).is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
|
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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
granted_global: false,
|
|
|
|
|
..Permissions::new_env(&Some(svec!["HOME"]), &None, 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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
2021-10-13 13:04:44 -04:00
|
|
|
|
let mut main_perms = Permissions {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
env: Permissions::new_env(&Some(vec![]), &None, false).unwrap(),
|
|
|
|
|
hrtime: Permissions::new_hrtime(true, false),
|
|
|
|
|
net: Permissions::new_net(&Some(svec!["foo", "bar"]), &None, 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 {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
env: Permissions::new_env(&Some(vec![]), &None, false).unwrap(),
|
|
|
|
|
net: Permissions::new_net(&Some(svec!["foo"]), &None, 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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
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() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
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();
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(
|
|
|
|
|
worker_perms.write.flag_denied_list,
|
|
|
|
|
main_perms.write.flag_denied_list
|
|
|
|
|
);
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
2022-08-10 15:13:53 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_handle_empty_value() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert!(
|
|
|
|
|
Permissions::new_read(&Some(vec![PathBuf::new()]), &None, false).is_err()
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
Permissions::new_env(&Some(vec![String::new()]), &None, false).is_err()
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
Permissions::new_sys(&Some(vec![String::new()]), &None, false).is_err()
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
Permissions::new_run(&Some(vec![String::new()]), &None, false).is_err()
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
Permissions::new_ffi(&Some(vec![PathBuf::new()]), &None, false).is_err()
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
Permissions::new_net(&Some(svec![String::new()]), &None, false).is_err()
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
Permissions::new_write(&Some(vec![PathBuf::new()]), &None, false)
|
|
|
|
|
.is_err()
|
|
|
|
|
);
|
2022-08-10 15:13:53 -04:00
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|