Repository navigation
fix(rrweb): contain cross-origin iframe shadow observation errors #5150
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
Merged
dustinbyrne
merged 5 commits into
PostHog:main
from
deepanshupal:fix/rrweb-cross-origin-shadow-security-error
Oct 3, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4dc72ee
fix(rrweb): guard cross-origin shadow observation
deepanshupal 2fae2c3
test(rrweb): cover cross-origin shadow observation
deepanshupal 5e33587
chore: add rrweb and posthog-js patch changeset
deepanshupal 7c8e4ba
test: clean up shadow DOM managers after failed assertions
deepanshupal 0fa2c4f
fix: keep rrweb changeset scoped to posthog-js
deepanshupal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'posthog-js': patch | ||
| --- | ||
|
|
||
| Stop a SecurityError escaping the recorder when an iframe becomes cross-origin while shadow DOM observation starts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { afterEach, describe, it, expect, vi } from 'vitest'; | ||
| import { Mirror } from '@posthog/rrweb-snapshot'; | ||
|
|
||
| // jsdom's ShadowRoot doesn't pass isNativeShadowDom's toString check, | ||
|
|
@@ -19,9 +19,25 @@ import { mutationBuffers } from '../../src/record/observer'; | |
| import MutationBuffer from '../../src/record/mutation'; | ||
|
|
||
| describe('ShadowDomManager', () => { | ||
| const managers: ShadowDomManager[] = []; | ||
| const elements: HTMLElement[] = []; | ||
|
|
||
| afterEach(() => { | ||
| try { | ||
| for (const manager of managers.splice(0).reverse()) { | ||
| manager.reset(); | ||
| } | ||
| } finally { | ||
| for (const element of elements.splice(0)) { | ||
| element.remove(); | ||
| } | ||
| vi.restoreAllMocks(); | ||
| } | ||
| }); | ||
|
|
||
| function createManager() { | ||
| const mirror = new Mirror(); | ||
| return new ShadowDomManager({ | ||
| const manager = new ShadowDomManager({ | ||
| mutationCb: vi.fn(), | ||
| scrollCb: vi.fn(), | ||
| bypassOptions: { | ||
|
|
@@ -57,12 +73,62 @@ describe('ShadowDomManager', () => { | |
| }, | ||
| mirror, | ||
| }); | ||
| managers.push(manager); | ||
| return manager; | ||
| } | ||
|
|
||
| it('ignores SecurityError when a frame navigates cross-origin', () => { | ||
| const manager = createManager(); | ||
| const iframe = { | ||
| contentWindow: { | ||
| get Element() { | ||
| throw new DOMException('Cross-origin frame', 'SecurityError'); | ||
| }, | ||
| }, | ||
| contentDocument: document, | ||
| } as unknown as HTMLIFrameElement; | ||
|
|
||
| expect(() => manager.observeAttachShadow(iframe)).not.toThrow(); | ||
| }); | ||
|
|
||
| it('does not swallow unrelated errors while observing a frame', () => { | ||
| const manager = createManager(); | ||
| const iframe = { | ||
| contentWindow: { | ||
| get Element() { | ||
| throw new TypeError('Broken Element getter'); | ||
| }, | ||
| }, | ||
| contentDocument: document, | ||
| } as unknown as HTMLIFrameElement; | ||
|
|
||
| expect(() => manager.observeAttachShadow(iframe)).toThrow( | ||
| 'Broken Element getter', | ||
| ); | ||
| }); | ||
|
|
||
| it('still patches same-origin iframe shadow roots', () => { | ||
| const manager = createManager(); | ||
| const iframe = document.createElement('iframe'); | ||
| elements.push(iframe); | ||
| document.body.appendChild(iframe); | ||
| const iframeDoc = iframe.contentDocument as Document; | ||
| const iframeElement = iframe.contentWindow?.Element; | ||
|
|
||
| expect(iframeElement).toBeDefined(); | ||
| const before = iframeElement?.prototype.attachShadow; | ||
| manager.observeAttachShadow(iframe); | ||
| expect(iframeElement?.prototype.attachShadow).not.toBe(before); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Agent-drafted, reviewed by Tue. If this |
||
|
|
||
| manager.reset(); | ||
| expect(iframeElement?.prototype.attachShadow).toBe(before); | ||
| }); | ||
|
|
||
| it('reset() should not call MutationBuffer.reset() from restoreHandler to avoid infinite recursion', () => { | ||
| const manager = createManager(); | ||
|
|
||
| const host = document.createElement('div'); | ||
| elements.push(host); | ||
| document.body.appendChild(host); | ||
| host.attachShadow({ mode: 'open' }); | ||
|
|
||
|
|
@@ -73,15 +139,13 @@ describe('ShadowDomManager', () => { | |
| manager.reset(); | ||
|
|
||
| expect(resetSpy).not.toHaveBeenCalled(); | ||
|
|
||
| resetSpy.mockRestore(); | ||
| document.body.removeChild(host); | ||
| }); | ||
|
|
||
| it('reset() removes shadow root buffers from mutationBuffers', () => { | ||
| const manager = createManager(); | ||
|
|
||
| const host = document.createElement('div'); | ||
| elements.push(host); | ||
| document.body.appendChild(host); | ||
|
|
||
| const buffersBeforeAdd = mutationBuffers.length; | ||
|
|
@@ -91,28 +155,26 @@ describe('ShadowDomManager', () => { | |
| manager.reset(); | ||
|
|
||
| expect(mutationBuffers.length).toBe(buffersBeforeAdd); | ||
|
|
||
| document.body.removeChild(host); | ||
| }); | ||
|
|
||
| it('reset() clears restore handlers so a second reset is a no-op', () => { | ||
| const manager = createManager(); | ||
|
|
||
| const host = document.createElement('div'); | ||
| elements.push(host); | ||
| document.body.appendChild(host); | ||
| host.attachShadow({ mode: 'open' }); | ||
|
|
||
| manager.reset(); | ||
|
|
||
| expect(() => manager.reset()).not.toThrow(); | ||
|
|
||
| document.body.removeChild(host); | ||
| }); | ||
|
|
||
| it('resetForDoc() only tears down handlers owned by the given document', () => { | ||
| const manager = createManager(); | ||
|
|
||
| const host = document.createElement('div'); | ||
| elements.push(host); | ||
| document.body.appendChild(host); | ||
|
|
||
| const buffersBeforeAdd = mutationBuffers.length; | ||
|
|
@@ -127,14 +189,13 @@ describe('ShadowDomManager', () => { | |
| // Tearing down the owning document removes them. | ||
| manager.resetForDoc(document); | ||
| expect(mutationBuffers.length).toBe(buffersBeforeAdd); | ||
|
|
||
| document.body.removeChild(host); | ||
| }); | ||
|
|
||
| it('keys a shadow root by its host owner document, not the passed document', () => { | ||
| const manager = createManager(); | ||
|
|
||
| const iframe = document.createElement('iframe'); | ||
| elements.push(iframe); | ||
| document.body.appendChild(iframe); | ||
| const iframeDoc = iframe.contentDocument as Document; | ||
| const host = iframeDoc.createElement('div'); | ||
|
|
@@ -154,7 +215,5 @@ describe('ShadowDomManager', () => { | |
| // Tearing down the iframe's own document does. | ||
| manager.resetForDoc(iframeDoc); | ||
| expect(mutationBuffers.length).toBe(buffersBeforeAdd); | ||
|
|
||
| document.body.removeChild(iframe); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 Agent-drafted, reviewed by Tue.
Thanks for this, the fix looks right. One thing missing: a changeset. rrweb changes only ship through a
posthog-jsrelease, so it needs to bump both: