Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(trusted-types): unwrap trusted scripts for eval

ldm05959b0893530
diff --git a/moli-renderer-v8/src/script_vm/security_policy.rs b/moli-renderer-v8/src/script_vm/security_policy.rs
index 01a1644c9..01dc5eba4 100644
--- a/moli-renderer-v8/src/script_vm/security_policy.rs
+++ b/moli-renderer-v8/src/script_vm/security_policy.rs
@@ -179,19 +179,12 @@ pub(super) unsafe extern "C" fn string_code_generation_check_callback(
}
}
} else {
- match trusted_types_code_generation_check(scope, source, is_code_like) {
- TrustedTypesCodeGenerationCheck::AllowOriginal => {
- StringCodeGenerationPolicyAction::AllowWithoutCsp
- }
- TrustedTypesCodeGenerationCheck::AllowModified(source) => {
- StringCodeGenerationPolicyAction::CheckCsp {
- allow_trusted_types_eval: false,
- modified_source: Some(source),
- }
- }
- TrustedTypesCodeGenerationCheck::Block => StringCodeGenerationPolicyAction::Block,
- }
+ let check = trusted_types_code_generation_check(scope, source, is_code_like);
+ code_generation_action_from_trusted_types_check(check)
}
+ } else if is_code_like || !source.is_string() {
+ let check = trusted_types_code_generation_check(scope, source, is_code_like);
+ code_generation_action_from_trusted_types_check(check)
} else {
match non_trusted_types_code_generation_source(source, is_code_like) {
NonTrustedTypesCodeGenerationSource::String => {
@@ -255,6 +248,23 @@ enum StringCodeGenerationPolicyAction {
Block,
}
+fn code_generation_action_from_trusted_types_check(
+ check: TrustedTypesCodeGenerationCheck,
+) -> StringCodeGenerationPolicyAction {
+ match check {
+ TrustedTypesCodeGenerationCheck::AllowOriginal => {
+ StringCodeGenerationPolicyAction::AllowWithoutCsp
+ }
+ TrustedTypesCodeGenerationCheck::AllowModified(source) => {
+ StringCodeGenerationPolicyAction::CheckCsp {
+ allow_trusted_types_eval: false,
+ modified_source: Some(source),
+ }
+ }
+ TrustedTypesCodeGenerationCheck::Block => StringCodeGenerationPolicyAction::Block,
+ }
+}
+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum NonTrustedTypesCodeGenerationSource {
String,
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
index 2377edfcf..88218fcae 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
@@ -663,6 +663,33 @@ fn trusted_types_eval_keyword_bypasses_default_policy_and_eval_csp() {
);
}
+#[test]
+fn trusted_script_eval_is_unwrapped_without_trusted_types_enforcement() {
+ let mut vm = new_storage_test_vm("https://trusted-script-eval.test/");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const policy = trustedTypes.createPolicy("eval", { createScript: value => value });
+ const trusted = policy.createScript("1 + 1");
+ const ordinary = { value: "ordinary" };
+ return JSON.stringify({
+ direct: eval(trusted),
+ indirect: eval?.(trusted),
+ ordinaryPassesThrough: eval(ordinary) === ordinary
+ });
+})()
+"#,
+ )
+ .expect("TrustedScript eval probe should evaluate");
+
+ assert_eq!(
+ result,
+ r#"{"direct":2,"indirect":2,"ordinaryPassesThrough":true}"#
+ );
+}
+
#[test]
fn trusted_types_default_policy_receives_type_and_eval_sink_arguments() {
let mut vm = new_storage_test_vm("https://trusted-types-default-callback-args.test/");