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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
RecordPlugin,
ICrossOriginIframeMirror,
} from '@sentry-internal/rrweb-types';
import { getIFrameContentWindow } from '@sentry-internal/rrdom';
import type { WebRTCDataChannel } from './types';

export const PLUGIN_NAME = 'rrweb/canvas-webrtc@1';
Expand Down Expand Up @@ -248,7 +249,7 @@ export class RRWebPluginCanvasWebRTCRecord {
if (remoteId === -1) return;

found = true;
iframe.contentWindow?.postMessage(
getIFrameContentWindow(iframe)?.postMessage(
{
type: 'rrweb-canvas-webrtc',
data: {
Expand Down
9 changes: 5 additions & 4 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
shouldMaskInput,
setTimeout,
clearTimeout,
getIframeContentDocument,
getIFrameContentDocument,
getIFrameContentWindow,
} from './utils';

let _id = 1;
Expand Down Expand Up @@ -521,7 +522,7 @@
listener: () => unknown,
iframeLoadTimeout: number,
) {
const win = iframeEl.contentWindow;
const win = getIFrameContentWindow(iframeEl);
if (!win) {
return;
}
Expand Down Expand Up @@ -772,7 +773,7 @@
// So we'll be conservative and keep textContent as-is.
} else if ((n.parentNode as HTMLStyleElement).sheet?.cssRules) {
textContent = stringifyStylesheet(
(n.parentNode as HTMLStyleElement).sheet!,

Check warning on line 776 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 776 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion
);
}
} catch (err) {
Expand Down Expand Up @@ -924,7 +925,7 @@
attributes.rel = null;
attributes.href = null;
attributes.crossorigin = null;
attributes._cssText = absoluteToStylesheet(cssText, stylesheet!.href!);

Check warning on line 928 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 928 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 928 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 928 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion
}
}
// dynamic stylesheet
Expand Down Expand Up @@ -1036,10 +1037,10 @@
const recordInlineImage = () => {
image.removeEventListener('load', recordInlineImage);
try {
canvasService!.width = image.naturalWidth;

Check warning on line 1040 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 1040 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion
canvasService!.height = image.naturalHeight;

Check warning on line 1041 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 1041 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion
canvasCtx!.drawImage(image, 0, 0);

Check warning on line 1042 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 1042 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion
attributes.rr_dataURL = canvasService!.toDataURL(

Check warning on line 1043 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 1043 in packages/rrweb-snapshot/src/snapshot.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion
dataURLOptions.type,
dataURLOptions.quality,
);
Expand Down Expand Up @@ -1099,7 +1100,7 @@
if (tagName === 'iframe' && !keepIframeSrcFn(attributes.src as string)) {
// Don't try to access `contentDocument` if iframe is blocked, otherwise it
// will trigger browser warnings.
if (!needBlock && !getIframeContentDocument(n as HTMLIFrameElement)) {
if (!needBlock && !getIFrameContentDocument(n as HTMLIFrameElement)) {
// we can't record it directly as we can't see into it
// preserve the src attribute so a decision can be taken at replay time
attributes.rr_src = attributes.src;
Expand Down Expand Up @@ -1447,7 +1448,7 @@
onceIframeLoaded(
n as HTMLIFrameElement,
() => {
const iframeDoc = getIframeContentDocument(n as HTMLIFrameElement);
const iframeDoc = getIFrameContentDocument(n as HTMLIFrameElement);
if (iframeDoc && onIframeLoad) {
const serializedIframeNode = serializeNodeWithId(iframeDoc, {
doc: iframeDoc,
Expand Down
17 changes: 15 additions & 2 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
* Browsers sometimes incorrectly escape `@import` on `.cssText` statements.
* This function tries to correct the escaping.
* more info: https://bugs.chromium.org/p/chromium/issues/detail?id=1472259
* @param cssImportRule

Check warning on line 74 in packages/rrweb-snapshot/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

tsdoc-param-tag-missing-hyphen: The @PARAM block should be followed by a parameter name and then a hyphen

Check warning on line 74 in packages/rrweb-snapshot/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

tsdoc-param-tag-missing-hyphen: The @PARAM block should be followed by a parameter name and then a hyphen
* @returns `cssText` with browser inconsistencies fixed, or null if not applicable.
*/
export function escapeImportStatement(rule: CSSImportRule): string {
Expand Down Expand Up @@ -496,10 +496,23 @@
* Get the content document of an iframe.
* Catching errors is necessary because some older browsers block access to the content document of a sandboxed iframe.
*/
export function getIframeContentDocument(iframe?: HTMLIFrameElement) {
export function getIFrameContentDocument(iframe?: HTMLIFrameElement) {
try {
return (iframe as HTMLIFrameElement).contentDocument;
} catch (e) {
} catch {
// noop
}
}

/**
* Get the content window of an iframe.
* Catching errors is necessary because iOS 18.5 Safari WebView throws SecurityError
* when accessing contentWindow on cross-origin iframes (instead of returning null).
*/
export function getIFrameContentWindow(iframe?: HTMLIFrameElement) {
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
try {
return (iframe as HTMLIFrameElement).contentWindow;
} catch {
// noop
}
}
59 changes: 58 additions & 1 deletion packages/rrweb-snapshot/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
*/
import { describe, it, test, expect } from 'vitest';
import { NodeType, serializedNode } from '../src/types';
import { extractFileExtension, isNodeMetaEqual } from '../src/utils';
import {
extractFileExtension,
isNodeMetaEqual,
getIFrameContentDocument,
getIFrameContentWindow,
} from '../src/utils';
import type { serializedNodeWithId } from '@sentry-internal/rrweb-snapshot';

describe('utils', () => {
Expand Down Expand Up @@ -199,4 +204,56 @@ describe('utils', () => {
expect(extension).toBe('js');
});
});

describe('getIFrameContentDocument', () => {
it('should return contentDocument for same-origin iframe', () => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const doc = getIFrameContentDocument(iframe);
expect(doc).toBe(iframe.contentDocument);
document.body.removeChild(iframe);
});

it('should return undefined when accessing contentDocument throws', () => {
const iframe = {
get contentDocument(): Document {
throw new DOMException('Blocked', 'SecurityError');
},
} as HTMLIFrameElement;
const doc = getIFrameContentDocument(iframe);
expect(doc).toBeUndefined();
});

it('should return undefined for undefined input', () => {
const doc = getIFrameContentDocument(undefined);
expect(doc).toBeUndefined();
});
});

describe('getIFrameContentWindow', () => {
it('should return contentWindow for same-origin iframe', () => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const win = getIFrameContentWindow(iframe);
expect(win).toBe(iframe.contentWindow);
document.body.removeChild(iframe);
});

it('should return undefined when accessing contentWindow throws', () => {
// Simulate iOS 18.5 Safari WebView behavior where accessing
// contentWindow on cross-origin iframes throws SecurityError
const iframe = {
get contentWindow(): Window {
throw new DOMException('Blocked', 'SecurityError');
},
} as HTMLIFrameElement;
const win = getIFrameContentWindow(iframe);
expect(win).toBeUndefined();
});

it('should return undefined for undefined input', () => {
const win = getIFrameContentWindow(undefined);
expect(win).toBeUndefined();
});
});
});
14 changes: 9 additions & 5 deletions packages/rrweb/src/record/iframe-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import type {
mutationCallBack,
} from '@sentry-internal/rrweb-types';
import type { StylesheetManager } from './stylesheet-manager';
import { getIFrameContentDocument } from '@sentry-internal/rrdom';
import {
getIFrameContentDocument,
getIFrameContentWindow,
} from '@sentry-internal/rrdom';

export interface IframeManagerInterface {
crossOriginIframeMirror: CrossOriginIframeMirror;
Expand Down Expand Up @@ -83,8 +86,8 @@ export class IframeManager implements IframeManagerInterface {

public addIframe(iframeEl: HTMLIFrameElement) {
this.iframes.set(iframeEl, true);
if (iframeEl.contentWindow)
this.crossOriginIframeMap.set(iframeEl.contentWindow, iframeEl);
const contentWindow = getIFrameContentWindow(iframeEl);
if (contentWindow) this.crossOriginIframeMap.set(contentWindow, iframeEl);
}

public addLoadListener(cb: (iframeEl: HTMLIFrameElement) => unknown) {
Expand All @@ -110,11 +113,12 @@ export class IframeManager implements IframeManagerInterface {
});

// Receive messages (events) coming from cross-origin iframes that are nested in this same-origin iframe.
if (this.recordCrossOriginIframes)
iframeEl.contentWindow?.addEventListener(
if (this.recordCrossOriginIframes) {
getIFrameContentWindow(iframeEl)?.addEventListener(
'message',
this.handleMessage.bind(this),
);
}

this.loadListener?.(iframeEl);

Expand Down
6 changes: 4 additions & 2 deletions packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
SlimDOMOptions,
createMirror,
} from '@sentry-internal/rrweb-snapshot';
import { getIFrameContentWindow } from '@sentry-internal/rrdom';
import { initObservers, mutationBuffers } from './observer';
import {
on,
Expand Down Expand Up @@ -463,8 +464,9 @@
},
onIframeLoad: (iframe, childSn) => {
iframeManager.attachIframe(iframe, childSn);
if (iframe.contentWindow) {
canvasManager.addWindow(iframe.contentWindow as IWindow);
const contentWindow = getIFrameContentWindow(iframe);
if (contentWindow) {
canvasManager.addWindow(contentWindow as IWindow);
}
shadowDomManager.observeAttachShadow(iframe);
},
Expand Down Expand Up @@ -643,7 +645,7 @@
plugins
?.filter((p) => p.observer)
?.map((p) => ({
observer: p.observer!,

Check warning on line 648 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion

Check warning on line 648 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion
options: p.options,
callback: (payload: object) =>
wrappedEmit({
Expand All @@ -661,7 +663,7 @@

iframeManager.addLoadListener((iframeEl) => {
try {
handlers.push(observe(iframeEl.contentDocument!));

Check warning on line 666 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

Forbidden non-null assertion
} catch (error) {
// TODO: handle internal error
console.warn(error);
Expand Down
10 changes: 7 additions & 3 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import {
getShadowHost,
closestElementOfNode,
} from '../utils';
import { getIFrameContentDocument } from '@sentry-internal/rrdom';
import {
getIFrameContentDocument,
getIFrameContentWindow,
} from '@sentry-internal/rrdom';

type DoubleLinkedListNode = {
previous: DoubleLinkedListNode | null;
Expand Down Expand Up @@ -369,8 +372,9 @@ export default class MutationBuffer {
}

this.iframeManager.attachIframe(iframe, childSn);
if (iframe.contentWindow) {
this.canvasManager.addWindow(iframe.contentWindow as IWindow);
const contentWindow = getIFrameContentWindow(iframe);
if (contentWindow) {
this.canvasManager.addWindow(contentWindow as IWindow);
}
this.shadowDomManager.observeAttachShadow(iframe);
},
Expand Down
Loading