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): avoid duplicate reports for transformed trusted scripts

ldm09279ef478a65
diff --git a/moli-renderer-v8/src/context_bootstrap/trusted_types.rs b/moli-renderer-v8/src/context_bootstrap/trusted_types.rs
index 61a065d29..e1341f01b 100644
--- a/moli-renderer-v8/src/context_bootstrap/trusted_types.rs
+++ b/moli-renderer-v8/src/context_bootstrap/trusted_types.rs
@@ -429,7 +429,7 @@ fn trusted_script_string_for_code_generation_sink(
Some(original.to_owned())
}
DefaultTrustedTypePolicyOutcome::Value(_) => {
- dispatch_trusted_types_sink_violation_event(scope, sink, violation_sample);
+ // TrustedScript was supplied; rejecting transformed source is a compilation error.
throw_eval_error(scope, transformed_value_error);
None
}
@@ -1007,7 +1007,7 @@ fn trusted_script_string_for_function_constructor<'s>(
if default_value == default_policy_source {
return Some(original);
}
- dispatch_trusted_types_sink_violation_event(scope, "Function", &violation_sample);
+ // The policy supplied TrustedScript, so this is not a sink mismatch.
throw_eval_error(
scope,
"Trusted Types default policy must not transform strings passed to Function.",
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/trusted_types.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/trusted_types.rs
index b0b0cdd87..023e799ec 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/trusted_types.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/trusted_types.rs
@@ -1717,3 +1717,28 @@ fn svg_script_href_base_val_uses_the_owner_reflected_trusted_script_url_sink() {
r#"[{"blockedURI":"trusted-types-sink","effectiveDirective":"require-trusted-types-for","sample":"SVGScriptElement href|plain.js"}]"#
);
}
+
+#[test]
+fn transformed_trusted_script_rejection_does_not_report_a_sink_mismatch() {
+ let mut vm = new_storage_test_vm("https://trusted-script-compilation.test/");
+ vm.set_response_content_security_policies(&[
+ "script-src 'unsafe-eval'; require-trusted-types-for 'script'".to_owned(),
+ ]);
+ assert_eq!(vm.eval(r#"
+ globalThis.compilationReports = [];
+ document.addEventListener('securitypolicyviolation', e => compilationReports.push(e.blockedURI));
+ trustedTypes.createPolicy('default', {createScript: source => source + ' '});
+ (() => {
+ let rejected = 0;
+ for (const compile of [() => eval('1'), () => Function('return 1;')]) {
+ try { compile(); } catch (error) {
+ if (!(error instanceof EvalError)) throw error;
+ rejected++;
+ }
+ }
+ return rejected;
+ })()
+ "#).unwrap(), "2");
+ let _ = drain_pre_domcontentloaded_non_script_page_tasks_for_test(&mut vm);
+ assert_eq!(vm.eval("JSON.stringify(compilationReports)").unwrap(), "[]");
+}