2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2020-01-16 14:47:02 -05:00
|
|
|
use crate::fs as deno_fs;
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-01-20 09:45:44 -05:00
|
|
|
use std::path::Path;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &State) {
|
2019-10-11 14:41:54 -04:00
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_query_permission",
|
|
|
|
s.stateful_json_op(op_query_permission),
|
2019-10-11 14:41:54 -04:00
|
|
|
);
|
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_revoke_permission",
|
|
|
|
s.stateful_json_op(op_revoke_permission),
|
2019-10-11 14:41:54 -04:00
|
|
|
);
|
2019-11-11 10:33:29 -05:00
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_request_permission",
|
|
|
|
s.stateful_json_op(op_request_permission),
|
2019-11-11 10:33:29 -05:00
|
|
|
);
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct PermissionArgs {
|
|
|
|
name: String,
|
|
|
|
url: Option<String>,
|
|
|
|
path: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-01-16 14:47:02 -05:00
|
|
|
fn resolve_path(path: &str) -> String {
|
2020-01-20 09:45:44 -05:00
|
|
|
deno_fs::resolve_from_cwd(Path::new(path))
|
2020-01-16 14:47:02 -05:00
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
pub fn op_query_permission(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-10-27 11:22:53 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-10-27 11:22:53 -04:00
|
|
|
let args: PermissionArgs = serde_json::from_value(args)?;
|
2020-02-08 14:34:31 -05:00
|
|
|
let state = state.borrow();
|
2020-03-12 13:07:57 -04:00
|
|
|
let resolved_path = args.path.as_deref().map(resolve_path);
|
2020-02-08 14:34:31 -05:00
|
|
|
let perm = state.permissions.get_permission_state(
|
2019-10-27 11:22:53 -04:00
|
|
|
&args.name,
|
2020-03-12 13:07:57 -04:00
|
|
|
&args.url.as_deref(),
|
|
|
|
&resolved_path.as_deref().map(Path::new),
|
2019-10-27 11:22:53 -04:00
|
|
|
)?;
|
|
|
|
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_revoke_permission(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-10-27 11:22:53 -04:00
|
|
|
let args: PermissionArgs = serde_json::from_value(args)?;
|
2020-02-08 14:34:31 -05:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let permissions = &mut state.permissions;
|
2019-10-27 11:22:53 -04:00
|
|
|
match args.name.as_ref() {
|
2019-11-24 10:42:30 -05:00
|
|
|
"run" => permissions.allow_run.revoke(),
|
|
|
|
"read" => permissions.allow_read.revoke(),
|
|
|
|
"write" => permissions.allow_write.revoke(),
|
|
|
|
"net" => permissions.allow_net.revoke(),
|
|
|
|
"env" => permissions.allow_env.revoke(),
|
2019-12-05 15:30:20 -05:00
|
|
|
"plugin" => permissions.allow_plugin.revoke(),
|
2019-11-24 10:42:30 -05:00
|
|
|
"hrtime" => permissions.allow_hrtime.revoke(),
|
2019-10-27 11:22:53 -04:00
|
|
|
_ => {}
|
|
|
|
};
|
2020-03-12 13:07:57 -04:00
|
|
|
let resolved_path = args.path.as_deref().map(resolve_path);
|
2019-11-24 10:42:30 -05:00
|
|
|
let perm = permissions.get_permission_state(
|
2019-10-27 11:22:53 -04:00
|
|
|
&args.name,
|
2020-03-12 13:07:57 -04:00
|
|
|
&args.url.as_deref(),
|
|
|
|
&resolved_path.as_deref().map(Path::new),
|
2019-10-27 11:22:53 -04:00
|
|
|
)?;
|
|
|
|
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2019-11-11 10:33:29 -05:00
|
|
|
|
|
|
|
pub fn op_request_permission(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-11-11 10:33:29 -05:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-11-11 10:33:29 -05:00
|
|
|
let args: PermissionArgs = serde_json::from_value(args)?;
|
2020-02-08 14:34:31 -05:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let permissions = &mut state.permissions;
|
2020-03-12 13:07:57 -04:00
|
|
|
let resolved_path = args.path.as_deref().map(resolve_path);
|
2019-11-11 10:33:29 -05:00
|
|
|
let perm = match args.name.as_ref() {
|
2019-11-24 10:42:30 -05:00
|
|
|
"run" => Ok(permissions.request_run()),
|
2020-03-12 13:07:57 -04:00
|
|
|
"read" => {
|
|
|
|
Ok(permissions.request_read(&resolved_path.as_deref().map(Path::new)))
|
|
|
|
}
|
|
|
|
"write" => {
|
|
|
|
Ok(permissions.request_write(&resolved_path.as_deref().map(Path::new)))
|
|
|
|
}
|
|
|
|
"net" => permissions.request_net(&args.url.as_deref()),
|
2019-11-24 10:42:30 -05:00
|
|
|
"env" => Ok(permissions.request_env()),
|
2019-12-05 15:30:20 -05:00
|
|
|
"plugin" => Ok(permissions.request_plugin()),
|
2019-11-24 10:42:30 -05:00
|
|
|
"hrtime" => Ok(permissions.request_hrtime()),
|
2020-02-23 14:51:29 -05:00
|
|
|
n => Err(OpError::other(format!("No such permission name: {}", n))),
|
2019-11-11 10:33:29 -05:00
|
|
|
}?;
|
|
|
|
Ok(JsonOp::Sync(json!({ "state": perm.to_string() })))
|
|
|
|
}
|