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/nervous-actors-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb": patch
---

fix: rrweb recorder may throw error when stopping recording after an iframe becomes cross-origin
20 changes: 19 additions & 1 deletion packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@
plugins
?.filter((p) => p.observer)
?.map((p) => ({
observer: p.observer!,

Check warning on line 560 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 560 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 560 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/index.ts#L560

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
options: p.options,
callback: (payload: object) =>
wrappedEmit({
Expand All @@ -575,7 +575,7 @@

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

Check warning on line 578 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 578 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/index.ts#L578

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
} catch (error) {
// TODO: handle internal error
console.warn(error);
Expand Down Expand Up @@ -617,7 +617,25 @@
);
}
return () => {
handlers.forEach((h) => h());
handlers.forEach((handler) => {
try {
handler();
} catch (error) {
const msg = String(error).toLowerCase();
/**
* https://github.com/rrweb-io/rrweb/pull/1695
* This error can occur in a known scenario:
* If an iframe is initially same-origin and observed, but later its
location is changed in an opaque way to a cross-origin URL (perhaps within the iframe via its `document.location` or a redirect)
* attempting to execute the handler in the stop record function will
throw a "cannot access cross-origin frame" error.
* This error is expected and can be safely ignored.
*/
if (!msg.includes('cross-origin')) {
console.warn(error);
}
}
});
processedNodeManager.destroy();
recording = false;
unregisterErrorHandler();
Expand Down
21 changes: 21 additions & 0 deletions packages/rrweb/test/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,27 @@ describe('record', function (this: ISuite) {

await assertSnapshot(ctx.events);
});

it('does not throw error when stopping recording after iframe becomes cross-origin', async () => {
await ctx.page.evaluate(async () => {
const { record } = (window as unknown as IWindow).rrweb;
const stopRecord = record({
emit: (window as unknown as IWindow).emit,
});
const iframe = document.createElement('iframe');
(window as any).stopRecord = stopRecord;
(window as any).iframe = iframe;
document.body.appendChild(iframe);
});
await waitForRAF(ctx.page);
await ctx.page.evaluate(async () => {
(window as any).iframe.src = 'https://www.example.com'; // Change the same origin iframe to a cross origin iframe after it's recorded
});
await waitForRAF(ctx.page);
await ctx.page.evaluate(() => {
(window as any).stopRecord?.();
});
});
});

describe('record iframes', function (this: ISuite) {
Expand Down
Loading