mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 04:51:22 +00:00
de28e6fc09
Issue https://github.com/denoland/deno/issues/22222 ![image](https://github.com/denoland/deno/assets/34997667/2af8474b-b919-4519-98ce-9d29bc7829f2) This PR moves `runtime/permissions` code to a upstream crate called `deno_permissions`. The `deno_permissions::PermissionsContainer` is put into the OpState and can be used instead of the current trait-based permissions system. For this PR, I've migrated `deno_fetch` to the new crate but kept the rest of the trait-based system as a wrapper of `deno_permissions` crate. Doing the migration all at once is error prone and hard to review. Comparing incremental compile times for `ext/fetch` on Mac M1: | profile | `cargo build --bin deno` | `cargo plonk build --bin deno` | | --------- | ------------- | ------------------- | | `debug` | 20 s | 0.8s | | `release` | 4 mins 12 s | 1.4s |
168 lines
4.7 KiB
Rust
168 lines
4.7 KiB
Rust
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use ::deno_permissions::parse_sys_kind;
|
|
use ::deno_permissions::PermissionState;
|
|
use ::deno_permissions::PermissionsContainer;
|
|
use deno_core::error::custom_error;
|
|
use deno_core::error::uri_error;
|
|
use deno_core::error::AnyError;
|
|
use deno_core::op2;
|
|
use deno_core::url;
|
|
use deno_core::OpState;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use std::path::Path;
|
|
|
|
deno_core::extension!(
|
|
deno_permissions,
|
|
ops = [
|
|
op_query_permission,
|
|
op_revoke_permission,
|
|
op_request_permission,
|
|
],
|
|
);
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct PermissionArgs {
|
|
name: String,
|
|
path: Option<String>,
|
|
host: Option<String>,
|
|
variable: Option<String>,
|
|
kind: Option<String>,
|
|
command: Option<String>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct PermissionStatus {
|
|
state: String,
|
|
partial: bool,
|
|
}
|
|
|
|
impl From<PermissionState> for PermissionStatus {
|
|
fn from(state: PermissionState) -> Self {
|
|
PermissionStatus {
|
|
state: if state == PermissionState::GrantedPartial {
|
|
PermissionState::Granted.to_string()
|
|
} else {
|
|
state.to_string()
|
|
},
|
|
partial: state == PermissionState::GrantedPartial,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[op2]
|
|
#[serde]
|
|
pub fn op_query_permission(
|
|
state: &mut OpState,
|
|
#[serde] args: PermissionArgs,
|
|
) -> Result<PermissionStatus, AnyError> {
|
|
let permissions = state.borrow::<PermissionsContainer>().0.lock();
|
|
let path = args.path.as_deref();
|
|
let perm = match args.name.as_ref() {
|
|
"read" => permissions.read.query(path.map(Path::new)),
|
|
"write" => permissions.write.query(path.map(Path::new)),
|
|
"net" => permissions.net.query(
|
|
match args.host.as_deref() {
|
|
None => None,
|
|
Some(h) => Some(parse_host(h)?),
|
|
}
|
|
.as_ref(),
|
|
),
|
|
"env" => permissions.env.query(args.variable.as_deref()),
|
|
"sys" => permissions
|
|
.sys
|
|
.query(args.kind.as_deref().map(parse_sys_kind).transpose()?),
|
|
"run" => permissions.run.query(args.command.as_deref()),
|
|
"ffi" => permissions.ffi.query(args.path.as_deref().map(Path::new)),
|
|
"hrtime" => permissions.hrtime.query(),
|
|
n => {
|
|
return Err(custom_error(
|
|
"ReferenceError",
|
|
format!("No such permission name: {n}"),
|
|
))
|
|
}
|
|
};
|
|
Ok(PermissionStatus::from(perm))
|
|
}
|
|
|
|
#[op2]
|
|
#[serde]
|
|
pub fn op_revoke_permission(
|
|
state: &mut OpState,
|
|
#[serde] args: PermissionArgs,
|
|
) -> Result<PermissionStatus, AnyError> {
|
|
let mut permissions = state.borrow_mut::<PermissionsContainer>().0.lock();
|
|
let path = args.path.as_deref();
|
|
let perm = match args.name.as_ref() {
|
|
"read" => permissions.read.revoke(path.map(Path::new)),
|
|
"write" => permissions.write.revoke(path.map(Path::new)),
|
|
"net" => permissions.net.revoke(
|
|
match args.host.as_deref() {
|
|
None => None,
|
|
Some(h) => Some(parse_host(h)?),
|
|
}
|
|
.as_ref(),
|
|
),
|
|
"env" => permissions.env.revoke(args.variable.as_deref()),
|
|
"sys" => permissions
|
|
.sys
|
|
.revoke(args.kind.as_deref().map(parse_sys_kind).transpose()?),
|
|
"run" => permissions.run.revoke(args.command.as_deref()),
|
|
"ffi" => permissions.ffi.revoke(args.path.as_deref().map(Path::new)),
|
|
"hrtime" => permissions.hrtime.revoke(),
|
|
n => {
|
|
return Err(custom_error(
|
|
"ReferenceError",
|
|
format!("No such permission name: {n}"),
|
|
))
|
|
}
|
|
};
|
|
Ok(PermissionStatus::from(perm))
|
|
}
|
|
|
|
#[op2]
|
|
#[serde]
|
|
pub fn op_request_permission(
|
|
state: &mut OpState,
|
|
#[serde] args: PermissionArgs,
|
|
) -> Result<PermissionStatus, AnyError> {
|
|
let mut permissions = state.borrow_mut::<PermissionsContainer>().0.lock();
|
|
let path = args.path.as_deref();
|
|
let perm = match args.name.as_ref() {
|
|
"read" => permissions.read.request(path.map(Path::new)),
|
|
"write" => permissions.write.request(path.map(Path::new)),
|
|
"net" => permissions.net.request(
|
|
match args.host.as_deref() {
|
|
None => None,
|
|
Some(h) => Some(parse_host(h)?),
|
|
}
|
|
.as_ref(),
|
|
),
|
|
"env" => permissions.env.request(args.variable.as_deref()),
|
|
"sys" => permissions
|
|
.sys
|
|
.request(args.kind.as_deref().map(parse_sys_kind).transpose()?),
|
|
"run" => permissions.run.request(args.command.as_deref()),
|
|
"ffi" => permissions.ffi.request(args.path.as_deref().map(Path::new)),
|
|
"hrtime" => permissions.hrtime.request(),
|
|
n => {
|
|
return Err(custom_error(
|
|
"ReferenceError",
|
|
format!("No such permission name: {n}"),
|
|
))
|
|
}
|
|
};
|
|
Ok(PermissionStatus::from(perm))
|
|
}
|
|
|
|
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()))
|
|
}
|