2019-01-22 04:03:30 +09:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-01-13 22:30:38 -08:00
|
|
|
use atty;
|
2018-10-27 06:11:39 -07:00
|
|
|
|
2019-01-13 22:30:38 -08:00
|
|
|
use crate::flags::DenoFlags;
|
2018-10-27 06:11:39 -07:00
|
|
|
|
2019-02-08 00:19:50 +03:00
|
|
|
use ansi_term::Style;
|
2019-01-13 22:30:38 -08:00
|
|
|
use crate::errors::permission_denied;
|
|
|
|
use crate::errors::DenoResult;
|
2018-10-27 06:11:39 -07:00
|
|
|
use std::io;
|
2018-11-30 11:03:00 +08:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-10-27 06:11:39 -07:00
|
|
|
|
2018-11-04 22:21:21 -08:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
2018-11-30 11:03:00 +08:00
|
|
|
#[derive(Debug, Default)]
|
2018-10-27 06:11:39 -07:00
|
|
|
pub struct DenoPermissions {
|
2019-03-04 17:04:19 +01:00
|
|
|
// Keep in sync with src/permissions.ts
|
2019-02-08 23:59:38 +03:00
|
|
|
pub allow_read: AtomicBool,
|
2018-11-30 11:03:00 +08:00
|
|
|
pub allow_write: AtomicBool,
|
|
|
|
pub allow_net: AtomicBool,
|
|
|
|
pub allow_env: AtomicBool,
|
|
|
|
pub allow_run: AtomicBool,
|
2019-03-13 12:43:47 -04:00
|
|
|
pub no_prompts: AtomicBool,
|
2018-10-27 06:11:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DenoPermissions {
|
2019-03-01 19:25:50 -05:00
|
|
|
pub fn from_flags(flags: &DenoFlags) -> Self {
|
2018-11-04 22:21:21 -08:00
|
|
|
Self {
|
2019-02-08 23:59:38 +03:00
|
|
|
allow_read: AtomicBool::new(flags.allow_read),
|
2018-11-30 11:03:00 +08:00
|
|
|
allow_write: AtomicBool::new(flags.allow_write),
|
|
|
|
allow_env: AtomicBool::new(flags.allow_env),
|
|
|
|
allow_net: AtomicBool::new(flags.allow_net),
|
|
|
|
allow_run: AtomicBool::new(flags.allow_run),
|
2019-03-13 12:43:47 -04:00
|
|
|
no_prompts: AtomicBool::new(flags.no_prompts),
|
2018-10-27 06:11:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 11:03:00 +08:00
|
|
|
pub fn check_run(&self) -> DenoResult<()> {
|
|
|
|
if self.allow_run.load(Ordering::SeqCst) {
|
2018-11-15 20:07:40 -08:00
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
// TODO get location (where access occurred)
|
2019-03-13 12:43:47 -04:00
|
|
|
let r = self.try_permissions_prompt("access to run a subprocess");
|
2018-11-15 20:07:40 -08:00
|
|
|
if r.is_ok() {
|
2018-11-30 11:03:00 +08:00
|
|
|
self.allow_run.store(true, Ordering::SeqCst);
|
2018-11-15 20:07:40 -08:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2019-02-08 23:59:38 +03:00
|
|
|
pub fn check_read(&self, filename: &str) -> DenoResult<()> {
|
|
|
|
if self.allow_read.load(Ordering::SeqCst) {
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
// TODO get location (where access occurred)
|
2019-03-13 12:43:47 -04:00
|
|
|
let r =
|
|
|
|
self.try_permissions_prompt(&format!("read access to \"{}\"", filename));;
|
2019-02-08 23:59:38 +03:00
|
|
|
if r.is_ok() {
|
|
|
|
self.allow_read.store(true, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-11-30 11:03:00 +08:00
|
|
|
pub fn check_write(&self, filename: &str) -> DenoResult<()> {
|
|
|
|
if self.allow_write.load(Ordering::SeqCst) {
|
2018-10-27 06:11:39 -07:00
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
// TODO get location (where access occurred)
|
2019-03-13 12:43:47 -04:00
|
|
|
let r =
|
|
|
|
self.try_permissions_prompt(&format!("write access to \"{}\"", filename));;
|
2018-10-27 06:11:39 -07:00
|
|
|
if r.is_ok() {
|
2018-11-30 11:03:00 +08:00
|
|
|
self.allow_write.store(true, Ordering::SeqCst);
|
2018-10-27 06:11:39 -07:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-11-30 11:03:00 +08:00
|
|
|
pub fn check_net(&self, domain_name: &str) -> DenoResult<()> {
|
|
|
|
if self.allow_net.load(Ordering::SeqCst) {
|
2018-10-27 06:11:39 -07:00
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
// TODO get location (where access occurred)
|
2019-03-13 12:43:47 -04:00
|
|
|
let r = self.try_permissions_prompt(&format!(
|
|
|
|
"network access to \"{}\"",
|
|
|
|
domain_name
|
|
|
|
));
|
2018-10-27 06:11:39 -07:00
|
|
|
if r.is_ok() {
|
2018-11-30 11:03:00 +08:00
|
|
|
self.allow_net.store(true, Ordering::SeqCst);
|
2018-10-27 06:11:39 -07:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-11-30 11:03:00 +08:00
|
|
|
pub fn check_env(&self) -> DenoResult<()> {
|
|
|
|
if self.allow_env.load(Ordering::SeqCst) {
|
2018-10-27 06:11:39 -07:00
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
// TODO get location (where access occurred)
|
2019-03-13 12:43:47 -04:00
|
|
|
let r = self.try_permissions_prompt(&"access to environment variables");
|
2018-10-27 06:11:39 -07:00
|
|
|
if r.is_ok() {
|
2018-11-30 11:03:00 +08:00
|
|
|
self.allow_env.store(true, Ordering::SeqCst);
|
2018-10-27 06:11:39 -07:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
2019-03-01 19:25:50 -05:00
|
|
|
|
2019-03-13 12:43:47 -04:00
|
|
|
/// Try to present the user with a permission prompt
|
|
|
|
/// will error with permission_denied if no_prompts is enabled
|
|
|
|
fn try_permissions_prompt(&self, message: &str) -> DenoResult<()> {
|
|
|
|
if self.no_prompts.load(Ordering::SeqCst) {
|
|
|
|
return Err(permission_denied());
|
|
|
|
}
|
|
|
|
permission_prompt(message)
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:04:19 +01:00
|
|
|
pub fn allows_run(&self) -> bool {
|
|
|
|
return self.allow_run.load(Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn allows_read(&self) -> bool {
|
|
|
|
return self.allow_read.load(Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn allows_write(&self) -> bool {
|
|
|
|
return self.allow_write.load(Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn allows_net(&self) -> bool {
|
|
|
|
return self.allow_net.load(Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn allows_env(&self) -> bool {
|
|
|
|
return self.allow_env.load(Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn revoke_run(&self) -> DenoResult<()> {
|
|
|
|
self.allow_run.store(false, Ordering::SeqCst);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn revoke_read(&self) -> DenoResult<()> {
|
|
|
|
self.allow_read.store(false, Ordering::SeqCst);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn revoke_write(&self) -> DenoResult<()> {
|
|
|
|
self.allow_write.store(false, Ordering::SeqCst);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn revoke_net(&self) -> DenoResult<()> {
|
|
|
|
self.allow_net.store(false, Ordering::SeqCst);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn revoke_env(&self) -> DenoResult<()> {
|
|
|
|
self.allow_env.store(false, Ordering::SeqCst);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-03-01 19:25:50 -05:00
|
|
|
pub fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
allow_read: AtomicBool::new(false),
|
|
|
|
allow_write: AtomicBool::new(false),
|
|
|
|
allow_env: AtomicBool::new(false),
|
|
|
|
allow_net: AtomicBool::new(false),
|
|
|
|
allow_run: AtomicBool::new(false),
|
2019-03-13 12:43:47 -04:00
|
|
|
..Default::default()
|
2019-03-01 19:25:50 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-27 06:11:39 -07:00
|
|
|
}
|
|
|
|
|
2018-11-04 06:04:24 -08:00
|
|
|
fn permission_prompt(message: &str) -> DenoResult<()> {
|
2018-10-27 06:11:39 -07:00
|
|
|
if !atty::is(atty::Stream::Stdin) || !atty::is(atty::Stream::Stderr) {
|
|
|
|
return Err(permission_denied());
|
|
|
|
};
|
2019-02-08 00:19:50 +03:00
|
|
|
let msg = format!("⚠️ Deno requests {}. Grant? [yN] ", message);
|
2018-10-27 06:11:39 -07:00
|
|
|
// print to stderr so that if deno is > to a file this is still displayed.
|
2019-02-08 00:19:50 +03:00
|
|
|
eprint!("{}", Style::new().bold().paint(msg));
|
2018-10-27 06:11:39 -07:00
|
|
|
let mut input = String::new();
|
|
|
|
let stdin = io::stdin();
|
|
|
|
let _nread = stdin.read_line(&mut input)?;
|
|
|
|
let ch = input.chars().next().unwrap();
|
|
|
|
let is_yes = ch == 'y' || ch == 'Y';
|
|
|
|
if is_yes {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(permission_denied())
|
|
|
|
}
|
|
|
|
}
|