1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-04 08:54:20 -05:00
denoland-deno/runtime/ops/permissions.rs

134 lines
4 KiB
Rust
Raw Normal View History

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
use crate::permissions::Permissions;
use deno_core::error::custom_error;
use deno_core::error::uri_error;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::url;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use serde::Deserialize;
use std::path::Path;
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);
}
#[derive(Deserialize)]
struct PermissionArgs {
name: String,
path: Option<String>,
host: Option<String>,
}
pub fn op_query_permission(
state: &mut OpState,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let args: PermissionArgs = serde_json::from_value(args)?;
let permissions = state.borrow::<Permissions>();
let path = args.path.as_deref();
2020-08-18 16:29:32 -04:00
let perm = match args.name.as_ref() {
"read" => permissions.query_read(&path.as_deref().map(Path::new)),
"write" => permissions.query_write(&path.as_deref().map(Path::new)),
"net" => permissions.query_net(
&match args.host.as_deref() {
None => None,
Some(h) => Some(parse_host(h)?),
}
.as_ref(),
),
2020-08-18 16:29:32 -04:00
"env" => permissions.query_env(),
"run" => permissions.query_run(),
"plugin" => permissions.query_plugin(),
"hrtime" => permissions.query_hrtime(),
n => {
return Err(custom_error(
"ReferenceError",
format!("No such permission name: {}", n),
))
}
2020-08-18 16:29:32 -04:00
};
Ok(json!({ "state": perm.to_string() }))
}
pub fn op_revoke_permission(
state: &mut OpState,
2019-08-26 08:50:21 -04:00
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let args: PermissionArgs = serde_json::from_value(args)?;
let permissions = state.borrow_mut::<Permissions>();
let path = args.path.as_deref();
2020-08-18 16:29:32 -04:00
let perm = match args.name.as_ref() {
"read" => permissions.revoke_read(&path.as_deref().map(Path::new)),
"write" => permissions.revoke_write(&path.as_deref().map(Path::new)),
"net" => permissions.revoke_net(
&match args.host.as_deref() {
None => None,
Some(h) => Some(parse_host(h)?),
}
.as_ref(),
),
2020-08-18 16:29:32 -04:00
"env" => permissions.revoke_env(),
"run" => permissions.revoke_run(),
"plugin" => permissions.revoke_plugin(),
"hrtime" => permissions.revoke_hrtime(),
n => {
return Err(custom_error(
"ReferenceError",
format!("No such permission name: {}", n),
))
}
2020-08-18 16:29:32 -04:00
};
Ok(json!({ "state": perm.to_string() }))
}
2019-11-11 10:33:29 -05:00
pub fn op_request_permission(
state: &mut OpState,
2019-11-11 10:33:29 -05:00
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
2019-11-11 10:33:29 -05:00
let args: PermissionArgs = serde_json::from_value(args)?;
let permissions = state.borrow_mut::<Permissions>();
let path = args.path.as_deref();
2019-11-11 10:33:29 -05:00
let perm = match args.name.as_ref() {
2020-08-18 16:29:32 -04:00
"read" => permissions.request_read(&path.as_deref().map(Path::new)),
"write" => permissions.request_write(&path.as_deref().map(Path::new)),
"net" => permissions.request_net(
&match args.host.as_deref() {
None => None,
Some(h) => Some(parse_host(h)?),
}
.as_ref(),
),
2020-08-18 16:29:32 -04:00
"env" => permissions.request_env(),
"run" => permissions.request_run(),
"plugin" => permissions.request_plugin(),
"hrtime" => permissions.request_hrtime(),
n => {
return Err(custom_error(
"ReferenceError",
format!("No such permission name: {}", n),
))
}
2020-08-18 16:29:32 -04:00
};
Ok(json!({ "state": perm.to_string() }))
2019-11-11 10:33:29 -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()))
}