2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::custom_error;
|
2020-12-30 17:35:28 -05:00
|
|
|
use deno_core::error::uri_error;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-12-30 17:35:28 -05:00
|
|
|
use deno_core::url;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-01-20 09:45:44 -05:00
|
|
|
use std::path::Path;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_sync(rt, "op_query_permission", op_query_permission);
|
|
|
|
super::reg_json_sync(rt, "op_revoke_permission", op_revoke_permission);
|
|
|
|
super::reg_json_sync(rt, "op_request_permission", op_request_permission);
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
#[derive(Deserialize)]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct PermissionArgs {
|
2019-10-27 11:22:53 -04:00
|
|
|
name: String,
|
|
|
|
path: Option<String>,
|
2020-12-30 17:35:28 -05:00
|
|
|
host: Option<String>,
|
2021-04-09 18:12:00 -04:00
|
|
|
command: Option<String>,
|
2019-10-27 11:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_query_permission(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: PermissionArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow::<Permissions>();
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = args.path.as_deref();
|
2020-08-18 16:29:32 -04:00
|
|
|
let perm = match args.name.as_ref() {
|
2021-03-17 17:45:12 -04:00
|
|
|
"read" => permissions.read.query(path.as_deref().map(Path::new)),
|
|
|
|
"write" => permissions.write.query(path.as_deref().map(Path::new)),
|
|
|
|
"net" => permissions.net.query(
|
|
|
|
match args.host.as_deref() {
|
2020-12-30 17:35:28 -05:00
|
|
|
None => None,
|
|
|
|
Some(h) => Some(parse_host(h)?),
|
|
|
|
}
|
|
|
|
.as_ref(),
|
|
|
|
),
|
2021-03-17 17:45:12 -04:00
|
|
|
"env" => permissions.env.query(),
|
2021-04-09 18:12:00 -04:00
|
|
|
"run" => permissions.run.query(args.command.as_deref()),
|
2021-03-17 17:45:12 -04:00
|
|
|
"plugin" => permissions.plugin.query(),
|
|
|
|
"hrtime" => permissions.hrtime.query(),
|
2020-08-25 18:22:15 -04:00
|
|
|
n => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error(
|
2020-08-25 18:22:15 -04:00
|
|
|
"ReferenceError",
|
|
|
|
format!("No such permission name: {}", n),
|
|
|
|
))
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
};
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(perm.to_string())
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_revoke_permission(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: PermissionArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = args.path.as_deref();
|
2020-08-18 16:29:32 -04:00
|
|
|
let perm = match args.name.as_ref() {
|
2021-03-17 17:45:12 -04:00
|
|
|
"read" => permissions.read.revoke(path.as_deref().map(Path::new)),
|
|
|
|
"write" => permissions.write.revoke(path.as_deref().map(Path::new)),
|
|
|
|
"net" => permissions.net.revoke(
|
|
|
|
match args.host.as_deref() {
|
2020-12-30 17:35:28 -05:00
|
|
|
None => None,
|
|
|
|
Some(h) => Some(parse_host(h)?),
|
|
|
|
}
|
|
|
|
.as_ref(),
|
|
|
|
),
|
2021-03-17 17:45:12 -04:00
|
|
|
"env" => permissions.env.revoke(),
|
2021-04-09 18:12:00 -04:00
|
|
|
"run" => permissions.run.revoke(args.command.as_deref()),
|
2021-03-17 17:45:12 -04:00
|
|
|
"plugin" => permissions.plugin.revoke(),
|
|
|
|
"hrtime" => permissions.hrtime.revoke(),
|
2020-08-25 18:22:15 -04:00
|
|
|
n => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error(
|
2020-08-25 18:22:15 -04:00
|
|
|
"ReferenceError",
|
|
|
|
format!("No such permission name: {}", n),
|
|
|
|
))
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
};
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(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-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: PermissionArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = args.path.as_deref();
|
2019-11-11 10:33:29 -05:00
|
|
|
let perm = match args.name.as_ref() {
|
2021-03-17 17:45:12 -04:00
|
|
|
"read" => permissions.read.request(path.as_deref().map(Path::new)),
|
|
|
|
"write" => permissions.write.request(path.as_deref().map(Path::new)),
|
|
|
|
"net" => permissions.net.request(
|
|
|
|
match args.host.as_deref() {
|
2020-12-30 17:35:28 -05:00
|
|
|
None => None,
|
|
|
|
Some(h) => Some(parse_host(h)?),
|
|
|
|
}
|
|
|
|
.as_ref(),
|
|
|
|
),
|
2021-03-17 17:45:12 -04:00
|
|
|
"env" => permissions.env.request(),
|
2021-04-09 18:12:00 -04:00
|
|
|
"run" => permissions.run.request(args.command.as_deref()),
|
2021-03-17 17:45:12 -04:00
|
|
|
"plugin" => permissions.plugin.request(),
|
|
|
|
"hrtime" => permissions.hrtime.request(),
|
2020-08-25 18:22:15 -04:00
|
|
|
n => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error(
|
2020-08-25 18:22:15 -04:00
|
|
|
"ReferenceError",
|
|
|
|
format!("No such permission name: {}", n),
|
|
|
|
))
|
|
|
|
}
|
2020-08-18 16:29:32 -04:00
|
|
|
};
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(perm.to_string())
|
2019-11-11 10:33:29 -05:00
|
|
|
}
|
2020-12-30 17:35:28 -05:00
|
|
|
|
|
|
|
fn parse_host(host_str: &str) -> Result<(String, Option<u16>), AnyError> {
|
|
|
|
let url = url::Url::parse(&format!("http://{}/", host_str))
|
|
|
|
.map_err(|_| uri_error("Invalid host"))?;
|
|
|
|
if url.path() != "/" {
|
|
|
|
return Err(uri_error("Invalid host"));
|
|
|
|
}
|
|
|
|
let hostname = url.host_str().unwrap();
|
|
|
|
Ok((hostname.to_string(), url.port()))
|
|
|
|
}
|