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

Avoid triggering a CSP (content security policy) error #846

Merged
merged 3 commits into from
May 22, 2023

Conversation

eoghanmurray
Copy link
Contributor

Fix for #816 with .setAttribute('style') — see CSP style-src: unsafe-inline

@eoghanmurray eoghanmurray force-pushed the style-csp-fix branch 2 times, most recently from 5e391d0 to 92febda Compare March 3, 2022 11:51
@changeset-bot
Copy link

changeset-bot bot commented Mar 27, 2023

⚠️ No Changeset found

Latest commit: f36073d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@YunFeng0817
Copy link
Member

Seems like this pull request can cause the failure of two test cases. Could you please check it out?

@eoghanmurray
Copy link
Contributor Author

eoghanmurray commented Apr 3, 2023

I've got errors running test suite on my computer even on the trunk.
What exactly are the tests that are failing for you?
Should the test suite have been run against this PR here? I don't see any message.


Edit: my bad not getting the tests running; my PR was indeed causing failures which I've now fixed...

// avoid upsetting original document from a Content Security point of view
unattachedDoc = document.implementation.createHTMLDocument();
} catch (e) {
// fallback to more direct method
Copy link

Choose a reason for hiding this comment

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

Do we know when we will fallback? I'm curious what would trigger errors and the fallback case? Unsupported browsers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm just not 100% about that document.implementation.createHTMLDocument(); so took the prudent option of falling back to the previous method (which is working fine but wouldn't work for you from a CSP POV)

I just looked it up now and it seems fine:
https://caniuse.com/?search=implementation.createhtmldocument
E.g. it could fail on IE11 as there is a 'title' attribute missing
I don't think anyone is supporting IE11

You could add some reporting in there if it happens, or to add a custom commit on top (for your deployment) to revert this last commit.
I'm putting this out to a 'canary' environment today, but I don't have any reporting on whether the fallback kicks in; I'm happy enough that it worked in the rrweb test environment (Puppeteer/Chrome).

…a doctype and wasn't a HTML document, so the child style element didn't have the `old.style` attribute available
@YunFeng0817 YunFeng0817 merged commit a6ce718 into rrweb-io:master May 22, 2023
eoghanmurray added a commit to eoghanmurray/rrweb that referenced this pull request Jul 27, 2023
* Fix for rrweb-io#816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')`

* The bare unattachedDoc that I previously naively created didn't have a doctype and wasn't a HTML document, so the child style element didn't have the `old.style` attribute available

* Add a try/catch to provide some robustness in case `document.implementation.createHTMLDocument` isn't available
eoghanmurray added a commit to eoghanmurray/rrweb that referenced this pull request Jul 27, 2023
* Fix for rrweb-io#816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')`

* The bare unattachedDoc that I previously naively created didn't have a doctype and wasn't a HTML document, so the child style element didn't have the `old.style` attribute available

* Add a try/catch to provide some robustness in case `document.implementation.createHTMLDocument` isn't available
eoghanmurray added a commit to eoghanmurray/rrweb that referenced this pull request Jul 27, 2023
* Fix for rrweb-io#816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')`

* The bare unattachedDoc that I previously naively created didn't have a doctype and wasn't a HTML document, so the child style element didn't have the `old.style` attribute available

* Add a try/catch to provide some robustness in case `document.implementation.createHTMLDocument` isn't available
eoghanmurray added a commit to eoghanmurray/rrweb that referenced this pull request Aug 3, 2023
* Fix for rrweb-io#816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')`

* The bare unattachedDoc that I previously naively created didn't have a doctype and wasn't a HTML document, so the child style element didn't have the `old.style` attribute available

* Add a try/catch to provide some robustness in case `document.implementation.createHTMLDocument` isn't available
eoghanmurray added a commit to eoghanmurray/rrweb that referenced this pull request Aug 8, 2023
* Fix for rrweb-io#816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')`

* The bare unattachedDoc that I previously naively created didn't have a doctype and wasn't a HTML document, so the child style element didn't have the `old.style` attribute available

* Add a try/catch to provide some robustness in case `document.implementation.createHTMLDocument` isn't available
eoghanmurray added a commit to eoghanmurray/rrweb that referenced this pull request Aug 8, 2023
* Fix for rrweb-io#816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')`

* The bare unattachedDoc that I previously naively created didn't have a doctype and wasn't a HTML document, so the child style element didn't have the `old.style` attribute available

* Add a try/catch to provide some robustness in case `document.implementation.createHTMLDocument` isn't available
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.

4 participants