Conversation
|
Two things for the top comment, I don't think you do fold set and filter html anymore? Also this also doesn't touch execCommand with the insertHTML command. (This is probably the right move because that isnt specced correctly to start with. But worth calling out probably). |
Thanks, OP updated. |
|
Did we decide it was okay to not enforce the sanitizing if the document was XML? |
That's what I understood but @mozfreddyb, @evilpie or @otherdaniel would know more. Sanitization is not specified for the XML parser. |
What I remember is that we specified Sanitizer API only for methods that would inherently only support HTML syntax. This is probably best articulated in the hopelessly outdated explainer Nearly all interesting bits are specified in terms of DOM & DOM operations, so I'd expect this to be easy to adapt to XML. But IMHO, application to XML-parser parse trees requires a second look, since it invalidates one of the assumptions we had when specifying any of this. A silly example, but the only one I can think of: CDataSection in https://wicg.github.io/sanitizer-api/#sanitize-core step 1.1. That shouldn't be difficult to fix; but at least for now Sanitizer would assert-fail on (some) XML parse trees. In our implementation, there's a runtime assert there. |
|
I guess my main concern is people defining a trusted types policy with this new function thinking it protects them and then it doesn't because they're in XHTML or something? Assuming I'm reading this right you'd end up with a default policy explicitly setup to remove unsafe and then it actually no-ops when it's called by a legacy sync in XML. |
You mean sink? Yea it's limited in that way. But |
I think that the specific guidance to developers to be to check the type of document when they create the default policy, use createHTML with the appropriate userland sanitizer if either this is an XML document or TrustedParserOptions is not supported, and createParserOptions otherwise |
|
Non-authoratative LGTM. I'm still slightly unsure about the XML case mentioned above but if the consensus is that it's fine then I buy that. |
Updated in w3c/trusted-types#606 |
|
LGTM. The way the options are handled across TT is now quite complicated, but from what I can tell the behavior is still correct. |
|
Applied multiple comments from offline review by @annevk. |
810d7a7 to
02b7179
Compare
annevk
left a comment
There was a problem hiding this comment.
Looks good, but might need some changes given my latest comments here: w3c/trusted-types#606 (review).
18c1232 to
f2b459f
Compare
createParserOptions TrustedParseOptions in IDL Still wrap things in set and filter HTML Add clarification about XMLL vs HTML Some fixes from ChatGPT Throw when applying sanitizer to XML document in a legacy method Rebaseline to new TT changes Handle default sanitizer correctly when safe is false, and fix TT throwIfMissing polarity Revert "Handle default sanitizer correctly when safe is false, and fix TT throwIfMissing polarity" This reverts commit 7898aca. Handle null in TrustedParserOptions Fix ccf Add some null checks Address review comments for PR 12583 (TrustedParserOptions & sanitizer options) Address Opus review notes Improve scripting mode switchh nit Fix unclosed li tags in script preparation and fragment parsing Use <span> in IDL Fix <span> in WebIDL Remove <code> in WebIDL Fix WebIDL type xref and line wrapping in parseHTMLUnsafe and fragment parsing Editorial: fix phrasing, line wrapping, and formatting in sanitizer options Editorial: fix phrasing, line wrapping, and formatting in sanitizer options nits Address review comments on TrustedParserOptions & sanitizer options specfmt Export 'canonicalize the configuration' and 'built-in safe default configuration' Editorial: Remove unused ParseHTMLUnsafeOptions from fragment parsing algorithm steps signature Editorial: address review comments on CPO PR Editorial: address style and markup reviews on CPO Editorial: address review comments on CPO PR (B4, B9, N5, N6, N8) Address review comments on parser options and trusted types More explicit naming Pass node document's type to get trusted type compliant input Editorial: address review comments on TT and XML handling Remove comment Editorial: Remove inaccurate domintro sentences about XML TypeError Editorial: note that sanitization is not supported for XML documents TrustedParserOptions->TrustedHTMLParserOptions Editorial: Rename sanitizerSpec to sanitizerInput in get a sanitizer instance from options Address review feedback on fragment parser options and trusted types
This lets Trusted Types reject author-supplied `runScripts` without breaking `createContextualFragment`. See w3c/trusted-types#616. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
I think this and w3c/trusted-types#606 are ready to land. |
AI reviewLine numbers are
The |
Fixed all of the above |
In the places where Trusted Types' createHTML is called, we now also check for createParserOptions and call that if it's available, by calling "get trusted type compliant input" instead of "get trusted type compliant string".
This now allows a default policy to supply a sanitizer or change script-running behavior for the legacy markup insertion methods (innerHTML, outerHTML, insertAdjacentHTML(), createContextualFragment()), and to alter the sanitizer/script behavior of modern calls like setHTMLUnsafe() and parseHTMLUnsafe().
The exceptions to this are srcdoc, document.write(), DOMParser.parseFromString(), and the insertHTML execCommand. We can resolve separately whether these should also be sanitized with createParserOptions.
Structural changes
The safe boolean and scriptingMode parameters of "set and filter HTML" / "fragment parsing algorithm steps" are replaced by a single fragment parser mode enum: Safe (setHTML(), parseHTML()), Unsafe (setHTMLUnsafe(), parseHTMLUnsafe()), and Legacy (innerHTML, outerHTML, insertAdjacentHTML(), createContextualFragment()). Safe and Unsafe always use the HTML parser and allow declarative shadow roots; Legacy keeps today's behavior (XML parser in XML documents, no declarative shadow roots).
Since all markup insertion methods can now include a sanitizer, most of the steps from "set and filter HTML" are folded into the "fragment parsing algorithm steps" (which moved next to innerHTML), with handling of a null sanitizer when appropriate.
"get trusted type compliant input" takes an optionsFromAuthor boolean. It is true for author-supplied options (setHTMLUnsafe(), parseHTMLUnsafe()) and false when the sink constructs the options itself (the legacy sinks, including createContextualFragment(), which sets runScripts to true). This lets Trusted Types throw when TrustedHTML is combined with unvetted author options that need vetting (runScripts: true or a non-empty sanitizer), while not throwing for sink-constructed options.
Observable changes beyond the Trusted Types integration
The = {} defaults are removed from SetHTMLUnsafeOptions.sanitizer and ParseHTMLUnsafeOptions.sanitizer so that a default policy can distinguish an omitted sanitizer member from an explicit configuration.
A Sanitizer object passed to setHTML()/parseHTML() is no longer mutated: the sink now works on a fresh copy of its configuration, so sanitizer.get() is unchanged after use. Previously the "remove unsafe" step mutated the author's object in place.
"configure a sanitizer" clones its input configuration before canonicalizing, fixing an issue where the built-in safe default configuration singleton was mutated in place on first use.
createContextualFragment() now creates its fallback body context element in the range's start node's node document (previously "this's node document", but a
Rangehas no node document).A default policy is only allowed to provide a sanitizer for HTML documents. In XML documents the createParserOptions shortcut does not apply, so createHTML (or a TrustedHTML) is still required, exactly as today. (setHTMLUnsafe() in an XHTML document still uses the HTML parser and therefore does honor createParserOptions.)
Together with w3c/trusted-types#606, which must land first (this PR links to anchors defined there); more details on the Trusted Types side are described there.
Sanitizer)(See WHATWG Working Mode: Changes for more details.)
/dom.html ( diff )
/dynamic-markup-insertion.html ( diff )
/iframe-embed-object.html ( diff )
/infrastructure.html ( diff )
/parsing.html ( diff )
/scripting.html ( diff )