Skip to content

Support Trusted Types API to prevent XSS attacks #1123

@ValerianClerc

Description

@ValerianClerc

Problem

Currently, html-dom-parser unsafely assigns strings to innerHtml. Browsers now support a method for developers to assert that any innerHtml has been sanitized and is safe to assign to innerHtml, in the form of TrustedTypes. See documentation here:

Suggested Solution

I can think of 2 ways to fix this:

  1. Allow the user to pass in a TrustedHTML object instead of a string. This would mean that the caller of html-dom-parser would do the sanitization, and then pass in a safe string into html-dom-parser.
    • This strategy requires relaxing the check for typeof "string" here in the code. However, we would also need to avoid any re-assigning of the html string, removing instances like this, which would make special character escapes hard to handle.
  2. Allow the user to pass in a TrustedTypePolicy, and then html-dom-parser would run policy.createHtml(myString) before assigning anything to innerHtml.

Keywords

security, trustedtypes, innerhtml

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions