Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rrweb-cross-origin-shadow.md
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.
18 changes: 8 additions & 10 deletions packages/rrweb/rrweb/src/record/shadow-dom-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(() => {

Copy link
Copy Markdown
Contributor

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-js release, so it needs to bump both:

---
'@posthog/rrweb': patch
'posthog-js': patch
---

Stop a SecurityError escaping the recorder when an iframe becomes cross-origin while shadow DOM observation starts.

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);
});
}

/**
Expand Down
85 changes: 72 additions & 13 deletions packages/rrweb/rrweb/test/record/shadow-dom-manager.test.ts
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,
Expand All @@ -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: {
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

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.

If this expect fails, manager.reset() and iframe.remove() never run, so the patched attachShadow leaks into the rest of the file. Same for the tests above. Could the cleanup move into try/finally or an afterEach?


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' });

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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');
Expand All @@ -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);
});
});
Loading