Open Bug 2081266 Opened 1 month ago Updated 17 days ago

Sanitizing while parsing keeps the form owner of a replaced form and loses declarative shadow DOM on a replaced host

Categories

(Core :: DOM: HTML Parser, defect)

defect

Tracking

()

People

(Reporter: zcorpan, Unassigned)

References

(Blocks 1 open bug)

Details

(Findings by Claude Code, Opus 5.5, high effort.)

I found these while reviewing bug 2062652 against https://github.com/whatwg/html/pull/12756 (at 53c8837), with dom.security.sanitizer.while-parsing enabled. I ran the sanitizer-api tests from https://github.com/web-platform-tests/wpt/pull/62492.

  1. When a form is replaced with its children, its controls still get it as their form owner.
Document.parseHTMLUnsafe("<form id=f><input></form>", {sanitizer: {replaceWithChildrenElements: ["form"]}})

Here input.form is the detached <form id="f">, but the spec expects null: the insertion target redirection map has an entry for the form element pointer's element. The nsHtml5TreeBuilder::createElement overload that takes aFormElement calls nsHtml5TreeOperation::SetFormElement without checking for this. sanitizer-api/sethtml-form-owner.html fails on this.

  1. A declarative shadow root whose host is replaced with its children is attached to that host, so its content is lost.
Document.parseHTMLUnsafe("<div><template shadowrootmode=open>s</template>x</div>", {sanitizer: {replaceWithChildrenElements: ["div"]}}).body.innerHTML

Fx returns x; the spec expects <template shadowrootmode="open">s</template>x (the template goes where the host's children go). appendToCurrentNodeAndPushTemplateElement only checks the sanitizer action for the template itself, not its host. The WPT test for this passes only because of 3.

  1. With a sanitizer, setHTML() and setHTMLUnsafe() (SetAndFilterHTML) pass Nothing() as the custom element registry, and nsContentUtils::ParseFragmentHTML always disallows declarative shadow roots. The setHTMLUnsafe() path without a sanitizer passes both. As a result, allowed custom elements aren't constructed before the call returns, scoped registries are ignored, and no declarative shadow roots are created. Tests fail in sethtml-with-custom-elements.html, sanitizer-custom-elements-constructor.html and sethtml-with-declarative-shadow-root.html. This predates bug 2062652. When the registry gets passed through, createElement also needs to skip the custom element definition for elements the sanitizer removes or replaces with their children, since the spec gives those a null registry. Today the missing registry hides that.

  2. Some comments no longer match the spec. SanitizerState::SanitizeMergedAttributes and addAttributesToElement say the spec re-sanitizes html/body after merging attributes and asserts "Keep"; the PR filters each token attribute before merging instead. Comments in Sanitizer.cpp, nsContentUtils.h, nsHtml5StringParser.cpp and nsHtml5TreeBuilderHSupplement.h cite "sanitize an Element", "remove javascript navigation URLs" and the #sanitize anchor, none of which are in the PR anymore.

The severity field is not set for this bug.
:jjaschke, could you have a look please?

For more information, please visit BugBot documentation.

Flags: needinfo?(jjaschke)
Severity: -- → S3
Flags: needinfo?(jjaschke)
You need to log in before you can comment on or make changes to this bug.