Skip to content

fix: rrweb recorder may throw error when stopping recording after an iframe becomes cross-origin #1695

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -617,7 +617,25 @@ function record<T = eventWithTime>(
);
}
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
src attribute is changed to a cross-origin URL,
* 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
15 changes: 15 additions & 0 deletions packages/rrweb/test/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,21 @@ 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');
document.body.appendChild(iframe);
await new Promise((resolve) => setTimeout(resolve, 1000));
iframe.src = 'https://www.example.com'; // Change the same origin iframe to a cross origin iframe after it's recorded
await new Promise((resolve) => setTimeout(resolve, 1000));
stopRecord?.();
});
});
});

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