Skip to content

Commit cf49599

Browse files
feat: permission stack traces in ops (#26938)
This commit improves permission prompts by adding an option to print a full trace of where the permissions is being requested. Due to big performance hint of stack trace collection, this is only enabled when `DENO_TRACE_PERMISSIONS` env var is present. Closes #20756 --------- Co-authored-by: Bartek Iwańczuk <[email protected]>
1 parent 8f72798 commit cf49599

34 files changed

+294
-181
lines changed

cli/args/flags.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -1160,25 +1160,26 @@ static ENV_VARIABLES_HELP: &str = cstr!(
11601160
<y>Docs:</> <c>https://docs.deno.com/go/env-vars</>
11611161
11621162
<g>DENO_AUTH_TOKENS</> A semi-colon separated list of bearer tokens and hostnames
1163-
to use when fetching remote modules from private repositories
1164-
1165-
<g>DENO_CERT</> Load certificate authorities from PEM encoded file
1166-
<g>DENO_DIR</> Set the cache directory
1167-
<g>DENO_INSTALL_ROOT</> Set deno install's output directory
1168-
<p(245)>(defaults to $HOME/.deno/bin)</>
1169-
<g>DENO_NO_PACKAGE_JSON</> Disables auto-resolution of package.json
1170-
<g>DENO_NO_UPDATE_CHECK</> Set to disable checking if a newer Deno version is available
1171-
<g>DENO_TLS_CA_STORE</> Comma-separated list of order dependent certificate stores.
1172-
Possible values: "system", "mozilla".
1173-
<p(245)>(defaults to "mozilla")</>
1174-
<g>HTTP_PROXY</> Proxy address for HTTP requests
1175-
<p(245)>(module downloads, fetch)</>
1176-
<g>HTTPS_PROXY</> Proxy address for HTTPS requests
1177-
<p(245)>(module downloads, fetch)</>
1178-
<g>NO_COLOR</> Set to disable color
1179-
<g>NO_PROXY</> Comma-separated list of hosts which do not use a proxy
1180-
<p(245)>(module downloads, fetch)</>
1181-
<g>NPM_CONFIG_REGISTRY</> URL to use for the npm registry."#
1163+
to use when fetching remote modules from private repositories
1164+
1165+
<g>DENO_CERT</> Load certificate authorities from PEM encoded file
1166+
<g>DENO_DIR</> Set the cache directory
1167+
<g>DENO_INSTALL_ROOT</> Set deno install's output directory
1168+
<p(245)>(defaults to $HOME/.deno/bin)</>
1169+
<g>DENO_NO_PACKAGE_JSON</> Disables auto-resolution of package.json
1170+
<g>DENO_NO_UPDATE_CHECK</> Set to disable checking if a newer Deno version is available
1171+
<g>DENO_TLS_CA_STORE</> Comma-separated list of order dependent certificate stores.
1172+
<g>DENO_TRACE_PERMISSIONS</> Environmental variable to enable stack traces in permission prompts.
1173+
Possible values: "system", "mozilla".
1174+
<p(245)>(defaults to "mozilla")</>
1175+
<g>HTTP_PROXY</> Proxy address for HTTP requests
1176+
<p(245)>(module downloads, fetch)</>
1177+
<g>HTTPS_PROXY</> Proxy address for HTTPS requests
1178+
<p(245)>(module downloads, fetch)</>
1179+
<g>NO_COLOR</> Set to disable color
1180+
<g>NO_PROXY</> Comma-separated list of hosts which do not use a proxy
1181+
<p(245)>(module downloads, fetch)</>
1182+
<g>NPM_CONFIG_REGISTRY</> URL to use for the npm registry."#
11821183
);
11831184

11841185
static DENO_HELP: &str = cstr!(
@@ -3346,6 +3347,8 @@ fn permission_args(app: Command, requires: Option<&'static str>) -> Command {
33463347
<p(245)>--deny-run | --deny-run="whoami,ps"</>
33473348
<g>--deny-ffi[=<<PATH>...]</> (Unstable) Deny loading dynamic libraries. Optionally specify denied directories or files.
33483349
<p(245)>--deny-ffi | --deny-ffi="./libfoo.so"</>
3350+
<g>DENO_TRACE_PERMISSIONS</> Environmental variable to enable stack traces in permission prompts.
3351+
<p(245)>DENO_TRACE_PERMISSIONS=1 deno run main.ts</>
33493352
"#))
33503353
.arg(
33513354
{

cli/args/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,10 @@ pub fn resolve_no_prompt(flags: &PermissionFlags) -> bool {
19121912
flags.no_prompt || has_flag_env_var("DENO_NO_PROMPT")
19131913
}
19141914

1915+
pub fn has_trace_permissions_enabled() -> bool {
1916+
has_flag_env_var("DENO_TRACE_PERMISSIONS")
1917+
}
1918+
19151919
pub fn has_flag_env_var(name: &str) -> bool {
19161920
let value = env::var(name);
19171921
matches!(value.as_ref().map(|s| s.as_str()), Ok("1"))

cli/ops/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn op_bench_get_origin(state: &mut OpState) -> String {
5151
#[derive(Clone)]
5252
struct PermissionsHolder(Uuid, PermissionsContainer);
5353

54-
#[op2]
54+
#[op2(stack_trace)]
5555
#[serde]
5656
pub fn op_pledge_test_permissions(
5757
state: &mut OpState,

cli/ops/testing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ deno_core::extension!(deno_test,
4646
#[derive(Clone)]
4747
struct PermissionsHolder(Uuid, PermissionsContainer);
4848

49-
#[op2]
49+
#[op2(stack_trace)]
5050
#[serde]
5151
pub fn op_pledge_test_permissions(
5252
state: &mut OpState,

cli/worker.rs

+4
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ impl CliMainWorkerFactory {
617617
origin_storage_dir,
618618
stdio,
619619
skip_op_registration: shared.options.skip_op_registration,
620+
enable_stack_trace_arg_in_ops: crate::args::has_trace_permissions_enabled(
621+
),
620622
};
621623

622624
let mut worker = MainWorker::bootstrap_from_options(
@@ -813,6 +815,8 @@ fn create_web_worker_callback(
813815
strace_ops: shared.options.strace_ops.clone(),
814816
close_on_idle: args.close_on_idle,
815817
maybe_worker_metadata: args.maybe_worker_metadata,
818+
enable_stack_trace_arg_in_ops: crate::args::has_trace_permissions_enabled(
819+
),
816820
};
817821

818822
WebWorker::bootstrap_from_options(services, options)

ext/fetch/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ impl FetchPermissions for deno_permissions::PermissionsContainer {
397397
}
398398
}
399399

400-
#[op2]
400+
#[op2(stack_trace)]
401401
#[serde]
402402
#[allow(clippy::too_many_arguments)]
403403
pub fn op_fetch<FP>(
@@ -866,7 +866,7 @@ fn default_true() -> bool {
866866
true
867867
}
868868

869-
#[op2]
869+
#[op2(stack_trace)]
870870
#[smi]
871871
pub fn op_fetch_custom_client<FP>(
872872
state: &mut OpState,

ext/ffi/call.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ fn ffi_call(
287287
}
288288
}
289289

290-
#[op2(async)]
290+
#[op2(async, stack_trace)]
291291
#[serde]
292292
pub fn op_ffi_call_ptr_nonblocking<FP>(
293293
scope: &mut v8::HandleScope,
@@ -385,7 +385,7 @@ pub fn op_ffi_call_nonblocking(
385385
})
386386
}
387387

388-
#[op2(reentrant)]
388+
#[op2(reentrant, stack_trace)]
389389
#[serde]
390390
pub fn op_ffi_call_ptr<FP>(
391391
scope: &mut v8::HandleScope,

ext/ffi/callback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ pub struct RegisterCallbackArgs {
561561
result: NativeType,
562562
}
563563

564-
#[op2]
564+
#[op2(stack_trace)]
565565
pub fn op_ffi_unsafe_callback_create<FP, 'scope>(
566566
state: &mut OpState,
567567
scope: &mut v8::HandleScope<'scope>,

ext/ffi/dlfcn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub struct FfiLoadArgs {
124124
symbols: HashMap<String, ForeignSymbol>,
125125
}
126126

127-
#[op2]
127+
#[op2(stack_trace)]
128128
pub fn op_ffi_load<'scope, FP>(
129129
scope: &mut v8::HandleScope<'scope>,
130130
state: Rc<RefCell<OpState>>,

ext/ffi/repr.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub enum ReprError {
4949
Permission(#[from] deno_permissions::PermissionCheckError),
5050
}
5151

52-
#[op2(fast)]
52+
#[op2(fast, stack_trace)]
5353
pub fn op_ffi_ptr_create<FP>(
5454
state: &mut OpState,
5555
#[bigint] ptr_number: usize,
@@ -63,7 +63,7 @@ where
6363
Ok(ptr_number as *mut c_void)
6464
}
6565

66-
#[op2(fast)]
66+
#[op2(fast, stack_trace)]
6767
pub fn op_ffi_ptr_equals<FP>(
6868
state: &mut OpState,
6969
a: *const c_void,
@@ -78,7 +78,7 @@ where
7878
Ok(a == b)
7979
}
8080

81-
#[op2]
81+
#[op2(stack_trace)]
8282
pub fn op_ffi_ptr_of<FP>(
8383
state: &mut OpState,
8484
#[anybuffer] buf: *const u8,
@@ -92,7 +92,7 @@ where
9292
Ok(buf as *mut c_void)
9393
}
9494

95-
#[op2(fast)]
95+
#[op2(fast, stack_trace)]
9696
pub fn op_ffi_ptr_of_exact<FP>(
9797
state: &mut OpState,
9898
buf: v8::Local<v8::ArrayBufferView>,
@@ -112,7 +112,7 @@ where
112112
Ok(buf.as_ptr() as _)
113113
}
114114

115-
#[op2(fast)]
115+
#[op2(fast, stack_trace)]
116116
pub fn op_ffi_ptr_offset<FP>(
117117
state: &mut OpState,
118118
ptr: *mut c_void,
@@ -142,7 +142,7 @@ unsafe extern "C" fn noop_deleter_callback(
142142
) {
143143
}
144144

145-
#[op2(fast)]
145+
#[op2(fast, stack_trace)]
146146
#[bigint]
147147
pub fn op_ffi_ptr_value<FP>(
148148
state: &mut OpState,
@@ -157,7 +157,7 @@ where
157157
Ok(ptr as usize)
158158
}
159159

160-
#[op2]
160+
#[op2(stack_trace)]
161161
pub fn op_ffi_get_buf<FP, 'scope>(
162162
scope: &mut v8::HandleScope<'scope>,
163163
state: &mut OpState,
@@ -189,7 +189,7 @@ where
189189
Ok(array_buffer)
190190
}
191191

192-
#[op2]
192+
#[op2(stack_trace)]
193193
pub fn op_ffi_buf_copy_into<FP>(
194194
state: &mut OpState,
195195
src: *mut c_void,
@@ -219,7 +219,7 @@ where
219219
}
220220
}
221221

222-
#[op2]
222+
#[op2(stack_trace)]
223223
pub fn op_ffi_cstr_read<FP, 'scope>(
224224
scope: &mut v8::HandleScope<'scope>,
225225
state: &mut OpState,
@@ -244,7 +244,7 @@ where
244244
Ok(value)
245245
}
246246

247-
#[op2(fast)]
247+
#[op2(fast, stack_trace)]
248248
pub fn op_ffi_read_bool<FP>(
249249
state: &mut OpState,
250250
ptr: *mut c_void,
@@ -264,7 +264,7 @@ where
264264
Ok(unsafe { ptr::read_unaligned::<bool>(ptr.offset(offset) as *const bool) })
265265
}
266266

267-
#[op2(fast)]
267+
#[op2(fast, stack_trace)]
268268
pub fn op_ffi_read_u8<FP>(
269269
state: &mut OpState,
270270
ptr: *mut c_void,
@@ -286,7 +286,7 @@ where
286286
})
287287
}
288288

289-
#[op2(fast)]
289+
#[op2(fast, stack_trace)]
290290
pub fn op_ffi_read_i8<FP>(
291291
state: &mut OpState,
292292
ptr: *mut c_void,
@@ -308,7 +308,7 @@ where
308308
})
309309
}
310310

311-
#[op2(fast)]
311+
#[op2(fast, stack_trace)]
312312
pub fn op_ffi_read_u16<FP>(
313313
state: &mut OpState,
314314
ptr: *mut c_void,
@@ -330,7 +330,7 @@ where
330330
})
331331
}
332332

333-
#[op2(fast)]
333+
#[op2(fast, stack_trace)]
334334
pub fn op_ffi_read_i16<FP>(
335335
state: &mut OpState,
336336
ptr: *mut c_void,
@@ -352,7 +352,7 @@ where
352352
})
353353
}
354354

355-
#[op2(fast)]
355+
#[op2(fast, stack_trace)]
356356
pub fn op_ffi_read_u32<FP>(
357357
state: &mut OpState,
358358
ptr: *mut c_void,
@@ -372,7 +372,7 @@ where
372372
Ok(unsafe { ptr::read_unaligned::<u32>(ptr.offset(offset) as *const u32) })
373373
}
374374

375-
#[op2(fast)]
375+
#[op2(fast, stack_trace)]
376376
pub fn op_ffi_read_i32<FP>(
377377
state: &mut OpState,
378378
ptr: *mut c_void,
@@ -392,7 +392,7 @@ where
392392
Ok(unsafe { ptr::read_unaligned::<i32>(ptr.offset(offset) as *const i32) })
393393
}
394394

395-
#[op2(fast)]
395+
#[op2(fast, stack_trace)]
396396
#[bigint]
397397
pub fn op_ffi_read_u64<FP>(
398398
state: &mut OpState,
@@ -418,7 +418,7 @@ where
418418
Ok(value)
419419
}
420420

421-
#[op2(fast)]
421+
#[op2(fast, stack_trace)]
422422
#[bigint]
423423
pub fn op_ffi_read_i64<FP>(
424424
state: &mut OpState,
@@ -444,7 +444,7 @@ where
444444
Ok(value)
445445
}
446446

447-
#[op2(fast)]
447+
#[op2(fast, stack_trace)]
448448
pub fn op_ffi_read_f32<FP>(
449449
state: &mut OpState,
450450
ptr: *mut c_void,
@@ -464,7 +464,7 @@ where
464464
Ok(unsafe { ptr::read_unaligned::<f32>(ptr.offset(offset) as *const f32) })
465465
}
466466

467-
#[op2(fast)]
467+
#[op2(fast, stack_trace)]
468468
pub fn op_ffi_read_f64<FP>(
469469
state: &mut OpState,
470470
ptr: *mut c_void,
@@ -484,7 +484,7 @@ where
484484
Ok(unsafe { ptr::read_unaligned::<f64>(ptr.offset(offset) as *const f64) })
485485
}
486486

487-
#[op2(fast)]
487+
#[op2(fast, stack_trace)]
488488
pub fn op_ffi_read_ptr<FP>(
489489
state: &mut OpState,
490490
ptr: *mut c_void,

0 commit comments

Comments
 (0)