Skip to content

Commit

Permalink
Avoid triggering a CSP (content security policy) error (rrweb-io#846)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
eoghanmurray committed Jul 27, 2023
1 parent fb5ed64 commit b5a3a8e
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ export default class MutationBuffer {
if (isIgnored(m.target, this.mirror)) {
return;
}
let unattachedDoc;
try {
// avoid upsetting original document from a Content Security point of view
unattachedDoc = document.implementation.createHTMLDocument();
} catch (e) {
// fallback to more direct method
unattachedDoc = this.doc;
}
switch (m.type) {
case 'characterData': {
const value = m.target.textContent;
Expand Down Expand Up @@ -554,7 +562,7 @@ export default class MutationBuffer {
}

if (attributeName === 'style') {
const old = this.doc.createElement('span');
const old = unattachedDoc.createElement('span');
if (m.oldValue) {
old.setAttribute('style', m.oldValue);
}
Expand Down

0 comments on commit b5a3a8e

Please sign in to comment.