@@ -28,14 +28,15 @@ use js::glue::{
28
28
use js:: jsapi:: {
29
29
AsmJSOption , BuildIdCharVector , CompilationType , ContextOptionsRef , Dispatchable as JSRunnable ,
30
30
Dispatchable_MaybeShuttingDown , GCDescription , GCOptions , GCProgress , GCReason ,
31
- GetPromiseUserInputEventHandlingState , HandleObject , HandleString , HandleValue , Heap ,
32
- InitConsumeStreamCallback , InitDispatchToEventLoop , JS_AddExtraGCRootsTracer ,
33
- JS_InitDestroyPrincipalsCallback , JS_InitReadPrincipalsCallback , JS_NewObject ,
34
- JS_SetGCCallback , JS_SetGCParameter , JS_SetGlobalJitCompilerOption ,
35
- JS_SetOffthreadIonCompilationEnabled , JS_SetParallelParsingEnabled , JS_SetReservedSlot ,
36
- JS_SetSecurityCallbacks , JSCLASS_RESERVED_SLOTS_MASK , JSCLASS_RESERVED_SLOTS_SHIFT , JSClass ,
37
- JSClassOps , JSContext as RawJSContext , JSGCParamKey , JSGCStatus , JSJitCompilerOption , JSObject ,
38
- JSSecurityCallbacks , JSTracer , JobQueue , MimeType , MutableHandleObject ,
31
+ GetPromiseUserInputEventHandlingState , HandleObject , HandleString ,
32
+ HandleValue as RawHandleValue , Heap , InitConsumeStreamCallback , InitDispatchToEventLoop ,
33
+ JS_AddExtraGCRootsTracer , JS_InitDestroyPrincipalsCallback , JS_InitReadPrincipalsCallback ,
34
+ JS_NewObject , JS_NewStringCopyN , JS_SetGCCallback , JS_SetGCParameter ,
35
+ JS_SetGlobalJitCompilerOption , JS_SetOffthreadIonCompilationEnabled ,
36
+ JS_SetParallelParsingEnabled , JS_SetReservedSlot , JS_SetSecurityCallbacks ,
37
+ JSCLASS_RESERVED_SLOTS_MASK , JSCLASS_RESERVED_SLOTS_SHIFT , JSClass , JSClassOps ,
38
+ JSContext as RawJSContext , JSGCParamKey , JSGCStatus , JSJitCompilerOption , JSObject ,
39
+ JSSecurityCallbacks , JSTracer , JobQueue , MimeType , MutableHandleObject , MutableHandleString ,
39
40
PromiseRejectionHandlingState , PromiseUserInputEventHandlingState , RuntimeCode ,
40
41
SetDOMCallbacks , SetGCSliceCallback , SetJobQueue , SetPreserveWrapperCallbacks ,
41
42
SetProcessBuildIdOp , SetPromiseRejectionTrackerCallback , StreamConsumer as JSStreamConsumer ,
@@ -45,8 +46,8 @@ use js::panic::wrap_panic;
45
46
pub ( crate ) use js:: rust:: ThreadSafeJSContext ;
46
47
use js:: rust:: wrappers:: { GetPromiseIsHandled , JS_GetPromiseResult } ;
47
48
use js:: rust:: {
48
- Handle , HandleObject as RustHandleObject , IntoHandle , JSEngine , JSEngineHandle , <
4861
span class="pl-v x x-last">ParentRuntime,
49
- Runtime as RustRuntime ,
49
+ Handle , HandleObject as RustHandleObject , HandleValue , IntoHandle , JSEngine , JSEngineHandle ,
50
+ ParentRuntime , Runtime as RustRuntime ,
50
51
} ;
51
52
use malloc_size_of:: MallocSizeOfOps ;
52
53
use malloc_size_of_derive:: MallocSizeOf ;
@@ -62,7 +63,7 @@ use crate::dom::bindings::codegen::Bindings::PromiseBinding::PromiseJobCallback;
62
63
use crate :: dom:: bindings:: codegen:: Bindings :: ResponseBinding :: Response_Binding :: ResponseMethods ;
63
64
use crate :: dom:: bindings:: codegen:: Bindings :: ResponseBinding :: ResponseType as DOMResponseType ;
64
65
use crate :: dom:: bindings:: conversions:: {
65
- get_dom_class, private_from_object, root_from_handleobject,
66
+ get_dom_class, private_from_object, root_from_handleobject, root_from_object ,
66
67
} ;
67
68
use crate :: dom:: bindings:: error:: { Error , throw_dom_exception} ;
68
69
use crate :: dom:: bindings:: inheritance:: Castable ;
@@ -71,6 +72,7 @@ use crate::dom::bindings::refcounted::{
71
72
} ;
72
73
use crate :: dom:: bindings:: reflector:: { DomGlobal , DomObject } ;
73
74
use crate :: dom:: bindings:: root:: trace_roots;
75
+ use crate :: dom:: bindings:: str:: DOMString ;
74
76
use crate :: dom:: bindings:: utils:: DOM_CALLBACKS ;
75
77
use crate :: dom:: bindings:: { principals, settings_stack} ;
76
78
use crate :: dom:: csp:: CspReporting ;
@@ -80,6 +82,7 @@ use crate::dom::globalscope::GlobalScope;
80
82
use crate :: dom:: promise:: Promise ;
81
83
use crate :: dom:: promiserejectionevent:: PromiseRejectionEvent ;
82
84
use crate :: dom:: response:: Response ;
85
+ use crate :: dom:: trustedscript:: TrustedScript ;
83
86
use crate :: microtask:: { EnqueuedPromiseCallback , Microtask , MicrotaskQueue } ;
84
87
use crate :: realms:: { AlreadyInRealm , InRealm , enter_realm} ;
85
88
use crate :: script_module:: EnsureModuleHooksInitialized ;
@@ -98,7 +101,7 @@ static JOB_QUEUE_TRAPS: JobQueueTraps = JobQueueTraps {
98
101
99
102
static SECURITY_CALLBACKS : JSSecurityCallbacks = JSSecurityCallbacks {
100
103
contentSecurityPolicyAllows : Some ( content_security_policy_allows) ,
101
- codeForEvalGets : None , //TODO
104
+ codeForEvalGets : Some ( code_for_eval_gets ) ,
102
105
subsumes : Some ( principals:: subsumes) ,
103
106
} ;
104
107
@@ -468,16 +471,43 @@ unsafe extern "C" fn promise_rejection_tracker(
468
471
} )
469
472
}
470
473
474
+ #[ allow( unsafe_code) ]
475
+ fn safely_convert_null_to_string ( cx : JSContext , str_ : HandleString ) -> DOMString {
476
+ DOMString :: from ( match std:: ptr:: NonNull :: new ( * str_) {
477
+ None => "" . to_owned ( ) ,
478
+ Some ( str_) => unsafe { jsstr_to_string ( * cx, str_) } ,
479
+ } )
480
+ }
481
+
482
+ #[ allow( unsafe_code) ]
483
+ unsafe extern "C" fn code_for_eval_gets (
484
+ cx : * mut RawJSContext ,
485
+ code : HandleObject ,
486
+ code_for_eval : MutableHandleString ,
487
+ ) -> bool {
488
+ let cx = JSContext :: from_ptr ( cx) ;
489
+ if let Ok ( trusted_script) = root_from_object :: < TrustedScript > ( code. get ( ) , * cx) {
490
+ let script_string = trusted_script. data ( ) ;
491
+ let new_string = JS_NewStringCopyN (
492
+ * cx,
493
+ script_string. as_ptr ( ) as * const libc:: c_char ,
494
+ script_string. len ( ) ,
495
+ ) ;
496
+ code_for_eval. set ( new_string) ;
497
+ }
498
+ true
499
+ }
500
+
471
501
#[ allow( unsafe_code) ]
472
502
unsafe extern "C" fn content_security_policy_allows (
473
503
cx : * mut RawJSContext ,
474
504
runtime_code : RuntimeCode ,
475
- sample : HandleString ,
476
- _compilation_type : CompilationType ,
477
- _parameter_strings : u8 , //FIXME in bindings generation
478
- _body_string : HandleString ,
479
- _parameter_args : u8 , //FIXME in bindings generation
480
- _body_arg : HandleValue ,
505
+ code_string : HandleString ,
506
+ compilation_type : CompilationType ,
507
+ parameter_strings : u8 , //FIXME in bindings generation
508
+ body_string : HandleString ,
509
+ parameter_args : u8 , //FIXME in bindings generation
510
+ body_arg : RawHandleValue ,
481
511
can_compile_strings : * mut bool ,
482
512
) -> bool {
483
513
let mut allowed = false ;
@@ -488,13 +518,17 @@ unsafe extern "C" fn content_security_policy_allows(
488
518
let global = & GlobalScope :: from_context ( * cx, InRealm :: Already ( & in_realm_proof) ) ;
489
519
490
520
allowed = match runtime_code {
491
- RuntimeCode :: JS => {
492
- let source = std:: ptr:: NonNull :: new ( * sample)
493
- . map_or_else ( String :: new, |jsstr| jsstr_to_string ( * cx, jsstr) ) ;
494
- global
495
- . get_csp_list ( )
496
- . is_js_evaluation_allowed ( global, & source)
497
- } ,
521
+ RuntimeCode :: JS => TrustedScript :: can_compile_string_with_trusted_type (
522
+ cx,
523
+ global,
524
+ safely_convert_null_to_string ( cx, code_string) ,
525
+ compilation_type,
526
+ parameter_strings,
527
+ safely_convert_null_to_string ( cx, body_string) ,
528
+ parameter_args,
529
+ HandleValue :: from_raw ( body_arg) ,
530
+ CanGc :: note ( ) ,
531
+ ) ,
498
532
RuntimeCode :: WASM => global. get_csp_list ( ) . is_wasm_evaluation_allowed ( global) ,
499
533
} ;
500
534
} ) ;
0 commit comments