Skip to content

feat(options): add CSP support with trustedTypePolicy#1439

Merged
remarkablemark merged 6 commits intoremarkablemark:masterfrom
LiveHelperChat:master
May 5, 2026
Merged

feat(options): add CSP support with trustedTypePolicy#1439
remarkablemark merged 6 commits intoremarkablemark:masterfrom
LiveHelperChat:master

Conversation

@remdex
Copy link
Copy Markdown
Contributor

@remdex remdex commented May 4, 2026

What is the motivation for this pull request?

Closes #1123

What is the current behavior?

What is the new behavior?

Will support trusted policy workflows. E.g

let trustedHtml = (window.trustedTypes && window.trustedTypes.createPolicy)
                ? window.trustedTypes.createPolicy('csp-react-html', {createHTML: function(s) { return s; }})
                : null;

root.render(parse('<h1>HTMLReactParser loaded with Webpack</h1>',{
   trustedTypePolicy : trustedHtml
}));

Checklist:

  • Tests
  • Documentation

@remdex remdex requested a review from remarkablemark as a code owner May 4, 2026 12:31
Copilot AI review requested due to automatic review settings May 4, 2026 12:31
@github-actions github-actions Bot review requested due to automatic review settings May 4, 2026 12:31
@remarkablemark remarkablemark changed the title CSP Support feat: add CSP Support May 4, 2026
@remarkablemark remarkablemark changed the title feat: add CSP Support feat: add CSP support May 4, 2026
Copy link
Copy Markdown
Owner

@remarkablemark remarkablemark left a comment

Choose a reason for hiding this comment

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

Thanks for opening the PR!

Can you help address the lint errors?

npm run lint:fix

And fix the test coverage? (If it's too tricky, you can use v8 ignore comment):

npm run test:ci

Comment thread src/types.ts Outdated
* Lint fixes. Actually I was not sure what to do there. So just ignore those. As those test was not written by me initially. Maybe you will change something.
* Test added for 100% coverage.
Copilot AI review requested due to automatic review settings May 4, 2026 20:55
Copy link
Copy Markdown

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

Adds Trusted Types policy support to the browser (client) parsing path so consumers can comply with CSP Trusted Types enforcement by generating TrustedHTML right before innerHTML assignments.

Changes:

  • Introduces trustedTypePolicy on the exported options type and threads options through both server/client entrypoints for a consistent API surface.
  • Updates the client domparser implementation to call trustedTypePolicy.createHTML immediately before any innerHTML sink usage (template/document paths).
  • Adds tests and documentation for the new option, and tweaks ESLint config for the type assertion tests.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/types.ts Adds TrustedTypePolicy and HTMLDOMParserOptions (including trustedTypePolicy).
src/server/html-to-dom.ts Switches options type to HTMLDOMParserOptions for the public API signature.
src/client/html-to-dom.ts Accepts options and forwards trustedTypePolicy into the DOM parsing implementation.
src/client/domparser.ts Applies Trusted Types policy output to innerHTML sinks (template/document parsing paths).
README.md Documents the new trustedTypePolicy option and reorganizes the options section.
eslint.config.mts Disables no-unsafe-call for the type-test directory.
tests/types/index.test.ts Adds a type-level usage case for trustedTypePolicy.
tests/client/index.test.ts Adds runtime tests to ensure the policy hook is invoked when parsing in the client.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/types.ts Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (76d8558) to head (590ecb0).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1439   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          120       122    +2     
  Branches        30        31    +1     
=========================================
+ Hits           120       122    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/client/domparser.ts
Copy link
Copy Markdown
Owner

@remarkablemark remarkablemark left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the lint errors! The only thing that's failing now is the type check:

npm run lint:tsc

Comment thread __tests__/types/index.test.mts Outdated
Comment thread __tests__/types/index.test.ts Outdated
Copy link
Copy Markdown
Owner

@remarkablemark remarkablemark left a comment

Choose a reason for hiding this comment

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

LGTM!

@remarkablemark remarkablemark changed the title feat: add CSP support feat(options): add CSP support with trustedTypePolicy May 5, 2026
@remarkablemark remarkablemark merged commit 25da34e into remarkablemark:master May 5, 2026
12 of 13 checks passed
@remarkablemark
Copy link
Copy Markdown
Owner

@remdex
Copy link
Copy Markdown
Contributor Author

remdex commented May 5, 2026

Also if you could take care of react would be prefect :D

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.

Support Trusted Types API to prevent XSS attacks

3 participants