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 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,18 @@ function record<T = eventWithTime>(
);
}
return () => {
handlers.forEach((h) => h());
handlers.forEach((handler) => {
try {
handler();
} catch (error) {
/**
* 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.
*/
}
});
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should generate a mutation I believe

await new Promise((resolve) => setTimeout(resolve, 1000));
stopRecord?.();
});
});
});

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