From 4dc72ee7347d30e9b343d246ca4750b376e4d1b3 Mon Sep 17 00:00:00 2001 From: Deepanshu Pal <40927968+DeepanshuPal@users.noreply.github.com> Date: Wed, 30 Sep 2026 03:19:52 +0530 Subject: [PATCH 1/5] fix(rrweb): guard cross-origin shadow observation --- .../rrweb/src/record/shadow-dom-manager.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/rrweb/rrweb/src/record/shadow-dom-manager.ts b/packages/rrweb/rrweb/src/record/shadow-dom-manager.ts index f23d402a41..6bc47381c1 100644 --- a/packages/rrweb/rrweb/src/record/shadow-dom-manager.ts +++ b/packages/rrweb/rrweb/src/record/shadow-dom-manager.ts @@ -10,7 +10,7 @@ import { initAdoptedStyleSheetObserver, mutationBuffers, } from './observer'; -import { inDom } from '../utils'; +import { callSafely, inDom } from '../utils'; import type { Mirror } from '@posthog/rrweb-snapshot'; import { isNativeShadowDom } from '@posthog/rrweb-snapshot'; import dom, { patch } from '@posthog/rrweb-utils'; @@ -121,16 +121,14 @@ export class ShadowDomManager { * Monkey patch 'attachShadow' of an IFrameElement to observe newly added shadow doms. */ public observeAttachShadow(iframeElement: HTMLIFrameElement): void { - if (!iframeElement.contentWindow || !iframeElement.contentDocument) return; + callSafely(() => { + const iframeWindow = iframeElement.contentWindow as + (Window & { Element: { prototype: Element } }) | null; + const iframeDocument = iframeElement.contentDocument; + if (!iframeWindow || !iframeDocument) return; - this.patchAttachShadow( - ( - iframeElement.contentWindow as Window & { - Element: { prototype: Element }; - } - ).Element, - iframeElement.contentDocument, - ); + this.patchAttachShadow(iframeWindow.Element, iframeDocument); + }); } /** From 2fae2c3605c37bb94249abf3af2e6577f5e77e4b Mon Sep 17 00:00:00 2001 From: Deepanshu Pal <40927968+DeepanshuPal@users.noreply.github.com> Date: Wed, 30 Sep 2026 03:20:38 +0530 Subject: [PATCH 2/5] test(rrweb): cover cross-origin shadow observation --- .../test/record/shadow-dom-manager.test.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/rrweb/rrweb/test/record/shadow-dom-manager.test.ts b/packages/rrweb/rrweb/test/record/shadow-dom-manager.test.ts index 0e5af092a1..8b0c869266 100644 --- a/packages/rrweb/rrweb/test/record/shadow-dom-manager.test.ts +++ b/packages/rrweb/rrweb/test/record/shadow-dom-manager.test.ts @@ -59,6 +59,55 @@ describe('ShadowDomManager', () => { }); } + 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(); + manager.reset(); + }); + + 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', + ); + manager.reset(); + }); + + it('still patches same-origin iframe shadow roots', () => { + const manager = createManager(); + const iframe = document.createElement('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); + + manager.reset(); + expect(iframeElement?.prototype.attachShadow).toBe(before); + iframe.remove(); + }); + it('reset() should not call MutationBuffer.reset() from restoreHandler to avoid infinite recursion', () => { const manager = createManager(); From 5e3358753492456a151aeed98232891e5f16a29d Mon Sep 17 00:00:00 2001 From: Deepanshu Pal <40927968+DeepanshuPal@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:00:11 +0530 Subject: [PATCH 3/5] chore: add rrweb and posthog-js patch changeset --- .changeset/rrweb-cross-origin-shadow.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/rrweb-cross-origin-shadow.md diff --git a/.changeset/rrweb-cross-origin-shadow.md b/.changeset/rrweb-cross-origin-shadow.md new file mode 100644 index 0000000000..8a4f25246c --- /dev/null +++ b/.changeset/rrweb-cross-origin-shadow.md @@ -0,0 +1,6 @@ +--- +'@posthog/rrweb': patch +'posthog-js': patch +--- + +Stop a SecurityError escaping the recorder when an iframe becomes cross-origin while shadow DOM observation starts. From 7c8e4ba9a2f8b0c98e53d22651f01e99376ad915 Mon Sep 17 00:00:00 2001 From: Deepanshu Pal <40927968+DeepanshuPal@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:00:39 +0530 Subject: [PATCH 4/5] test: clean up shadow DOM managers after failed assertions --- .../test/record/shadow-dom-manager.test.ts | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/packages/rrweb/rrweb/test/record/shadow-dom-manager.test.ts b/packages/rrweb/rrweb/test/record/shadow-dom-manager.test.ts index 8b0c869266..0f712fe8fc 100644 --- a/packages/rrweb/rrweb/test/record/shadow-dom-manager.test.ts +++ b/packages/rrweb/rrweb/test/record/shadow-dom-manager.test.ts @@ -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,6 +73,8 @@ describe('ShadowDomManager', () => { }, mirror, }); + managers.push(manager); + return manager; } it('ignores SecurityError when a frame navigates cross-origin', () => { @@ -71,7 +89,6 @@ describe('ShadowDomManager', () => { } as unknown as HTMLIFrameElement; expect(() => manager.observeAttachShadow(iframe)).not.toThrow(); - manager.reset(); }); it('does not swallow unrelated errors while observing a frame', () => { @@ -88,12 +105,12 @@ describe('ShadowDomManager', () => { expect(() => manager.observeAttachShadow(iframe)).toThrow( 'Broken Element getter', ); - manager.reset(); }); 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; @@ -105,13 +122,13 @@ describe('ShadowDomManager', () => { manager.reset(); expect(iframeElement?.prototype.attachShadow).toBe(before); - iframe.remove(); }); 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' }); @@ -122,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; @@ -140,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; @@ -176,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'); @@ -203,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); }); }); From 0fa2c4fb055fae8df281039ba7564da4c57500e8 Mon Sep 17 00:00:00 2001 From: Deepanshu Pal <40927968+DeepanshuPal@users.noreply.github.com> Date: Sat, 3 Oct 2026 01:30:11 +0530 Subject: [PATCH 5/5] fix: keep rrweb changeset scoped to posthog-js Co-authored-by: Dustin Byrne --- .changeset/rrweb-cross-origin-shadow.md | 1 - 1 file changed, 1 deletion(-) diff --git a/.changeset/rrweb-cross-origin-shadow.md b/.changeset/rrweb-cross-origin-shadow.md index 8a4f25246c..b22b7d478a 100644 --- a/.changeset/rrweb-cross-origin-shadow.md +++ b/.changeset/rrweb-cross-origin-shadow.md @@ -1,5 +1,4 @@ --- -'@posthog/rrweb': patch 'posthog-js': patch ---