Skip to content
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
12 changes: 11 additions & 1 deletion packages/browser-utils/src/getNativeImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,15 @@ export function fetch(...rest: Parameters<typeof WINDOW.fetch>): ReturnType<type
* avoiding triggering change detection.
*/
export function setTimeout(...rest: Parameters<typeof WINDOW.setTimeout>): ReturnType<typeof WINDOW.setTimeout> {
return getNativeImplementation('setTimeout')(...rest);
try {
return getNativeImplementation('setTimeout')(...rest);
} catch (e) {
// The implementation retrieved from the (since removed) sandbox iframe may throw when invoked,
// e.g. Firefox throws `NS_ERROR_NOT_INITIALIZED` for timers of a detached window.
// In this case, fall back to (and cache) the window's own setTimeout.
DEBUG_BUILD && debug.warn('Could not use native setTimeout implementation, falling back to window.setTimeout: ', e);
const fallback = WINDOW.setTimeout.bind(WINDOW) as typeof WINDOW.setTimeout;
cachedImplementations.setTimeout = fallback;
return fallback(...rest);
}
Comment thread
sentry[bot] marked this conversation as resolved.
}
53 changes: 53 additions & 0 deletions packages/browser-utils/test/getNativeImplementation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @vitest-environment jsdom
*/

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { clearCachedImplementation, setTimeout as sentrySetTimeout } from '../src/getNativeImplementation';
import { WINDOW } from '../src/types';

describe('setTimeout', () => {
const originalSetTimeout = WINDOW.setTimeout;

beforeEach(() => {
clearCachedImplementation('setTimeout');
});

afterEach(() => {
WINDOW.setTimeout = originalSetTimeout;
clearCachedImplementation('setTimeout');
vi.restoreAllMocks();
});

it('falls back to window.setTimeout if the sandboxed iframe implementation throws', async () => {
// Simulate a wrapped (non-native) window.setTimeout, e.g. by a browser extension
const wrappedSetTimeout = vi.fn((...args: Parameters<typeof originalSetTimeout>) => originalSetTimeout(...args));
WINDOW.setTimeout = wrappedSetTimeout as unknown as typeof WINDOW.setTimeout;

// Simulate Firefox throwing `NS_ERROR_NOT_INITIALIZED` when calling setTimeout of a detached iframe window
const detachedSetTimeout = vi.fn(() => {
const error = new Error('');
error.name = 'NS_ERROR_NOT_INITIALIZED';
throw error;
});
const fakeIframe = { hidden: false, contentWindow: { setTimeout: detachedSetTimeout } };

const document = WINDOW.document as Document;
vi.spyOn(document, 'createElement').mockReturnValue(fakeIframe as unknown as HTMLElement);
vi.spyOn(document.head, 'appendChild').mockImplementation(node => node);
vi.spyOn(document.head, 'removeChild').mockImplementation(node => node);

const callback = vi.fn();

expect(() => sentrySetTimeout(callback, 0)).not.toThrow();
expect(detachedSetTimeout).toHaveBeenCalledTimes(1);
expect(wrappedSetTimeout).toHaveBeenCalledTimes(1);

// Subsequent calls use the cached fallback and do not hit the broken implementation again
sentrySetTimeout(callback, 0);
expect(detachedSetTimeout).toHaveBeenCalledTimes(1);
expect(wrappedSetTimeout).toHaveBeenCalledTimes(2);

await vi.waitFor(() => expect(callback).toHaveBeenCalledTimes(2));
});
});
Loading