2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-01-14 01:30:38 -05:00
|
|
|
use atty;
|
2018-10-27 09:11:39 -04:00
|
|
|
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::flags::DenoFlags;
|
2018-10-27 09:11:39 -04:00
|
|
|
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::errors::permission_denied;
|
|
|
|
use crate::errors::DenoResult;
|
2018-10-27 09:11:39 -04:00
|
|
|
use std::io;
|
2018-11-29 22:03:00 -05:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-10-27 09:11:39 -04:00
|
|
|
|
2018-11-05 01:21:21 -05:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
|
2018-11-29 22:03:00 -05:00
|
|
|
#[derive(Debug, Default)]
|
2018-10-27 09:11:39 -04:00
|
|
|
pub struct DenoPermissions {
|
2018-11-29 22:03:00 -05:00
|
|
|
pub allow_write: AtomicBool,
|
|
|
|
pub allow_net: AtomicBool,
|
|
|
|
pub allow_env: AtomicBool,
|
|
|
|
pub allow_run: AtomicBool,
|
2018-10-27 09:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DenoPermissions {
|
2018-11-05 01:21:21 -05:00
|
|
|
pub fn new(flags: &DenoFlags) -> Self {
|
|
|
|
Self {
|
2018-11-29 22:03:00 -05: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),
|
2018-10-27 09:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 22:03:00 -05:00
|
|
|
pub fn check_run(&self) -> DenoResult<()> {
|
|
|
|
if self.allow_run.load(Ordering::SeqCst) {
|
2018-11-15 23:07:40 -05:00
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
// TODO get location (where access occurred)
|
|
|
|
let r = permission_prompt("Deno requests access to run a subprocess.");
|
|
|
|
if r.is_ok() {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.allow_run.store(true, Ordering::SeqCst);
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-11-29 22:03:00 -05:00
|
|
|
pub fn check_write(&self, filename: &str) -> DenoResult<()> {
|
|
|
|
if self.allow_write.load(Ordering::SeqCst) {
|
2018-10-27 09:11:39 -04:00
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
// TODO get location (where access occurred)
|
2018-11-04 09:04:24 -05:00
|
|
|
let r = permission_prompt(&format!(
|
2018-10-27 09:11:39 -04:00
|
|
|
"Deno requests write access to \"{}\".",
|
|
|
|
filename
|
|
|
|
));;
|
|
|
|
if r.is_ok() {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.allow_write.store(true, Ordering::SeqCst);
|
2018-10-27 09:11:39 -04:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-11-29 22:03:00 -05:00
|
|
|
pub fn check_net(&self, domain_name: &str) -> DenoResult<()> {
|
|
|
|
if self.allow_net.load(Ordering::SeqCst) {
|
2018-10-27 09:11:39 -04:00
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
// TODO get location (where access occurred)
|
2018-11-04 09:04:24 -05:00
|
|
|
let r = permission_prompt(&format!(
|
2018-10-27 09:11:39 -04:00
|
|
|
"Deno requests network access to \"{}\".",
|
|
|
|
domain_name
|
|
|
|
));
|
|
|
|
if r.is_ok() {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.allow_net.store(true, Ordering::SeqCst);
|
2018-10-27 09:11:39 -04:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2018-11-29 22:03:00 -05:00
|
|
|
pub fn check_env(&self) -> DenoResult<()> {
|
|
|
|
if self.allow_env.load(Ordering::SeqCst) {
|
2018-10-27 09:11:39 -04:00
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
// TODO get location (where access occurred)
|
2018-11-04 09:04:24 -05:00
|
|
|
let r =
|
|
|
|
permission_prompt(&"Deno requests access to environment variables.");
|
2018-10-27 09:11:39 -04:00
|
|
|
if r.is_ok() {
|
2018-11-29 22:03:00 -05:00
|
|
|
self.allow_env.store(true, Ordering::SeqCst);
|
2018-10-27 09:11:39 -04:00
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 09:04:24 -05:00
|
|
|
fn permission_prompt(message: &str) -> DenoResult<()> {
|
2018-10-27 09:11:39 -04:00
|
|
|
if !atty::is(atty::Stream::Stdin) || !atty::is(atty::Stream::Stderr) {
|
|
|
|
return Err(permission_denied());
|
|
|
|
};
|
|
|
|
// print to stderr so that if deno is > to a file this is still displayed.
|
|
|
|
eprint!("{} Grant? [yN] ", message);
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|