Add trusted type checks for global event handler attributes (#38718)

This only covers the global event handlers for now, while I figure out
which others we are missing. We don't seem to be missing the
WindowEventHandlers, but not sure where the others coming from.

Part of #36258

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
This commit is contained in:
Tim van der Lippe 2025-08-17 21:44:22 +02:00 committed by GitHub
parent 9da8142e2a
commit a31235e52b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 110 additions and 401 deletions

View file

@ -68,6 +68,98 @@ use crate::dom::workerglobalscope::WorkerGlobalScope;
use crate::realms::{InRealm, enter_realm};
use crate::script_runtime::CanGc;
/// <https://html.spec.whatwg.org/multipage/#event-handler-content-attributes>
/// containing the values from
/// <https://html.spec.whatwg.org/multipage/#globaleventhandlers>
static CONTENT_EVENT_HANDLER_NAMES: [&str; 83] = [
"onabort",
"onauxclick",
"onbeforeinput",
"onbeforematch",
"onbeforetoggle",
"onblur",
"oncancel",
"oncanplay",
"oncanplaythrough",
"onchange",
"onclick",
"onclose",
"oncommand",
"oncontextlost",
"oncontextmenu",
"oncontextrestored",
"oncopy",
"oncuechange",
"oncut",
"ondblclick",
"ondrag",
"ondragend",
"ondragenter",
"ondragleave",
"ondragover",
"ondragstart",
"ondrop",
"ondurationchange",
"onemptied",
"onended",
"onerror",
"onfocus",
"onformdata",
"oninput",
"oninvalid",
"onkeydown",
"onkeypress",
"onkeyup",
"onload",
"onloadeddata",
"onloadedmetadata",
"onloadstart",
"onmousedown",
"onmouseenter",
"onmouseleave",
"onmousemove",
"onmouseout",
"onmouseover",
"onmouseup",
"onpaste",
"onpause",
"onplay",
"onplaying",
"onprogress",
"onratechange",
"onreset",
"onresize",
"onscroll",
"onscrollend",
"onsecuritypolicyviolation",
"onseeked",
"onseeking",
"onselect",
"onslotchange",
"onstalled",
"onsubmit",
"onsuspend",
"ontimeupdate",
"ontoggle",
"onvolumechange",
"onwaiting",
"onwebkitanimationend",
"onwebkitanimationiteration",
"onwebkitanimationstart",
"onwebkittransitionend",
"onwheel",
// https://drafts.csswg.org/css-animations/#interface-globaleventhandlers-idl
"onanimationend",
"onanimationiteration",
// https://drafts.csswg.org/css-transitions/#interface-globaleventhandlers-idl
"ontransitionrun",
"ontransitionend",
"ontransitioncancel",
// https://w3c.github.io/selection-api/#extensions-to-globaleventhandlers-interface
"onselectstart",
"onselectionchange",
];
#[derive(Clone, JSTraceable, MallocSizeOf, PartialEq)]
#[allow(clippy::enum_variant_names)]
pub(crate) enum CommonEventHandler {
@ -956,6 +1048,11 @@ impl EventTarget {
);
}
}
/// <https://html.spec.whatwg.org/multipage/#event-handler-content-attributes>
pub(crate) fn is_content_event_handler(name: &str) -> bool {
CONTENT_EVENT_HANDLER_NAMES.contains(&name)
}
}
impl EventTargetMethods<crate::DomTypeHolder> for EventTarget {

View file

@ -20,6 +20,7 @@ use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::csp::CspReporting;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::trustedhtml::TrustedHTML;
use crate::dom::trustedscript::TrustedScript;
@ -120,8 +121,18 @@ impl TrustedTypePolicyFactory {
// Step 1: Let data be null.
//
// We return the if directly
// Step 2: If attributeNs is null, and attribute is the name of an event handler content attribute, then:
// TODO(36258): look up event handlers
// Step 2: If attributeNs is null, « HTML namespace, SVG namespace, MathML namespace » contains
// elements namespace, and attribute is the name of an event handler content attribute:
if attribute_namespace.is_none() &&
matches!(*element_namespace, ns!(html) | ns!(svg) | ns!(mathml)) &&
EventTarget::is_content_event_handler(attribute)
{
// Step 2.1. Return (Element, null, attribute, TrustedScript, "Element " + attribute).
return Some((
TrustedType::TrustedScript,
"Element ".to_owned() + attribute,
));
}
// Step 3: Find the row in the following table, where element is in the first column,
// attributeNs is in the second column, and attribute is in the third column.
// If a matching row is found, set data to that row.