fix(trusted-types): use node document policies for attribute sinks

Select attribute sink requirements, default policies, and report owners from the element's current node document while preserving the operation's exception realm. Avoid retrying native Attr mutations after conversion throws, and preserve native Attr namespace metadata and canonical cache identity.

Validated with cargo fmt, full-workspace Clippy, and all 17,730 nextest tests. The 219-case Trusted Types suite gains three passing cases and 36 passing subtests in both CLI and CDP, with no other semantic changes; all 13 DOM attribute/adoption regression cases remain passing.
This commit is contained in:
ldm0
2026-11-21 19:25:39 +08:00
parent b3ceadbb5f
commit dfbeaafe26
7 changed files with 385 additions and 49 deletions
+3 -2
View File
@@ -555,11 +555,12 @@ pub(crate) use self::time_ranges::new_time_ranges_value;
#[cfg(test)]
pub(crate) use self::trusted_types::trusted_types_lazy_state_materialized;
pub(crate) use self::trusted_types::{
TrustedTypesCodeGenerationCheck, check_javascript_url_trusted_types,
TrustedTypeKind, TrustedTypesCodeGenerationCheck, check_javascript_url_trusted_types,
install_trusted_types_eval_runtime_state, install_trusted_types_runtime_state,
trusted_html_string_or_throw, trusted_html_value_string,
trusted_script_string_for_script_element_execution, trusted_script_string_or_type_error,
trusted_script_url_string_or_throw, trusted_types_code_generation_check,
trusted_script_url_string_or_throw, trusted_type_kind, trusted_type_string,
trusted_type_string_or_throw, trusted_types_code_generation_check,
};
pub(crate) use self::url_search_params_runtime::url_search_params_request_body;
pub(crate) use self::web_storage::install_storage_aliases_for_window;
@@ -5,6 +5,7 @@ use crate::util::{get_private_value, set_private_value};
use crate::web_api_interfaces;
use crate::webidl;
use moli_webapi::trusted_values::*;
pub(crate) use moli_webapi::trusted_values::{TrustedTypeKind, trusted_type_string};
use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
mod policy_callbacks;
@@ -217,7 +218,7 @@ pub(crate) fn trusted_script_url_string_or_throw<'s>(
requirements,
sink,
api_name,
TrustedTypeErrorKind::Type,
None,
)
}
@@ -235,7 +236,7 @@ pub(crate) fn trusted_html_string_or_throw<'s>(
requirements,
sink,
api_name,
TrustedTypeErrorKind::Type,
None,
)
}
@@ -260,7 +261,7 @@ pub(crate) fn trusted_script_string_or_type_error<'s>(
requirements,
sink,
api_name,
TrustedTypeErrorKind::Type,
None,
)
}
@@ -503,14 +504,16 @@ enum DefaultTrustedTypePolicyOutcome {
Exception,
}
fn trusted_type_string_or_throw<'s>(
/// An explicit global selects the sink's policy and report destination, without
/// changing the realm in which argument conversion and TypeError creation run.
pub(crate) fn trusted_type_string_or_throw<'s>(
scope: &mut v8::PinScope<'s, '_>,
value: v8::Local<'s, v8::Value>,
kind: TrustedTypeKind,
requirements: TrustedTypesForScriptRequirements,
sink: &str,
api_name: &'static str,
error_kind: TrustedTypeErrorKind,
policy_global: Option<v8::Local<'s, v8::Object>>,
) -> Option<String> {
if let Some(value) = trusted_type_string(scope, value, kind) {
return Some(value);
@@ -519,7 +522,9 @@ fn trusted_type_string_or_throw<'s>(
return js_value_to_string(scope, value);
}
let original = js_value_to_string(scope, value)?;
let default_policy = apply_default_trusted_type_policy_outcome(scope, &original, kind, sink);
let global = policy_global.unwrap_or_else(|| scope.get_current_context().global(scope));
let default_policy =
apply_default_trusted_type_policy_outcome_for_global(scope, global, &original, kind, sink);
let default_policy_rejected = match default_policy {
DefaultTrustedTypePolicyOutcome::Value(value) => {
return Some(value.as_str_lossy().to_owned());
@@ -528,12 +533,21 @@ fn trusted_type_string_or_throw<'s>(
DefaultTrustedTypePolicyOutcome::Unavailable => false,
DefaultTrustedTypePolicyOutcome::Rejected => true,
};
dispatch_trusted_types_sink_violation_event(scope, sink, &original);
if let Some(global) = policy_global {
if let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) {
unsafe { &mut *host_ptr }
.dispatch_trusted_types_sink_csp_violation_event_for_global_best_effort(
scope, host_ptr, global, sink, &original,
);
}
} else {
dispatch_trusted_types_sink_violation_event(scope, sink, &original);
}
if requirements.is_enforced() {
if default_policy_rejected {
throw_trusted_type_policy_result_error(scope, error_kind, kind);
throw_trusted_type_policy_result_error(scope, TrustedTypeErrorKind::Type, kind);
} else {
throw_trusted_type_error(scope, error_kind, api_name, kind, sink);
throw_trusted_type_error(scope, TrustedTypeErrorKind::Type, api_name, kind, sink);
}
None
} else {
@@ -566,6 +580,16 @@ fn apply_default_trusted_type_policy_outcome<'s>(
sink: &str,
) -> DefaultTrustedTypePolicyOutcome {
let global = scope.get_current_context().global(scope);
apply_default_trusted_type_policy_outcome_for_global(scope, global, input, kind, sink)
}
fn apply_default_trusted_type_policy_outcome_for_global<'s>(
scope: &mut v8::PinScope<'s, '_>,
global: v8::Local<'s, v8::Object>,
input: &str,
kind: TrustedTypeKind,
sink: &str,
) -> DefaultTrustedTypePolicyOutcome {
let Some(policy) = get_private_value(scope, global, TRUSTED_TYPES_DEFAULT_POLICY_SLOT)
.and_then(|policy| v8::Local::<v8::Object>::try_from(policy).ok())
else {
@@ -591,6 +615,16 @@ fn apply_default_trusted_type_policy_outcome<'s>(
}
}
pub(crate) fn trusted_type_kind<'s>(
scope: &mut v8::PinScope<'s, '_>,
value: v8::Local<'s, v8::Value>,
) -> Option<TrustedTypeKind> {
let object = v8::Local::<v8::Object>::try_from(value).ok()?;
TRUSTED_TYPE_KINDS
.into_iter()
.find(|kind| kind.interface().is_instance(scope, object))
}
fn js_value_to_string<'s>(
scope: &mut v8::PinScope<'s, '_>,
value: v8::Local<'s, v8::Value>,
@@ -64,6 +64,21 @@ fn policy_owner_dispatch_scope(scope: &mut v8::PinScope<'_, '_>) -> OwnerDispatc
OwnerDispatchScope::Top
}
fn policy_owner_dispatch_scope_for_global<'s>(
scope: &mut v8::PinScope<'s, '_>,
global: v8::Local<'s, v8::Object>,
) -> OwnerDispatchScope {
if let Some(handle) = get_private_value(scope, global, CHILD_BROWSING_CONTEXT_HANDLE_SLOT)
.and_then(|value| child_window_handle_from_marker_data(scope, value))
{
return OwnerDispatchScope::Child(handle);
}
if let Some(id) = crate::native_bridge::lightweight_popup_id_from_window(scope, global) {
return OwnerDispatchScope::LightweightPopup(id);
}
OwnerDispatchScope::Top
}
impl DocumentCspOutcome {
pub(crate) fn blocks_request(&self) -> bool {
matches!(self, Self::Blocked(_))
@@ -357,6 +372,17 @@ impl PageScriptHost {
)
}
pub(crate) fn trusted_types_for_script_requirements_for_global<'s>(
&self,
scope: &mut v8::PinScope<'s, '_>,
global: v8::Local<'s, v8::Object>,
) -> TrustedTypesForScriptRequirements {
self.trusted_types_for_script_requirements_for_owner(
policy_owner_dispatch_scope_for_global(scope, global),
)
.unwrap_or_default()
}
fn trusted_types_sink_csp_violations_for_owner(
&self,
owner: OwnerDispatchScope,
@@ -1275,8 +1301,23 @@ impl PageScriptHost {
sink: &str,
sample: &str,
) {
let owner = policy_owner_dispatch_scope(scope);
self.dispatch_trusted_types_sink_csp_violation_event_with_location_best_effort(
scope, host_ptr, sink, sample, true,
scope, host_ptr, owner, sink, sample, true,
);
}
pub(crate) fn dispatch_trusted_types_sink_csp_violation_event_for_global_best_effort<'s>(
&mut self,
scope: &mut v8::PinScope<'s, '_>,
host_ptr: *mut PageScriptHost,
global: v8::Local<'s, v8::Object>,
sink: &str,
sample: &str,
) {
let owner = policy_owner_dispatch_scope_for_global(scope, global);
self.dispatch_trusted_types_sink_csp_violation_event_with_location_best_effort(
scope, host_ptr, owner, sink, sample, true,
);
}
@@ -1290,8 +1331,9 @@ impl PageScriptHost {
sink: &str,
sample: &str,
) {
let owner = policy_owner_dispatch_scope(scope);
self.dispatch_trusted_types_sink_csp_violation_event_with_location_best_effort(
scope, host_ptr, sink, sample, false,
scope, host_ptr, owner, sink, sample, false,
);
}
@@ -1316,7 +1358,7 @@ impl PageScriptHost {
&mut self,
scope: &mut v8::PinScope<'s, '_>,
host_ptr: *mut PageScriptHost,
owner: OwnerDispatchScope,
sink: &str,
sample: &str,
capture_current_script_location: bool,
@@ -1324,7 +1366,6 @@ impl PageScriptHost {
let source_location = (capture_current_script_location && !self.active_inspector_dispatch)
.then(|| crate::content_security_policy::current_script_violation_location(scope))
.flatten();
let owner = policy_owner_dispatch_scope(scope);
for mut violation in self.trusted_types_sink_csp_violations_for_owner(owner, sink, sample) {
if let Some((source_file, line_number, column_number)) = &source_location {
violation.source_file.clone_from(source_file);
@@ -823,10 +823,11 @@ pub(in crate::native_bridge) fn set_attribute_node<'s>(
if detached_native_handle_for_runtime(scope, runtime_ptr, backing).is_none() {
return live_set_attribute_node(scope, element, attr);
}
let result = custom_elements::with_custom_element_reaction_scope(scope, runtime_ptr, |scope| {
// Native conversion can throw after invoking the default policy. Do not
// retry the mutation through the live path after that exception.
custom_elements::with_custom_element_reaction_scope(scope, runtime_ptr, |scope| {
detached_native_set_attribute_node(scope, element, attr)
});
result.or_else(|| live_set_attribute_node(scope, element, attr))
})
}
pub(in crate::native_bridge) fn remove_attribute_node<'s>(
@@ -265,6 +265,25 @@ pub(in crate::native_bridge) fn trusted_attribute_value_string<'s>(
value: v8::Local<'s, v8::Value>,
setter: TrustedAttributeSetter,
) -> Option<String> {
// Web IDL converts the union before the DOM algorithm observes the node
// document. A user-defined toString can adopt the element into another realm.
let input_kind = crate::context_bootstrap::trusted_type_kind(scope, value);
let value = if input_kind.is_some() || value.is_string() {
value
} else {
let value = match crate::webidl::convert::<crate::webidl::DomString>(
scope,
value,
setter.conversion_context(),
) {
Ok(value) => value.0,
Err(error) => {
crate::webidl::throw_error(scope, &error);
return None;
}
};
crate::util::v8_string(scope, &value)?.into()
};
let sink = runtime_and_handle.and_then(|(runtime_ptr, handle)| {
let element = unsafe { &*runtime_ptr }
.dom_host()
@@ -276,40 +295,35 @@ pub(in crate::native_bridge) fn trusted_attribute_value_string<'s>(
attribute_namespace,
local_name,
)?;
Some((runtime_ptr, sink))
Some((runtime_ptr, handle, sink))
});
if let Some((runtime_ptr, sink)) = sink {
let requirements = unsafe { &*runtime_ptr }.trusted_types_for_script_requirements(scope);
return match sink {
TrustedAttributeSink::Html(sink) => {
crate::context_bootstrap::trusted_html_string_or_throw(
scope,
value,
requirements,
sink,
setter.api_name(),
)
}
TrustedAttributeSink::Script(sink) => {
crate::context_bootstrap::trusted_script_string_or_type_error(
scope,
value,
requirements,
&sink,
setter.api_name(),
)
}
TrustedAttributeSink::ScriptUrl(sink) => {
crate::context_bootstrap::trusted_script_url_string_or_throw(
scope,
value,
requirements,
sink,
setter.api_name(),
)
}
if let Some((runtime_ptr, handle, sink)) = sink {
let context =
super::super::node::node_owner_document_relevant_context(scope, runtime_ptr, handle)
.unwrap_or_else(|| scope.get_current_context());
let global = context.global(scope);
let requirements = unsafe { &*runtime_ptr }
.trusted_types_for_script_requirements_for_global(scope, global);
use crate::context_bootstrap::TrustedTypeKind;
let (kind, sink) = match &sink {
TrustedAttributeSink::Html(sink) => (TrustedTypeKind::Html, *sink),
TrustedAttributeSink::Script(sink) => (TrustedTypeKind::Script, sink.as_str()),
TrustedAttributeSink::ScriptUrl(sink) => (TrustedTypeKind::ScriptUrl, *sink),
};
return crate::context_bootstrap::trusted_type_string_or_throw(
scope,
value,
kind,
requirements,
sink,
setter.api_name(),
Some(global),
);
}
if let Some(kind) = input_kind {
return crate::context_bootstrap::trusted_type_string(scope, value, kind);
}
match crate::webidl::convert::<crate::webidl::DomString>(
@@ -95,6 +95,7 @@ mod traversal;
mod traversal_coordinator;
mod traversal_receivers;
mod trusted_types;
mod trusted_types_attributes;
mod value_event_init;
mod value_events;
mod view_transition_dictionary;
@@ -0,0 +1,244 @@
use super::*;
#[tokio::test]
async fn adopted_attribute_sinks_use_the_node_document_global_and_preserve_exception_realms() {
let mut vm = new_storage_test_vm("https://adopted-attribute-types.test/");
vm.eval(r#"
(() => {
const root = document.documentElement || document.appendChild(document.createElement("html"));
const body = document.body || root.appendChild(document.createElement("body"));
const frame = document.createElement("iframe");
frame.id = "tt-frame";
frame.srcdoc = `<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'"><body></body>`;
body.appendChild(frame);
})()
"#).expect("Trusted Types child setup should evaluate");
run_child_navigation_commit_and_host_load_for_test(&mut vm, "TT child should commit").await;
let result = vm.eval(r#"
(() => {
const child = document.getElementById("tt-frame").contentWindow;
const childDoc = child.document;
const secondary = childDoc.implementation.createHTMLDocument("");
const explicit = trustedTypes.createPolicy("pass", { createScript: value => value });
const setters = [
(element, value) => element.setAttribute("onclick", value),
(element, value) => element.setAttributeNS(null, "onclick", value),
...["setAttributeNode", "setAttributeNodeNS", "setNamedItem", "setNamedItemNS"].map(method =>
(element, value) => {
const attr = document.createAttribute("onclick");
attr.value = value;
return method.startsWith("setNamed") ? element.attributes[method](attr) : element[method](attr);
}),
...["value", "nodeValue", "textContent"].map(property =>
(element, value) => { element.getAttributeNode("onclick")[property] = value; })
];
globalThis.__attributeReports = { top: [], child: [] };
// Observe the receiving global independently of the event's DOM target.
window.addEventListener("securitypolicyviolation", event => __attributeReports.top.push(event.sample));
child.addEventListener("securitypolicyviolation", event => __attributeReports.child.push(event.sample));
const moveAndSet = (source, target, set, index) => {
const element = source.createElement("button");
element.setAttribute("onclick", explicit.createScript("initial"));
target.adoptNode(element);
// Like the adoption WPT, derive the exception realm from the exposed
// receiver, without assuming that adoption keeps the original wrapper realm.
const receiver = index < 6 ? element : element.getAttributeNode("onclick");
const expectedError = new receiver.constructor.constructor(explicit.createScript("return TypeError"))();
let outcome = "none";
try { set(element, "input"); } catch (error) {
outcome = `${error.name}:${error instanceof expectedError}`;
}
return [outcome, element.ownerDocument === target, element.getAttribute("onclick")];
};
const intoPlain = setters.map((set, index) => moveAndSet(childDoc, document, set, index));
const intoEnforced = setters.map((set, index) => moveAndSet(document, childDoc, set, index));
const intoSecondary = setters.map((set, index) => moveAndSet(document, secondary, set, index));
const stringConversions = [false, true].map(namespaced => {
const element = document.createElement("button");
let converted = 0;
const value = { toString() { converted++; childDoc.adoptNode(element); return "converted-input"; }};
let blocked = false;
try {
if (namespaced) element.setAttributeNS(null, "onclick", value);
else element.setAttribute("onclick", value);
} catch (error) { blocked = error instanceof TypeError; }
return [blocked, converted, element.ownerDocument === childDoc, element.hasAttribute("onclick")];
});
const calls = [];
trustedTypes.createPolicy("default", { createScript: value => { calls.push("top"); return `top-${value}`; }});
child.trustedTypes.createPolicy("default", { createScript: value => { calls.push("child"); return `child-${value}`; }});
const defaulted = setters.map((set, index) => moveAndSet(document, childDoc, set, index));
const secondaryDefaulted = setters.map((set, index) => moveAndSet(document, secondary, set, index));
return JSON.stringify({ intoPlain, intoEnforced, intoSecondary, stringConversions, defaulted, secondaryDefaulted, calls });
})()
"#).expect("adopted attribute sink checks should evaluate");
let allowed = vec![serde_json::json!(["none", true, "input"]); 9];
let blocked = vec![serde_json::json!(["TypeError:true", true, "initial"]); 9];
let converted = vec![serde_json::json!(["none", true, "child-input"]); 9];
assert_eq!(
serde_json::from_str::<serde_json::Value>(&result).unwrap(),
serde_json::json!({
"intoPlain": allowed, "intoEnforced": blocked, "intoSecondary": blocked,
"stringConversions": [[true, 1, true, false], [true, 1, true, false]],
"defaulted": converted, "secondaryDefaulted": converted, "calls": vec!["child"; 18]
})
);
drain_pre_domcontentloaded_non_script_page_tasks_for_test(&mut vm);
assert_eq!(
vm.eval("JSON.stringify([__attributeReports.top.length, __attributeReports.child.length])")
.unwrap(),
"[0,20]"
);
}
#[test]
fn secondary_document_attribute_nodes_enforce_trusted_types_without_retrying() {
let mut vm = new_storage_test_vm("https://secondary-document-attribute-types.test/");
vm.set_response_content_security_policies(&["require-trusted-types-for 'script'".to_owned()]);
let result = vm
.eval(
r#"
(() => {
const secondary = document.implementation.createHTMLDocument("");
const setters = [
(element, attr) => element.setAttributeNode(attr),
(element, attr) => element.setAttributeNodeNS(attr),
(element, attr) => element.attributes.setNamedItem(attr),
(element, attr) => element.attributes.setNamedItemNS(attr)
];
const rejected = setters.map(set => {
const element = secondary.createElement("button");
const attr = secondary.createAttribute("onclick");
attr.value = "blocked";
let rejected = false;
try { set(element, attr); } catch (error) { rejected = error instanceof TypeError; }
return [rejected, attr.ownerElement === null, element.hasAttribute("onclick")];
});
const calls = [];
const exception = new RangeError("policy callback");
trustedTypes.createPolicy("default", { createScript(value) {
calls.push(value);
if (value === "throw") throw exception;
return `safe-${value}`;
}});
const accepted = setters.map(set => {
const element = secondary.createElement("button");
const attr = secondary.createAttribute("onclick");
attr.value = "input";
const old = set(element, attr);
return [old === null, attr.ownerElement === element,
element.getAttribute("onclick"), element.getAttributeNode("onclick") === attr];
});
const abrupt = setters.map(set => {
const element = secondary.createElement("button");
const attr = secondary.createAttribute("onclick");
attr.value = "throw";
let preserved = false;
try { set(element, attr); } catch (error) { preserved = error === exception; }
return [preserved, attr.ownerElement === null, element.hasAttribute("onclick")];
});
return JSON.stringify({ rejected, accepted, abrupt, calls });
})()
"#,
)
.expect("secondary document attribute node checks should evaluate");
assert_eq!(
result,
r#"{"rejected":[[true,true,false],[true,true,false],[true,true,false],[true,true,false]],"accepted":[[true,true,"safe-input",true],[true,true,"safe-input",true],[true,true,"safe-input",true],[true,true,"safe-input",true]],"abrupt":[[true,true,false],[true,true,false],[true,true,false],[true,true,false]],"calls":["input","input","input","input","throw","throw","throw","throw"]}"#
);
}
#[test]
fn attribute_node_lookup_preserves_native_namespaces_and_shared_identity() {
let mut vm = new_storage_test_vm("https://attribute-node-namespace.test/");
let result = vm
.eval(
r#"
(() => {
const ns = "http://www.w3.org/1999/xlink";
const secondary = document.implementation.createHTMLDocument("");
const results = [];
for (const doc of [document, secondary]) {
for (const nsFirst of [false, true]) {
const element = doc.createElementNS("http://www.w3.org/2000/svg", "script");
element.setAttributeNS(ns, "xlink:href", "initial");
element.getAttribute = element.getAttributeNS = () => { throw new Error("overridden getter"); };
const attr = nsFirst ? element.getAttributeNodeNS(ns, "href")
: element.getAttributeNode("xlink:href");
const identity = attr === element.getAttributeNodeNS(ns, "href") &&
attr === element.getAttributeNode("xlink:href") &&
attr === element.attributes.item(0) &&
attr === element.attributes.getNamedItem("xlink:href") &&
attr === element.attributes.getNamedItemNS(ns, "href");
const metadata = [attr.name, attr.localName, attr.prefix, attr.namespaceURI];
delete element.getAttribute;
delete element.getAttributeNS;
element.removeAttributeNode(attr);
element.setAttributeNode(attr);
attr.value = "changed";
results.push([metadata, identity, attr.ownerElement === element,
element.getAttributeNS(ns, "href"), element.attributes.length]);
}
}
return JSON.stringify(results);
})()
"#,
)
.expect("attribute node namespace checks should evaluate");
let expected = serde_json::json!([
[
"xlink:href",
"href",
"xlink",
"http://www.w3.org/1999/xlink"
],
true,
true,
"changed",
1
]);
assert_eq!(
serde_json::from_str::<serde_json::Value>(&result).unwrap(),
serde_json::json!([expected, expected, expected, expected])
);
}
#[test]
fn attribute_node_cache_distinguishes_equal_qualified_names_in_different_namespaces() {
let mut vm = new_storage_test_vm("https://attribute-node-cache-namespace.test/");
let result = vm
.eval(
r#"
(() => {
const secondary = document.implementation.createHTMLDocument("");
return JSON.stringify([document, secondary].map(doc => {
const element = doc.createElementNS("http://www.w3.org/2000/svg", "g");
element.setAttributeNS("urn:first", "same:name", "first");
element.setAttributeNS("urn:second", "same:name", "second");
const first = element.getAttributeNodeNS("urn:first", "name");
const second = element.getAttributeNodeNS("urn:second", "name");
const byName = element.getAttributeNode("same:name");
const indexed = [element.attributes[0], element.attributes[1]];
second.value = "updated";
return [first !== second, byName === first, indexed[0] === first, indexed[1] === second,
first.namespaceURI, second.namespaceURI, first.value, second.value];
}));
})()
"#,
)
.expect("namespace cache identity checks should evaluate");
let row = serde_json::json!([
true,
true,
true,
true,
"urn:first",
"urn:second",
"first",
"updated"
]);
assert_eq!(
serde_json::from_str::<serde_json::Value>(&result).unwrap(),
serde_json::json!([row, row])
);
}