Skip to content

Commit 5421a29

Browse files
authored
refactor: remove permission traits + generics from extension crates (#31284)
I also moved the `RuntimePermissionDescriptorParser` struct from deno_runtime into deno_permissions, so that extension crates can use it in tests and stuff like that
1 parent 8b31587 commit 5421a29

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1222
-2319
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/lib/npm/permission_checker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::path::Path;
77
use std::path::PathBuf;
88

99
use deno_error::JsErrorBox;
10-
use deno_runtime::deno_node::NodePermissions;
1110
use deno_runtime::deno_permissions::OpenAccessKind;
11+
use deno_runtime::deno_permissions::PermissionsContainer;
1212
use parking_lot::Mutex;
1313

1414
use crate::sys::DenoLibSys;
@@ -49,7 +49,7 @@ impl<TSys: DenoLibSys> NpmRegistryReadPermissionChecker<TSys> {
4949
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
5050
pub fn ensure_read_permission<'a>(
5151
&self,
52-
permissions: &mut dyn NodePermissions,
52+
permissions: &mut PermissionsContainer,
5353
path: Cow<'a, Path>,
5454
) -> Result<Cow<'a, Path>, JsErrorBox> {
5555
if permissions.query_read_all() {

cli/module_loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ impl<TGraphContainer: ModuleGraphContainer> NodeRequireLoader
13231323
{
13241324
fn ensure_read_permission<'a>(
13251325
&self,
1326-
permissions: &mut dyn deno_runtime::deno_node::NodePermissions,
1326+
permissions: &mut PermissionsContainer,
13271327
path: Cow<'a, Path>,
13281328
) -> Result<Cow<'a, Path>, JsErrorBox> {
13291329
if let Ok(url) = deno_path_util::url_from_file_path(&path) {

cli/rt/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ impl ModuleLoader for EmbeddedModuleLoader {
610610
impl NodeRequireLoader for EmbeddedModuleLoader {
611611
fn ensure_read_permission<'a>(
612612
&self,
613-
permissions: &mut dyn deno_runtime::deno_node::NodePermissions,
613+
permissions: &mut PermissionsContainer,
614614
path: Cow<'a, Path>,
615615
) -> Result<Cow<'a, Path>, JsErrorBox> {
616616
if self.shared.modules.has_file(&path) {

ext/fetch/lib.rs

Lines changed: 13 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ use deno_core::v8;
5151
use deno_error::JsErrorBox;
5252
pub use deno_fs::FsError;
5353
use deno_path_util::PathToUrlError;
54-
use deno_permissions::CheckedPath;
5554
use deno_permissions::OpenAccessKind;
5655
use deno_permissions::PermissionCheckError;
56+
use deno_permissions::PermissionsContainer;
5757
use deno_tls::Proxy;
5858
use deno_tls::RootCertStoreProvider;
5959
use deno_tls::SocketUse;
@@ -146,12 +146,11 @@ impl Default for Options {
146146

147147
deno_core::extension!(deno_fetch,
148148
deps = [ deno_webidl, deno_web ],
149-
parameters = [FP: FetchPermissions],
150149
ops = [
151-
op_fetch<FP>,
150+
op_fetch,
152151
op_fetch_send,
153152
op_utf8_to_byte_string,
154-
op_fetch_custom_client<FP>,
153+
op_fetch_custom_client,
155154
op_fetch_promise_is_settled,
156155
],
157156
esm = [
@@ -404,91 +403,12 @@ impl Drop for ResourceToBodyAdapter {
404403
}
405404
}
406405

407-
pub trait FetchPermissions {
408-
fn check_net(
409-
&mut self,
410-
host: &str,
411-
port: u16,
412-
api_name: &str,
413-
) -> Result<(), PermissionCheckError>;
414-
fn check_net_url(
415-
&mut self,
416-
url: &Url,
417-
api_name: &str,
418-
) -> Result<(), PermissionCheckError>;
419-
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
420-
fn check_open<'a>(
421-
&mut self,
422-
path: Cow<'a, Path>,
423-
open_access: OpenAccessKind,
424-
api_name: &str,
425-
) -> Result<CheckedPath<'a>, PermissionCheckError>;
426-
fn check_net_vsock(
427-
&mut self,
428-
cid: u32,
429-
port: u32,
430-
api_name: &str,
431-
) -> Result<(), PermissionCheckError>;
432-
}
433-
434-
impl FetchPermissions for deno_permissions::PermissionsContainer {
435-
#[inline(always)]
436-
fn check_net(
437-
&mut self,
438-
host: &str,
439-
port: u16,
440-
api_name: &str,
441-
) -> Result<(), PermissionCheckError> {
442-
deno_permissions::PermissionsContainer::check_net(
443-
self,
444-
&(host, Some(port)),
445-
api_name,
446-
)
447-
}
448-
449-
#[inline(always)]
450-
fn check_net_url(
451-
&mut self,
452-
url: &Url,
453-
api_name: &str,
454-
) -> Result<(), PermissionCheckError> {
455-
deno_permissions::PermissionsContainer::check_net_url(self, url, api_name)
456-
}
457-
458-
#[inline(always)]
459-
fn check_open<'a>(
460-
&mut self,
461-
path: Cow<'a, Path>,
462-
open_access: OpenAccessKind,
463-
api_name: &str,
464-
) -> Result<CheckedPath<'a>, PermissionCheckError> {
465-
deno_permissions::PermissionsContainer::check_open(
466-
self,
467-
path,
468-
open_access,
469-
Some(api_name),
470-
)
471-
}
472-
473-
#[inline(always)]
474-
fn check_net_vsock(
475-
&mut self,
476-
cid: u32,
477-
port: u32,
478-
api_name: &str,
479-
) -> Result<(), PermissionCheckError> {
480-
deno_permissions::PermissionsContainer::check_net_vsock(
481-
self, cid, port, api_name,
482-
)
483-
}
484-
}
485-
486406
#[op2(stack_trace)]
487407
#[serde]
488408
#[allow(clippy::too_many_arguments)]
489409
#[allow(clippy::large_enum_variant)]
490410
#[allow(clippy::result_large_err)]
491-
pub fn op_fetch<FP>(
411+
pub fn op_fetch(
492412
state: &mut OpState,
493413
#[serde] method: ByteString,
494414
#[string] url: String,
@@ -497,10 +417,7 @@ pub fn op_fetch<FP>(
497417
has_body: bool,
498418
#[buffer] data: Option<JsBuffer>,
499419
#[smi] resource: Option<ResourceId>,
500-
) -> Result<FetchReturn, FetchError>
501-
where
502-
FP: FetchPermissions + 'static,
503-
{
420+
) -> Result<FetchReturn, FetchError> {
504421
let (client, allow_host) = if let Some(rid) = client_rid {
505422
let r = state.resource_table.get::<HttpClientResource>(rid)?;
506423
(r.client.clone(), r.allow_host)
@@ -533,7 +450,7 @@ where
533450
(request_rid, maybe_cancel_handle_rid)
534451
}
535452
"http" | "https" => {
536-
let permissions = state.borrow_mut::<FP>();
453+
let permissions = state.borrow_mut::<PermissionsContainer>();
537454
permissions.check_net_url(&url, "fetch()")?;
538455

539456
let maybe_authority = extract_authority(&mut url);
@@ -922,23 +839,21 @@ fn default_true() -> bool {
922839
#[op2(stack_trace)]
923840
#[smi]
924841
#[allow(clippy::result_large_err)]
925-
pub fn op_fetch_custom_client<FP>(
842+
pub fn op_fetch_custom_client(
926843
state: &mut OpState,
927844
#[serde] mut args: CreateHttpClientArgs,
928845
#[cppgc] tls_keys: &TlsKeysHolder,
929-
) -> Result<ResourceId, FetchError>
930-
where
931-
FP: FetchPermissions + 'static,
932-
{
846+
) -> Result<ResourceId, FetchError> {
933847
if let Some(proxy) = &mut args.proxy {
934-
let permissions = state.borrow_mut::<FP>();
848+
let permissions = state.borrow_mut::<PermissionsContainer>();
935849
match proxy {
936850
Proxy::Http { url, .. } => {
937851
let url = Url::parse(url)?;
938852
permissions.check_net_url(&url, "Deno.createHttpClient()")?;
939853
}
940854
Proxy::Tcp { hostname, port } => {
941-
permissions.check_net(hostname, *port, "Deno.createHttpClient()")?;
855+
permissions
856+
.check_net(&(hostname, Some(*port)), "Deno.createHttpClient()")?;
942857
}
943858
Proxy::Unix {
944859
path: original_path,
@@ -948,15 +863,15 @@ where
948863
.check_open(
949864
Cow::Borrowed(path),
950865
OpenAccessKind::ReadWriteNoFollow,
951-
"Deno.createHttpClient()",
866+
Some("Deno.createHttpClient()"),
952867
)?
953868
.into_path();
954869
if path != resolved_path {
955870
*original_path = resolved_path.to_string_lossy().into_owned();
956871
}
957872
}
958873
Proxy::Vsock { cid, port } => {
959-
let permissions = state.borrow_mut::<FP>();
874+
let permissions = state.borrow_mut::<PermissionsContainer>();
960875
permissions.check_net_vsock(*cid, *port, "Deno.createHttpClient()")?;
961876
}
962877
}

ext/ffi/call.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use deno_core::serde_v8::BigInt as V8BigInt;
1313
use deno_core::serde_v8::ExternalPointer;
1414
use deno_core::unsync::spawn_blocking;
1515
use deno_core::v8;
16+
use deno_permissions::PermissionsContainer;
1617
use libffi::middle::Arg;
1718
use num_bigint::BigInt;
1819
use serde::Serialize;
1920

20-
use crate::FfiPermissions;
2121
use crate::ForeignFunction;
2222
use crate::callback::PtrSymbol;
2323
use crate::dlfcn::DynamicLibraryResource;
@@ -300,24 +300,20 @@ fn ffi_call(
300300

301301
#[op2(async, stack_trace)]
302302
#[serde]
303-
pub fn op_ffi_call_ptr_nonblocking<FP>(
303+
pub fn op_ffi_call_ptr_nonblocking(
304304
scope: &mut v8::PinScope<'_, '_>,
305305
state: Rc<RefCell<OpState>>,
306306
pointer: *mut c_void,
307307
#[serde] def: ForeignFunction,
308308
parameters: v8::Local<v8::Array>,
309309
out_buffer: Option<v8::Local<v8::TypedArray>>,
310-
) -> Result<
311-
impl Future<Output = Result<FfiValue, CallError>> + use<FP>,
312-
CallError,
313-
>
310+
) -> Result<impl Future<Output = Result<FfiValue, CallError>> + use<>, CallError>
314311
where
315-
FP: FfiPermissions + 'static,
316312
{
317313
{
318314
let mut state = state.borrow_mut();
319-
let permissions = state.borrow_mut::<FP>();
320-
permissions.check_partial_no_path()?;
315+
let permissions = state.borrow_mut::<PermissionsContainer>();
316+
permissions.check_ffi_partial_no_path()?;
321317
};
322318

323319
let symbol = PtrSymbol::new(pointer, &def)?;
@@ -399,21 +395,18 @@ pub fn op_ffi_call_nonblocking(
399395

400396
#[op2(reentrant, stack_trace)]
401397
#[serde]
402-
pub fn op_ffi_call_ptr<FP>(
398+
pub fn op_ffi_call_ptr(
403399
scope: &mut v8::PinScope<'_, '_>,
404400
state: Rc<RefCell<OpState>>,
405401
pointer: *mut c_void,
406402
#[serde] def: ForeignFunction,
407403
parameters: v8::Local<v8::Array>,
408404
out_buffer: Option<v8::Local<v8::TypedArray>>,
409-
) -> Result<FfiValue, CallError>
410-
where
411-
FP: FfiPermissions + 'static,
412-
{
405+
) -> Result<FfiValue, CallError> {
413406
{
414407
let mut state = state.borrow_mut();
415-
let permissions = state.borrow_mut::<FP>();
416-
permissions.check_partial_no_path()?;
408+
let permissions = state.borrow_mut::<PermissionsContainer>();
409+
permissions.check_ffi_partial_no_path()?;
417410
};
418411

419412
let symbol = PtrSymbol::new(pointer, &def)?;

ext/ffi/callback.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ use deno_core::ResourceId;
2121
use deno_core::V8CrossThreadTaskSpawner;
2222
use deno_core::op2;
2323
use deno_core::v8;
24+
use deno_permissions::PermissionsContainer;
2425
use libffi::middle::Cif;
2526
use serde::Deserialize;
2627

27-
use crate::FfiPermissions;
2828
use crate::ForeignFunction;
2929
use crate::symbol::NativeType;
3030

@@ -575,17 +575,14 @@ pub struct RegisterCallbackArgs {
575575
}
576576

577577
#[op2(stack_trace)]
578-
pub fn op_ffi_unsafe_callback_create<FP, 'scope>(
578+
pub fn op_ffi_unsafe_callback_create<'scope>(
579579
state: &mut OpState,
580580
scope: &mut v8::PinScope<'scope, '_>,
581581
#[serde] args: RegisterCallbackArgs,
582582
cb: v8::Local<v8::Function>,
583-
) -> Result<v8::Local<'scope, v8::Value>, CallbackError>
584-
where
585-
FP: FfiPermissions + 'static,
586-
{
587-
let permissions = state.borrow_mut::<FP>();
588-
permissions.check_partial_no_path()?;
583+
) -> Result<v8::Local<'scope, v8::Value>, CallbackError> {
584+
let permissions = state.borrow_mut::<PermissionsContainer>();
585+
permissions.check_ffi_partial_no_path()?;
589586

590587
let thread_id: u32 = LOCAL_THREAD_ID.with(|s| {
591588
let value = *s.borrow();

ext/ffi/dlfcn.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ use deno_core::op2;
1414
use deno_core::v8;
1515
use deno_error::JsErrorBox;
1616
use deno_error::JsErrorClass;
17+
use deno_permissions::PermissionsContainer;
1718
use denort_helper::DenoRtNativeAddonLoaderRc;
1819
use dlopen2::raw::Library;
1920
use serde::Deserialize;
2021
use serde_value::ValueDeserializer;
2122

22-
use crate::FfiPermissions;
2323
use crate::ir::out_buffer_as_ptr;
2424
use crate::symbol::NativeType;
2525
use crate::symbol::Symbol;
@@ -143,20 +143,18 @@ impl<'de> Deserialize<'de> for ForeignSymbol {
143143
}
144144

145145
#[op2(stack_trace)]
146-
pub fn op_ffi_load<'scope, FP>(
146+
pub fn op_ffi_load<'scope>(
147147
scope: &mut v8::PinScope<'scope, '_>,
148148
state: Rc<RefCell<OpState>>,
149149
#[string] path: &str,
150150
#[serde] symbols: HashMap<String, ForeignSymbol>,
151-
) -> Result<v8::Local<'scope, v8::Value>, DlfcnError>
152-
where
153-
FP: FfiPermissions + 'static,
154-
{
151+
) -> Result<v8::Local<'scope, v8::Value>, DlfcnError> {
155152
let (path, denort_helper) = {
156153
let mut state = state.borrow_mut();
157-
let permissions = state.borrow_mut::<FP>();
154+
let permissions = state.borrow_mut::<PermissionsContainer>();
158155
(
159-
permissions.check_partial_with_path(Cow::Borrowed(Path::new(path)))?,
156+
permissions
157+
.check_ffi_partial_with_path(Cow::Borrowed(Path::new(path)))?,
160158
state.try_borrow::<DenoRtNativeAddonLoaderRc>().cloned(),
161159
)
162160
};

0 commit comments

Comments
 (0)