rrweb: Regression: support for strict CSP broken by style mutation
The optimization in PR https://github.com/rrweb-io/rrweb/pull/464 broke support for users who run a strict Content-Security-Policy (CSP).
Specifically, this line sets the style attribute on a DOM node and will be blocked without style-src: 'unsafe-inline' (which is the unsafe CSP).
For any apps that pull in the affected versions and have a strict CSP, this issue will generate a very large numbers of errors like the one below (one for each time that line of code runs). As a possible side effect, any error reporting services or report-url endpoints may be quickly overwhelmed with error reports.

About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 1
- Comments: 32 (17 by maintainers)
Commits related to this issue
- Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` — committed to statcounter/rrweb by eoghanmurray 5 years ago
- Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` — committed to statcounter/rrweb by eoghanmurray 5 years ago
- Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` — committed to statcounter/rrweb by eoghanmurray 5 years ago
- Fix for rrweb-io#816 - avoid triggering a CSP (content security policy) error with setAttribute('style') — committed to Analyzee/rrweb by AntonioStipic 4 years ago
- Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` — committed to eoghanmurray/rrweb by eoghanmurray 5 years ago
- Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` — committed to eoghanmurray/rrweb by eoghanmurray 5 years ago
- Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` — committed to eoghanmurray/rrweb by eoghanmurray 5 years ago
- Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` — committed to eoghanmurray/rrweb by eoghanmurray 5 years ago
- Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` — committed to statcounter/rrweb by eoghanmurray 5 years ago
- Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` — committed to statcounter/rrweb by eoghanmurray 5 years ago
- Avoid triggering a CSP (content security policy) error (#846) * Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` * The bare unattachedDoc that I ... — committed to rrweb-io/rrweb by eoghanmurray 3 years ago
- Avoid triggering a CSP (content security policy) error (#846) * Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` * The bare unattachedDoc that I ... — committed to eoghanmurray/rrweb by eoghanmurray 3 years ago
- Avoid triggering a CSP (content security policy) error (#846) * Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` * The bare unattachedDoc that I ... — committed to eoghanmurray/rrweb by eoghanmurray 3 years ago
- Avoid triggering a CSP (content security policy) error (#846) * Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` * The bare unattachedDoc that I ... — committed to eoghanmurray/rrweb by eoghanmurray 3 years ago
- Avoid triggering a CSP (content security policy) error (#846) * Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` * The bare unattachedDoc that I ... — committed to eoghanmurray/rrweb by eoghanmurray 3 years ago
- Avoid triggering a CSP (content security policy) error (#846) * Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` * The bare unattachedDoc that I ... — committed to eoghanmurray/rrweb by eoghanmurray 3 years ago
- Avoid triggering a CSP (content security policy) error (#846) * Fix for #816 - avoid triggering a CSP (content security policy) error with `.setAttribute('style')` * The bare unattachedDoc that I ... — committed to eoghanmurray/rrweb by eoghanmurray 3 years ago
Hello. I just ran in to this same issue. It looks like creating a temporary
spanelement withdocument.implementation.createHTMLDocument();and then callingsetAttribute('style', m.oldValue)still triggers CSP errors in both Chrome and Edge, but not Firefox or Safari interestingly enough. I’m currently testing outold.style.cssText = m.oldValue;as a solution (borrowing the suggestion made above) but wanted to see if I was missing something obvious about why this would not work. I’m happy to submit a PR if this fix seems viable, but figured I’d ask around first.I don’t have the whole context but from what I understand you could modify an element with
element.style.color = 'red';instead of dealing with its attribute (the latter triggering the CSP).In the meantime, my workaround is to use
style-src-attr 'unsafe-inline'instead of doing it onstyle-srcwhich would break in case of already using anoncefor third-party files.Our test suite was able to validate that it didn’t work — I’ve added some commits today to make it actually work so #846 might be able to land in trunk soon.
Sure. Just some more context on how I discovered the issue. I recently updated our app to MUI v5 which is supposed to have strict CSP compatibility now (since they switched to emotion / styled-components and no longer use CSS in JS). I enabled the strict CSP headers and tested the site in staging where we are NOT running rrweb and saw no errors. When moved to prod where recording is enabled, I saw the CSP errors triggered by the referenced mutation line from rrweb.
I actually would expect the app’s attribute updates to trigger CSP but there must be a reason they are allowed.
Here is one of the things triggering a modification that gets picked up by the rrweb mutation.