Skip to content

JavaScript: Add setHTMLUnsafe and parseHTMLUnsafe as XSS sinks#21648

Open
sunnyeo wants to merge 1 commit intogithub:mainfrom
sunnyeo:js/add-setHTMLUnsafe-parseHTMLUnsafe-sinks
Open

JavaScript: Add setHTMLUnsafe and parseHTMLUnsafe as XSS sinks#21648
sunnyeo wants to merge 1 commit intogithub:mainfrom
sunnyeo:js/add-setHTMLUnsafe-parseHTMLUnsafe-sinks

Conversation

@sunnyeo
Copy link
Copy Markdown

@sunnyeo sunnyeo commented Apr 3, 2026

Summary

Add two new HTML Sanitizer API methods as DOM-based XSS sinks:

  • Element.setHTMLUnsafe(html) — Added to interpretsArgumentsAsHtml in DOM.qll, following the same pattern as insertAdjacentHTML and document.write. Receiver validation via isDomNode is inherited from DomMethodCallNode.

  • Document.parseHTMLUnsafe(html) — Added to HtmlParserSink in DomBasedXssCustomizations.qll, following the same GlobalVarRefNode pattern as DOMParser.parseFromString. This is a static method on the Document class.

Both methods are part of the HTML Sanitizer API and are shipping in browsers (Chrome 124+, Firefox 148+). Unlike their safe counterparts (setHTML, parseHTML), these methods do not sanitize input and are therefore XSS sinks.

Changes

File Change
DOM.qll Add setHTMLUnsafe (arg 0) to interpretsArgumentsAsHtml
DomBasedXssCustomizations.qll Add Document.parseHTMLUnsafe (arg 0) to HtmlParserSink

Design decisions

  • setHTMLUnsafe in interpretsArgumentsAsHtml: This is a DOM element instance method (like insertAdjacentHTML, write), so DomMethodCallNode handles receiver validation via isDomNode.

  • parseHTMLUnsafe in HtmlParserSink: This is a static method on the Document class (Document.parseHTMLUnsafe(html)). The Document class reference is not recognized by isDomNode (which tracks DOM node instances, not class constructors). We use GlobalVarRefNode to match the Document receiver, following the same pattern as DOMParser.parseFromString.

  • ShadowRoot.setHTMLUnsafe: ShadowRoot is not currently tracked by domValueRef(), so isDomNode does not cover it. Adding ShadowRoot support to domValueRef() is a separate enhancement tracked independently.

References

Add support for two new HTML Sanitizer API methods that interpret
arguments as HTML without sanitization:

- `Element.setHTMLUnsafe(html)`: Added to `interpretsArgumentsAsHtml`
  in DOM.qll, following the same pattern as `insertAdjacentHTML` and
  `document.write`. Receiver validation via `isDomNode` is inherited
  from `DomMethodCallNode`.

- `Document.parseHTMLUnsafe(html)`: Added to `HtmlParserSink` in
  DomBasedXssCustomizations.qll, following the same
  `GlobalVarRefNode` pattern as `DOMParser.parseFromString`. This
  is a static method on the `Document` class.

Both methods are part of the HTML Sanitizer API and are shipping in
browsers (Chrome 124+, Firefox 148+). Unlike their safe counterparts
(`setHTML`, `parseHTML`), these methods do not sanitize input and are
therefore XSS sinks.

References:
- https://developer.mozilla.org/en-US/docs/Web/API/Element/setHTMLUnsafe
- https://developer.mozilla.org/en-US/docs/Web/API/Document/parseHTMLUnsafe_static
Copilot AI review requested due to automatic review settings April 3, 2026 17:46
@sunnyeo sunnyeo requested a review from a team as a code owner April 3, 2026 17:46
@github-actions github-actions bot added the JS label Apr 3, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the JavaScript DOM-based XSS dataflow modeling to recognize two HTML Sanitizer API “unsafe” methods as sinks, ensuring flows into these APIs are reported as potential XSS.

Changes:

  • Add Element.setHTMLUnsafe(html) (argument 0) as an HTML-interpreting DOM sink via interpretsArgumentsAsHtml.
  • Add Document.parseHTMLUnsafe(html) (argument 0) as an HTML parser sink in HtmlParserSink.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
javascript/ql/lib/semmle/javascript/security/dataflow/DomBasedXssCustomizations.qll Extends HtmlParserSink to include Document.parseHTMLUnsafe argument 0.
javascript/ql/lib/semmle/javascript/security/dataflow/DOM.qll Extends DOM method-call HTML interpretation to include setHTMLUnsafe argument 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants