Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace innerHTML with Trusted Types friendly ways #821

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Snurppa
Copy link

@Snurppa Snurppa commented Aug 23, 2023

This will enable usage in Trusted Types context.
As Element.innerHTML is considered to be dangerous injection sink, its usage is denied when Content-Security-Policy enforces require-trusted-types-for 'script' directive.

E.g. it throws something like this from clearElement function:

TypeError: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment.
    at D.clearElement (xyz.js:733:62)
    at D.start (xyz.js:710:176)

textContent in MDN.

replaceChildren has ~91% coverage

Off-topic FYI, some info about TT status with TypeScript: https://github.com/google/tsec#writing-trusted-type-compatible-code

This will enable usage in [Trusted Types](https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API)
context.
As Element.innerHTML is considered to be dangerous injection sink, its usage is denied
when `Content-Security-Policy` enforces `require-trusted-types-for 'script'` directive.

E.g. it throws something like this from `clearElement` function:
```
TypeError: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment.
    at D.clearElement (xyz.js:733:62)
    at D.start (xyz.js:710:176)
```

[textContent](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent#differences_from_innerhtml) in MDN.
[replaceChildren](https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren) has [~91% coverage](https://caniuse.com/mdn-api_element_replacechildren)
@Snurppa Snurppa force-pushed the feature/unsafe-innerhtml branch from a22dd3a to c332881 Compare August 23, 2023 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant