GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — fix: use CSS Declaration `replaceSync` to parse styles to avoid CSP violations by logaretm · Pull Request #286 · getsentry/rrweb Conversation
|
| File | Description |
|---|---|
| packages/rrweb/src/record/style-declaration-parser.ts | New parser abstraction using replaceSync() to avoid CSP issues. |
| packages/rrweb/src/record/mutation.ts | Switch style-diff old-style parsing to StyleDeclarationParser. |
| packages/rrweb/test/style-declaration-parser.test.ts | Add unit tests for fallback vs constructable stylesheet parsing. |
| packages/rrweb/test/record.test.ts | Add regression test for style mutations with separators inside quoted strings. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return ( | ||
| this.parseWithConstructableStylesheet(styleText) || | ||
| this.parseWithDetachedElement(styleText) | ||
| ); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
parse() always falls back to parseWithDetachedElement() when constructable stylesheet parsing returns null (including when replaceSync() throws on invalid CSS). In strict CSP environments this can reintroduce the very style-src-attr violation this change is trying to avoid. Consider not using the detached-element fallback when replaceSync is available but parsing fails; instead return null/an empty declaration list and let the caller treat it as “unknown old style” (which is safer under CSP).
| return ( | |
| this.parseWithConstructableStylesheet(styleText) || | |
| this.parseWithDetachedElement(styleText) | |
| ); | |
| if ( | |
| typeof CSSStyleSheet === 'undefined' || | |
| typeof CSSStyleSheet.prototype.replaceSync !== 'function' | |
| ) { | |
| return this.parseWithDetachedElement(styleText); | |
| } | |
| return this.parseWithConstructableStylesheet(styleText); |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| try { | ||
| const sheet = new CSSStyleSheet(); | ||
| sheet.replaceSync(`x { ${styleText} }`); | ||
|
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
parseWithConstructableStylesheet() allocates a new CSSStyleSheet on every parse. Style mutations can be frequent, so this can add avoidable GC/CPU overhead. Consider caching a single CSSStyleSheet instance on the parser when replaceSync is available and reusing it for subsequent replaceSync() calls.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| public parse(styleText: string): CSSStyleDeclaration | null { | ||
| return ( | ||
| this.parseWithConstructableStylesheet(styleText) || | ||
| this.parseWithDetachedElement(styleText) | ||
| ); | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The parse() return type is CSSStyleDeclaration | null, but with the current implementation it will always return a CSSStyleDeclaration (because parseWithDetachedElement() always returns one and isn’t guarded). Either make parse() non-nullable to match behavior, or add error handling so parseWithDetachedElement() can fail and return null (which also makes it easier to avoid CSP-unsafe fallbacks when replaceSync is available).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Reuse a single constructable stylesheet across parse() calls instead of allocating a new one per style mutation. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…nParser" This reverts commit 60ae612.
|
AI found an optimization with style allocation but decided to scrap it because it can cause bugs in the future where cached references values change by parsing new values. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Bumps all rrweb dependencies in tests and internal packages to the latest versions. The changelog for these new releases is: - [(rrweb-snapshot) Rewrite vulnerable regexes](getsentry/rrweb#289) - [Use CSS Declaration replaceSync to parse styles to avoid CSP violations](getsentry/rrweb#286) - [Wrap iframe contentWindow access in try-catch](getsentry/rrweb#275) Full changelog can be [found here](https://github.com/getsentry/rrweb/releases/tag/2.42.0).
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.