2024-01-01 14:58:21 -05:00
|
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
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;
|
2024-08-19 21:20:06 -04:00
|
|
|
|
use deno_core::unsync::sync::AtomicFlag;
|
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;
|
2024-09-28 07:55:01 -04:00
|
|
|
|
use deno_path_util::normalize_path;
|
|
|
|
|
use deno_path_util::url_to_file_path;
|
2024-02-07 11:25:14 -05:00
|
|
|
|
use deno_terminal::colors;
|
2024-03-26 19:27:40 -04:00
|
|
|
|
use fqdn::FQDN;
|
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;
|
2024-04-19 20:12:03 -04:00
|
|
|
|
use std::ffi::OsStr;
|
2019-03-18 16:46:23 -04:00
|
|
|
|
use std::fmt;
|
2024-03-13 16:30:48 -04:00
|
|
|
|
use std::fmt::Debug;
|
2020-08-18 16:29:32 -04:00
|
|
|
|
use std::hash::Hash;
|
2024-07-05 18:45:06 -04:00
|
|
|
|
use std::net::IpAddr;
|
|
|
|
|
use std::net::Ipv6Addr;
|
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::string::ToString;
|
2023-01-07 11:25:34 -05:00
|
|
|
|
use std::sync::Arc;
|
2019-03-18 16:46:23 -04:00
|
|
|
|
|
2024-03-12 13:42:26 -04:00
|
|
|
|
pub mod prompter;
|
2022-12-17 19:12:28 -05:00
|
|
|
|
use prompter::permission_prompt;
|
|
|
|
|
use prompter::PERMISSION_EMOJI;
|
|
|
|
|
|
|
|
|
|
pub use prompter::set_prompt_callbacks;
|
2024-10-17 12:39:18 -04:00
|
|
|
|
pub use prompter::set_prompter;
|
|
|
|
|
pub use prompter::PermissionPrompter;
|
2022-12-17 19:12:28 -05:00
|
|
|
|
pub use prompter::PromptCallback;
|
2024-10-17 14:36:34 -04:00
|
|
|
|
pub use prompter::PromptResponse;
|
2019-06-22 12:02:51 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
#[error("Requires {access}, {}", format_permission_error(.name))]
|
|
|
|
|
pub struct PermissionDeniedError {
|
|
|
|
|
access: String,
|
|
|
|
|
name: &'static str,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn format_permission_error(name: &'static str) -> String {
|
|
|
|
|
if is_standalone() {
|
|
|
|
|
format!("specify the required permissions during compilation using `deno compile --allow-{name}`")
|
|
|
|
|
} else {
|
|
|
|
|
format!("run again with the --allow-{name} flag")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
/// Fast exit from permission check routines if this permission
|
|
|
|
|
/// is in the "fully-granted" state.
|
|
|
|
|
macro_rules! skip_check_if_is_permission_fully_granted {
|
|
|
|
|
($this:ident) => {
|
|
|
|
|
if $this.is_allow_all() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 13:42:26 -04:00
|
|
|
|
#[inline]
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn resolve_from_known_cwd(path: &Path, cwd: &Path) -> PathBuf {
|
2024-03-12 13:42:26 -04:00
|
|
|
|
if path.is_absolute() {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
normalize_path(path)
|
2024-03-12 13:42:26 -04:00
|
|
|
|
} else {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
normalize_path(cwd.join(path))
|
2024-03-12 13:42:26 -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)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn log_perm_access(
|
|
|
|
|
name: &'static 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
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn fmt_access(
|
|
|
|
|
name: &'static str,
|
|
|
|
|
info: impl FnOnce() -> Option<String>,
|
|
|
|
|
) -> String {
|
2021-04-12 07:24:41 -04:00
|
|
|
|
format!(
|
|
|
|
|
"{} access{}",
|
|
|
|
|
name,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
info().map(|info| format!(" to {info}")).unwrap_or_default(),
|
2021-04-12 07:24:41 -04:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn error(
|
|
|
|
|
name: &'static str,
|
|
|
|
|
info: impl FnOnce() -> Option<String>,
|
|
|
|
|
) -> PermissionDeniedError {
|
|
|
|
|
PermissionDeniedError {
|
|
|
|
|
access: Self::fmt_access(name, info),
|
|
|
|
|
name,
|
|
|
|
|
}
|
2021-04-12 07:24:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
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,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
name: &'static 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,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> (Result<(), PermissionDeniedError>, 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,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
name: &'static 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,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> (Result<(), PermissionDeniedError>, 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
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check(&mut self) -> Result<(), PermissionDeniedError> {
|
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
|
|
|
|
}
|
2024-03-13 16:30:48 -04:00
|
|
|
|
|
|
|
|
|
fn create_child_permissions(
|
|
|
|
|
&mut self,
|
|
|
|
|
flag: ChildUnitPermissionArg,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<Self, ChildPermissionError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
let mut perm = self.clone();
|
|
|
|
|
match flag {
|
|
|
|
|
ChildUnitPermissionArg::Inherit => {
|
|
|
|
|
// copy
|
|
|
|
|
}
|
|
|
|
|
ChildUnitPermissionArg::Granted => {
|
|
|
|
|
if self.check().is_err() {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(ChildPermissionError::Escalation);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
perm.state = PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
ChildUnitPermissionArg::NotGranted => {
|
|
|
|
|
perm.state = PermissionState::Prompt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if self.state == PermissionState::Denied {
|
|
|
|
|
perm.state = PermissionState::Denied;
|
|
|
|
|
}
|
|
|
|
|
Ok(perm)
|
|
|
|
|
}
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub trait QueryDescriptor: Debug {
|
|
|
|
|
type AllowDesc: Debug + Eq + Clone + Hash;
|
|
|
|
|
type DenyDesc: Debug + Eq + Clone + Hash;
|
|
|
|
|
|
|
|
|
|
fn flag_name() -> &'static str;
|
|
|
|
|
fn display_name(&self) -> Cow<str>;
|
|
|
|
|
|
|
|
|
|
fn from_allow(allow: &Self::AllowDesc) -> Self;
|
2024-03-13 16:30:48 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn as_allow(&self) -> Option<Self::AllowDesc>;
|
|
|
|
|
fn as_deny(&self) -> Self::DenyDesc;
|
2024-03-13 16:30:48 -04:00
|
|
|
|
|
|
|
|
|
/// Generic check function to check this descriptor against a `UnaryPermission`.
|
|
|
|
|
fn check_in_permission(
|
|
|
|
|
&self,
|
|
|
|
|
perm: &mut UnaryPermission<Self>,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError>;
|
2024-03-13 16:30:48 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_allow(&self, other: &Self::AllowDesc) -> bool;
|
|
|
|
|
fn matches_deny(&self, other: &Self::DenyDesc) -> bool;
|
|
|
|
|
|
|
|
|
|
/// Gets if this query descriptor should revoke the provided allow descriptor.
|
|
|
|
|
fn revokes(&self, other: &Self::AllowDesc) -> bool;
|
|
|
|
|
fn stronger_than_deny(&self, other: &Self::DenyDesc) -> bool;
|
|
|
|
|
fn overlaps_deny(&self, other: &Self::DenyDesc) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn format_display_name(display_name: Cow<str>) -> String {
|
|
|
|
|
if display_name.starts_with('<') && display_name.ends_with('>') {
|
|
|
|
|
display_name.into_owned()
|
|
|
|
|
} else {
|
|
|
|
|
format!("\"{}\"", display_name)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
|
pub struct UnaryPermission<TQuery: QueryDescriptor + ?Sized> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
granted_global: bool,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
granted_list: HashSet<TQuery::AllowDesc>,
|
2024-03-13 16:30:48 -04:00
|
|
|
|
flag_denied_global: bool,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
flag_denied_list: HashSet<TQuery::DenyDesc>,
|
2024-03-13 16:30:48 -04:00
|
|
|
|
prompt_denied_global: bool,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
prompt_denied_list: HashSet<TQuery::DenyDesc>,
|
2024-03-13 16:30:48 -04:00
|
|
|
|
prompt: bool,
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
2020-05-29 07:00:47 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl<TQuery: QueryDescriptor> Default for UnaryPermission<TQuery> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
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(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl<TQuery: QueryDescriptor> Clone for UnaryPermission<TQuery> {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
granted_global: self.granted_global,
|
|
|
|
|
granted_list: self.granted_list.clone(),
|
|
|
|
|
flag_denied_global: self.flag_denied_global,
|
|
|
|
|
flag_denied_list: self.flag_denied_list.clone(),
|
|
|
|
|
prompt_denied_global: self.prompt_denied_global,
|
|
|
|
|
prompt_denied_list: self.prompt_denied_list.clone(),
|
|
|
|
|
prompt: self.prompt,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<TQuery: QueryDescriptor> UnaryPermission<TQuery> {
|
2024-03-13 12:07:24 -04:00
|
|
|
|
pub fn allow_all() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
granted_global: true,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
pub fn is_allow_all(&self) -> bool {
|
|
|
|
|
self.granted_global
|
|
|
|
|
&& self.flag_denied_list.is_empty()
|
|
|
|
|
&& self.prompt_denied_list.is_empty()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check_all_api(
|
|
|
|
|
&mut self,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(None, false, api_name)
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
fn check_desc(
|
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
desc: Option<&TQuery>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_non_partial: bool,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
let (result, prompted, is_allow_all) = self
|
2024-03-12 18:12:29 -04:00
|
|
|
|
.query_desc(desc, AllowPartial::from(!assert_non_partial))
|
2023-08-03 07:19:19 -04:00
|
|
|
|
.check2(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
TQuery::flag_name(),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
api_name,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|| desc.map(|d| format_display_name(d.display_name())),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
self.prompt,
|
|
|
|
|
);
|
|
|
|
|
if prompted {
|
|
|
|
|
if result.is_ok() {
|
|
|
|
|
if is_allow_all {
|
|
|
|
|
self.insert_granted(None);
|
|
|
|
|
} else {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.insert_granted(desc);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.insert_prompt_denied(desc.map(|d| d.as_deny()));
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn query_desc(
|
|
|
|
|
&self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
desc: Option<&TQuery>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
allow_partial: AllowPartial,
|
|
|
|
|
) -> PermissionState {
|
2024-09-04 08:51:24 -04:00
|
|
|
|
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
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
2024-09-04 08:51:24 -04:00
|
|
|
|
}
|
|
|
|
|
AllowPartial::TreatAsPartialGranted => {
|
|
|
|
|
if self.is_partial_flag_denied(desc) {
|
|
|
|
|
PermissionState::GrantedPartial
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Granted
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-04 08:51:24 -04:00
|
|
|
|
} else if matches!(allow_partial, AllowPartial::TreatAsDenied)
|
|
|
|
|
&& self.is_partial_flag_denied(desc)
|
|
|
|
|
{
|
|
|
|
|
PermissionState::Denied
|
|
|
|
|
} else {
|
|
|
|
|
PermissionState::Prompt
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn request_desc(&mut self, desc: Option<&TQuery>) -> PermissionState {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
let state = self.query_desc(desc, AllowPartial::TreatAsPartialGranted);
|
|
|
|
|
if state == PermissionState::Granted {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.insert_granted(desc);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
if state != PermissionState::Prompt {
|
|
|
|
|
return state;
|
|
|
|
|
}
|
2024-10-03 08:28:38 -04:00
|
|
|
|
if !self.prompt {
|
|
|
|
|
return PermissionState::Denied;
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
let mut message = String::with_capacity(40);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
message.push_str(&format!("{} access", TQuery::flag_name()));
|
|
|
|
|
if let Some(desc) = desc {
|
|
|
|
|
message
|
|
|
|
|
.push_str(&format!(" to {}", format_display_name(desc.display_name())));
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
match permission_prompt(
|
|
|
|
|
&message,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
TQuery::flag_name(),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
Some("Deno.permissions.request()"),
|
|
|
|
|
true,
|
|
|
|
|
) {
|
|
|
|
|
PromptResponse::Allow => {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.insert_granted(desc);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
PermissionState::Granted
|
|
|
|
|
}
|
|
|
|
|
PromptResponse::Deny => {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.insert_prompt_denied(desc.map(|d| d.as_deny()));
|
2023-08-03 07:19:19 -04:00
|
|
|
|
PermissionState::Denied
|
|
|
|
|
}
|
|
|
|
|
PromptResponse::AllowAll => {
|
|
|
|
|
self.insert_granted(None);
|
|
|
|
|
PermissionState::Granted
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn revoke_desc(&mut self, desc: Option<&TQuery>) -> PermissionState {
|
2024-03-26 19:27:40 -04:00
|
|
|
|
match desc {
|
2023-08-30 13:52:01 -04:00
|
|
|
|
Some(desc) => {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.granted_list.retain(|v| !desc.revokes(v));
|
2023-08-30 13:52:01 -04:00
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn is_granted(&self, query: Option<&TQuery>) -> bool {
|
|
|
|
|
match query {
|
|
|
|
|
Some(query) => {
|
|
|
|
|
self.granted_global
|
|
|
|
|
|| self.granted_list.iter().any(|v| query.matches_allow(v))
|
|
|
|
|
}
|
|
|
|
|
None => self.granted_global,
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn is_flag_denied(&self, query: Option<&TQuery>) -> bool {
|
|
|
|
|
match query {
|
|
|
|
|
Some(query) => {
|
|
|
|
|
self.flag_denied_global
|
|
|
|
|
|| self.flag_denied_list.iter().any(|v| query.matches_deny(v))
|
|
|
|
|
}
|
|
|
|
|
None => self.flag_denied_global,
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn is_prompt_denied(&self, query: Option<&TQuery>) -> bool {
|
|
|
|
|
match query {
|
|
|
|
|
Some(query) => self
|
2023-08-03 07:19:19 -04:00
|
|
|
|
.prompt_denied_list
|
|
|
|
|
.iter()
|
2024-09-16 16:39:37 -04:00
|
|
|
|
.any(|v| query.stronger_than_deny(v)),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
None => self.prompt_denied_global || !self.prompt_denied_list.is_empty(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn is_partial_flag_denied(&self, query: Option<&TQuery>) -> bool {
|
|
|
|
|
match query {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
None => !self.flag_denied_list.is_empty(),
|
2024-09-16 16:39:37 -04:00
|
|
|
|
Some(query) => {
|
|
|
|
|
self.flag_denied_list.iter().any(|v| query.overlaps_deny(v))
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn insert_granted(&mut self, query: Option<&TQuery>) -> bool {
|
|
|
|
|
let desc = match query.map(|q| q.as_allow()) {
|
|
|
|
|
Some(Some(allow_desc)) => Some(allow_desc),
|
|
|
|
|
Some(None) => {
|
|
|
|
|
// the user was prompted for this descriptor in order to not
|
|
|
|
|
// expose anything about the system to the program, but the
|
|
|
|
|
// descriptor wasn't valid so no permission was raised
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
2023-08-03 07:19:19 -04:00
|
|
|
|
Self::list_insert(desc, &mut self.granted_global, &mut self.granted_list);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
true
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn insert_prompt_denied(&mut self, desc: Option<TQuery::DenyDesc>) {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
Self::list_insert(
|
|
|
|
|
desc,
|
|
|
|
|
&mut self.prompt_denied_global,
|
|
|
|
|
&mut self.prompt_denied_list,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn list_insert<T: Hash + Eq>(
|
2023-08-03 07:19:19 -04:00
|
|
|
|
desc: Option<T>,
|
|
|
|
|
list_global: &mut bool,
|
|
|
|
|
list: &mut HashSet<T>,
|
|
|
|
|
) {
|
|
|
|
|
match desc {
|
|
|
|
|
Some(desc) => {
|
|
|
|
|
list.insert(desc);
|
|
|
|
|
}
|
|
|
|
|
None => *list_global = true,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-13 16:30:48 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn create_child_permissions<E>(
|
2024-03-13 16:30:48 -04:00
|
|
|
|
&mut self,
|
|
|
|
|
flag: ChildUnaryPermissionArg,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
parse: impl Fn(&str) -> Result<Option<TQuery::AllowDesc>, E>,
|
|
|
|
|
) -> Result<UnaryPermission<TQuery>, ChildPermissionError>
|
|
|
|
|
where
|
|
|
|
|
ChildPermissionError: From<E>,
|
|
|
|
|
{
|
2024-03-13 16:30:48 -04:00
|
|
|
|
let mut perms = Self::default();
|
|
|
|
|
|
|
|
|
|
match flag {
|
|
|
|
|
ChildUnaryPermissionArg::Inherit => {
|
2024-06-14 07:40:57 -04:00
|
|
|
|
perms.clone_from(self);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::Granted => {
|
|
|
|
|
if self.check_all_api(None).is_err() {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(ChildPermissionError::Escalation);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
perms.granted_global = true;
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted => {}
|
|
|
|
|
ChildUnaryPermissionArg::GrantedList(granted_list) => {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
perms.granted_list = granted_list
|
2024-03-13 16:30:48 -04:00
|
|
|
|
.iter()
|
2024-09-16 16:39:37 -04:00
|
|
|
|
.filter_map(|i| parse(i).transpose())
|
2024-11-04 12:17:21 -05:00
|
|
|
|
.collect::<Result<_, E>>()?;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
if !perms.granted_list.iter().all(|desc| {
|
|
|
|
|
TQuery::from_allow(desc)
|
|
|
|
|
.check_in_permission(self, None)
|
|
|
|
|
.is_ok()
|
|
|
|
|
}) {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(ChildPermissionError::Escalation);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
perms.flag_denied_global = self.flag_denied_global;
|
|
|
|
|
perms.prompt_denied_global = self.prompt_denied_global;
|
|
|
|
|
perms.prompt = self.prompt;
|
2024-06-14 07:40:57 -04:00
|
|
|
|
perms.flag_denied_list.clone_from(&self.flag_denied_list);
|
|
|
|
|
perms
|
|
|
|
|
.prompt_denied_list
|
|
|
|
|
.clone_from(&self.prompt_denied_list);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
|
|
|
|
|
Ok(perms)
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub struct PathQueryDescriptor {
|
|
|
|
|
pub requested: String,
|
|
|
|
|
pub resolved: PathBuf,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PathQueryDescriptor {
|
|
|
|
|
pub fn into_ffi(self) -> FfiQueryDescriptor {
|
|
|
|
|
FfiQueryDescriptor(self)
|
|
|
|
|
}
|
2021-03-21 08:49:58 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn into_read(self) -> ReadQueryDescriptor {
|
|
|
|
|
ReadQueryDescriptor(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn into_write(self) -> WriteQueryDescriptor {
|
|
|
|
|
WriteQueryDescriptor(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub struct ReadQueryDescriptor(pub PathQueryDescriptor);
|
|
|
|
|
|
|
|
|
|
impl QueryDescriptor for ReadQueryDescriptor {
|
|
|
|
|
type AllowDesc = ReadDescriptor;
|
|
|
|
|
type DenyDesc = ReadDescriptor;
|
|
|
|
|
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"read"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn display_name(&self) -> Cow<str> {
|
|
|
|
|
Cow::Borrowed(self.0.requested.as_str())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_allow(allow: &Self::AllowDesc) -> Self {
|
|
|
|
|
PathQueryDescriptor {
|
|
|
|
|
requested: allow.0.to_string_lossy().into_owned(),
|
|
|
|
|
resolved: allow.0.clone(),
|
|
|
|
|
}
|
|
|
|
|
.into_read()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_allow(&self) -> Option<Self::AllowDesc> {
|
|
|
|
|
Some(ReadDescriptor(self.0.resolved.clone()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_deny(&self) -> Self::DenyDesc {
|
|
|
|
|
ReadDescriptor(self.0.resolved.clone())
|
|
|
|
|
}
|
2024-03-13 12:07:24 -04:00
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
fn check_in_permission(
|
|
|
|
|
&self,
|
|
|
|
|
perm: &mut UnaryPermission<Self>,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-26 19:27:40 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(perm);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
perm.check_desc(Some(self), true, api_name)
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_allow(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.0.resolved.starts_with(&other.0)
|
2024-03-13 12:07:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.0.resolved.starts_with(&other.0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn revokes(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.matches_allow(other)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn stronger_than_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
other.0.starts_with(&self.0.resolved)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn overlaps_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.stronger_than_deny(other)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub struct ReadDescriptor(pub PathBuf);
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub struct WriteQueryDescriptor(pub PathQueryDescriptor);
|
|
|
|
|
|
|
|
|
|
impl QueryDescriptor for WriteQueryDescriptor {
|
|
|
|
|
type AllowDesc = WriteDescriptor;
|
|
|
|
|
type DenyDesc = WriteDescriptor;
|
|
|
|
|
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"write"
|
|
|
|
|
}
|
2021-03-21 08:49:58 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn display_name(&self) -> Cow<str> {
|
|
|
|
|
Cow::Borrowed(&self.0.requested)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_allow(allow: &Self::AllowDesc) -> Self {
|
|
|
|
|
WriteQueryDescriptor(PathQueryDescriptor {
|
|
|
|
|
requested: allow.0.to_string_lossy().into_owned(),
|
|
|
|
|
resolved: allow.0.clone(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_allow(&self) -> Option<Self::AllowDesc> {
|
|
|
|
|
Some(WriteDescriptor(self.0.resolved.clone()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_deny(&self) -> Self::DenyDesc {
|
|
|
|
|
WriteDescriptor(self.0.resolved.clone())
|
|
|
|
|
}
|
2024-03-13 12:07:24 -04:00
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
fn check_in_permission(
|
|
|
|
|
&self,
|
|
|
|
|
perm: &mut UnaryPermission<Self>,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-26 19:27:40 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(perm);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
perm.check_desc(Some(self), true, api_name)
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_allow(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.0.resolved.starts_with(&other.0)
|
2024-03-13 12:07:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.0.resolved.starts_with(&other.0)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn revokes(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.matches_allow(other)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn stronger_than_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
other.0.starts_with(&self.0.resolved)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlaps_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.stronger_than_deny(other)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub struct WriteDescriptor(pub PathBuf);
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2024-07-05 18:45:06 -04:00
|
|
|
|
pub enum Host {
|
|
|
|
|
Fqdn(FQDN),
|
|
|
|
|
Ip(IpAddr),
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum HostParseError {
|
|
|
|
|
#[error("invalid IPv6 address: '{0}'")]
|
|
|
|
|
InvalidIpv6(String),
|
|
|
|
|
#[error("invalid host: '{0}'")]
|
|
|
|
|
InvalidHost(String),
|
|
|
|
|
#[error("invalid empty host: '{0}'")]
|
|
|
|
|
InvalidEmptyHost(String),
|
|
|
|
|
#[error("invalid host '{host}': {error}")]
|
|
|
|
|
Fqdn {
|
|
|
|
|
#[source]
|
|
|
|
|
error: fqdn::Error,
|
|
|
|
|
host: String,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 05:28:53 -04:00
|
|
|
|
impl Host {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn parse(s: &str) -> Result<Self, HostParseError> {
|
2024-07-05 18:45:06 -04:00
|
|
|
|
if s.starts_with('[') && s.ends_with(']') {
|
|
|
|
|
let ip = s[1..s.len() - 1]
|
|
|
|
|
.parse::<Ipv6Addr>()
|
2024-11-04 12:17:21 -05:00
|
|
|
|
.map_err(|_| HostParseError::InvalidIpv6(s.to_string()))?;
|
2024-07-05 18:45:06 -04:00
|
|
|
|
return Ok(Host::Ip(IpAddr::V6(ip)));
|
|
|
|
|
}
|
|
|
|
|
let (without_trailing_dot, has_trailing_dot) =
|
|
|
|
|
s.strip_suffix('.').map_or((s, false), |s| (s, true));
|
|
|
|
|
if let Ok(ip) = without_trailing_dot.parse::<IpAddr>() {
|
|
|
|
|
if has_trailing_dot {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(HostParseError::InvalidHost(
|
|
|
|
|
without_trailing_dot.to_string(),
|
|
|
|
|
));
|
2024-07-05 18:45:06 -04:00
|
|
|
|
}
|
|
|
|
|
Ok(Host::Ip(ip))
|
|
|
|
|
} else {
|
|
|
|
|
let lower = if s.chars().all(|c| c.is_ascii_lowercase()) {
|
|
|
|
|
Cow::Borrowed(s)
|
|
|
|
|
} else {
|
|
|
|
|
Cow::Owned(s.to_ascii_lowercase())
|
|
|
|
|
};
|
2024-09-06 05:28:53 -04:00
|
|
|
|
let fqdn = {
|
|
|
|
|
use std::str::FromStr;
|
2024-11-04 12:17:21 -05:00
|
|
|
|
FQDN::from_str(&lower).map_err(|e| HostParseError::Fqdn {
|
|
|
|
|
error: e,
|
|
|
|
|
host: s.to_string(),
|
|
|
|
|
})?
|
2024-09-06 05:28:53 -04:00
|
|
|
|
};
|
2024-07-05 18:45:06 -04:00
|
|
|
|
if fqdn.is_root() {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(HostParseError::InvalidEmptyHost(s.to_string()));
|
2024-07-05 18:45:06 -04:00
|
|
|
|
}
|
|
|
|
|
Ok(Host::Fqdn(fqdn))
|
|
|
|
|
}
|
2021-03-21 08:49:58 -04:00
|
|
|
|
}
|
2024-09-06 05:28:53 -04:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
#[track_caller]
|
|
|
|
|
fn must_parse(s: &str) -> Self {
|
|
|
|
|
Self::parse(s).unwrap()
|
|
|
|
|
}
|
2022-08-10 15:13:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 18:45:06 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub struct NetDescriptor(pub Host, pub Option<u16>);
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl QueryDescriptor for NetDescriptor {
|
|
|
|
|
type AllowDesc = NetDescriptor;
|
|
|
|
|
type DenyDesc = NetDescriptor;
|
|
|
|
|
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"net"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn display_name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(format!("{}", self))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_allow(allow: &Self::AllowDesc) -> Self {
|
|
|
|
|
allow.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_allow(&self) -> Option<Self::AllowDesc> {
|
|
|
|
|
Some(self.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_deny(&self) -> Self::DenyDesc {
|
|
|
|
|
self.clone()
|
|
|
|
|
}
|
2024-03-13 12:07:24 -04:00
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
fn check_in_permission(
|
|
|
|
|
&self,
|
|
|
|
|
perm: &mut UnaryPermission<Self>,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-26 19:27:40 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(perm);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
perm.check_desc(Some(self), false, api_name)
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_allow(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.0 == other.0 && (other.1.is_none() || self.1 == other.1)
|
2024-03-13 12:07:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.0 == other.0 && (other.1.is_none() || self.1 == other.1)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn revokes(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.matches_allow(other)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stronger_than_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.matches_deny(other)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn overlaps_deny(&self, _other: &Self::DenyDesc) -> bool {
|
|
|
|
|
false
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum NetDescriptorParseError {
|
|
|
|
|
#[error("invalid value '{0}': URLs are not supported, only domains and ips")]
|
|
|
|
|
Url(String),
|
|
|
|
|
#[error("invalid IPv6 address in '{hostname}': '{ip}'")]
|
|
|
|
|
InvalidIpv6 { hostname: String, ip: String },
|
|
|
|
|
#[error("invalid port in '{hostname}': '{port}'")]
|
|
|
|
|
InvalidPort { hostname: String, port: String },
|
|
|
|
|
#[error("invalid host: '{0}'")]
|
|
|
|
|
InvalidHost(String),
|
|
|
|
|
#[error("invalid empty port in '{0}'")]
|
|
|
|
|
EmptyPort(String),
|
|
|
|
|
#[error("ipv6 addresses must be enclosed in square brackets: '{0}'")]
|
|
|
|
|
Ipv6MissingSquareBrackets(String),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
Host(#[from] HostParseError),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum NetDescriptorFromUrlParseError {
|
|
|
|
|
#[error("Missing host in url: '{0}'")]
|
|
|
|
|
MissingHost(Url),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
Host(#[from] HostParseError),
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 05:28:53 -04:00
|
|
|
|
impl NetDescriptor {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn parse(hostname: &str) -> Result<Self, NetDescriptorParseError> {
|
2024-09-20 14:10:46 -04:00
|
|
|
|
if hostname.starts_with("http://") || hostname.starts_with("https://") {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(NetDescriptorParseError::Url(hostname.to_string()));
|
2024-09-20 14:10:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 18:45:06 -04:00
|
|
|
|
// If this is a IPv6 address enclosed in square brackets, parse it as such.
|
|
|
|
|
if hostname.starts_with('[') {
|
|
|
|
|
if let Some((ip, after)) = hostname.split_once(']') {
|
|
|
|
|
let ip = ip[1..].parse::<Ipv6Addr>().map_err(|_| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
NetDescriptorParseError::InvalidIpv6 {
|
|
|
|
|
hostname: hostname.to_string(),
|
|
|
|
|
ip: ip.to_string(),
|
|
|
|
|
}
|
2024-07-05 18:45:06 -04:00
|
|
|
|
})?;
|
|
|
|
|
let port = if let Some(port) = after.strip_prefix(':') {
|
|
|
|
|
let port = port.parse::<u16>().map_err(|_| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
NetDescriptorParseError::InvalidPort {
|
|
|
|
|
hostname: hostname.to_string(),
|
|
|
|
|
port: port.to_string(),
|
|
|
|
|
}
|
2024-07-05 18:45:06 -04:00
|
|
|
|
})?;
|
|
|
|
|
Some(port)
|
|
|
|
|
} else if after.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(NetDescriptorParseError::InvalidHost(
|
|
|
|
|
hostname.to_string(),
|
|
|
|
|
));
|
2024-07-05 18:45:06 -04:00
|
|
|
|
};
|
|
|
|
|
return Ok(NetDescriptor(Host::Ip(IpAddr::V6(ip)), port));
|
|
|
|
|
} else {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(NetDescriptorParseError::InvalidHost(hostname.to_string()));
|
2024-07-05 18:45:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise it is an IPv4 address or a FQDN with an optional port.
|
|
|
|
|
let (host, port) = match hostname.split_once(':') {
|
|
|
|
|
Some((_, "")) => {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(NetDescriptorParseError::EmptyPort(hostname.to_string()));
|
2024-07-05 18:45:06 -04:00
|
|
|
|
}
|
|
|
|
|
Some((host, port)) => (host, port),
|
|
|
|
|
None => (hostname, ""),
|
|
|
|
|
};
|
2024-09-06 05:28:53 -04:00
|
|
|
|
let host = Host::parse(host)?;
|
2021-03-21 08:49:58 -04:00
|
|
|
|
|
2024-07-05 18:45:06 -04:00
|
|
|
|
let port = if port.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
let port = port.parse::<u16>().map_err(|_| {
|
|
|
|
|
// If the user forgot to enclose an IPv6 address in square brackets, we
|
|
|
|
|
// should give them a hint. There are always at least two colons in an
|
|
|
|
|
// IPv6 address, so this heuristic finds likely a bare IPv6 address.
|
|
|
|
|
if port.contains(':') {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
NetDescriptorParseError::Ipv6MissingSquareBrackets(
|
|
|
|
|
hostname.to_string(),
|
|
|
|
|
)
|
2024-07-05 18:45:06 -04:00
|
|
|
|
} else {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
NetDescriptorParseError::InvalidPort {
|
|
|
|
|
hostname: hostname.to_string(),
|
|
|
|
|
port: port.to_string(),
|
|
|
|
|
}
|
2024-07-05 18:45:06 -04:00
|
|
|
|
}
|
|
|
|
|
})?;
|
|
|
|
|
Some(port)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(NetDescriptor(host, port))
|
2021-03-21 08:49:58 -04:00
|
|
|
|
}
|
2024-09-25 21:50:54 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn from_url(url: &Url) -> Result<Self, NetDescriptorFromUrlParseError> {
|
|
|
|
|
let host = url.host_str().ok_or_else(|| {
|
|
|
|
|
NetDescriptorFromUrlParseError::MissingHost(url.clone())
|
|
|
|
|
})?;
|
2024-09-25 21:50:54 -04:00
|
|
|
|
let host = Host::parse(host)?;
|
|
|
|
|
let port = url.port_or_known_default();
|
|
|
|
|
Ok(NetDescriptor(host, port))
|
|
|
|
|
}
|
2021-03-21 08:49:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for NetDescriptor {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2024-07-05 18:45:06 -04:00
|
|
|
|
match &self.0 {
|
|
|
|
|
Host::Fqdn(fqdn) => write!(f, "{fqdn}"),
|
|
|
|
|
Host::Ip(IpAddr::V4(ip)) => write!(f, "{ip}"),
|
|
|
|
|
Host::Ip(IpAddr::V6(ip)) => write!(f, "[{ip}]"),
|
|
|
|
|
}?;
|
|
|
|
|
if let Some(port) = self.1 {
|
|
|
|
|
write!(f, ":{}", port)?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
2021-03-21 08:49:58 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-27 09:11:39 -04:00
|
|
|
|
|
2024-09-25 21:50:54 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub struct ImportDescriptor(NetDescriptor);
|
|
|
|
|
|
|
|
|
|
impl QueryDescriptor for ImportDescriptor {
|
|
|
|
|
type AllowDesc = ImportDescriptor;
|
|
|
|
|
type DenyDesc = ImportDescriptor;
|
|
|
|
|
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"import"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn display_name(&self) -> Cow<str> {
|
|
|
|
|
self.0.display_name()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_allow(allow: &Self::AllowDesc) -> Self {
|
|
|
|
|
Self(NetDescriptor::from_allow(&allow.0))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_allow(&self) -> Option<Self::AllowDesc> {
|
|
|
|
|
self.0.as_allow().map(ImportDescriptor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_deny(&self) -> Self::DenyDesc {
|
|
|
|
|
Self(self.0.as_deny())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_in_permission(
|
|
|
|
|
&self,
|
|
|
|
|
perm: &mut UnaryPermission<Self>,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(perm);
|
|
|
|
|
perm.check_desc(Some(self), false, api_name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn matches_allow(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.0.matches_allow(&other.0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn matches_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.0.matches_deny(&other.0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn revokes(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.0.revokes(&other.0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stronger_than_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.0.stronger_than_deny(&other.0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlaps_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.0.overlaps_deny(&other.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ImportDescriptor {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn parse(specifier: &str) -> Result<Self, NetDescriptorParseError> {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
Ok(ImportDescriptor(NetDescriptor::parse(specifier)?))
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn from_url(url: &Url) -> Result<Self, NetDescriptorFromUrlParseError> {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
Ok(ImportDescriptor(NetDescriptor::from_url(url)?))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
#[error("Empty env not allowed")]
|
|
|
|
|
pub struct EnvDescriptorParseError;
|
|
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl QueryDescriptor for EnvDescriptor {
|
|
|
|
|
type AllowDesc = EnvDescriptor;
|
|
|
|
|
type DenyDesc = EnvDescriptor;
|
|
|
|
|
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"env"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn display_name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(self.0.as_ref())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_allow(allow: &Self::AllowDesc) -> Self {
|
|
|
|
|
allow.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_allow(&self) -> Option<Self::AllowDesc> {
|
|
|
|
|
Some(self.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_deny(&self) -> Self::DenyDesc {
|
|
|
|
|
self.clone()
|
|
|
|
|
}
|
2024-03-13 12:07:24 -04:00
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
fn check_in_permission(
|
|
|
|
|
&self,
|
|
|
|
|
perm: &mut UnaryPermission<Self>,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-26 19:27:40 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(perm);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
perm.check_desc(Some(self), false, api_name)
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_allow(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self == other
|
2024-03-13 12:07:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self == other
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn revokes(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self == other
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stronger_than_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self == other
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlaps_deny(&self, _other: &Self::DenyDesc) -> bool {
|
|
|
|
|
false
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 15:50:59 -04:00
|
|
|
|
impl AsRef<str> for EnvDescriptor {
|
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
|
self.0.as_ref()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 18:57:49 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug, Serialize, Deserialize)]
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub enum RunQueryDescriptor {
|
|
|
|
|
Path {
|
|
|
|
|
requested: String,
|
|
|
|
|
resolved: PathBuf,
|
|
|
|
|
},
|
|
|
|
|
/// This variant won't actually grant permissions because the path of
|
|
|
|
|
/// the executable is unresolved. It's mostly used so that prompts and
|
|
|
|
|
/// everything works the same way as when the command is resolved,
|
|
|
|
|
/// meaning that a script can't tell
|
|
|
|
|
/// if a command is resolved or not based on how long something
|
|
|
|
|
/// takes to ask for permissions.
|
2021-10-13 13:04:44 -04:00
|
|
|
|
Name(String),
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum PathResolveError {
|
|
|
|
|
#[error("failed resolving cwd: {0}")]
|
|
|
|
|
CwdResolve(#[source] std::io::Error),
|
|
|
|
|
#[error("Empty path is not allowed")]
|
|
|
|
|
EmptyPath,
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl RunQueryDescriptor {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn parse(
|
|
|
|
|
requested: &str,
|
|
|
|
|
) -> Result<RunQueryDescriptor, PathResolveError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
if is_path(requested) {
|
|
|
|
|
let path = PathBuf::from(requested);
|
|
|
|
|
let resolved = if path.is_absolute() {
|
|
|
|
|
normalize_path(path)
|
|
|
|
|
} else {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
let cwd =
|
|
|
|
|
std::env::current_dir().map_err(PathResolveError::CwdResolve)?;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
normalize_path(cwd.join(path))
|
|
|
|
|
};
|
|
|
|
|
Ok(RunQueryDescriptor::Path {
|
|
|
|
|
requested: requested.to_string(),
|
|
|
|
|
resolved,
|
|
|
|
|
})
|
2024-09-04 08:51:24 -04:00
|
|
|
|
} else {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
match which::which(requested) {
|
|
|
|
|
Ok(resolved) => Ok(RunQueryDescriptor::Path {
|
|
|
|
|
requested: requested.to_string(),
|
|
|
|
|
resolved,
|
|
|
|
|
}),
|
|
|
|
|
Err(_) => Ok(RunQueryDescriptor::Name(requested.to_string())),
|
2024-09-04 08:51:24 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl QueryDescriptor for RunQueryDescriptor {
|
|
|
|
|
type AllowDesc = AllowRunDescriptor;
|
|
|
|
|
type DenyDesc = DenyRunDescriptor;
|
|
|
|
|
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"run"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn display_name(&self) -> Cow<str> {
|
|
|
|
|
match self {
|
|
|
|
|
RunQueryDescriptor::Path { requested, .. } => Cow::Borrowed(requested),
|
|
|
|
|
RunQueryDescriptor::Name(name) => Cow::Borrowed(name),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_allow(allow: &Self::AllowDesc) -> Self {
|
|
|
|
|
RunQueryDescriptor::Path {
|
|
|
|
|
requested: allow.0.to_string_lossy().into_owned(),
|
|
|
|
|
resolved: allow.0.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_allow(&self) -> Option<Self::AllowDesc> {
|
|
|
|
|
match self {
|
|
|
|
|
RunQueryDescriptor::Path { resolved, .. } => {
|
|
|
|
|
Some(AllowRunDescriptor(resolved.clone()))
|
|
|
|
|
}
|
|
|
|
|
RunQueryDescriptor::Name(_) => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_deny(&self) -> Self::DenyDesc {
|
|
|
|
|
match self {
|
|
|
|
|
RunQueryDescriptor::Path {
|
|
|
|
|
resolved,
|
|
|
|
|
requested,
|
|
|
|
|
} => {
|
|
|
|
|
if requested.contains('/')
|
|
|
|
|
|| (cfg!(windows) && requested.contains("\\"))
|
|
|
|
|
{
|
|
|
|
|
DenyRunDescriptor::Path(resolved.clone())
|
|
|
|
|
} else {
|
|
|
|
|
DenyRunDescriptor::Name(requested.clone())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
RunQueryDescriptor::Name(name) => DenyRunDescriptor::Name(name.clone()),
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-13 12:07:24 -04:00
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
fn check_in_permission(
|
|
|
|
|
&self,
|
|
|
|
|
perm: &mut UnaryPermission<Self>,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-26 19:27:40 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(perm);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
perm.check_desc(Some(self), false, api_name)
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_allow(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
RunQueryDescriptor::Path { resolved, .. } => *resolved == other.0,
|
|
|
|
|
RunQueryDescriptor::Name(_) => false,
|
|
|
|
|
}
|
2024-03-13 12:07:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
match other {
|
|
|
|
|
DenyRunDescriptor::Name(deny_desc) => match self {
|
|
|
|
|
RunQueryDescriptor::Path { resolved, .. } => {
|
|
|
|
|
denies_run_name(deny_desc, resolved)
|
|
|
|
|
}
|
|
|
|
|
RunQueryDescriptor::Name(query) => query == deny_desc,
|
|
|
|
|
},
|
|
|
|
|
DenyRunDescriptor::Path(deny_desc) => match self {
|
|
|
|
|
RunQueryDescriptor::Path { resolved, .. } => {
|
|
|
|
|
resolved.starts_with(deny_desc)
|
|
|
|
|
}
|
|
|
|
|
RunQueryDescriptor::Name(query) => denies_run_name(query, deny_desc),
|
|
|
|
|
},
|
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn revokes(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
RunQueryDescriptor::Path {
|
|
|
|
|
resolved,
|
|
|
|
|
requested,
|
|
|
|
|
} => {
|
|
|
|
|
if *resolved == other.0 {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if is_path(requested) {
|
|
|
|
|
false
|
|
|
|
|
} else {
|
|
|
|
|
denies_run_name(requested, &other.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
RunQueryDescriptor::Name(query) => denies_run_name(query, &other.0),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stronger_than_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.matches_deny(other)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
|
|
|
|
fn overlaps_deny(&self, _other: &Self::DenyDesc) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum RunDescriptorArg {
|
|
|
|
|
Name(String),
|
|
|
|
|
Path(PathBuf),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum AllowRunDescriptorParseResult {
|
|
|
|
|
/// An error occured getting the descriptor that should
|
|
|
|
|
/// be surfaced as a warning when launching deno, but should
|
|
|
|
|
/// be ignored when creating a worker.
|
|
|
|
|
Unresolved(Box<which::Error>),
|
|
|
|
|
Descriptor(AllowRunDescriptor),
|
2023-08-30 13:52:01 -04:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum RunDescriptorParseError {
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
Which(#[from] which::Error),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
PathResolve(#[from] PathResolveError),
|
|
|
|
|
#[error("Empty run query is not allowed")]
|
|
|
|
|
EmptyRunQuery,
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
|
|
|
|
pub struct AllowRunDescriptor(pub PathBuf);
|
|
|
|
|
|
|
|
|
|
impl AllowRunDescriptor {
|
|
|
|
|
pub fn parse(
|
|
|
|
|
text: &str,
|
|
|
|
|
cwd: &Path,
|
|
|
|
|
) -> Result<AllowRunDescriptorParseResult, which::Error> {
|
|
|
|
|
let is_path = is_path(text);
|
|
|
|
|
// todo(dsherret): canonicalize in #25458
|
|
|
|
|
let path = if is_path {
|
|
|
|
|
resolve_from_known_cwd(Path::new(text), cwd)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
} else {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
match which::which_in(text, std::env::var_os("PATH"), cwd) {
|
|
|
|
|
Ok(path) => path,
|
|
|
|
|
Err(err) => match err {
|
|
|
|
|
which::Error::BadAbsolutePath | which::Error::BadRelativePath => {
|
|
|
|
|
return Err(err);
|
|
|
|
|
}
|
|
|
|
|
which::Error::CannotFindBinaryPath
|
|
|
|
|
| which::Error::CannotGetCurrentDir
|
|
|
|
|
| which::Error::CannotCanonicalize => {
|
|
|
|
|
return Ok(AllowRunDescriptorParseResult::Unresolved(Box::new(err)))
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-09-04 08:51:24 -04:00
|
|
|
|
}
|
2024-09-16 16:39:37 -04:00
|
|
|
|
};
|
|
|
|
|
Ok(AllowRunDescriptorParseResult::Descriptor(
|
|
|
|
|
AllowRunDescriptor(path),
|
|
|
|
|
))
|
2023-08-30 13:52:01 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub enum DenyRunDescriptor {
|
|
|
|
|
/// Warning: You may want to construct with `RunDescriptor::from()` for case
|
|
|
|
|
/// handling.
|
|
|
|
|
Name(String),
|
|
|
|
|
/// Warning: You may want to construct with `RunDescriptor::from()` for case
|
|
|
|
|
/// handling.
|
|
|
|
|
Path(PathBuf),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl DenyRunDescriptor {
|
|
|
|
|
pub fn parse(text: &str, cwd: &Path) -> Self {
|
|
|
|
|
if text.contains('/') || cfg!(windows) && text.contains('\\') {
|
|
|
|
|
let path = resolve_from_known_cwd(Path::new(&text), cwd);
|
|
|
|
|
DenyRunDescriptor::Path(path)
|
|
|
|
|
} else {
|
|
|
|
|
DenyRunDescriptor::Name(text.to_string())
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-09 18:12:00 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn is_path(text: &str) -> bool {
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
|
text.contains('/') || text.contains('\\') || Path::new(text).is_absolute()
|
|
|
|
|
} else {
|
|
|
|
|
text.contains('/')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn denies_run_name(name: &str, cmd_path: &Path) -> bool {
|
|
|
|
|
let Some(file_stem) = cmd_path.file_stem() else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
let Some(file_stem) = file_stem.to_str() else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
if file_stem.len() < name.len() {
|
|
|
|
|
return false;
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let (prefix, suffix) = file_stem.split_at(name.len());
|
|
|
|
|
if !prefix.eq_ignore_ascii_case(name) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// be broad and consider anything like `deno.something` as matching deny perms
|
|
|
|
|
suffix.is_empty() || suffix.starts_with('.')
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum SysDescriptorParseError {
|
|
|
|
|
#[error("unknown system info kind \"{0}\"")]
|
|
|
|
|
InvalidKind(String), // TypeError
|
|
|
|
|
#[error("Empty sys not allowed")]
|
|
|
|
|
Empty, // Error
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2024-09-30 09:19:24 -04:00
|
|
|
|
pub struct SysDescriptor(String);
|
|
|
|
|
|
|
|
|
|
impl SysDescriptor {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn parse(kind: String) -> Result<Self, SysDescriptorParseError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
match kind.as_str() {
|
2024-11-12 10:55:49 -05:00
|
|
|
|
"hostname" | "inspector" | "osRelease" | "osUptime" | "loadavg"
|
2024-09-30 09:19:24 -04:00
|
|
|
|
| "networkInterfaces" | "systemMemoryInfo" | "uid" | "gid" | "cpus"
|
2024-11-01 01:18:33 -04:00
|
|
|
|
| "homedir" | "getegid" | "statfs" | "getPriority" | "setPriority"
|
|
|
|
|
| "userInfo" => Ok(Self(kind)),
|
|
|
|
|
|
|
|
|
|
// the underlying permission check changed to `userInfo` to better match the API,
|
|
|
|
|
// alias this to avoid breaking existing projects with `--allow-sys=username`
|
|
|
|
|
"username" => Ok(Self("userInfo".into())),
|
2024-11-04 12:17:21 -05:00
|
|
|
|
_ => Err(SysDescriptorParseError::InvalidKind(kind)),
|
2024-09-30 09:19:24 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn into_string(self) -> String {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-28 08:46:50 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl QueryDescriptor for SysDescriptor {
|
|
|
|
|
type AllowDesc = SysDescriptor;
|
|
|
|
|
type DenyDesc = SysDescriptor;
|
|
|
|
|
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"sys"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn display_name(&self) -> Cow<str> {
|
|
|
|
|
Cow::from(self.0.to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_allow(allow: &Self::AllowDesc) -> Self {
|
|
|
|
|
allow.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_allow(&self) -> Option<Self::AllowDesc> {
|
|
|
|
|
Some(self.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_deny(&self) -> Self::DenyDesc {
|
|
|
|
|
self.clone()
|
|
|
|
|
}
|
2024-03-13 12:07:24 -04:00
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
fn check_in_permission(
|
|
|
|
|
&self,
|
|
|
|
|
perm: &mut UnaryPermission<Self>,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-26 19:27:40 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(perm);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
perm.check_desc(Some(self), false, api_name)
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_allow(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self == other
|
2024-03-13 12:07:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self == other
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn revokes(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self == other
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stronger_than_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self == other
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlaps_deny(&self, _other: &Self::DenyDesc) -> bool {
|
|
|
|
|
false
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub struct FfiQueryDescriptor(pub PathQueryDescriptor);
|
|
|
|
|
|
|
|
|
|
impl QueryDescriptor for FfiQueryDescriptor {
|
|
|
|
|
type AllowDesc = FfiDescriptor;
|
|
|
|
|
type DenyDesc = FfiDescriptor;
|
|
|
|
|
|
|
|
|
|
fn flag_name() -> &'static str {
|
|
|
|
|
"ffi"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn display_name(&self) -> Cow<str> {
|
|
|
|
|
Cow::Borrowed(&self.0.requested)
|
|
|
|
|
}
|
2021-08-06 17:28:10 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn from_allow(allow: &Self::AllowDesc) -> Self {
|
|
|
|
|
PathQueryDescriptor {
|
|
|
|
|
requested: allow.0.to_string_lossy().into_owned(),
|
|
|
|
|
resolved: allow.0.clone(),
|
|
|
|
|
}
|
|
|
|
|
.into_ffi()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_allow(&self) -> Option<Self::AllowDesc> {
|
|
|
|
|
Some(FfiDescriptor(self.0.resolved.clone()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_deny(&self) -> Self::DenyDesc {
|
|
|
|
|
FfiDescriptor(self.0.resolved.clone())
|
|
|
|
|
}
|
2024-03-13 12:07:24 -04:00
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
fn check_in_permission(
|
|
|
|
|
&self,
|
|
|
|
|
perm: &mut UnaryPermission<Self>,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-26 19:27:40 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(perm);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
perm.check_desc(Some(self), true, api_name)
|
2024-03-13 16:30:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_allow(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.0.resolved.starts_with(&other.0)
|
2024-03-13 12:07:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn matches_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.0.resolved.starts_with(&other.0)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn revokes(&self, other: &Self::AllowDesc) -> bool {
|
|
|
|
|
self.matches_allow(other)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn stronger_than_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
other.0.starts_with(&self.0.resolved)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlaps_deny(&self, other: &Self::DenyDesc) -> bool {
|
|
|
|
|
self.stronger_than_deny(other)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub struct FfiDescriptor(pub PathBuf);
|
|
|
|
|
|
|
|
|
|
impl UnaryPermission<ReadQueryDescriptor> {
|
|
|
|
|
pub fn query(&self, desc: Option<&ReadQueryDescriptor>) -> PermissionState {
|
|
|
|
|
self.query_desc(desc, AllowPartial::TreatAsPartialGranted)
|
2020-12-29 13:34:35 -05:00
|
|
|
|
}
|
2020-05-29 11:27:43 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn request(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: Option<&ReadQueryDescriptor>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
self.request_desc(path)
|
2018-10-27 09:11:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn revoke(
|
|
|
|
|
&mut self,
|
|
|
|
|
desc: Option<&ReadQueryDescriptor>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
self.revoke_desc(desc)
|
2020-05-29 11:27:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
desc: &ReadQueryDescriptor,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(Some(desc), true, api_name)
|
2023-08-03 07:19:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn check_partial(
|
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
desc: &ReadQueryDescriptor,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(Some(desc), false, api_name)
|
2019-02-08 15:59:38 -05:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_all(
|
|
|
|
|
&mut self,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(None, false, api_name)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl UnaryPermission<WriteQueryDescriptor> {
|
|
|
|
|
pub fn query(&self, path: Option<&WriteQueryDescriptor>) -> PermissionState {
|
|
|
|
|
self.query_desc(path, AllowPartial::TreatAsPartialGranted)
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn request(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: Option<&WriteQueryDescriptor>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
self.request_desc(path)
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn revoke(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: Option<&WriteQueryDescriptor>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
self.revoke_desc(path)
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
path: &WriteQueryDescriptor,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(Some(path), true, api_name)
|
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,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
path: &WriteQueryDescriptor,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(Some(path), false, api_name)
|
2023-04-12 09:13:32 -04:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_all(
|
|
|
|
|
&mut self,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(None, false, api_name)
|
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> {
|
2024-07-05 18:45:06 -04:00
|
|
|
|
pub fn query(&self, host: Option<&NetDescriptor>) -> PermissionState {
|
|
|
|
|
self.query_desc(host, AllowPartial::TreatAsPartialGranted)
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 18:45:06 -04:00
|
|
|
|
pub fn request(&mut self, host: Option<&NetDescriptor>) -> PermissionState {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.request_desc(host)
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 18:45:06 -04:00
|
|
|
|
pub fn revoke(&mut self, host: Option<&NetDescriptor>) -> PermissionState {
|
|
|
|
|
self.revoke_desc(host)
|
2019-11-11 10:33:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 18:45:06 -04:00
|
|
|
|
pub fn check(
|
2021-04-11 22:15:43 -04:00
|
|
|
|
&mut self,
|
2024-07-05 18:45:06 -04:00
|
|
|
|
host: &NetDescriptor,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(Some(host), false, api_name)
|
2019-06-22 12:02:51 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_all(&mut self) -> Result<(), PermissionDeniedError> {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
|
|
|
|
self.check_desc(None, false, None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UnaryPermission<ImportDescriptor> {
|
|
|
|
|
pub fn query(&self, host: Option<&ImportDescriptor>) -> PermissionState {
|
|
|
|
|
self.query_desc(host, AllowPartial::TreatAsPartialGranted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request(
|
2022-09-27 16:36:33 -04:00
|
|
|
|
&mut self,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
host: Option<&ImportDescriptor>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
self.request_desc(host)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self, host: Option<&ImportDescriptor>) -> PermissionState {
|
|
|
|
|
self.revoke_desc(host)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
host: &ImportDescriptor,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-25 21:50:54 -04:00
|
|
|
|
self.check_desc(Some(host), false, api_name)
|
2019-12-05 15:30:20 -05:00
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_all(&mut self) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(None, false, 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(
|
2024-03-26 19:27:40 -04:00
|
|
|
|
env.map(EnvDescriptor::new).as_ref(),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
AllowPartial::TreatAsPartialGranted,
|
|
|
|
|
)
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn request(&mut self, env: Option<&str>) -> PermissionState {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.request_desc(env.map(EnvDescriptor::new).as_ref())
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn revoke(&mut self, env: Option<&str>) -> PermissionState {
|
2024-03-26 19:27:40 -04:00
|
|
|
|
self.revoke_desc(env.map(EnvDescriptor::new).as_ref())
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
|
|
|
|
env: &str,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(Some(&EnvDescriptor::new(env)), false, api_name)
|
2021-04-13 07:25:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_all(&mut self) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(None, false, None)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
impl UnaryPermission<SysDescriptor> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
pub fn query(&self, kind: Option<&SysDescriptor>) -> PermissionState {
|
|
|
|
|
self.query_desc(kind, AllowPartial::TreatAsPartialGranted)
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 09:19:24 -04:00
|
|
|
|
pub fn request(&mut self, kind: Option<&SysDescriptor>) -> PermissionState {
|
|
|
|
|
self.request_desc(kind)
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 09:19:24 -04:00
|
|
|
|
pub fn revoke(&mut self, kind: Option<&SysDescriptor>) -> PermissionState {
|
|
|
|
|
self.revoke_desc(kind)
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
2024-09-30 09:19:24 -04:00
|
|
|
|
kind: &SysDescriptor,
|
2022-09-28 08:46:50 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.check_desc(Some(kind), false, api_name)
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
2023-08-03 07:19:19 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_all(&mut self) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(None, false, None)
|
2022-09-28 08:46:50 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl UnaryPermission<RunQueryDescriptor> {
|
|
|
|
|
pub fn query(&self, cmd: Option<&RunQueryDescriptor>) -> PermissionState {
|
|
|
|
|
self.query_desc(cmd, AllowPartial::TreatAsPartialGranted)
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn request(
|
|
|
|
|
&mut self,
|
|
|
|
|
cmd: Option<&RunQueryDescriptor>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
self.request_desc(cmd)
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn revoke(
|
|
|
|
|
&mut self,
|
|
|
|
|
cmd: Option<&RunQueryDescriptor>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
self.revoke_desc(cmd)
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
cmd: &RunQueryDescriptor,
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(Some(cmd), false, api_name)
|
2021-04-09 18:12:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_all(
|
|
|
|
|
&mut self,
|
|
|
|
|
api_name: Option<&str>,
|
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(None, false, api_name)
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
2024-09-04 08:51:24 -04:00
|
|
|
|
|
|
|
|
|
/// Queries without prompting
|
|
|
|
|
pub fn query_all(&mut self, api_name: Option<&str>) -> bool {
|
|
|
|
|
if self.is_allow_all() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
let (result, _prompted, _is_allow_all) =
|
|
|
|
|
self.query_desc(None, AllowPartial::TreatAsDenied).check2(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
RunQueryDescriptor::flag_name(),
|
2024-09-04 08:51:24 -04:00
|
|
|
|
api_name,
|
|
|
|
|
|| None,
|
|
|
|
|
/* prompt */ false,
|
|
|
|
|
);
|
|
|
|
|
result.is_ok()
|
|
|
|
|
}
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl UnaryPermission<FfiQueryDescriptor> {
|
|
|
|
|
pub fn query(&self, path: Option<&FfiQueryDescriptor>) -> PermissionState {
|
|
|
|
|
self.query_desc(path, AllowPartial::TreatAsPartialGranted)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn request(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: Option<&FfiQueryDescriptor>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
self.request_desc(path)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn revoke(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: Option<&FfiQueryDescriptor>,
|
|
|
|
|
) -> PermissionState {
|
|
|
|
|
self.revoke_desc(path)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub fn check(
|
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
path: &FfiQueryDescriptor,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(Some(path), true, api_name)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn check_partial(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: Option<&FfiQueryDescriptor>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(path, false, None)
|
2021-08-06 17:28:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_all(&mut self) -> Result<(), PermissionDeniedError> {
|
2024-03-13 16:30:48 -04:00
|
|
|
|
skip_check_if_is_permission_fully_granted!(self);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_desc(None, false, Some("all"))
|
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 {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub read: UnaryPermission<ReadQueryDescriptor>,
|
|
|
|
|
pub write: UnaryPermission<WriteQueryDescriptor>,
|
2021-03-21 08:49:58 -04:00
|
|
|
|
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>,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub run: UnaryPermission<RunQueryDescriptor>,
|
|
|
|
|
pub ffi: UnaryPermission<FfiQueryDescriptor>,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
pub import: UnaryPermission<ImportDescriptor>,
|
2024-03-13 16:30:48 -04:00
|
|
|
|
pub all: UnitPermission,
|
2021-03-17 17:45:12 -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 {
|
2024-03-13 12:07:24 -04:00
|
|
|
|
pub allow_all: bool,
|
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_net: Option<Vec<String>>,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
pub deny_net: Option<Vec<String>>,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub allow_ffi: Option<Vec<String>>,
|
|
|
|
|
pub deny_ffi: Option<Vec<String>>,
|
|
|
|
|
pub allow_read: Option<Vec<String>>,
|
|
|
|
|
pub deny_read: Option<Vec<String>>,
|
|
|
|
|
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>>,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub allow_write: Option<Vec<String>>,
|
|
|
|
|
pub deny_write: Option<Vec<String>>,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
pub allow_import: Option<Vec<String>>,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
pub prompt: bool,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum PermissionsFromOptionsError {
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
PathResolve(#[from] PathResolveError),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
SysDescriptorParse(#[from] SysDescriptorParseError),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
NetDescriptorParse(#[from] NetDescriptorParseError),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
EnvDescriptorParse(#[from] EnvDescriptorParseError),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
RunDescriptorParse(#[from] RunDescriptorParseError),
|
|
|
|
|
#[error("Empty command name not allowed in --allow-run=...")]
|
|
|
|
|
RunEmptyCommandName,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
|
impl Permissions {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn new_unary<TQuery>(
|
|
|
|
|
allow_list: Option<HashSet<TQuery::AllowDesc>>,
|
|
|
|
|
deny_list: Option<HashSet<TQuery::DenyDesc>>,
|
2021-04-11 22:15:43 -04:00
|
|
|
|
prompt: bool,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> UnaryPermission<TQuery>
|
2024-03-13 12:07:24 -04:00
|
|
|
|
where
|
2024-09-16 16:39:37 -04:00
|
|
|
|
TQuery: QueryDescriptor,
|
2024-03-13 12:07:24 -04:00
|
|
|
|
{
|
2024-11-04 12:17:21 -05:00
|
|
|
|
UnaryPermission::<TQuery> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
granted_global: global_from_option(allow_list.as_ref()),
|
|
|
|
|
granted_list: allow_list.unwrap_or_default(),
|
|
|
|
|
flag_denied_global: global_from_option(deny_list.as_ref()),
|
|
|
|
|
flag_denied_list: deny_list.unwrap_or_default(),
|
2021-08-06 17:28:10 -04:00
|
|
|
|
prompt,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
..Default::default()
|
2024-11-04 12:17:21 -05:00
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
pub const fn new_all(allow_state: bool) -> UnitPermission {
|
|
|
|
|
unit_permission_from_flag_bools(
|
|
|
|
|
allow_state,
|
|
|
|
|
false,
|
|
|
|
|
"all",
|
|
|
|
|
"all",
|
|
|
|
|
false, // never prompt for all
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn from_options(
|
|
|
|
|
parser: &dyn PermissionDescriptorParser,
|
|
|
|
|
opts: &PermissionsOptions,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<Self, PermissionsFromOptionsError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn resolve_allow_run(
|
|
|
|
|
parser: &dyn PermissionDescriptorParser,
|
|
|
|
|
allow_run: &[String],
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<HashSet<AllowRunDescriptor>, PermissionsFromOptionsError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut new_allow_run = HashSet::with_capacity(allow_run.len());
|
|
|
|
|
for unresolved in allow_run {
|
|
|
|
|
if unresolved.is_empty() {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
return Err(PermissionsFromOptionsError::RunEmptyCommandName);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
}
|
|
|
|
|
match parser.parse_allow_run_descriptor(unresolved)? {
|
|
|
|
|
AllowRunDescriptorParseResult::Descriptor(descriptor) => {
|
|
|
|
|
new_allow_run.insert(descriptor);
|
|
|
|
|
}
|
|
|
|
|
AllowRunDescriptorParseResult::Unresolved(err) => {
|
|
|
|
|
log::info!(
|
|
|
|
|
"{} Failed to resolve '{}' for allow-run: {}",
|
|
|
|
|
colors::gray("Info"),
|
|
|
|
|
unresolved,
|
|
|
|
|
err
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(new_allow_run)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn parse_maybe_vec<T: Eq + PartialEq + Hash, E>(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
items: Option<&[String]>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
parse: impl Fn(&str) -> Result<T, E>,
|
|
|
|
|
) -> Result<Option<HashSet<T>>, PermissionsFromOptionsError>
|
|
|
|
|
where
|
|
|
|
|
PermissionsFromOptionsError: From<E>,
|
|
|
|
|
{
|
2024-09-16 16:39:37 -04:00
|
|
|
|
match items {
|
|
|
|
|
Some(items) => Ok(Some(
|
|
|
|
|
items
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|item| parse(item))
|
|
|
|
|
.collect::<Result<HashSet<_>, _>>()?,
|
|
|
|
|
)),
|
|
|
|
|
None => Ok(None),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut deny_write = parse_maybe_vec(opts.deny_write.as_deref(), |item| {
|
|
|
|
|
parser.parse_write_descriptor(item)
|
|
|
|
|
})?;
|
|
|
|
|
let allow_run = opts
|
|
|
|
|
.allow_run
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|raw_allow_run| {
|
|
|
|
|
match resolve_allow_run(parser, raw_allow_run) {
|
|
|
|
|
Ok(resolved_allow_run) => {
|
|
|
|
|
if resolved_allow_run.is_empty() && !raw_allow_run.is_empty() {
|
|
|
|
|
None // convert to no permissions if now empty
|
|
|
|
|
} else {
|
|
|
|
|
Some(Ok(resolved_allow_run))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(err) => Some(Err(err)),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.transpose()?;
|
|
|
|
|
// add the allow_run list to deny_write
|
|
|
|
|
if let Some(allow_run_vec) = &allow_run {
|
|
|
|
|
if !allow_run_vec.is_empty() {
|
|
|
|
|
let deny_write = deny_write.get_or_insert_with(Default::default);
|
|
|
|
|
deny_write.extend(
|
|
|
|
|
allow_run_vec
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|item| WriteDescriptor(item.0.clone())),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-10 15:13:53 -04:00
|
|
|
|
Ok(Self {
|
2024-03-13 12:07:24 -04:00
|
|
|
|
read: Permissions::new_unary(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
parse_maybe_vec(opts.allow_read.as_deref(), |item| {
|
|
|
|
|
parser.parse_read_descriptor(item)
|
|
|
|
|
})?,
|
|
|
|
|
parse_maybe_vec(opts.deny_read.as_deref(), |item| {
|
|
|
|
|
parser.parse_read_descriptor(item)
|
|
|
|
|
})?,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
opts.prompt,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
),
|
2024-03-13 12:07:24 -04:00
|
|
|
|
write: Permissions::new_unary(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
parse_maybe_vec(opts.allow_write.as_deref(), |item| {
|
|
|
|
|
parser.parse_write_descriptor(item)
|
|
|
|
|
})?,
|
|
|
|
|
deny_write,
|
2023-08-03 07:19:19 -04:00
|
|
|
|
opts.prompt,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
),
|
2024-03-13 12:07:24 -04:00
|
|
|
|
net: Permissions::new_unary(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
parse_maybe_vec(opts.allow_net.as_deref(), |item| {
|
|
|
|
|
parser.parse_net_descriptor(item)
|
|
|
|
|
})?,
|
|
|
|
|
parse_maybe_vec(opts.deny_net.as_deref(), |item| {
|
|
|
|
|
parser.parse_net_descriptor(item)
|
|
|
|
|
})?,
|
2024-03-13 12:07:24 -04:00
|
|
|
|
opts.prompt,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
),
|
2024-03-13 12:07:24 -04:00
|
|
|
|
env: Permissions::new_unary(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
parse_maybe_vec(opts.allow_env.as_deref(), |item| {
|
|
|
|
|
parser.parse_env_descriptor(item)
|
|
|
|
|
})?,
|
|
|
|
|
parse_maybe_vec(opts.deny_env.as_deref(), |text| {
|
|
|
|
|
parser.parse_env_descriptor(text)
|
|
|
|
|
})?,
|
2024-03-13 12:07:24 -04:00
|
|
|
|
opts.prompt,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
),
|
2024-03-13 12:07:24 -04:00
|
|
|
|
sys: Permissions::new_unary(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
parse_maybe_vec(opts.allow_sys.as_deref(), |text| {
|
|
|
|
|
parser.parse_sys_descriptor(text)
|
|
|
|
|
})?,
|
|
|
|
|
parse_maybe_vec(opts.deny_sys.as_deref(), |text| {
|
|
|
|
|
parser.parse_sys_descriptor(text)
|
|
|
|
|
})?,
|
2024-03-13 12:07:24 -04:00
|
|
|
|
opts.prompt,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
),
|
2024-03-13 12:07:24 -04:00
|
|
|
|
run: Permissions::new_unary(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
allow_run,
|
|
|
|
|
parse_maybe_vec(opts.deny_run.as_deref(), |text| {
|
|
|
|
|
parser.parse_deny_run_descriptor(text)
|
|
|
|
|
})?,
|
2024-03-13 12:07:24 -04:00
|
|
|
|
opts.prompt,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
),
|
2024-03-13 12:07:24 -04:00
|
|
|
|
ffi: Permissions::new_unary(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
parse_maybe_vec(opts.allow_ffi.as_deref(), |text| {
|
|
|
|
|
parser.parse_ffi_descriptor(text)
|
|
|
|
|
})?,
|
|
|
|
|
parse_maybe_vec(opts.deny_ffi.as_deref(), |text| {
|
|
|
|
|
parser.parse_ffi_descriptor(text)
|
|
|
|
|
})?,
|
2024-03-13 12:07:24 -04:00
|
|
|
|
opts.prompt,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
),
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: Permissions::new_unary(
|
|
|
|
|
parse_maybe_vec(opts.allow_import.as_deref(), |item| {
|
|
|
|
|
parser.parse_import_descriptor(item)
|
|
|
|
|
})?,
|
|
|
|
|
None,
|
|
|
|
|
opts.prompt,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
),
|
2024-03-13 16:30:48 -04:00
|
|
|
|
all: Permissions::new_all(opts.allow_all),
|
2022-08-10 15:13:53 -04:00
|
|
|
|
})
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
/// Create a set of permissions that explicitly allow everything.
|
2021-03-17 17:45:12 -04:00
|
|
|
|
pub fn allow_all() -> Self {
|
|
|
|
|
Self {
|
2024-03-13 12:07:24 -04:00
|
|
|
|
read: UnaryPermission::allow_all(),
|
|
|
|
|
write: UnaryPermission::allow_all(),
|
|
|
|
|
net: UnaryPermission::allow_all(),
|
|
|
|
|
env: UnaryPermission::allow_all(),
|
|
|
|
|
sys: UnaryPermission::allow_all(),
|
|
|
|
|
run: UnaryPermission::allow_all(),
|
|
|
|
|
ffi: UnaryPermission::allow_all(),
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: UnaryPermission::allow_all(),
|
2024-03-13 16:30:48 -04:00
|
|
|
|
all: Permissions::new_all(true),
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 16:30:48 -04:00
|
|
|
|
/// Create a set of permissions that enable nothing, but will allow prompting.
|
|
|
|
|
pub fn none_with_prompt() -> Self {
|
|
|
|
|
Self::none(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a set of permissions that enable nothing, and will not allow prompting.
|
|
|
|
|
pub fn none_without_prompt() -> Self {
|
|
|
|
|
Self::none(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn none(prompt: bool) -> Self {
|
|
|
|
|
Self {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
read: Permissions::new_unary(None, None, prompt),
|
|
|
|
|
write: Permissions::new_unary(None, None, prompt),
|
|
|
|
|
net: Permissions::new_unary(None, None, prompt),
|
|
|
|
|
env: Permissions::new_unary(None, None, prompt),
|
|
|
|
|
sys: Permissions::new_unary(None, None, prompt),
|
|
|
|
|
run: Permissions::new_unary(None, None, prompt),
|
|
|
|
|
ffi: Permissions::new_unary(None, None, prompt),
|
|
|
|
|
import: Permissions::new_unary(None, None, prompt),
|
2024-03-13 16:30:48 -04:00
|
|
|
|
all: Permissions::new_all(false),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 17:45:12 -04:00
|
|
|
|
}
|
2020-10-26 15:56:00 -04:00
|
|
|
|
|
2024-09-25 21:50:54 -04:00
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
|
|
|
|
pub enum CheckSpecifierKind {
|
|
|
|
|
Static,
|
|
|
|
|
Dynamic,
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum ChildPermissionError {
|
|
|
|
|
#[error("Can't escalate parent thread permissions")]
|
|
|
|
|
Escalation,
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
PathResolve(#[from] PathResolveError),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
NetDescriptorParse(#[from] NetDescriptorParseError),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
EnvDescriptorParse(#[from] EnvDescriptorParseError),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
SysDescriptorParse(#[from] SysDescriptorParseError),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
RunDescriptorParse(#[from] RunDescriptorParseError),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum PermissionCheckError {
|
|
|
|
|
#[error(transparent)]
|
|
|
|
|
PermissionDenied(#[from] PermissionDeniedError),
|
|
|
|
|
#[error("Invalid file path.\n Specifier: {0}")]
|
|
|
|
|
InvalidFilePath(Url),
|
|
|
|
|
#[error(transparent)]
|
|
|
|
|
NetDescriptorForUrlParse(#[from] NetDescriptorFromUrlParseError),
|
|
|
|
|
#[error(transparent)]
|
|
|
|
|
SysDescriptorParse(#[from] SysDescriptorParseError),
|
|
|
|
|
#[error(transparent)]
|
|
|
|
|
PathResolve(#[from] PathResolveError),
|
|
|
|
|
#[error(transparent)]
|
|
|
|
|
HostParse(#[from] HostParseError),
|
|
|
|
|
}
|
|
|
|
|
|
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)]
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub struct PermissionsContainer {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
descriptor_parser: Arc<dyn PermissionDescriptorParser>,
|
|
|
|
|
inner: Arc<Mutex<Permissions>>,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
}
|
2023-01-07 11:25:34 -05:00
|
|
|
|
|
|
|
|
|
impl PermissionsContainer {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn new(
|
|
|
|
|
descriptor_parser: Arc<dyn PermissionDescriptorParser>,
|
|
|
|
|
perms: Permissions,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
descriptor_parser,
|
|
|
|
|
inner: Arc::new(Mutex::new(perms)),
|
|
|
|
|
}
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
pub fn allow_all(
|
|
|
|
|
descriptor_parser: Arc<dyn PermissionDescriptorParser>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Self::new(descriptor_parser, Permissions::allow_all())
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 09:19:24 -04:00
|
|
|
|
pub fn create_child_permissions(
|
|
|
|
|
&self,
|
|
|
|
|
child_permissions_arg: ChildPermissionsArg,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionsContainer, ChildPermissionError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
fn is_granted_unary(arg: &ChildUnaryPermissionArg) -> bool {
|
|
|
|
|
match arg {
|
|
|
|
|
ChildUnaryPermissionArg::Inherit | ChildUnaryPermissionArg::Granted => {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
ChildUnaryPermissionArg::NotGranted
|
|
|
|
|
| ChildUnaryPermissionArg::GrantedList(_) => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut worker_perms = Permissions::none_without_prompt();
|
|
|
|
|
|
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
worker_perms.all = inner
|
|
|
|
|
.all
|
|
|
|
|
.create_child_permissions(ChildUnitPermissionArg::Inherit)?;
|
|
|
|
|
|
|
|
|
|
// downgrade the `worker_perms.all` based on the other values
|
|
|
|
|
if worker_perms.all.query() == PermissionState::Granted {
|
|
|
|
|
let unary_perms = [
|
|
|
|
|
&child_permissions_arg.read,
|
|
|
|
|
&child_permissions_arg.write,
|
|
|
|
|
&child_permissions_arg.net,
|
|
|
|
|
&child_permissions_arg.import,
|
|
|
|
|
&child_permissions_arg.env,
|
|
|
|
|
&child_permissions_arg.sys,
|
|
|
|
|
&child_permissions_arg.run,
|
|
|
|
|
&child_permissions_arg.ffi,
|
|
|
|
|
];
|
|
|
|
|
let allow_all = unary_perms.into_iter().all(is_granted_unary);
|
|
|
|
|
if !allow_all {
|
|
|
|
|
worker_perms.all.revoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WARNING: When adding a permission here, ensure it is handled
|
|
|
|
|
// in the worker_perms.all block above
|
2024-11-04 12:17:21 -05:00
|
|
|
|
worker_perms.read = inner.read.create_child_permissions(
|
|
|
|
|
child_permissions_arg.read,
|
|
|
|
|
|text| {
|
|
|
|
|
Ok::<_, PathResolveError>(Some(
|
|
|
|
|
self.descriptor_parser.parse_read_descriptor(text)?,
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
)?;
|
|
|
|
|
worker_perms.write = inner.write.create_child_permissions(
|
|
|
|
|
child_permissions_arg.write,
|
|
|
|
|
|text| {
|
|
|
|
|
Ok::<_, PathResolveError>(Some(
|
|
|
|
|
self.descriptor_parser.parse_write_descriptor(text)?,
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
)?;
|
|
|
|
|
worker_perms.import = inner.import.create_child_permissions(
|
|
|
|
|
child_permissions_arg.import,
|
|
|
|
|
|text| {
|
|
|
|
|
Ok::<_, NetDescriptorParseError>(Some(
|
|
|
|
|
self.descriptor_parser.parse_import_descriptor(text)?,
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
)?;
|
|
|
|
|
worker_perms.net = inner.net.create_child_permissions(
|
|
|
|
|
child_permissions_arg.net,
|
|
|
|
|
|text| {
|
|
|
|
|
Ok::<_, NetDescriptorParseError>(Some(
|
|
|
|
|
self.descriptor_parser.parse_net_descriptor(text)?,
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
)?;
|
|
|
|
|
worker_perms.env = inner.env.create_child_permissions(
|
|
|
|
|
child_permissions_arg.env,
|
|
|
|
|
|text| {
|
|
|
|
|
Ok::<_, EnvDescriptorParseError>(Some(
|
|
|
|
|
self.descriptor_parser.parse_env_descriptor(text)?,
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
)?;
|
|
|
|
|
worker_perms.sys = inner.sys.create_child_permissions(
|
|
|
|
|
child_permissions_arg.sys,
|
|
|
|
|
|text| {
|
|
|
|
|
Ok::<_, SysDescriptorParseError>(Some(
|
|
|
|
|
self.descriptor_parser.parse_sys_descriptor(text)?,
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
)?;
|
2024-09-30 09:19:24 -04:00
|
|
|
|
worker_perms.run = inner.run.create_child_permissions(
|
|
|
|
|
child_permissions_arg.run,
|
|
|
|
|
|text| match self.descriptor_parser.parse_allow_run_descriptor(text)? {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
AllowRunDescriptorParseResult::Unresolved(_) => {
|
|
|
|
|
Ok::<_, RunDescriptorParseError>(None)
|
|
|
|
|
}
|
2024-09-30 09:19:24 -04:00
|
|
|
|
AllowRunDescriptorParseResult::Descriptor(desc) => Ok(Some(desc)),
|
|
|
|
|
},
|
|
|
|
|
)?;
|
2024-11-04 12:17:21 -05:00
|
|
|
|
worker_perms.ffi = inner.ffi.create_child_permissions(
|
|
|
|
|
child_permissions_arg.ffi,
|
|
|
|
|
|text| {
|
|
|
|
|
Ok::<_, PathResolveError>(Some(
|
|
|
|
|
self.descriptor_parser.parse_ffi_descriptor(text)?,
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
)?;
|
2024-09-30 09:19:24 -04:00
|
|
|
|
|
|
|
|
|
Ok(PermissionsContainer::new(
|
|
|
|
|
self.descriptor_parser.clone(),
|
|
|
|
|
worker_perms,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_specifier(
|
|
|
|
|
&self,
|
|
|
|
|
specifier: &ModuleSpecifier,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
kind: CheckSpecifierKind,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
match specifier.scheme() {
|
|
|
|
|
"file" => {
|
|
|
|
|
if inner.read.is_allow_all() || kind == CheckSpecifierKind::Static {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 07:55:01 -04:00
|
|
|
|
match url_to_file_path(specifier) {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok(path) => inner
|
|
|
|
|
.read
|
|
|
|
|
.check(
|
|
|
|
|
&PathQueryDescriptor {
|
|
|
|
|
requested: path.to_string_lossy().into_owned(),
|
|
|
|
|
resolved: path,
|
|
|
|
|
}
|
|
|
|
|
.into_read(),
|
|
|
|
|
Some("import()"),
|
|
|
|
|
)
|
|
|
|
|
.map_err(PermissionCheckError::PermissionDenied),
|
|
|
|
|
Err(_) => {
|
|
|
|
|
Err(PermissionCheckError::InvalidFilePath(specifier.clone()))
|
|
|
|
|
}
|
2024-09-25 21:50:54 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
"data" => Ok(()),
|
|
|
|
|
"blob" => Ok(()),
|
|
|
|
|
_ => {
|
|
|
|
|
if inner.import.is_allow_all() {
|
|
|
|
|
return Ok(()); // avoid allocation below
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let desc = self
|
|
|
|
|
.descriptor_parser
|
|
|
|
|
.parse_import_descriptor_from_url(specifier)?;
|
|
|
|
|
inner.import.check(&desc, Some("import()"))?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_read(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&self,
|
|
|
|
|
path: &str,
|
2023-01-07 11:25:34 -05:00
|
|
|
|
api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_read_with_api_name(path, Some(api_name))
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
2024-03-12 13:42:26 -04:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_read_with_api_name(
|
|
|
|
|
&self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
path: &str,
|
2024-03-12 13:42:26 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.read;
|
|
|
|
|
if inner.is_allow_all() {
|
|
|
|
|
Ok(PathBuf::from(path))
|
|
|
|
|
} else {
|
|
|
|
|
let desc = self.descriptor_parser.parse_path_query(path)?.into_read();
|
|
|
|
|
inner.check(&desc, api_name)?;
|
|
|
|
|
Ok(desc.0.resolved)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_read_path<'a>(
|
|
|
|
|
&self,
|
|
|
|
|
path: &'a Path,
|
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<Cow<'a, Path>, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.read;
|
|
|
|
|
if inner.is_allow_all() {
|
|
|
|
|
Ok(Cow::Borrowed(path))
|
|
|
|
|
} else {
|
|
|
|
|
let desc = PathQueryDescriptor {
|
|
|
|
|
requested: path.to_string_lossy().into_owned(),
|
|
|
|
|
resolved: path.to_path_buf(),
|
|
|
|
|
}
|
|
|
|
|
.into_read();
|
|
|
|
|
inner.check(&desc, api_name)?;
|
|
|
|
|
Ok(Cow::Owned(desc.0.resolved))
|
|
|
|
|
}
|
2024-03-12 13:42:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
/// As `check_read()`, but permission error messages will anonymize the path
|
|
|
|
|
/// by replacing it with the given `display`.
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_read_blind(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
display: &str,
|
|
|
|
|
api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.read;
|
|
|
|
|
skip_check_if_is_permission_fully_granted!(inner);
|
|
|
|
|
inner.check(
|
|
|
|
|
&PathQueryDescriptor {
|
|
|
|
|
requested: format!("<{}>", display),
|
|
|
|
|
resolved: path.to_path_buf(),
|
|
|
|
|
}
|
|
|
|
|
.into_read(),
|
|
|
|
|
Some(api_name),
|
2024-11-04 12:17:21 -05:00
|
|
|
|
)?;
|
|
|
|
|
Ok(())
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_read_all(
|
|
|
|
|
&self,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
|
|
|
|
self.inner.lock().read.check_all(Some(api_name))?;
|
|
|
|
|
Ok(())
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-04 15:55:41 -04:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn query_read_all(&self) -> bool {
|
|
|
|
|
self.inner.lock().read.query(None) == PermissionState::Granted
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_write(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&self,
|
|
|
|
|
path: &str,
|
2023-01-07 11:25:34 -05:00
|
|
|
|
api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.check_write_with_api_name(path, Some(api_name))
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
2024-03-12 13:42:26 -04:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_write_with_api_name(
|
|
|
|
|
&self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
path: &str,
|
2024-03-12 13:42:26 -04:00
|
|
|
|
api_name: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.write;
|
|
|
|
|
if inner.is_allow_all() {
|
|
|
|
|
Ok(PathBuf::from(path))
|
|
|
|
|
} else {
|
|
|
|
|
let desc = self.descriptor_parser.parse_path_query(path)?.into_write();
|
|
|
|
|
inner.check(&desc, api_name)?;
|
|
|
|
|
Ok(desc.0.resolved)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_write_path<'a>(
|
|
|
|
|
&self,
|
|
|
|
|
path: &'a Path,
|
|
|
|
|
api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<Cow<'a, Path>, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.write;
|
|
|
|
|
if inner.is_allow_all() {
|
|
|
|
|
Ok(Cow::Borrowed(path))
|
|
|
|
|
} else {
|
|
|
|
|
let desc = PathQueryDescriptor {
|
|
|
|
|
requested: path.to_string_lossy().into_owned(),
|
|
|
|
|
resolved: path.to_path_buf(),
|
|
|
|
|
}
|
|
|
|
|
.into_write();
|
|
|
|
|
inner.check(&desc, Some(api_name))?;
|
|
|
|
|
Ok(Cow::Owned(desc.0.resolved))
|
|
|
|
|
}
|
2024-03-12 13:42:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_write_all(
|
|
|
|
|
&self,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
|
|
|
|
self.inner.lock().write.check_all(Some(api_name))?;
|
|
|
|
|
Ok(())
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
/// As `check_write()`, but permission error messages will anonymize the path
|
|
|
|
|
/// by replacing it with the given `display`.
|
2023-04-12 09:13:32 -04:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_write_blind(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&self,
|
2023-04-12 09:13:32 -04:00
|
|
|
|
path: &Path,
|
|
|
|
|
display: &str,
|
|
|
|
|
api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.write;
|
|
|
|
|
skip_check_if_is_permission_fully_granted!(inner);
|
|
|
|
|
inner.check(
|
|
|
|
|
&PathQueryDescriptor {
|
|
|
|
|
requested: format!("<{}>", display),
|
|
|
|
|
resolved: path.to_path_buf(),
|
|
|
|
|
}
|
|
|
|
|
.into_write(),
|
|
|
|
|
Some(api_name),
|
2024-11-04 12:17:21 -05:00
|
|
|
|
)?;
|
|
|
|
|
Ok(())
|
2023-04-12 09:13:32 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-12 13:42:26 -04:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_write_partial(
|
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
path: &str,
|
2024-03-12 13:42:26 -04:00
|
|
|
|
api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.write;
|
|
|
|
|
if inner.is_allow_all() {
|
|
|
|
|
Ok(PathBuf::from(path))
|
|
|
|
|
} else {
|
|
|
|
|
let desc = self.descriptor_parser.parse_path_query(path)?.into_write();
|
|
|
|
|
inner.check_partial(&desc, Some(api_name))?;
|
|
|
|
|
Ok(desc.0.resolved)
|
|
|
|
|
}
|
2024-03-12 13:42:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_run(
|
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
cmd: &RunQueryDescriptor,
|
2023-01-07 11:25:34 -05:00
|
|
|
|
api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
|
|
|
|
self.inner.lock().run.check(cmd, Some(api_name))?;
|
|
|
|
|
Ok(())
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_run_all(
|
|
|
|
|
&mut self,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
|
|
|
|
self.inner.lock().run.check_all(Some(api_name))?;
|
|
|
|
|
Ok(())
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 08:51:24 -04:00
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn query_run_all(&mut self, api_name: &str) -> bool {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
self.inner.lock().run.query_all(Some(api_name))
|
2024-09-04 08:51:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_sys(
|
|
|
|
|
&self,
|
|
|
|
|
kind: &str,
|
|
|
|
|
api_name: &str,
|
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.inner.lock().sys.check(
|
|
|
|
|
&self.descriptor_parser.parse_sys_descriptor(kind)?,
|
|
|
|
|
Some(api_name),
|
2024-11-04 12:17:21 -05:00
|
|
|
|
)?;
|
|
|
|
|
Ok(())
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_env(&mut self, var: &str) -> Result<(), PermissionCheckError> {
|
|
|
|
|
self.inner.lock().env.check(var, None)?;
|
|
|
|
|
Ok(())
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_env_all(&mut self) -> Result<(), PermissionCheckError> {
|
|
|
|
|
self.inner.lock().env.check_all()?;
|
|
|
|
|
Ok(())
|
2023-01-07 11:25:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 20:12:03 -04:00
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_sys_all(&mut self) -> Result<(), PermissionCheckError> {
|
|
|
|
|
self.inner.lock().sys.check_all()?;
|
|
|
|
|
Ok(())
|
2024-04-19 20:12:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_ffi_all(&mut self) -> Result<(), PermissionCheckError> {
|
|
|
|
|
self.inner.lock().ffi.check_all()?;
|
|
|
|
|
Ok(())
|
2024-04-19 20:12:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This checks to see if the allow-all flag was passed, not whether all
|
|
|
|
|
/// permissions are enabled!
|
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_was_allow_all_flag_passed(
|
|
|
|
|
&mut self,
|
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
|
|
|
|
self.inner.lock().all.check()?;
|
|
|
|
|
Ok(())
|
2024-04-19 20:12:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Checks special file access, returning the failed permission type if
|
|
|
|
|
/// not successful.
|
|
|
|
|
pub fn check_special_file(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &Path,
|
|
|
|
|
_api_name: &str,
|
|
|
|
|
) -> Result<(), &'static str> {
|
|
|
|
|
let error_all = |_| "all";
|
|
|
|
|
|
|
|
|
|
// Safe files with no major additional side-effects. While there's a small risk of someone
|
|
|
|
|
// draining system entropy by just reading one of these files constantly, that's not really
|
|
|
|
|
// something we worry about as they already have --allow-read to /dev.
|
|
|
|
|
if cfg!(unix)
|
|
|
|
|
&& (path == OsStr::new("/dev/random")
|
|
|
|
|
|| path == OsStr::new("/dev/urandom")
|
|
|
|
|
|| path == OsStr::new("/dev/zero")
|
|
|
|
|
|| path == OsStr::new("/dev/null"))
|
|
|
|
|
{
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 13:21:36 -04:00
|
|
|
|
/// We'll allow opening /proc/self/fd/{n} without additional permissions under the following conditions:
|
|
|
|
|
///
|
|
|
|
|
/// 1. n > 2. This allows for opening bash-style redirections, but not stdio
|
|
|
|
|
/// 2. the fd referred to by n is a pipe
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
fn is_fd_file_is_pipe(path: &Path) -> bool {
|
|
|
|
|
if let Some(fd) = path.file_name() {
|
|
|
|
|
if let Ok(s) = std::str::from_utf8(fd.as_encoded_bytes()) {
|
|
|
|
|
if let Ok(n) = s.parse::<i32>() {
|
|
|
|
|
if n > 2 {
|
|
|
|
|
// SAFETY: This is proper use of the stat syscall
|
|
|
|
|
unsafe {
|
|
|
|
|
let mut stat = std::mem::zeroed::<libc::stat>();
|
|
|
|
|
if libc::fstat(n, &mut stat as _) == 0
|
|
|
|
|
&& ((stat.st_mode & libc::S_IFMT) & libc::S_IFIFO) != 0
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// On unixy systems, we allow opening /dev/fd/XXX for valid FDs that
|
|
|
|
|
// are pipes.
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
if path.starts_with("/dev/fd") && is_fd_file_is_pipe(path) {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 20:12:03 -04:00
|
|
|
|
if cfg!(target_os = "linux") {
|
2024-05-10 13:21:36 -04:00
|
|
|
|
// On Linux, we also allow opening /proc/self/fd/XXX for valid FDs that
|
|
|
|
|
// are pipes.
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
|
if path.starts_with("/proc/self/fd") && is_fd_file_is_pipe(path) {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2024-04-19 20:12:03 -04:00
|
|
|
|
if path.starts_with("/dev")
|
|
|
|
|
|| path.starts_with("/proc")
|
|
|
|
|
|| path.starts_with("/sys")
|
|
|
|
|
{
|
|
|
|
|
if path.ends_with("/environ") {
|
|
|
|
|
self.check_env_all().map_err(|_| "env")?;
|
|
|
|
|
} else {
|
|
|
|
|
self.check_was_allow_all_flag_passed().map_err(error_all)?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if cfg!(unix) {
|
|
|
|
|
if path.starts_with("/dev") {
|
|
|
|
|
self.check_was_allow_all_flag_passed().map_err(error_all)?;
|
|
|
|
|
}
|
|
|
|
|
} else if cfg!(target_os = "windows") {
|
2024-05-08 16:39:06 -04:00
|
|
|
|
// \\.\nul is allowed
|
|
|
|
|
let s = path.as_os_str().as_encoded_bytes();
|
|
|
|
|
if s.eq_ignore_ascii_case(br#"\\.\nul"#) {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 20:12:03 -04:00
|
|
|
|
fn is_normalized_windows_drive_path(path: &Path) -> bool {
|
|
|
|
|
let s = path.as_os_str().as_encoded_bytes();
|
|
|
|
|
// \\?\X:\
|
|
|
|
|
if s.len() < 7 {
|
|
|
|
|
false
|
|
|
|
|
} else if s.starts_with(br#"\\?\"#) {
|
|
|
|
|
s[4].is_ascii_alphabetic() && s[5] == b':' && s[6] == b'\\'
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If this is a normalized drive path, accept it
|
|
|
|
|
if !is_normalized_windows_drive_path(path) {
|
|
|
|
|
self.check_was_allow_all_flag_passed().map_err(error_all)?;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-27 09:42:20 -04:00
|
|
|
|
#[inline(always)]
|
2024-03-12 13:42:26 -04:00
|
|
|
|
pub fn check_net_url(
|
2023-05-27 09:42:20 -04:00
|
|
|
|
&mut self,
|
|
|
|
|
url: &Url,
|
|
|
|
|
api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
if inner.net.is_allow_all() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
let desc = self.descriptor_parser.parse_net_descriptor_from_url(url)?;
|
2024-11-04 12:17:21 -05:00
|
|
|
|
inner.net.check(&desc, Some(api_name))?;
|
|
|
|
|
Ok(())
|
2023-05-27 09:42:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
2024-03-12 13:42:26 -04:00
|
|
|
|
pub fn check_net<T: AsRef<str>>(
|
2021-06-28 19:43:03 -04:00
|
|
|
|
&mut self,
|
|
|
|
|
host: &(T, Option<u16>),
|
2022-09-27 16:36:33 -04:00
|
|
|
|
api_name: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.net;
|
|
|
|
|
skip_check_if_is_permission_fully_granted!(inner);
|
2024-09-06 05:28:53 -04:00
|
|
|
|
let hostname = Host::parse(host.0.as_ref())?;
|
2024-07-05 18:45:06 -04:00
|
|
|
|
let descriptor = NetDescriptor(hostname, host.1);
|
2024-11-04 12:17:21 -05:00
|
|
|
|
inner.check(&descriptor, Some(api_name))?;
|
|
|
|
|
Ok(())
|
2021-06-28 19:43:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_ffi(
|
|
|
|
|
&mut self,
|
|
|
|
|
path: &str,
|
|
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.ffi;
|
|
|
|
|
if inner.is_allow_all() {
|
|
|
|
|
Ok(PathBuf::from(path))
|
|
|
|
|
} else {
|
|
|
|
|
let desc = self.descriptor_parser.parse_path_query(path)?.into_ffi();
|
|
|
|
|
inner.check(&desc, None)?;
|
|
|
|
|
Ok(desc.0.resolved)
|
|
|
|
|
}
|
2022-10-05 10:06:44 -04:00
|
|
|
|
}
|
2021-08-06 17:28:10 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
2023-03-22 00:13:24 -04:00
|
|
|
|
#[inline(always)]
|
2024-11-04 12:17:21 -05:00
|
|
|
|
pub fn check_ffi_partial_no_path(
|
|
|
|
|
&mut self,
|
|
|
|
|
) -> Result<(), PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.ffi;
|
2024-11-04 12:17:21 -05:00
|
|
|
|
if !inner.is_allow_all() {
|
|
|
|
|
inner.check_partial(None)?;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
}
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok(())
|
2024-09-16 16:39:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn check_ffi_partial_with_path(
|
2023-08-22 01:56:00 -04:00
|
|
|
|
&mut self,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
path: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PathBuf, PermissionCheckError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let mut inner = self.inner.lock();
|
|
|
|
|
let inner = &mut inner.ffi;
|
|
|
|
|
if inner.is_allow_all() {
|
|
|
|
|
Ok(PathBuf::from(path))
|
|
|
|
|
} else {
|
|
|
|
|
let desc = self.descriptor_parser.parse_path_query(path)?.into_ffi();
|
|
|
|
|
inner.check_partial(Some(&desc))?;
|
|
|
|
|
Ok(desc.0.resolved)
|
|
|
|
|
}
|
2023-08-22 01:56:00 -04:00
|
|
|
|
}
|
2024-09-30 09:19:24 -04:00
|
|
|
|
|
|
|
|
|
// query
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn query_read(
|
|
|
|
|
&self,
|
|
|
|
|
path: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, PathResolveError> {
|
2024-10-04 15:55:41 -04:00
|
|
|
|
let inner = self.inner.lock();
|
|
|
|
|
let permission = &inner.read;
|
|
|
|
|
if permission.is_allow_all() {
|
|
|
|
|
return Ok(PermissionState::Granted);
|
|
|
|
|
}
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
2024-10-04 15:55:41 -04:00
|
|
|
|
permission.query(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
path
|
|
|
|
|
.map(|path| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok::<_, PathResolveError>(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.descriptor_parser.parse_path_query(path)?.into_read(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn query_write(
|
|
|
|
|
&self,
|
|
|
|
|
path: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, PathResolveError> {
|
2024-10-04 15:55:41 -04:00
|
|
|
|
let inner = self.inner.lock();
|
|
|
|
|
let permission = &inner.write;
|
|
|
|
|
if permission.is_allow_all() {
|
|
|
|
|
return Ok(PermissionState::Granted);
|
|
|
|
|
}
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
2024-10-04 15:55:41 -04:00
|
|
|
|
permission.query(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
path
|
|
|
|
|
.map(|path| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok::<_, PathResolveError>(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.descriptor_parser.parse_path_query(path)?.into_write(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn query_net(
|
|
|
|
|
&self,
|
|
|
|
|
host: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, NetDescriptorParseError> {
|
2024-10-04 15:55:41 -04:00
|
|
|
|
let inner = self.inner.lock();
|
|
|
|
|
let permission = &inner.net;
|
|
|
|
|
if permission.is_allow_all() {
|
|
|
|
|
return Ok(PermissionState::Granted);
|
|
|
|
|
}
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
2024-10-04 15:55:41 -04:00
|
|
|
|
permission.query(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
match host {
|
|
|
|
|
None => None,
|
|
|
|
|
Some(h) => Some(self.descriptor_parser.parse_net_descriptor(h)?),
|
|
|
|
|
}
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn query_env(&self, var: Option<&str>) -> PermissionState {
|
2024-10-04 15:55:41 -04:00
|
|
|
|
let inner = self.inner.lock();
|
|
|
|
|
let permission = &inner.env;
|
|
|
|
|
if permission.is_allow_all() {
|
|
|
|
|
return PermissionState::Granted;
|
|
|
|
|
}
|
|
|
|
|
permission.query(var)
|
2024-09-30 09:19:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn query_sys(
|
|
|
|
|
&self,
|
|
|
|
|
kind: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, SysDescriptorParseError> {
|
2024-10-04 15:55:41 -04:00
|
|
|
|
let inner = self.inner.lock();
|
|
|
|
|
let permission = &inner.sys;
|
|
|
|
|
if permission.is_allow_all() {
|
|
|
|
|
return Ok(PermissionState::Granted);
|
|
|
|
|
}
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
2024-10-04 15:55:41 -04:00
|
|
|
|
permission.query(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
kind
|
|
|
|
|
.map(|kind| self.descriptor_parser.parse_sys_descriptor(kind))
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn query_run(
|
|
|
|
|
&self,
|
|
|
|
|
cmd: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, RunDescriptorParseError> {
|
2024-10-04 15:55:41 -04:00
|
|
|
|
let inner = self.inner.lock();
|
|
|
|
|
let permission = &inner.run;
|
|
|
|
|
if permission.is_allow_all() {
|
|
|
|
|
return Ok(PermissionState::Granted);
|
|
|
|
|
}
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
2024-10-04 15:55:41 -04:00
|
|
|
|
permission.query(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
cmd
|
|
|
|
|
.map(|request| self.descriptor_parser.parse_run_query(request))
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn query_ffi(
|
|
|
|
|
&self,
|
|
|
|
|
path: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, PathResolveError> {
|
2024-10-04 15:55:41 -04:00
|
|
|
|
let inner = self.inner.lock();
|
|
|
|
|
let permission = &inner.ffi;
|
|
|
|
|
if permission.is_allow_all() {
|
|
|
|
|
return Ok(PermissionState::Granted);
|
|
|
|
|
}
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
2024-10-04 15:55:41 -04:00
|
|
|
|
permission.query(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
path
|
|
|
|
|
.map(|path| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok::<_, PathResolveError>(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.descriptor_parser.parse_path_query(path)?.into_ffi(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// revoke
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn revoke_read(
|
|
|
|
|
&self,
|
|
|
|
|
path: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, PathResolveError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().read.revoke(
|
|
|
|
|
path
|
|
|
|
|
.map(|path| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok::<_, PathResolveError>(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.descriptor_parser.parse_path_query(path)?.into_read(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn revoke_write(
|
|
|
|
|
&self,
|
|
|
|
|
path: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, PathResolveError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().write.revoke(
|
|
|
|
|
path
|
|
|
|
|
.map(|path| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok::<_, PathResolveError>(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.descriptor_parser.parse_path_query(path)?.into_write(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn revoke_net(
|
|
|
|
|
&self,
|
|
|
|
|
host: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, NetDescriptorParseError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().net.revoke(
|
|
|
|
|
match host {
|
|
|
|
|
None => None,
|
|
|
|
|
Some(h) => Some(self.descriptor_parser.parse_net_descriptor(h)?),
|
|
|
|
|
}
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn revoke_env(&self, var: Option<&str>) -> PermissionState {
|
|
|
|
|
self.inner.lock().env.revoke(var)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn revoke_sys(
|
|
|
|
|
&self,
|
|
|
|
|
kind: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, SysDescriptorParseError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().sys.revoke(
|
|
|
|
|
kind
|
|
|
|
|
.map(|kind| self.descriptor_parser.parse_sys_descriptor(kind))
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn revoke_run(
|
|
|
|
|
&self,
|
|
|
|
|
cmd: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, RunDescriptorParseError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().run.revoke(
|
|
|
|
|
cmd
|
|
|
|
|
.map(|request| self.descriptor_parser.parse_run_query(request))
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn revoke_ffi(
|
|
|
|
|
&self,
|
|
|
|
|
path: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, PathResolveError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().ffi.revoke(
|
|
|
|
|
path
|
|
|
|
|
.map(|path| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok::<_, PathResolveError>(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.descriptor_parser.parse_path_query(path)?.into_ffi(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// request
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn request_read(
|
|
|
|
|
&self,
|
|
|
|
|
path: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, PathResolveError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().read.request(
|
|
|
|
|
path
|
|
|
|
|
.map(|path| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok::<_, PathResolveError>(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.descriptor_parser.parse_path_query(path)?.into_read(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn request_write(
|
|
|
|
|
&self,
|
|
|
|
|
path: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, PathResolveError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().write.request(
|
|
|
|
|
path
|
|
|
|
|
.map(|path| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok::<_, PathResolveError>(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.descriptor_parser.parse_path_query(path)?.into_write(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn request_net(
|
|
|
|
|
&self,
|
|
|
|
|
host: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, NetDescriptorParseError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().net.request(
|
|
|
|
|
match host {
|
|
|
|
|
None => None,
|
|
|
|
|
Some(h) => Some(self.descriptor_parser.parse_net_descriptor(h)?),
|
|
|
|
|
}
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn request_env(&self, var: Option<&str>) -> PermissionState {
|
|
|
|
|
self.inner.lock().env.request(var)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn request_sys(
|
|
|
|
|
&self,
|
|
|
|
|
kind: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, SysDescriptorParseError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().sys.request(
|
|
|
|
|
kind
|
|
|
|
|
.map(|kind| self.descriptor_parser.parse_sys_descriptor(kind))
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn request_run(
|
|
|
|
|
&self,
|
|
|
|
|
cmd: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, RunDescriptorParseError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().run.request(
|
|
|
|
|
cmd
|
|
|
|
|
.map(|request| self.descriptor_parser.parse_run_query(request))
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
pub fn request_ffi(
|
|
|
|
|
&self,
|
|
|
|
|
path: Option<&str>,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PermissionState, PathResolveError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
Ok(
|
|
|
|
|
self.inner.lock().ffi.request(
|
|
|
|
|
path
|
|
|
|
|
.map(|path| {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
Ok::<_, PathResolveError>(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
self.descriptor_parser.parse_path_query(path)?.into_ffi(),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.transpose()?
|
|
|
|
|
.as_ref(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-08-22 01:56:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 12:07:24 -04:00
|
|
|
|
const fn unit_permission_from_flag_bools(
|
2023-08-03 07:19:19 -04:00
|
|
|
|
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
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn global_from_option<T>(flag: Option<&HashSet<T>>) -> bool {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
matches!(flag, Some(v) if v.is_empty())
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
net: ChildUnaryPermissionArg,
|
|
|
|
|
ffi: ChildUnaryPermissionArg,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: ChildUnaryPermissionArg,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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,
|
|
|
|
|
net: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::Inherit,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: ChildUnaryPermissionArg::Inherit,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
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,
|
|
|
|
|
net: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::NotGranted,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: ChildUnaryPermissionArg::NotGranted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
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 == "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
|
|
|
|
})?;
|
2024-09-25 21:50:54 -04:00
|
|
|
|
} else if key == "import" {
|
|
|
|
|
let arg = serde_json::from_value::<ChildUnaryPermissionArg>(value);
|
|
|
|
|
child_permissions_arg.import = arg.map_err(|e| {
|
|
|
|
|
de::Error::custom(format!("(deno.permissions.import) {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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
/// Parses and normalizes permissions.
|
|
|
|
|
///
|
|
|
|
|
/// This trait is necessary because this crate doesn't have access
|
|
|
|
|
/// to the file system.
|
|
|
|
|
pub trait PermissionDescriptorParser: Debug + Send + Sync {
|
|
|
|
|
fn parse_read_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<ReadDescriptor, PathResolveError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
|
|
|
|
fn parse_write_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<WriteDescriptor, PathResolveError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn parse_net_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
|
|
|
|
) -> Result<NetDescriptor, NetDescriptorParseError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
2024-09-25 21:50:54 -04:00
|
|
|
|
fn parse_net_descriptor_from_url(
|
|
|
|
|
&self,
|
|
|
|
|
url: &Url,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<NetDescriptor, NetDescriptorFromUrlParseError> {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
NetDescriptor::from_url(url)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_import_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<ImportDescriptor, NetDescriptorParseError>;
|
2024-09-25 21:50:54 -04:00
|
|
|
|
|
|
|
|
|
fn parse_import_descriptor_from_url(
|
|
|
|
|
&self,
|
|
|
|
|
url: &Url,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<ImportDescriptor, NetDescriptorFromUrlParseError> {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
ImportDescriptor::from_url(url)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn parse_env_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
|
|
|
|
) -> Result<EnvDescriptor, EnvDescriptorParseError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn parse_sys_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
|
|
|
|
) -> Result<SysDescriptor, SysDescriptorParseError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
|
|
|
|
fn parse_allow_run_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<AllowRunDescriptorParseResult, RunDescriptorParseError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
|
|
|
|
fn parse_deny_run_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<DenyRunDescriptor, PathResolveError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
2024-11-04 12:17:21 -05:00
|
|
|
|
fn parse_ffi_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
|
|
|
|
) -> Result<FfiDescriptor, PathResolveError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
|
|
|
|
// queries
|
|
|
|
|
|
|
|
|
|
fn parse_path_query(
|
|
|
|
|
&self,
|
|
|
|
|
path: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PathQueryDescriptor, PathResolveError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
|
|
|
|
fn parse_run_query(
|
|
|
|
|
&self,
|
|
|
|
|
requested: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<RunQueryDescriptor, RunDescriptorParseError>;
|
2024-09-16 16:39:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 21:20:06 -04:00
|
|
|
|
static IS_STANDALONE: AtomicFlag = AtomicFlag::lowered();
|
|
|
|
|
|
|
|
|
|
pub fn mark_standalone() {
|
|
|
|
|
IS_STANDALONE.raise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_standalone() -> bool {
|
|
|
|
|
IS_STANDALONE.is_raised()
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2024-07-05 18:45:06 -04:00
|
|
|
|
use fqdn::fqdn;
|
2022-12-17 19:12:28 -05:00
|
|
|
|
use prompter::tests::*;
|
2024-07-05 18:45:06 -04:00
|
|
|
|
use std::net::Ipv4Addr;
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
// Creates vector of strings, Vec<String>
|
|
|
|
|
macro_rules! svec {
|
|
|
|
|
($($x:expr),*) => (vec![$($x.to_string()),*]);
|
|
|
|
|
}
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
2024-09-30 09:19:24 -04:00
|
|
|
|
#[derive(Debug, Clone)]
|
2024-09-16 16:39:37 -04:00
|
|
|
|
struct TestPermissionDescriptorParser;
|
|
|
|
|
|
|
|
|
|
impl TestPermissionDescriptorParser {
|
|
|
|
|
fn join_path_with_root(&self, path: &str) -> PathBuf {
|
|
|
|
|
if path.starts_with("C:\\") {
|
|
|
|
|
PathBuf::from(path)
|
|
|
|
|
} else {
|
|
|
|
|
PathBuf::from("/").join(path)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-04 08:51:24 -04:00
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
impl PermissionDescriptorParser for TestPermissionDescriptorParser {
|
|
|
|
|
fn parse_read_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<ReadDescriptor, PathResolveError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
Ok(ReadDescriptor(self.join_path_with_root(text)))
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn parse_write_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<WriteDescriptor, PathResolveError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
Ok(WriteDescriptor(self.join_path_with_root(text)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_net_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<NetDescriptor, NetDescriptorParseError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
NetDescriptor::parse(text)
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-25 21:50:54 -04:00
|
|
|
|
fn parse_import_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<ImportDescriptor, NetDescriptorParseError> {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
ImportDescriptor::parse(text)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn parse_env_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<EnvDescriptor, EnvDescriptorParseError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
Ok(EnvDescriptor::new(text))
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn parse_sys_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<SysDescriptor, SysDescriptorParseError> {
|
2024-09-30 09:19:24 -04:00
|
|
|
|
SysDescriptor::parse(text.to_string())
|
2024-09-16 16:39:37 -04:00
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn parse_allow_run_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<AllowRunDescriptorParseResult, RunDescriptorParseError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
Ok(AllowRunDescriptorParseResult::Descriptor(
|
|
|
|
|
AllowRunDescriptor(self.join_path_with_root(text)),
|
|
|
|
|
))
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn parse_deny_run_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<DenyRunDescriptor, PathResolveError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
if text.contains("/") {
|
|
|
|
|
Ok(DenyRunDescriptor::Path(self.join_path_with_root(text)))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(DenyRunDescriptor::Name(text.to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn parse_ffi_descriptor(
|
|
|
|
|
&self,
|
|
|
|
|
text: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<FfiDescriptor, PathResolveError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
Ok(FfiDescriptor(self.join_path_with_root(text)))
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn parse_path_query(
|
|
|
|
|
&self,
|
|
|
|
|
path: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<PathQueryDescriptor, PathResolveError> {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
Ok(PathQueryDescriptor {
|
|
|
|
|
resolved: self.join_path_with_root(path),
|
|
|
|
|
requested: path.to_string(),
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
fn parse_run_query(
|
|
|
|
|
&self,
|
|
|
|
|
requested: &str,
|
2024-11-04 12:17:21 -05:00
|
|
|
|
) -> Result<RunQueryDescriptor, RunDescriptorParseError> {
|
|
|
|
|
RunQueryDescriptor::parse(requested).map_err(Into::into)
|
2024-09-16 16:39:37 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-29 11:27:43 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
#[test]
|
|
|
|
|
fn check_paths() {
|
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
|
|
|
|
let allowlist = svec!["/a/specific/dir/name", "/a/specific", "/b/c"];
|
|
|
|
|
|
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let perms = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
allow_read: Some(allowlist.clone()),
|
|
|
|
|
allow_write: Some(allowlist.clone()),
|
|
|
|
|
allow_ffi: Some(allowlist),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let mut perms = PermissionsContainer::new(Arc::new(parser), perms);
|
|
|
|
|
|
|
|
|
|
let cases = [
|
|
|
|
|
// Inside of /a/specific and /a/specific/dir/name
|
|
|
|
|
("/a/specific/dir/name", true),
|
|
|
|
|
// Inside of /a/specific but outside of /a/specific/dir/name
|
|
|
|
|
("/a/specific/dir", true),
|
|
|
|
|
// Inside of /a/specific and /a/specific/dir/name
|
|
|
|
|
("/a/specific/dir/name/inner", true),
|
|
|
|
|
// Inside of /a/specific but outside of /a/specific/dir/name
|
|
|
|
|
("/a/specific/other/dir", true),
|
|
|
|
|
// Exact match with /b/c
|
|
|
|
|
("/b/c", true),
|
|
|
|
|
// Sub path within /b/c
|
|
|
|
|
("/b/c/sub/path", true),
|
|
|
|
|
// Sub path within /b/c, needs normalizing
|
|
|
|
|
("/b/c/sub/path/../path/.", true),
|
|
|
|
|
// Inside of /b but outside of /b/c
|
|
|
|
|
("/b/e", false),
|
|
|
|
|
// Inside of /a but outside of /a/specific
|
|
|
|
|
("/a/b", false),
|
|
|
|
|
];
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
for (path, is_ok) in cases {
|
|
|
|
|
assert_eq!(perms.check_read(path, "api").is_ok(), is_ok);
|
|
|
|
|
assert_eq!(perms.check_write(path, "api").is_ok(), is_ok);
|
|
|
|
|
assert_eq!(perms.check_ffi(path).is_ok(), is_ok);
|
|
|
|
|
}
|
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));
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let mut perms = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
allow_net: Some(svec![
|
|
|
|
|
"localhost",
|
|
|
|
|
"deno.land",
|
|
|
|
|
"github.com:3000",
|
|
|
|
|
"127.0.0.1",
|
|
|
|
|
"172.16.0.2:8000",
|
|
|
|
|
"www.github.com:443",
|
|
|
|
|
"80.example.com:80",
|
|
|
|
|
"443.example.com:443"
|
|
|
|
|
]),
|
|
|
|
|
..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),
|
2023-12-01 13:03:53 -05:00
|
|
|
|
("443.example.com", 444, false),
|
|
|
|
|
("80.example.com", 81, false),
|
|
|
|
|
("80.example.com", 80, true),
|
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 {
|
2024-09-06 05:28:53 -04:00
|
|
|
|
let host = Host::parse(host).unwrap();
|
2024-07-05 18:45:06 -04:00
|
|
|
|
let descriptor = NetDescriptor(host, Some(port));
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(
|
|
|
|
|
is_ok,
|
2024-07-05 18:45:06 -04:00
|
|
|
|
perms.net.check(&descriptor, None).is_ok(),
|
|
|
|
|
"{descriptor}",
|
2023-08-03 07:19:19 -04:00
|
|
|
|
);
|
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));
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let mut perms = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
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),
|
|
|
|
|
];
|
|
|
|
|
|
2024-07-05 18:45:06 -04:00
|
|
|
|
for (host_str, port) in domain_tests {
|
2024-09-06 05:28:53 -04:00
|
|
|
|
let host = Host::parse(host_str).unwrap();
|
2024-07-05 18:45:06 -04:00
|
|
|
|
let descriptor = NetDescriptor(host, Some(port));
|
|
|
|
|
assert!(
|
|
|
|
|
perms.net.check(&descriptor, None).is_ok(),
|
|
|
|
|
"expected {host_str}:{port} to pass"
|
|
|
|
|
);
|
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));
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let mut perms = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
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),
|
|
|
|
|
];
|
|
|
|
|
|
2024-07-05 18:45:06 -04:00
|
|
|
|
for (host_str, port) in domain_tests {
|
2024-09-06 05:28:53 -04:00
|
|
|
|
let host = Host::parse(host_str).unwrap();
|
2024-07-05 18:45:06 -04:00
|
|
|
|
let descriptor = NetDescriptor(host, Some(port));
|
|
|
|
|
assert!(
|
|
|
|
|
perms.net.check(&descriptor, None).is_err(),
|
|
|
|
|
"expected {host_str}:{port} to fail"
|
|
|
|
|
);
|
2021-01-19 09:39:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_check_net_url() {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let perms = Permissions::from_options(
|
|
|
|
|
&parser,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
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();
|
2024-09-25 21:50:54 -04:00
|
|
|
|
let mut perms = PermissionsContainer::new(Arc::new(parser), perms);
|
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 {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
let u = Url::parse(url_str).unwrap();
|
|
|
|
|
assert_eq!(is_ok, perms.check_net_url(&u, "api()").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") {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
svec!["C:\\a"]
|
2020-10-26 15:56:00 -04:00
|
|
|
|
} else {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
svec!["/a"]
|
2020-10-26 15:56:00 -04:00
|
|
|
|
};
|
2024-09-25 21:50:54 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let perms = Permissions::from_options(
|
|
|
|
|
&parser,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
allow_read: Some(read_allowlist),
|
2024-09-25 21:50:54 -04:00
|
|
|
|
allow_import: Some(svec!["localhost"]),
|
2024-09-16 16:39:37 -04:00
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.unwrap();
|
2024-09-25 21:50:54 -04:00
|
|
|
|
let perms = PermissionsContainer::new(Arc::new(parser), perms);
|
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(),
|
2024-09-25 21:50:54 -04:00
|
|
|
|
CheckSpecifierKind::Static,
|
|
|
|
|
true,
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
ModuleSpecifier::parse("http://localhost:4545/mod.ts").unwrap(),
|
|
|
|
|
CheckSpecifierKind::Dynamic,
|
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(),
|
2024-09-25 21:50:54 -04:00
|
|
|
|
CheckSpecifierKind::Dynamic,
|
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(),
|
2024-09-25 21:50:54 -04:00
|
|
|
|
CheckSpecifierKind::Dynamic,
|
2021-01-05 21:22:38 -05:00
|
|
|
|
true,
|
|
|
|
|
),
|
2020-10-26 15:56:00 -04:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if cfg!(target_os = "windows") {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
fixtures.push((
|
|
|
|
|
ModuleSpecifier::parse("file:///C:/a/mod.ts").unwrap(),
|
|
|
|
|
CheckSpecifierKind::Dynamic,
|
|
|
|
|
true,
|
|
|
|
|
));
|
|
|
|
|
fixtures.push((
|
|
|
|
|
ModuleSpecifier::parse("file:///C:/b/mod.ts").unwrap(),
|
|
|
|
|
CheckSpecifierKind::Static,
|
|
|
|
|
true,
|
|
|
|
|
));
|
2023-03-13 19:31:03 -04:00
|
|
|
|
fixtures.push((
|
|
|
|
|
ModuleSpecifier::parse("file:///C:/b/mod.ts").unwrap(),
|
2024-09-25 21:50:54 -04:00
|
|
|
|
CheckSpecifierKind::Dynamic,
|
2023-03-13 19:31:03 -04:00
|
|
|
|
false,
|
|
|
|
|
));
|
2020-10-26 15:56:00 -04:00
|
|
|
|
} else {
|
2024-09-25 21:50:54 -04:00
|
|
|
|
fixtures.push((
|
|
|
|
|
ModuleSpecifier::parse("file:///a/mod.ts").unwrap(),
|
|
|
|
|
CheckSpecifierKind::Dynamic,
|
|
|
|
|
true,
|
|
|
|
|
));
|
|
|
|
|
fixtures.push((
|
|
|
|
|
ModuleSpecifier::parse("file:///b/mod.ts").unwrap(),
|
|
|
|
|
CheckSpecifierKind::Static,
|
|
|
|
|
true,
|
|
|
|
|
));
|
|
|
|
|
fixtures.push((
|
|
|
|
|
ModuleSpecifier::parse("file:///b/mod.ts").unwrap(),
|
|
|
|
|
CheckSpecifierKind::Dynamic,
|
|
|
|
|
false,
|
|
|
|
|
));
|
2020-10-26 15:56:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 21:50:54 -04:00
|
|
|
|
for (specifier, kind, expected) in fixtures {
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(
|
2024-09-25 21:50:54 -04:00
|
|
|
|
perms.check_specifier(&specifier, kind).is_ok(),
|
2023-08-03 07:19:19 -04:00
|
|
|
|
expected,
|
|
|
|
|
"{}",
|
|
|
|
|
specifier,
|
|
|
|
|
);
|
2020-10-26 15:56:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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));
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
2021-03-17 17:45:12 -04:00
|
|
|
|
let perms1 = Permissions::allow_all();
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let perms2 = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
allow_read: Some(svec!["/foo"]),
|
|
|
|
|
allow_write: Some(svec!["/foo"]),
|
|
|
|
|
allow_ffi: Some(svec!["/foo"]),
|
|
|
|
|
allow_net: Some(svec!["127.0.0.1:8000"]),
|
|
|
|
|
allow_env: Some(svec!["HOME"]),
|
|
|
|
|
allow_sys: Some(svec!["hostname"]),
|
|
|
|
|
allow_run: Some(svec!["/deno"]),
|
|
|
|
|
allow_all: false,
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let perms3 = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
deny_read: Some(svec!["/foo"]),
|
|
|
|
|
deny_write: Some(svec!["/foo"]),
|
|
|
|
|
deny_ffi: Some(svec!["/foo"]),
|
|
|
|
|
deny_net: Some(svec!["127.0.0.1:8000"]),
|
|
|
|
|
deny_env: Some(svec!["HOME"]),
|
|
|
|
|
deny_sys: Some(svec!["hostname"]),
|
|
|
|
|
deny_run: Some(svec!["deno"]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let perms4 = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
allow_read: Some(vec![]),
|
|
|
|
|
deny_read: Some(svec!["/foo"]),
|
|
|
|
|
allow_write: Some(vec![]),
|
|
|
|
|
deny_write: Some(svec!["/foo"]),
|
|
|
|
|
allow_ffi: Some(vec![]),
|
|
|
|
|
deny_ffi: Some(svec!["/foo"]),
|
|
|
|
|
allow_net: Some(vec![]),
|
|
|
|
|
deny_net: Some(svec!["127.0.0.1:8000"]),
|
|
|
|
|
allow_env: Some(vec![]),
|
|
|
|
|
deny_env: Some(svec!["HOME"]),
|
|
|
|
|
allow_sys: Some(vec![]),
|
|
|
|
|
deny_sys: Some(svec!["hostname"]),
|
|
|
|
|
allow_run: Some(vec![]),
|
|
|
|
|
deny_run: Some(svec!["deno"]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2020-08-18 16:29:32 -04:00
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
{
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let read_query = |path: &str| parser.parse_path_query(path).unwrap().into_read();
|
|
|
|
|
let write_query = |path: &str| parser.parse_path_query(path).unwrap().into_write();
|
|
|
|
|
let ffi_query = |path: &str| parser.parse_path_query(path).unwrap().into_ffi();
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms1.read.query(None), PermissionState::Granted);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms1.read.query(Some(&read_query("/foo"))), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms2.read.query(None), PermissionState::Prompt);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms2.read.query(Some(&read_query("/foo"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.read.query(Some(&read_query("/foo/bar"))), PermissionState::Granted);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.read.query(None), PermissionState::Prompt);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms3.read.query(Some(&read_query("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms3.read.query(Some(&read_query("/foo/bar"))), PermissionState::Denied);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms4.read.query(None), PermissionState::GrantedPartial);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms4.read.query(Some(&read_query("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.read.query(Some(&read_query("/foo/bar"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.read.query(Some(&read_query("/bar"))), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms1.write.query(None), PermissionState::Granted);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms1.write.query(Some(&write_query("/foo"))), PermissionState::Granted);
|
2021-03-17 17:45:12 -04:00
|
|
|
|
assert_eq!(perms2.write.query(None), PermissionState::Prompt);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms2.write.query(Some(&write_query("/foo"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.write.query(Some(&write_query("/foo/bar"))), PermissionState::Granted);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.write.query(None), PermissionState::Prompt);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms3.write.query(Some(&write_query("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms3.write.query(Some(&write_query("/foo/bar"))), PermissionState::Denied);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms4.write.query(None), PermissionState::GrantedPartial);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms4.write.query(Some(&write_query("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.write.query(Some(&write_query("/foo/bar"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.write.query(Some(&write_query("/bar"))), PermissionState::Granted);
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert_eq!(perms1.ffi.query(None), PermissionState::Granted);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms1.ffi.query(Some(&ffi_query("/foo"))), PermissionState::Granted);
|
2022-12-14 15:38:53 -05:00
|
|
|
|
assert_eq!(perms2.ffi.query(None), PermissionState::Prompt);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms2.ffi.query(Some(&ffi_query("/foo"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.ffi.query(Some(&ffi_query("/foo/bar"))), PermissionState::Granted);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.ffi.query(None), PermissionState::Prompt);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms3.ffi.query(Some(&ffi_query("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms3.ffi.query(Some(&ffi_query("/foo/bar"))), PermissionState::Denied);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms4.ffi.query(None), PermissionState::GrantedPartial);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms4.ffi.query(Some(&ffi_query("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.ffi.query(Some(&ffi_query("/foo/bar"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.ffi.query(Some(&ffi_query("/bar"))), PermissionState::Granted);
|
2024-07-05 18:45:06 -04:00
|
|
|
|
assert_eq!(perms1.net.query(None), PermissionState::Granted);
|
2024-09-06 05:28:53 -04:00
|
|
|
|
assert_eq!(perms1.net.query(Some(&NetDescriptor(Host::must_parse("127.0.0.1"), None))), PermissionState::Granted);
|
2024-07-05 18:45:06 -04:00
|
|
|
|
assert_eq!(perms2.net.query(None), PermissionState::Prompt);
|
2024-09-06 05:28:53 -04:00
|
|
|
|
assert_eq!(perms2.net.query(Some(&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8000)))), PermissionState::Granted);
|
2024-07-05 18:45:06 -04:00
|
|
|
|
assert_eq!(perms3.net.query(None), PermissionState::Prompt);
|
2024-09-06 05:28:53 -04:00
|
|
|
|
assert_eq!(perms3.net.query(Some(&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8000)))), PermissionState::Denied);
|
2024-07-05 18:45:06 -04:00
|
|
|
|
assert_eq!(perms4.net.query(None), PermissionState::GrantedPartial);
|
2024-09-06 05:28:53 -04:00
|
|
|
|
assert_eq!(perms4.net.query(Some(&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8000)))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.net.query(Some(&NetDescriptor(Host::must_parse("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);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let sys_desc = |name: &str| SysDescriptor::parse(name.to_string()).unwrap();
|
2022-09-28 08:46:50 -04:00
|
|
|
|
assert_eq!(perms1.sys.query(None), PermissionState::Granted);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
assert_eq!(perms1.sys.query(Some(&sys_desc("osRelease"))), PermissionState::Granted);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms2.sys.query(None), PermissionState::Prompt);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
assert_eq!(perms2.sys.query(Some(&sys_desc("hostname"))), PermissionState::Granted);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.sys.query(None), PermissionState::Prompt);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
assert_eq!(perms3.sys.query(Some(&sys_desc("hostname"))), PermissionState::Denied);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms4.sys.query(None), PermissionState::GrantedPartial);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
assert_eq!(perms4.sys.query(Some(&sys_desc("hostname"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.sys.query(Some(&sys_desc("uid"))), PermissionState::Granted);
|
2021-04-09 18:12:00 -04:00
|
|
|
|
assert_eq!(perms1.run.query(None), PermissionState::Granted);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let deno_run_query = RunQueryDescriptor::Path {
|
|
|
|
|
requested: "deno".to_string(),
|
|
|
|
|
resolved: PathBuf::from("/deno"),
|
|
|
|
|
};
|
|
|
|
|
let node_run_query = RunQueryDescriptor::Path {
|
|
|
|
|
requested: "node".to_string(),
|
|
|
|
|
resolved: PathBuf::from("/node"),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(perms1.run.query(Some(&deno_run_query)), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms1.write.query(Some(&write_query("/deno"))), PermissionState::Granted);
|
2021-04-09 18:12:00 -04:00
|
|
|
|
assert_eq!(perms2.run.query(None), PermissionState::Prompt);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms2.run.query(Some(&deno_run_query)), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms2.write.query(Some(&write_query("/deno"))), PermissionState::Denied);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms3.run.query(None), PermissionState::Prompt);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms3.run.query(Some(&deno_run_query)), PermissionState::Denied);
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(perms4.run.query(None), PermissionState::GrantedPartial);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms4.run.query(Some(&deno_run_query)), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms4.run.query(Some(&node_run_query)), PermissionState::Granted);
|
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));
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
2024-10-03 08:28:38 -04:00
|
|
|
|
let mut perms: Permissions = Permissions::none_with_prompt();
|
|
|
|
|
let mut perms_no_prompt: Permissions = Permissions::none_without_prompt();
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let read_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_read();
|
|
|
|
|
let write_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_write();
|
|
|
|
|
let ffi_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_ffi();
|
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);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms.read.request(Some(&read_query("/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);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms.read.request(Some(&read_query("/foo/bar"))), PermissionState::Granted);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms.write.request(Some(&write_query("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms.write.query(Some(&write_query("/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);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms.ffi.request(Some(&ffi_query("/foo"))), PermissionState::Denied);
|
|
|
|
|
assert_eq!(perms.ffi.query(Some(&ffi_query("/foo/bar"))), PermissionState::Prompt);
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(true);
|
|
|
|
|
assert_eq!(perms.ffi.request(None), PermissionState::Denied);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-06 05:28:53 -04:00
|
|
|
|
assert_eq!(perms.net.request(Some(&NetDescriptor(Host::must_parse("127.0.0.1"), None))), PermissionState::Granted);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-06 05:28:53 -04:00
|
|
|
|
assert_eq!(perms.net.request(Some(&NetDescriptor(Host::must_parse("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);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let sys_desc = |name: &str| SysDescriptor::parse(name.to_string()).unwrap();
|
|
|
|
|
assert_eq!(perms.sys.request(Some(&sys_desc("hostname"))), PermissionState::Granted);
|
2022-09-28 08:46:50 -04:00
|
|
|
|
assert_eq!(perms.sys.query(None), PermissionState::Prompt);
|
|
|
|
|
prompt_value.set(false);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
assert_eq!(perms.sys.request(Some(&sys_desc("hostname"))), PermissionState::Granted);
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let run_query = RunQueryDescriptor::Path {
|
|
|
|
|
requested: "deno".to_string(),
|
|
|
|
|
resolved: PathBuf::from("/deno"),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(perms.run.request(Some(&run_query)), 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);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms.run.request(Some(&run_query)), PermissionState::Granted);
|
2024-10-03 08:28:38 -04:00
|
|
|
|
assert_eq!(perms_no_prompt.read.request(Some(&read_query("/foo"))), 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));
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let mut perms = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
allow_read: Some(svec!["/foo", "/foo/baz"]),
|
|
|
|
|
allow_write: Some(svec!["/foo", "/foo/baz"]),
|
|
|
|
|
allow_ffi: Some(svec!["/foo", "/foo/baz"]),
|
|
|
|
|
allow_net: Some(svec!["127.0.0.1", "127.0.0.1:8000"]),
|
|
|
|
|
allow_env: Some(svec!["HOME"]),
|
|
|
|
|
allow_sys: Some(svec!["hostname"]),
|
|
|
|
|
allow_run: Some(svec!["/deno"]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let read_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_read();
|
|
|
|
|
let write_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_write();
|
|
|
|
|
let ffi_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_ffi();
|
2020-08-18 16:29:32 -04:00
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
{
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert_eq!(perms.read.revoke(Some(&read_query("/foo/bar"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.read.query(Some(&read_query("/foo"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.read.query(Some(&read_query("/foo/baz"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms.write.revoke(Some(&write_query("/foo/bar"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.write.query(Some(&write_query("/foo"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.write.query(Some(&write_query("/foo/baz"))), PermissionState::Granted);
|
|
|
|
|
assert_eq!(perms.ffi.revoke(Some(&ffi_query("/foo/bar"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.ffi.query(Some(&ffi_query("/foo"))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.ffi.query(Some(&ffi_query("/foo/baz"))), PermissionState::Granted);
|
2024-09-06 05:28:53 -04:00
|
|
|
|
assert_eq!(perms.net.revoke(Some(&NetDescriptor(Host::must_parse("127.0.0.1"), Some(9000)))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.net.query(Some(&NetDescriptor(Host::must_parse("127.0.0.1"), None))), PermissionState::Prompt);
|
|
|
|
|
assert_eq!(perms.net.query(Some(&NetDescriptor(Host::must_parse("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);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let run_query = RunQueryDescriptor::Path {
|
|
|
|
|
requested: "deno".to_string(),
|
|
|
|
|
resolved: PathBuf::from("/deno"),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(perms.run.revoke(Some(&run_query)), PermissionState::Prompt);
|
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));
|
2024-03-13 16:30:48 -04:00
|
|
|
|
let mut perms = Permissions::none_with_prompt();
|
2021-10-26 11:49:07 -04:00
|
|
|
|
let prompt_value = PERMISSION_PROMPT_STUB_VALUE_SETTER.lock();
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let read_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_read();
|
|
|
|
|
let write_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_write();
|
|
|
|
|
let ffi_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_ffi();
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.read.check(&read_query("/foo"), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.read.check(&read_query("/foo"), None).is_ok());
|
|
|
|
|
assert!(perms.read.check(&read_query("/bar"), None).is_err());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.write.check(&write_query("/foo"), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.write.check(&write_query("/foo"), None).is_ok());
|
|
|
|
|
assert!(perms.write.check(&write_query("/bar"), None).is_err());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(true);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.ffi.check(&ffi_query("/foo"), None).is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.ffi.check(&ffi_query("/foo"), None).is_ok());
|
|
|
|
|
assert!(perms.ffi.check(&ffi_query("/bar"), None).is_err());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-07-05 18:45:06 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8000)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-07-05 18:45:06 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8000)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8001)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
2024-09-06 05:28:53 -04:00
|
|
|
|
.check(&NetDescriptor(Host::must_parse("127.0.0.1"), None), None)
|
2024-07-05 18:45:06 -04:00
|
|
|
|
.is_err());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("deno.land"), Some(8000)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
2024-09-06 05:28:53 -04:00
|
|
|
|
.check(&NetDescriptor(Host::must_parse("deno.land"), None), None)
|
2024-07-05 18:45:06 -04:00
|
|
|
|
.is_err());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2024-09-04 08:51:24 -04:00
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-04 18:57:49 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.run
|
|
|
|
|
.check(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&RunQueryDescriptor::Path {
|
|
|
|
|
requested: "cat".to_string(),
|
|
|
|
|
resolved: cwd.join("cat")
|
2024-09-04 18:57:49 -04:00
|
|
|
|
},
|
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-04 18:57:49 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.run
|
|
|
|
|
.check(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&RunQueryDescriptor::Path {
|
|
|
|
|
requested: "cat".to_string(),
|
|
|
|
|
resolved: cwd.join("cat")
|
2024-09-04 18:57:49 -04:00
|
|
|
|
},
|
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.run
|
|
|
|
|
.check(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&RunQueryDescriptor::Path {
|
|
|
|
|
requested: "ls".to_string(),
|
|
|
|
|
resolved: cwd.join("ls")
|
2024-09-04 18:57:49 -04:00
|
|
|
|
},
|
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert!(perms.env.check("HOME", None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert!(perms.env.check("HOME", None).is_ok());
|
|
|
|
|
assert!(perms.env.check("PATH", None).is_err());
|
2021-04-13 07:25:21 -04:00
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert!(perms.env.check("hostname", None).is_ok());
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert!(perms.env.check("hostname", None).is_ok());
|
|
|
|
|
assert!(perms.env.check("osRelease", None).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));
|
2024-03-13 16:30:48 -04:00
|
|
|
|
let mut perms = Permissions::none_with_prompt();
|
2021-10-26 11:49:07 -04:00
|
|
|
|
let prompt_value = PERMISSION_PROMPT_STUB_VALUE_SETTER.lock();
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let read_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_read();
|
|
|
|
|
let write_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_write();
|
|
|
|
|
let ffi_query =
|
|
|
|
|
|path: &str| parser.parse_path_query(path).unwrap().into_ffi();
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.read.check(&read_query("/foo"), None).is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.read.check(&read_query("/foo"), None).is_err());
|
|
|
|
|
assert!(perms.read.check(&read_query("/bar"), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.read.check(&read_query("/bar"), None).is_ok());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.write.check(&write_query("/foo"), None).is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.write.check(&write_query("/foo"), None).is_err());
|
|
|
|
|
assert!(perms.write.check(&write_query("/bar"), None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.write.check(&write_query("/bar"), None).is_ok());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.ffi.check(&ffi_query("/foo"), None).is_err());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(true);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.ffi.check(&ffi_query("/foo"), None).is_err());
|
|
|
|
|
assert!(perms.ffi.check(&ffi_query("/bar"), None).is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(perms.ffi.check(&ffi_query("/bar"), None).is_ok());
|
2022-12-14 15:38:53 -05:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-07-05 18:45:06 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8000)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-07-05 18:45:06 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8000)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8001)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("deno.land"), Some(8000)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-07-05 18:45:06 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("127.0.0.1"), Some(8001)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.net
|
|
|
|
|
.check(
|
2024-09-06 05:28:53 -04:00
|
|
|
|
&NetDescriptor(Host::must_parse("deno.land"), Some(8000)),
|
2024-07-05 18:45:06 -04:00
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-04 08:51:24 -04:00
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
let cwd = std::env::current_dir().unwrap();
|
2024-09-04 18:57:49 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.run
|
|
|
|
|
.check(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&RunQueryDescriptor::Path {
|
|
|
|
|
requested: "cat".to_string(),
|
|
|
|
|
resolved: cwd.join("cat")
|
2024-09-04 18:57:49 -04:00
|
|
|
|
},
|
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-04 18:57:49 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.run
|
|
|
|
|
.check(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&RunQueryDescriptor::Path {
|
|
|
|
|
requested: "cat".to_string(),
|
|
|
|
|
resolved: cwd.join("cat")
|
2024-09-04 18:57:49 -04:00
|
|
|
|
},
|
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_err());
|
|
|
|
|
assert!(perms
|
|
|
|
|
.run
|
|
|
|
|
.check(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&RunQueryDescriptor::Path {
|
|
|
|
|
requested: "ls".to_string(),
|
|
|
|
|
resolved: cwd.join("ls")
|
2024-09-04 18:57:49 -04:00
|
|
|
|
},
|
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-04 18:57:49 -04:00
|
|
|
|
assert!(perms
|
|
|
|
|
.run
|
|
|
|
|
.check(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&RunQueryDescriptor::Path {
|
|
|
|
|
requested: "ls".to_string(),
|
|
|
|
|
resolved: cwd.join("ls")
|
2024-09-04 18:57:49 -04:00
|
|
|
|
},
|
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
.is_ok());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert!(perms.env.check("HOME", None).is_err());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert!(perms.env.check("HOME", None).is_err());
|
|
|
|
|
assert!(perms.env.check("PATH", None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert!(perms.env.check("PATH", None).is_ok());
|
2021-04-13 07:25:21 -04:00
|
|
|
|
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let sys_desc = |name: &str| SysDescriptor::parse(name.to_string()).unwrap();
|
|
|
|
|
assert!(perms.sys.check(&sys_desc("hostname"), None).is_err());
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
assert!(perms.sys.check(&sys_desc("hostname"), None).is_err());
|
|
|
|
|
assert!(perms.sys.check(&sys_desc("osRelease"), None).is_ok());
|
2022-09-28 08:46:50 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
assert!(perms.sys.check(&sys_desc("osRelease"), None).is_ok());
|
2021-04-11 22:15:43 -04:00
|
|
|
|
}
|
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,
|
2024-09-16 16:39:37 -04:00
|
|
|
|
..Permissions::new_unary(
|
|
|
|
|
Some(HashSet::from([EnvDescriptor::new("HOME")])),
|
|
|
|
|
None,
|
|
|
|
|
false,
|
|
|
|
|
)
|
2021-04-13 07:25:21 -04:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert!(perms.env.check("HOME", None).is_ok());
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert!(perms.env.check("HOME", None).is_ok());
|
|
|
|
|
assert!(perms.env.check("hOmE", None).is_ok());
|
2021-04-13 07:25:21 -04:00
|
|
|
|
|
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
|
|
|
|
|
2024-03-12 18:12:29 -04:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_check_partial_denied() {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let mut perms = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
allow_read: Some(vec![]),
|
|
|
|
|
deny_read: Some(svec!["/foo/bar"]),
|
|
|
|
|
allow_write: Some(vec![]),
|
|
|
|
|
deny_write: Some(svec!["/foo/bar"]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2024-03-12 18:12:29 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let read_query = parser.parse_path_query("/foo").unwrap().into_read();
|
|
|
|
|
perms.read.check_partial(&read_query, None).unwrap();
|
|
|
|
|
assert!(perms.read.check(&read_query, None).is_err());
|
2024-03-12 18:12:29 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let write_query = parser.parse_path_query("/foo").unwrap().into_write();
|
|
|
|
|
perms.write.check_partial(&write_query, None).unwrap();
|
|
|
|
|
assert!(perms.write.check(&write_query, None).is_err());
|
2024-03-12 18:12:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 19:27:40 -04:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_net_fully_qualified_domain_name() {
|
2024-09-16 16:39:37 -04:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
|
|
|
|
let perms = Permissions::from_options(
|
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
allow_net: Some(svec!["allowed.domain", "1.1.1.1"]),
|
|
|
|
|
deny_net: Some(svec!["denied.domain", "2.2.2.2"]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let mut perms = PermissionsContainer::new(Arc::new(parser), perms);
|
|
|
|
|
let cases = [
|
|
|
|
|
("allowed.domain.", true),
|
|
|
|
|
("1.1.1.1", true),
|
|
|
|
|
("denied.domain.", false),
|
|
|
|
|
("2.2.2.2", false),
|
|
|
|
|
];
|
2024-03-26 19:27:40 -04:00
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
|
for (host, is_ok) in cases {
|
|
|
|
|
assert_eq!(perms.check_net(&(host, None), "api").is_ok(), is_ok);
|
|
|
|
|
}
|
2024-03-26 19:27:40 -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,
|
|
|
|
|
net: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::Inherit,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: ChildUnaryPermissionArg::Inherit,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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,
|
|
|
|
|
net: ChildUnaryPermissionArg::NotGranted,
|
|
|
|
|
ffi: ChildUnaryPermissionArg::NotGranted,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: ChildUnaryPermissionArg::NotGranted,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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!({
|
|
|
|
|
"env": true,
|
|
|
|
|
"net": true,
|
|
|
|
|
"ffi": true,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
"import": true,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
"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,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: ChildUnaryPermissionArg::Granted,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!({
|
|
|
|
|
"env": false,
|
|
|
|
|
"net": false,
|
|
|
|
|
"ffi": false,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
"import": false,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
"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,
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: ChildUnaryPermissionArg::NotGranted,
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
serde_json::from_value::<ChildPermissionsArg>(json!({
|
|
|
|
|
"env": ["foo", "bar"],
|
|
|
|
|
"net": ["foo", "bar:8000"],
|
|
|
|
|
"ffi": ["foo", "file:///bar/baz"],
|
2024-09-25 21:50:54 -04:00
|
|
|
|
"import": ["example.com"],
|
2021-10-13 13:04:44 -04:00
|
|
|
|
"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"
|
|
|
|
|
]),
|
2024-09-25 21:50:54 -04:00
|
|
|
|
import: ChildUnaryPermissionArg::GrantedList(svec!["example.com"]),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
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"
|
|
|
|
|
]),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_create_child_permissions() {
|
2022-12-17 19:12:28 -05:00
|
|
|
|
set_prompter(Box::new(TestPrompter));
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let main_perms = Permissions::from_options(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
allow_env: Some(vec![]),
|
|
|
|
|
allow_net: Some(svec!["foo", "bar"]),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let main_perms = PermissionsContainer::new(Arc::new(parser), main_perms);
|
2021-10-13 13:04:44 -04:00
|
|
|
|
assert_eq!(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
main_perms
|
|
|
|
|
.create_child_permissions(ChildPermissionsArg {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
env: ChildUnaryPermissionArg::Inherit,
|
|
|
|
|
net: ChildUnaryPermissionArg::GrantedList(svec!["foo"]),
|
|
|
|
|
ffi: ChildUnaryPermissionArg::NotGranted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2024-09-30 09:19:24 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap()
|
|
|
|
|
.inner
|
|
|
|
|
.lock()
|
|
|
|
|
.clone(),
|
2021-10-13 13:04:44 -04:00
|
|
|
|
Permissions {
|
2024-11-04 12:17:21 -05:00
|
|
|
|
env: Permissions::new_unary(Some(HashSet::new()), None, false),
|
2024-09-16 16:39:37 -04:00
|
|
|
|
net: Permissions::new_unary(
|
|
|
|
|
Some(HashSet::from([NetDescriptor::parse("foo").unwrap()])),
|
|
|
|
|
None,
|
|
|
|
|
false
|
2024-11-04 12:17:21 -05:00
|
|
|
|
),
|
2024-03-13 16:30:48 -04:00
|
|
|
|
..Permissions::none_without_prompt()
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
assert!(main_perms
|
|
|
|
|
.create_child_permissions(ChildPermissionsArg {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
net: ChildUnaryPermissionArg::Granted,
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2024-09-30 09:19:24 -04:00
|
|
|
|
})
|
|
|
|
|
.is_err());
|
|
|
|
|
assert!(main_perms
|
|
|
|
|
.create_child_permissions(ChildPermissionsArg {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
net: ChildUnaryPermissionArg::GrantedList(svec!["foo", "bar", "baz"]),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2024-09-30 09:19:24 -04:00
|
|
|
|
})
|
|
|
|
|
.is_err());
|
|
|
|
|
assert!(main_perms
|
|
|
|
|
.create_child_permissions(ChildPermissionsArg {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
ffi: ChildUnaryPermissionArg::GrantedList(svec!["foo"]),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2024-09-30 09:19:24 -04:00
|
|
|
|
})
|
|
|
|
|
.is_err());
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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();
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let main_perms = Permissions::from_options(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&TestPermissionDescriptorParser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
prompt: true,
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.unwrap();
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let main_perms = PermissionsContainer::new(
|
|
|
|
|
Arc::new(TestPermissionDescriptorParser),
|
|
|
|
|
main_perms,
|
|
|
|
|
);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(true);
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let worker_perms = main_perms
|
|
|
|
|
.create_child_permissions(ChildPermissionsArg {
|
2021-10-13 13:04:44 -04:00
|
|
|
|
read: ChildUnaryPermissionArg::Granted,
|
|
|
|
|
run: ChildUnaryPermissionArg::GrantedList(svec!["foo", "bar"]),
|
2022-03-20 17:46:39 -04:00
|
|
|
|
..ChildPermissionsArg::none()
|
2024-09-30 09:19:24 -04:00
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2024-03-13 16:30:48 -04:00
|
|
|
|
assert_eq!(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
main_perms.inner.lock().clone(),
|
|
|
|
|
worker_perms.inner.lock().clone()
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
main_perms.inner.lock().run.granted_list,
|
2024-03-13 16:30:48 -04:00
|
|
|
|
HashSet::from([
|
2024-09-16 16:39:37 -04:00
|
|
|
|
AllowRunDescriptor(PathBuf::from("/bar")),
|
|
|
|
|
AllowRunDescriptor(PathBuf::from("/foo")),
|
2024-03-13 16:30:48 -04:00
|
|
|
|
])
|
|
|
|
|
);
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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();
|
2024-09-16 16:39:37 -04:00
|
|
|
|
let parser = TestPermissionDescriptorParser;
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let main_perms = Permissions::from_options(
|
2024-09-16 16:39:37 -04:00
|
|
|
|
&parser,
|
|
|
|
|
&PermissionsOptions {
|
|
|
|
|
prompt: true,
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
)
|
2022-08-10 15:13:53 -04:00
|
|
|
|
.unwrap();
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let main_perms =
|
|
|
|
|
PermissionsContainer::new(Arc::new(parser.clone()), main_perms);
|
2021-10-26 11:49:07 -04:00
|
|
|
|
prompt_value.set(false);
|
2024-09-16 16:39:37 -04:00
|
|
|
|
assert!(main_perms
|
2024-09-30 09:19:24 -04:00
|
|
|
|
.inner
|
|
|
|
|
.lock()
|
2024-09-16 16:39:37 -04:00
|
|
|
|
.write
|
|
|
|
|
.check(&parser.parse_path_query("foo").unwrap().into_write(), None)
|
|
|
|
|
.is_err());
|
2024-09-30 09:19:24 -04:00
|
|
|
|
let worker_perms = main_perms
|
|
|
|
|
.create_child_permissions(ChildPermissionsArg::none())
|
|
|
|
|
.unwrap();
|
2023-08-03 07:19:19 -04:00
|
|
|
|
assert_eq!(
|
2024-09-30 09:19:24 -04:00
|
|
|
|
worker_perms.inner.lock().write.flag_denied_list.clone(),
|
|
|
|
|
main_perms.inner.lock().write.flag_denied_list
|
2023-08-03 07:19:19 -04:00
|
|
|
|
);
|
2021-10-13 13:04:44 -04:00
|
|
|
|
}
|
2022-08-10 15:13:53 -04:00
|
|
|
|
|
2024-07-05 18:45:06 -04:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_host_parse() {
|
|
|
|
|
let hosts = &[
|
|
|
|
|
("deno.land", Some(Host::Fqdn(fqdn!("deno.land")))),
|
|
|
|
|
("DENO.land", Some(Host::Fqdn(fqdn!("deno.land")))),
|
|
|
|
|
("deno.land.", Some(Host::Fqdn(fqdn!("deno.land")))),
|
|
|
|
|
(
|
|
|
|
|
"1.1.1.1",
|
|
|
|
|
Some(Host::Ip(IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1)))),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"::1",
|
|
|
|
|
Some(Host::Ip(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"[::1]",
|
|
|
|
|
Some(Host::Ip(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))),
|
|
|
|
|
),
|
|
|
|
|
("[::1", None),
|
|
|
|
|
("::1]", None),
|
|
|
|
|
("deno. land", None),
|
|
|
|
|
("1. 1.1.1", None),
|
|
|
|
|
("1.1.1.1.", None),
|
|
|
|
|
("1::1.", None),
|
|
|
|
|
("deno.land.", Some(Host::Fqdn(fqdn!("deno.land")))),
|
|
|
|
|
(".deno.land", None),
|
|
|
|
|
(
|
|
|
|
|
"::ffff:1.1.1.1",
|
|
|
|
|
Some(Host::Ip(IpAddr::V6(Ipv6Addr::new(
|
|
|
|
|
0, 0, 0, 0, 0, 0xffff, 0x0101, 0x0101,
|
|
|
|
|
)))),
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (host_str, expected) in hosts {
|
2024-09-06 05:28:53 -04:00
|
|
|
|
assert_eq!(Host::parse(host_str).ok(), *expected, "{host_str}");
|
2024-07-05 18:45:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_net_descriptor_parse() {
|
|
|
|
|
let cases = &[
|
|
|
|
|
(
|
|
|
|
|
"deno.land",
|
|
|
|
|
Some(NetDescriptor(Host::Fqdn(fqdn!("deno.land")), None)),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"DENO.land",
|
|
|
|
|
Some(NetDescriptor(Host::Fqdn(fqdn!("deno.land")), None)),
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
"deno.land:8000",
|
|
|
|
|
Some(NetDescriptor(Host::Fqdn(fqdn!("deno.land")), Some(8000))),
|
|
|
|
|
),
|
|
|
|
|
("deno.land:", None),
|
|
|
|
|
("deno.land:a", None),
|
|
|
|
|
("deno. land:a", None),
|
|
|
|
|
("deno.land.: a", None),
|
|
|
|
|
(
|
|
|
|
|
"1.1.1.1",
|
|
|
|
|
Some(NetDescriptor(
|
|
|
|
|
Host::Ip(IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1))),
|
|
|
|
|
None,
|
|
|
|
|
)),
|
|
|
|
|
),
|
|
|
|
|
("1.1.1.1.", None),
|
|
|
|
|
("1.1.1.1..", None),
|
|
|
|
|
(
|
|
|
|
|
"1.1.1.1:8000",
|
|
|
|
|
Some(NetDescriptor(
|
|
|
|
|
Host::Ip(IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1))),
|
|
|
|
|
Some(8000),
|
|
|
|
|
)),
|
|
|
|
|
),
|
|
|
|
|
("::", None),
|
|
|
|
|
(":::80", None),
|
|
|
|
|
("::80", None),
|
|
|
|
|
(
|
|
|
|
|
"[::]",
|
|
|
|
|
Some(NetDescriptor(
|
|
|
|
|
Host::Ip(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0))),
|
|
|
|
|
None,
|
|
|
|
|
)),
|
|
|
|
|
),
|
|
|
|
|
("[::1", None),
|
|
|
|
|
("::1]", None),
|
|
|
|
|
("::1]", None),
|
|
|
|
|
("[::1]:", None),
|
|
|
|
|
("[::1]:a", None),
|
|
|
|
|
(
|
|
|
|
|
"[::1]:443",
|
|
|
|
|
Some(NetDescriptor(
|
|
|
|
|
Host::Ip(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))),
|
|
|
|
|
Some(443),
|
|
|
|
|
)),
|
|
|
|
|
),
|
|
|
|
|
("", None),
|
|
|
|
|
("deno.land..", None),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (input, expected) in cases {
|
2024-09-06 05:28:53 -04:00
|
|
|
|
assert_eq!(NetDescriptor::parse(input).ok(), *expected, "'{input}'");
|
2024-07-05 18:45:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-16 16:39:37 -04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_denies_run_name() {
|
|
|
|
|
let cases = [
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
("deno", "C:\\deno.exe", true),
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
("deno", "C:\\sub\\deno.cmd", true),
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
("deno", "C:\\sub\\DeNO.cmd", true),
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
("DEno", "C:\\sub\\deno.cmd", true),
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
("deno", "C:\\other\\sub\\deno.batch", true),
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
("deno", "C:\\other\\sub\\deno", true),
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
("denort", "C:\\other\\sub\\deno.exe", false),
|
|
|
|
|
("deno", "/home/test/deno", true),
|
|
|
|
|
("deno", "/home/test/denot", false),
|
|
|
|
|
];
|
|
|
|
|
for (name, cmd_path, denies) in cases {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
denies_run_name(name, &PathBuf::from(cmd_path)),
|
|
|
|
|
denies,
|
|
|
|
|
"{} {}",
|
|
|
|
|
name,
|
|
|
|
|
cmd_path
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-08 19:20:30 -04:00
|
|
|
|
}
|