summaryrefslogtreecommitdiff
path: root/src/permissions.rs
blob: 5f9588a1eb7d37517d480cd24923f677eeaed236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use atty;

use crate::flags::DenoFlags;

use ansi_term::Style;
use crate::errors::permission_denied;
use crate::errors::DenoResult;
use std::io;
use std::sync::atomic::{AtomicBool, Ordering};

#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
#[derive(Debug, Default)]
pub struct DenoPermissions {
  // Keep in sync with src/permissions.ts
  pub allow_read: AtomicBool,
  pub allow_write: AtomicBool,
  pub allow_net: AtomicBool,
  pub allow_env: AtomicBool,
  pub allow_run: AtomicBool,
  pub no_prompts: AtomicBool,
}

impl DenoPermissions {
  pub fn from_flags(flags: &DenoFlags) -> Self {
    Self {
      allow_read: AtomicBool::new(flags.allow_read),
      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),
      no_prompts: AtomicBool::new(flags.no_prompts),
    }
  }

  pub fn check_run(&self) -> DenoResult<()> {
    if self.allow_run.load(Ordering::SeqCst) {
      return Ok(());
    };
    // TODO get location (where access occurred)
    let r = self.try_permissions_prompt("access to run a subprocess");
    if r.is_ok() {
      self.allow_run.store(true, Ordering::SeqCst);
    }
    r
  }

  pub fn check_read(&self, filename: &str) -> DenoResult<()> {
    if self.allow_read.load(Ordering::SeqCst) {
      return Ok(());
    };
    // TODO get location (where access occurred)
    let r =
      self.try_permissions_prompt(&format!("read access to \"{}\"", filename));;
    if r.is_ok() {
      self.allow_read.store(true, Ordering::SeqCst);
    }
    r
  }

  pub fn check_write(&self, filename: &str) -> DenoResult<()> {
    if self.allow_write.load(Ordering::SeqCst) {
      return Ok(());
    };
    // TODO get location (where access occurred)
    let r =
      self.try_permissions_prompt(&format!("write access to \"{}\"", filename));;
    if r.is_ok() {
      self.allow_write.store(true, Ordering::SeqCst);
    }
    r
  }

  pub fn check_net(&self, domain_name: &str) -> DenoResult<()> {
    if self.allow_net.load(Ordering::SeqCst) {
      return Ok(());
    };
    // TODO get location (where access occurred)
    let r = self.try_permissions_prompt(&format!(
      "network access to \"{}\"",
      domain_name
    ));
    if r.is_ok() {
      self.allow_net.store(true, Ordering::SeqCst);
    }
    r
  }

  pub fn check_env(&self) -> DenoResult<()> {
    if self.allow_env.load(Ordering::SeqCst) {
      return Ok(());
    };
    // TODO get location (where access occurred)
    let r = self.try_permissions_prompt(&"access to environment variables");
    if r.is_ok() {
      self.allow_env.store(true, Ordering::SeqCst);
    }
    r
  }

  /// 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)
  }

  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(());
  }

  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),
      ..Default::default()
    }
  }
}

fn permission_prompt(message: &str) -> DenoResult<()> {
  if !atty::is(atty::Stream::Stdin) || !atty::is(atty::Stream::Stderr) {
    return Err(permission_denied());
  };
  let msg = format!("⚠️  Deno requests {}. Grant? [yN] ", message);
  // print to stderr so that if deno is > to a file this is still displayed.
  eprint!("{}", Style::new().bold().paint(msg));
  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())
  }
}