From 0fda10f8ce48ba2a3eb1f11e1f34efdd0fdd4e37 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Thu, 16 Jul 2026 23:23:21 +0800 Subject: [PATCH] fix(trusted-types): gate execCommand HTML input --- .../wpt-cross-current/failed-cases.txt | 2 - .../wpt-cross-current/passed-cases.txt | 2 + .../src/native_bridge/document/lifecycle.rs | 20 ++++- .../tests/browser_api/trusted_types.rs | 79 +++++++++++++++++++ 4 files changed, 100 insertions(+), 3 deletions(-) diff --git a/moli-benchmark/wpt-cross-current/failed-cases.txt b/moli-benchmark/wpt-cross-current/failed-cases.txt index dad9c57eba3..505a1fc5bd3 100644 --- a/moli-benchmark/wpt-cross-current/failed-cases.txt +++ b/moli-benchmark/wpt-cross-current/failed-cases.txt @@ -3322,7 +3322,6 @@ trusted-types/TrustedTypePolicyFactory-getAttributeType-namespace.html trusted-types/TrustedTypePolicyFactory-getAttributeType-svg.html trusted-types/TrustedTypePolicyFactory-getAttributeType.html trusted-types/Window-TrustedTypes.html -trusted-types/block-Document-execCommand.html trusted-types/block-string-assignment-to-Document-write.html trusted-types/block-string-assignment-to-Element-outerHTML.html trusted-types/block-string-assignment-to-Element-setAttribute.html @@ -3357,7 +3356,6 @@ trusted-types/trusted-types-reporting-check-report-DedicatedWorker-create-policy trusted-types/trusted-types-reporting-check-report-DedicatedWorker-sink-mismatch.html trusted-types/trusted-types-reporting-clipping-of-sample.html trusted-types/trusted-types-reporting-for-DedicatedWorker-ServiceWorkerContainer-register.https.html -trusted-types/trusted-types-reporting-for-Document-execCommand.html trusted-types/trusted-types-reporting-for-Document-write.html trusted-types/trusted-types-reporting-for-Element-setAttribute.html trusted-types/trusted-types-reporting-for-HTMLIFrameElement-srcdoc.html diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt index e4a0ed519c5..398439d39ab 100644 --- a/moli-benchmark/wpt-cross-current/passed-cases.txt +++ b/moli-benchmark/wpt-cross-current/passed-cases.txt @@ -9487,6 +9487,7 @@ trusted-types/TrustedTypePolicyFactory-createPolicy-unenforced.html trusted-types/TrustedTypePolicyFactory-isXXX.html trusted-types/Window-block-eval-function-constructor.html trusted-types/Window-setTimeout-setInterval.html +trusted-types/block-Document-execCommand.html trusted-types/block-string-assignment-to-DOMParser-parseFromString.html trusted-types/block-string-assignment-to-DedicatedWorker-setTimeout-setInterval.html trusted-types/block-string-assignment-to-Document-parseHTMLUnsafe.html @@ -9556,6 +9557,7 @@ trusted-types/trusted-types-reporting-for-DedicatedWorker-eval.html trusted-types/trusted-types-reporting-for-DedicatedWorker-function-constructor.html trusted-types/trusted-types-reporting-for-DedicatedWorker-importScripts.html trusted-types/trusted-types-reporting-for-DedicatedWorker-setTimeout-setInterval.html +trusted-types/trusted-types-reporting-for-Document-execCommand.html trusted-types/trusted-types-reporting-for-Document-parseHTMLUnsafe.html trusted-types/trusted-types-reporting-for-Element-innerHTML.html trusted-types/trusted-types-reporting-for-Element-insertAdjacentHTML.html diff --git a/moli-renderer-v8/src/native_bridge/document/lifecycle.rs b/moli-renderer-v8/src/native_bridge/document/lifecycle.rs index 23598df1012..61173ac64ec 100644 --- a/moli-renderer-v8/src/native_bridge/document/lifecycle.rs +++ b/moli-renderer-v8/src/native_bridge/document/lifecycle.rs @@ -531,7 +531,7 @@ pub(in crate::native_bridge) fn node_document_exec_command_callback<'s>( return; } EditingCommand::InsertHtml => { - let Some(value) = editing_command_value(scope, &args) else { + let Some(value) = editing_command_insert_html_value(scope, runtime_ptr, &args) else { return; }; let inserted = exec_command_insert_html(scope, runtime_ptr, &value); @@ -746,6 +746,24 @@ fn editing_command_value<'s>( .map(|value| value.to_rust_string_lossy(scope)) } +fn editing_command_insert_html_value<'s>( + scope: &mut v8::PinScope<'s, '_>, + runtime_ptr: *mut PageScriptHost, + args: &v8::FunctionCallbackArguments<'s>, +) -> Option { + if args.length() < 3 || args.get(2).is_undefined() { + return Some(String::new()); + } + let requirements = unsafe { &*runtime_ptr }.trusted_types_for_script_requirements(scope); + crate::context_bootstrap::trusted_html_string_or_throw( + scope, + args.get(2), + requirements, + "Document execCommand", + "execCommand", + ) +} + fn exec_command_insert_html( scope: &mut v8::PinScope<'_, '_>, runtime_ptr: *mut PageScriptHost, 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 d2bbdc39354..d7dd969f120 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 @@ -680,6 +680,85 @@ fn dom_parser_gates_converted_union_source_after_webidl_argument_conversion() { ); } +#[test] +fn document_exec_command_gates_only_insert_html_values() { + let mut vm = new_storage_test_vm("https://exec-command-trusted-types.test/"); + vm.set_response_content_security_policies(&["require-trusted-types-for 'script'".to_owned()]); + + let result = vm + .eval( + r#" +(() => { + const errorName = callback => { + try { + callback(); + return "none"; + } catch (error) { + return error && error.name; + } + }; + const root = document.documentElement || document.appendChild(document.createElement("html")); + const body = document.body || root.appendChild(document.createElement("body")); + const input = document.createElement("input"); + body.append(input); + input.focus(); + + const blocked = [ + errorName(() => document.execCommand("insertHTML", false, "blocked")), + errorName(() => document.execCommand("InSeRtHtMl", false, null)) + ]; + const unprotected = [ + errorName(() => document.execCommand("insertHTML")), + errorName(() => document.execCommand("insertHTML", false, undefined)), + errorName(() => document.execCommand("paste", false, "plain")), + errorName(() => document.execCommand("insertText", false, "plain")) + ]; + + input.value = ""; + const custom = trustedTypes.createPolicy("exec-command-custom", { + createHTML: value => value + }); + const trustedReturned = document.execCommand( + "insertHTML", + false, + custom.createHTML("trusted") + ); + const trustedValue = input.value; + + let sourceConversions = 0; + const defaultCalls = []; + trustedTypes.createPolicy("default", { + createHTML: (value, type, sink) => { + defaultCalls.push([value, type, sink]); + return "default"; + } + }); + input.value = ""; + const defaultReturned = document.execCommand("insertHTML", false, { + toString() { + sourceConversions += 1; + return "original"; + } + }); + + return JSON.stringify({ + blocked, + unprotected, + trusted: [trustedReturned, trustedValue], + defaulted: [defaultReturned, input.value, sourceConversions], + defaultCalls + }); +})() +"#, + ) + .expect("Document.execCommand TrustedHTML sink probe should evaluate"); + + assert_eq!( + result, + r#"{"blocked":["TypeError","TypeError"],"unprotected":["none","none","none","none"],"trusted":[true,"trusted"],"defaulted":[true,"default",1],"defaultCalls":[["original","TrustedHTML","Document execCommand"]]}"# + ); +} + #[test] fn document_write_gates_concatenated_union_values_before_writeln_newline() { let mut vm = new_storage_test_vm("https://document-write-trusted-types.test/");