2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 02:34:02 +02:00
|
|
|
|
2022-09-29 19:34:16 +09:00
|
|
|
use crate::permissions::parse_sys_kind;
|
2023-01-07 17:25:34 +01:00
|
|
|
use crate::permissions::PermissionsContainer;
|
2020-09-14 18:48:57 +02:00
|
|
|
use deno_core::error::custom_error;
|
2020-12-30 22:35:28 +00:00
|
|
|
use deno_core::error::uri_error;
|
2020-09-14 18:48:57 +02:00
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 23:14:15 +05:30
|
|
|
use deno_core::op;
|
2020-12-30 22:35:28 +00:00
|
|
|
use deno_core::url;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-01-20 14:45:44 +00:00
|
|
|
use std::path::Path;
|
2019-08-14 17:03:02 +02:00
|
|
|
|
2023-03-17 12:22:15 -06:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_permissions,
|
|
|
|
ops = [
|
|
|
|
op_query_permission,
|
|
|
|
op_revoke_permission,
|
|
|
|
op_request_permission,
|
2023-03-18 18:30:04 -04:00
|
|
|
],
|
2023-03-17 12:22:15 -06:00
|
|
|
);
|
2019-10-11 11:41:54 -07:00
|
|
|
|
2019-10-28 00:22:53 +09:00
|
|
|
#[derive(Deserialize)]
|
2021-03-18 19:42:01 +01:00
|
|
|
pub struct PermissionArgs {
|
2019-10-28 00:22:53 +09:00
|
|
|
name: String,
|
|
|
|
path: Option<String>,
|
2020-12-30 22:35:28 +00:00
|
|
|
host: Option<String>,
|
2021-04-13 13:25:21 +02:00
|
|
|
variable: Option<String>,
|
2022-09-28 21:46:50 +09:00
|
|
|
kind: Option<String>,
|
2021-04-10 00:12:00 +02:00
|
|
|
command: Option<String>,
|
2019-10-28 00:22:53 +09:00
|
|
|
}
|
|
|
|
|
2022-03-14 23:14:15 +05:30
|
|
|
#[op]
|
2019-10-28 00:22:53 +09:00
|
|
|
pub fn op_query_permission(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 19:42:01 +01:00
|
|
|
args: PermissionArgs,
|
2021-04-05 18:40:24 +02:00
|
|
|
) -> Result<String, AnyError> {
|
2023-01-07 17:25:34 +01:00
|
|
|
let permissions = state.borrow::<PermissionsContainer>().0.lock();
|
2020-05-29 16:27:43 +01:00
|
|
|
let path = args.path.as_deref();
|
2020-08-18 21:29:32 +01:00
|
|
|
let perm = match args.name.as_ref() {
|
2021-12-04 14:19:06 +01:00
|
|
|
"read" => permissions.read.query(path.map(Path::new)),
|
|
|
|
"write" => permissions.write.query(path.map(Path::new)),
|
2021-03-17 22:45:12 +01:00
|
|
|
"net" => permissions.net.query(
|
|
|
|
match args.host.as_deref() {
|
2020-12-30 22:35:28 +00:00
|
|
|
None => None,
|
|
|
|
Some(h) => Some(parse_host(h)?),
|
|
|
|
}
|
|
|
|
.as_ref(),
|
|
|
|
),
|
2021-04-13 13:25:21 +02:00
|
|
|
"env" => permissions.env.query(args.variable.as_deref()),
|
2022-09-29 19:34:16 +09:00
|
|
|
"sys" => permissions
|
|
|
|
.sys
|
|
|
|
.query(args.kind.as_deref().map(parse_sys_kind).transpose()?),
|
2021-04-10 00:12:00 +02:00
|
|
|
"run" => permissions.run.query(args.command.as_deref()),
|
2021-10-13 18:04:44 +01:00
|
|
|
"ffi" => permissions.ffi.query(args.path.as_deref().map(Path::new)),
|
2021-03-17 22:45:12 +01:00
|
|
|
"hrtime" => permissions.hrtime.query(),
|
2020-08-26 00:22:15 +02:00
|
|
|
n => {
|
2020-09-14 18:48:57 +02:00
|
|
|
return Err(custom_error(
|
2020-08-26 00:22:15 +02:00
|
|
|
"ReferenceError",
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("No such permission name: {n}"),
|
2020-08-26 00:22:15 +02:00
|
|
|
))
|
|
|
|
}
|
2020-08-18 21:29:32 +01:00
|
|
|
};
|
2021-04-05 18:40:24 +02:00
|
|
|
Ok(perm.to_string())
|
2019-08-14 17:03:02 +02:00
|
|
|
}
|
|
|
|
|
2022-03-14 23:14:15 +05:30
|
|
|
#[op]
|
2019-08-14 17:03:02 +02:00
|
|
|
pub fn op_revoke_permission(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 19:42:01 +01:00
|
|
|
args: PermissionArgs,
|
2021-04-05 18:40:24 +02:00
|
|
|
) -> Result<String, AnyError> {
|
2023-01-07 17:25:34 +01:00
|
|
|
let mut permissions = state.borrow_mut::<PermissionsContainer>().0.lock();
|
2020-05-29 16:27:43 +01:00
|
|
|
let path = args.path.as_deref();
|
2020-08-18 21:29:32 +01:00
|
|
|
let perm = match args.name.as_ref() {
|
2021-12-04 14:19:06 +01:00
|
|
|
"read" => permissions.read.revoke(path.map(Path::new)),
|
|
|
|
"write" => permissions.write.revoke(path.map(Path::new)),
|
2021-03-17 22:45:12 +01:00
|
|
|
"net" => permissions.net.revoke(
|
|
|
|
match args.host.as_deref() {
|
2020-12-30 22:35:28 +00:00
|
|
|
None => None,
|
|
|
|
Some(h) => Some(parse_host(h)?),
|
|
|
|
}
|
|
|
|
.as_ref(),
|
|
|
|
),
|
2021-04-13 13:25:21 +02:00
|
|
|
"env" => permissions.env.revoke(args.variable.as_deref()),
|
2022-09-28 21:46:50 +09:00
|
|
|
"sys" => permissions
|
|
|
|
.sys
|
2022-09-29 19:34:16 +09:00
|
|
|
.revoke(args.kind.as_deref().map(parse_sys_kind).transpose()?),
|
2021-04-10 00:12:00 +02:00
|
|
|
"run" => permissions.run.revoke(args.command.as_deref()),
|
2021-10-13 18:04:44 +01:00
|
|
|
"ffi" => permissions.ffi.revoke(args.path.as_deref().map(Path::new)),
|
2021-03-17 22:45:12 +01:00
|
|
|
"hrtime" => permissions.hrtime.revoke(),
|
2020-08-26 00:22:15 +02:00
|
|
|
n => {
|
2020-09-14 18:48:57 +02:00
|
|
|
return Err(custom_error(
|
2020-08-26 00:22:15 +02:00
|
|
|
"ReferenceError",
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("No such permission name: {n}"),
|
2020-08-26 00:22:15 +02:00
|
|
|
))
|
|
|
|
}
|
2020-08-18 21:29:32 +01:00
|
|
|
};
|
2021-04-05 18:40:24 +02:00
|
|
|
Ok(perm.to_string())
|
2019-08-14 17:03:02 +02:00
|
|
|
}
|
2019-11-12 00:33:29 +09:00
|
|
|
|
2022-03-14 23:14:15 +05:30
|
|
|
#[op]
|
2019-11-12 00:33:29 +09:00
|
|
|
pub fn op_request_permission(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 19:42:01 +01:00
|
|
|
args: PermissionArgs,
|
2021-04-05 18:40:24 +02:00
|
|
|
) -> Result<String, AnyError> {
|
2023-01-07 17:25:34 +01:00
|
|
|
let mut permissions = state.borrow_mut::<PermissionsContainer>().0.lock();
|
2020-05-29 16:27:43 +01:00
|
|
|
let path = args.path.as_deref();
|
2019-11-12 00:33:29 +09:00
|
|
|
let perm = match args.name.as_ref() {
|
2021-12-04 14:19:06 +01:00
|
|
|
"read" => permissions.read.request(path.map(Path::new)),
|
|
|
|
"write" => permissions.write.request(path.map(Path::new)),
|
2021-03-17 22:45:12 +01:00
|
|
|
"net" => permissions.net.request(
|
|
|
|
match args.host.as_deref() {
|
2020-12-30 22:35:28 +00:00
|
|
|
None => None,
|
|
|
|
Some(h) => Some(parse_host(h)?),
|
|
|
|
}
|
|
|
|
.as_ref(),
|
|
|
|
),
|
2021-04-13 13:25:21 +02:00
|
|
|
"env" => permissions.env.request(args.variable.as_deref()),
|
2022-09-28 21:46:50 +09:00
|
|
|
"sys" => permissions
|
|
|
|
.sys
|
2022-09-29 19:34:16 +09:00
|
|
|
.request(args.kind.as_deref().map(parse_sys_kind).transpose()?),
|
2021-04-10 00:12:00 +02:00
|
|
|
"run" => permissions.run.request(args.command.as_deref()),
|
2021-10-13 18:04:44 +01:00
|
|
|
"ffi" => permissions.ffi.request(args.path.as_deref().map(Path::new)),
|
2021-03-17 22:45:12 +01:00
|
|
|
"hrtime" => permissions.hrtime.request(),
|
2020-08-26 00:22:15 +02:00
|
|
|
n => {
|
2020-09-14 18:48:57 +02:00
|
|
|
return Err(custom_error(
|
2020-08-26 00:22:15 +02:00
|
|
|
"ReferenceError",
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("No such permission name: {n}"),
|
2020-08-26 00:22:15 +02:00
|
|
|
))
|
|
|
|
}
|
2020-08-18 21:29:32 +01:00
|
|
|
};
|
2021-04-05 18:40:24 +02:00
|
|
|
Ok(perm.to_string())
|
2019-11-12 00:33:29 +09:00
|
|
|
}
|
2020-12-30 22:35:28 +00:00
|
|
|
|
|
|
|
fn parse_host(host_str: &str) -> Result<(String, Option<u16>), AnyError> {
|
2023-01-27 10:43:16 -05:00
|
|
|
let url = url::Url::parse(&format!("http://{host_str}/"))
|
2020-12-30 22:35:28 +00:00
|
|
|
.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()))
|
|
|
|
}
|