Skip to content

Truly independent web browser

LibWeb: Stop treating namespace-less elements as HTML for Trusted Types

Shannon Booth6a68c7453263
diff --git a/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy.rs b/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy.rs
index 0aced151485..de02a1a9012 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy.rs
@@ -440,7 +440,7 @@ pub(crate) fn get_trusted_types_compliant_attribute_value(
attribute_name: &Utf16String,
attribute_ns: Option<&Utf16String>,
element_local_name: &Utf16String,
- element_namespace: &Utf16String,
+ element_namespace: Option<&Utf16String>,
global: Gc<Object>,
new_value: &TrustedTypeOrString,
) -> WebIdlResult<Utf16String> {
@@ -494,26 +494,26 @@ pub(crate) const SCRIPT_SINK_GROUP: &str = "'script'";
// FIXME: Add-hoc definition of an element interface
pub(crate) struct ElementInterface {
pub(crate) element_name: &'static str,
- pub(crate) element_ns: Utf16String,
+ pub(crate) element_ns: Option<Utf16String>,
}
-pub(crate) fn element_interface(local_name: &Utf16String, element_ns: &Utf16String) -> ElementInterface {
+pub(crate) fn element_interface(local_name: &Utf16String, element_ns: Option<&Utf16String>) -> ElementInterface {
// FIXME: We don't have a method in ElementFactory that can give us the interface name but these are all the cases
// we care about in the table in get_trusted_type_data_for_attribute function
let local_name_view = Utf16View::of_string(local_name);
- let element_ns_view = Utf16View::of_string(element_ns);
- let element_name = if local_name_view == "iframe" && element_ns_view == HTML_NAMESPACE {
+ let element_ns_view = element_ns.map(Utf16View::of_string);
+ let element_name = if local_name_view == "iframe" && element_ns_view.is_some_and(|ns| ns == HTML_NAMESPACE) {
"HTMLIFrameElement"
- } else if local_name_view == "script" && element_ns_view == HTML_NAMESPACE {
+ } else if local_name_view == "script" && element_ns_view.is_some_and(|ns| ns == HTML_NAMESPACE) {
"HTMLScriptElement"
- } else if local_name_view == "script" && element_ns_view == SVG_NAMESPACE {
+ } else if local_name_view == "script" && element_ns_view.is_some_and(|ns| ns == SVG_NAMESPACE) {
"SVGScriptElement"
} else {
"Element"
};
ElementInterface {
element_name,
- element_ns: element_ns.clone(),
+ element_ns: element_ns.cloned(),
}
}
@@ -617,7 +617,8 @@ pub unsafe extern "C" fn web_rust_trusted_types_get_trusted_type_compliant_strin
/// # Safety
///
/// As web_rust_trusted_types_get_trusted_type_compliant_string(), with `attribute_ns` a live raw string if
-/// `has_attribute_ns`, and the names raw strings that C++ hands over.
+/// `has_attribute_ns`, `element_namespace` one if `has_element_namespace`, and the names raw strings that C++ hands
+/// over.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn web_rust_trusted_types_get_trusted_types_compliant_attribute_value(
vm: *mut c_void,
@@ -625,6 +626,7 @@ pub unsafe extern "C" fn web_rust_trusted_types_get_trusted_types_compliant_attr
has_attribute_ns: bool,
attribute_ns: usize,
element_local_name: usize,
+ has_element_namespace: bool,
element_namespace: usize,
global: *mut c_void,
input_kind: u8,
@@ -642,7 +644,7 @@ pub unsafe extern "C" fn web_rust_trusted_types_get_trusted_types_compliant_attr
Utf16String::from_raw_owned(attribute_name),
has_attribute_ns.then(|| Utf16String::from_raw_owned(attribute_ns)),
Utf16String::from_raw_owned(element_local_name),
- Utf16String::from_raw_owned(element_namespace),
+ has_element_namespace.then(|| Utf16String::from_raw_owned(element_namespace)),
)
};
let result = get_trusted_types_compliant_attribute_value(
@@ -650,7 +652,7 @@ pub unsafe extern "C" fn web_rust_trusted_types_get_trusted_types_compliant_attr
&attribute_name,
attribute_ns.as_ref(),
&element_local_name,
- &element_namespace,
+ element_namespace.as_ref(),
global,
&input,
);
diff --git a/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy_factory.rs b/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy_factory.rs
index e814bff3e43..2c53ca9c16d 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy_factory.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/trusted_types/trusted_type_policy_factory.rs
@@ -96,7 +96,7 @@ impl TrustedTypePolicyFactory {
let attr_ns = attr_ns.filter(|attr_ns| !attr_ns.is_empty());
// 5. Let interface be the element interface for localName and elementNs.
- let interface = element_interface(&local_name, &element_namespace);
+ let interface = element_interface(&local_name, Some(&element_namespace));
// 6. Let expectedType be null.
let mut expected_type = None;
@@ -294,13 +294,15 @@ pub(crate) fn get_trusted_type_data_for_attribute(
// 1. Let data be null.
let element_name = element.element_name;
- let element_ns = Utf16View::of_string(&element.element_ns);
+ let element_ns = element.element_ns.as_ref().map(Utf16View::of_string);
let attribute_view = Utf16View::of_string(attribute);
// 2. If attributeNs is null, « HTML namespace, SVG namespace, MathML namespace » contains element’s namespace, and attribute is the name of an event handler content attribute:
if attribute_view.starts_with(Utf16View::Ascii(b"on"))
&& attribute_ns.is_none()
- && (element_ns == HTML_NAMESPACE || element_ns == SVG_NAMESPACE || element_ns == MATHML_NAMESPACE)
+ && element_ns.is_some_and(|element_ns| {
+ element_ns == HTML_NAMESPACE || element_ns == SVG_NAMESPACE || element_ns == MATHML_NAMESPACE
+ })
&& is_event_handler_content_attribute(attribute)
{
// 1. Return (Element, null, attribute, TrustedScript, "Element " + attribute).
diff --git a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp
index 8564ef55390..655f91c9ea7 100644
--- a/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp
+++ b/Libraries/LibWeb/TrustedTypes/TrustedTypePolicy.cpp
@@ -17,7 +17,6 @@
#include <LibWeb/HTML/Sanitizer.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
-#include <LibWeb/Namespace.h>
#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
#include <LibWeb/TrustedTypes/TrustedHTML.h>
#include <LibWeb/TrustedTypes/TrustedHTMLParserOptions.h>
@@ -35,7 +34,7 @@ extern "C" {
// Defined in Bindings/Rust/src/trusted_types/trusted_type_policy.rs.
WebIDL::CallbackType* web_rust_trusted_types_trusted_type_policy_create_parser_options(TrustedTypePolicy*);
u8 web_rust_trusted_types_get_trusted_type_compliant_string(JSVM*, u8 expected_type, JSObject* global, u8 input_kind, void* input_cell, FlatPtr input_string, FlatPtr sink, FlatPtr sink_group, FlatPtr* string, JSValue* exception);
-u8 web_rust_trusted_types_get_trusted_types_compliant_attribute_value(JSVM*, FlatPtr attribute_name, bool has_attribute_ns, FlatPtr attribute_ns, FlatPtr element_local_name, FlatPtr element_namespace, JSObject* global, u8 input_kind, void* input_cell, FlatPtr input_string, FlatPtr* string, JSValue* exception);
+u8 web_rust_trusted_types_get_trusted_types_compliant_attribute_value(JSVM*, FlatPtr attribute_name, bool has_attribute_ns, FlatPtr attribute_ns, FlatPtr element_local_name, bool has_element_namespace, FlatPtr element_namespace, JSObject* global, u8 input_kind, void* input_cell, FlatPtr input_string, FlatPtr* string, JSValue* exception);
bool web_rust_trusted_types_process_value_with_a_default_policy(JSVM*, u8 trusted_type_name, JSObject* global, u8 input_kind, void* input_cell, FlatPtr input_string, FlatPtr sink, void** result, JSValue* exception);
// Called by Bindings/Rust/src/trusted_types/trusted_type_policy.rs.
@@ -464,7 +463,8 @@ WebIDL::ExceptionOr<Utf16String> get_trusted_types_compliant_attribute_value(Utf
attribute_ns.has_value(),
attribute_ns.has_value() ? attribute_ns->to_utf16_string().into_raw() : 0,
element.local_name().to_utf16_string().into_raw(),
- element.namespace_uri().value_or(Namespace::HTML).to_utf16_string().into_raw(),
+ element.namespace_uri().has_value(),
+ element.namespace_uri().has_value() ? element.namespace_uri()->to_utf16_string().into_raw() : 0,
JS::HostABI::object_to_abi(&global),
input_abi.kind,
input_abi.cell,
diff --git a/Tests/LibWeb/Text/expected/TrustedTypes/setAttribute-on-element-without-namespace.txt b/Tests/LibWeb/Text/expected/TrustedTypes/setAttribute-on-element-without-namespace.txt
new file mode 100644
index 00000000000..5b79e7e75eb
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/TrustedTypes/setAttribute-on-element-without-namespace.txt
@@ -0,0 +1,4 @@
+null script src: value
+null iframe srcdoc: value
+null div onclick: value
+http://www.w3.org/1999/xhtml script src: TypeError
diff --git a/Tests/LibWeb/Text/input/TrustedTypes/setAttribute-on-element-without-namespace.html b/Tests/LibWeb/Text/input/TrustedTypes/setAttribute-on-element-without-namespace.html
new file mode 100644
index 00000000000..2e83554c9dd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/TrustedTypes/setAttribute-on-element-without-namespace.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const cases = [
+ [null, "script", "src"],
+ [null, "iframe", "srcdoc"],
+ [null, "div", "onclick"],
+ ["http://www.w3.org/1999/xhtml", "script", "src"],
+ ];
+ for (const [namespace, localName, attribute] of cases) {
+ const element = document.createElementNS(namespace, localName);
+ try {
+ element.setAttribute(attribute, "value");
+ println(`${namespace} ${localName} ${attribute}: ${element.getAttribute(attribute)}`);
+ } catch (error) {
+ println(`${namespace} ${localName} ${attribute}: ${error.name}`);
+ }
+ }
+ });
+</script>