Trusted Types checks for onmessage and onreadystatechange attributes
Categories
(Core :: DOM: Events, enhancement)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox137 | --- | fixed |
People
(Reporter: fwang, Assigned: fwang)
References
(Blocks 1 open bug)
Details
(Keywords: dev-doc-complete)
Attachments
(2 files)
Tests:
- set-event-handlers-content-attributes.tentative.html
- TrustedTypePolicyFactory-getAttributeType-event-handler-content-attributes.tentative.html
Spec:
- TT: https://w3c.github.io/trusted-types/dist/spec/#abstract-opdef-get-trusted-type-data-for-attribute (and https://github.com/w3c/trusted-types/issues/520)
- WindowEventHandlers's onmessage: https://html.spec.whatwg.org/multipage/webappapis.html#windoweventhandlers
- Document's onreadystatechange: https://html.spec.whatwg.org/#the-document-object:handler-onreadystatechange
Implementation:
- TT: https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/dom/security/trusted-types/TrustedTypeUtils.cpp#599
- nsContentUtils: https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/dom/base/nsContentUtils.cpp#1016
onmessage is an event handler attribute of body/frameset but we use the EventNameType_None flag. Probably the onmessage content attribute does not work on body right now and there is a FIXME message:
https://searchfox.org/mozilla-central/rev/a965e3c683ecc035dee1de72bd33a8d91b1203ed/dom/events/EventNameList.h#300
// XXXbz Should the onmessage attribute on <body> really not work? If so, do we
// need a different macro to flag things like that (IDL, but not content
// attributes on body/frameset), or is just using EventNameType_None enough?
WINDOW_EVENT(message, eMessage, EventNameType_None, eBasicEventClass)
onreadystatechange is a document event and not an event handler attribute of any DOM element, however we use the EventNameType_HTMLXUL flag. Because nsContentUtils does not make a distinction for DOCUMENT_ONLY_EVENT (or WINDOW_ONLY_EVENT etc), it is currently wrongly treated as a event handler content attribute for TT.
DOCUMENT_ONLY_EVENT(readystatechange, eReadyStateChange, EventNameType_HTMLXUL,
eBasicEventClass)
Probably we would need a nsContentUtils::EventHandlerContentAttributeName that checks an extra flag. Defining FORWARDED_EVENT to set the flag a seems to be what we want, but we should make sure that still works for "HTMLMediaElement" (seems to be using EVENT macro) or "SVGAnimationElement" (seems to be using NON_IDL_EVENT macro).
Updated•1 year ago
|
| Assignee | ||
Comment 1•1 year ago
|
||
| Assignee | ||
Comment 2•1 year ago
|
||
WINDOW_ONLY_EVENT and DOCUMENT_ONLY_EVENT are not supposed to be used for event
names corresponding to event handler content attributes. However, the
EventNameType_HTML bit is set for onreadystatechange and onvisibilitychange
attributes, and this bit is used here:
- EventNameType_All: Only used in TrustedTypeUtils.cpp to treat attributes as
TrustedScript sinks for attribute mutation and
TrustedTypePolicyFactory.getAttributeType(). - EventNameType_HTML: Only used in nsIContent::IsEventAttributeName() (via its
Internal version) to register event handlers on HTML elements and treat
attributes as JavaScript in the XML serializer. - EventNameType_HTMLXUL: Not used outside EventNameList.h
This patch stops treating onreadystatechange/onvisibilitychange as HTML content
attributes and only treat them as IDL attributes on the Document object:
https://html.spec.whatwg.org/multipage/webappapis.html#event-handlers-on-elements,-document-objects,-and-window-objects
TODO: add intent to unship.
| Assignee | ||
Updated•1 year ago
|
Updated•1 year ago
|
Updated•1 year ago
|
| Assignee | ||
Comment 3•1 year ago
|
||
Probably the onmessage content attribute does not work on body right now and there is a FIXME message:
Testcase: data:text/html,<body onmessage="alert('Hello World!')" onload="this.dispatchEvent(new Event('message'))">
Probably we would need a nsContentUtils::EventHandlerContentAttributeName that checks an extra flag. Defining FORWARDED_EVENT to set the flag a seems to be what we want, but we should make sure that still works for "HTMLMediaElement" (seems to be using EVENT macro) or "SVGAnimationElement" (seems to be using NON_IDL_EVENT macro).
So it turned out this suggestion was wrong, I think we just don't need the EventNameType_HTML bit (probably EventNameType_XUL either).
Testcase: data:text/html,<div id="x" onreadystatechange="alert('Unexpected event!')"><script>x.dispatchEvent(new Event("readystatechange"))</script>
Updated•1 year ago
|
https://hg.mozilla.org/mozilla-central/rev/c8dfc7a544b4
https://hg.mozilla.org/mozilla-central/rev/fba1faefa13a
https://hg.mozilla.org/mozilla-central/rev/06c418a8e2bb
Comment 8•10 months ago
|
||
I'm attempting to update compatibility data for APIs affected by trusted types. Can you explain what this change means for web application developers? (i.e. most changes I've been documented have clear IDL indicating that a property or method parameter can now take a Trusted type, but I can't tell what developers should do differently because of this change.
Can you explain? More particularly, what the compatibility data and docs changes might look like.
| Assignee | ||
Comment 9•10 months ago
|
||
(In reply to Hamish Willee from comment #8)
I'm attempting to update compatibility data for APIs affected by trusted types. Can you explain what this change means for web application developers? (i.e. most changes I've been documented have clear IDL indicating that a property or method parameter can now take a Trusted type, but I can't tell what developers should do differently because of this change.
Can you explain? More particularly, what the compatibility data and docs changes might look like.
Hi. I think the dev-doc-needed request was just for the following intent-to-ship:
https://groups.google.com/a/mozilla.org/g/dev-platform/c/M6k6vwMfzKQ
https://groups.google.com/a/mozilla.org/g/dev-platform/c/LJC3FnNbmfI
They indeed do affect Trusted Types (because we run Trusted Types check when setting an "onevent" content attribute on an element that really supports it ; and because that also affects the corresponding result for trustedTypes.getAttributeType(element, "onevent")) but that's really a minor change, so probably not worth documenting.
Comment 10•10 months ago
|
||
Thanks Fred, I think the TLDR is that you're saying that there is nothing to document in this. Is that right?
I'm not sure I understand the rest of your response, but I THINK that what you're saying is that trustedTypes.getAttributeType()
- now "works" for
onmessageattribute, and presumably returnsnull? - no longer works for
onreadystatechange/onvisibilitychangeattributes on elements. We don't document the existence of this handler, so I think nothing to say.
If so then doing nothing makes sense - we don't need to update the docs that an attribute that doesn't require a trusted type will now be handled properly.
| Assignee | ||
Comment 11•10 months ago
|
||
Yes. What I'm saying is that what has changed is the behavior of <body onmessage="willnowrun()"> and <div onreadystatechange="wontrunanymore()"> when your dispatch a "message" or "readyonstatechange" event of them. I'll let you judge whether that's important enough to document.
Regarding how that affects TrustedTypes, I would say these changes don't need to be documented. But here there are for completeness:
-
trustedTypes.getAttributeType("body", "onmessage")will now return "TrustedScript" whiletrustedTypes.getAttributeType("div", "onreadystatechange")will now return null (it was the other way around before these two patches) -
But more interestingly,
document.body.setAttribute("onmessage", "unsafeHandler()")will run trusted type checks on"unsafeHandler()"whilemyDiv.setAttribute("onreadystatechange", "unsafeHandler()")won't run such checks any more (again it was the other way around before these two patches).
(same changes apply for other HTML elements than div, for "onvisibilitychange", and for other ways of setting attribute like setAttributeNS, setNamedItem, ...)
Updated•7 months ago
|
Description
•