-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(browser-utils): Stop leaking DOM instrumentation listeners on mismatched removals #24727
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
Merged
+209
−14
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
171dec6
fix(browser-utils): Stop leaking DOM instrumentation listeners on mis…
Lms24 9e27925
fix once or signal handlers being added to the set
Lms24 b8337a5
Merge remote-tracking branch 'origin/develop' into fix/dom-instrument…
Lms24 9ed8e57
fix null option issue
Lms24 f1b6735
switch to weakset
Lms24 d55f18f
fix more edge cases and add tests, fix typo
Lms24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 160 additions & 3 deletions
163
packages/browser-utils/test/instrumentation/dom.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,169 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { instrumentDOM } from '../../src/instrumentation/dom'; | ||
| import { WINDOW } from '../../src/types'; | ||
|
|
||
| // @ts-expect-error - idk | ||
| WINDOW.XMLHttpRequest = undefined; | ||
|
|
||
| describe('instrumentXHR', () => { | ||
| it('it does not throw if XMLHttpRequest is a key on window but not defined', () => { | ||
| type InstrumentedDocument = Document & { __sentry_instrumentation_handlers__?: Record<string, any> }; | ||
|
|
||
| describe('instrumentDOM', () => { | ||
| const { addEventListener: nativeAdd, removeEventListener: nativeRemove } = EventTarget.prototype; | ||
|
|
||
| // `instrumentDOM` patches `EventTarget.prototype` and isn't idempotent, so restore the native methods after every test. | ||
| afterEach(() => { | ||
| EventTarget.prototype.addEventListener = nativeAdd; | ||
| EventTarget.prototype.removeEventListener = nativeRemove; | ||
| delete (document as InstrumentedDocument).__sentry_instrumentation_handlers__; | ||
| }); | ||
|
|
||
| /** Runs `instrumentDOM` and returns a function counting the click listeners actually attached to `document`. */ | ||
| function instrumentAndTrackDocumentClickListeners(): () => number { | ||
| const documentClickListeners = { capture: new Set<unknown>(), bubble: new Set<unknown>() }; | ||
|
|
||
| const phase = (options?: boolean | EventListenerOptions): Set<unknown> => | ||
| (typeof options === 'boolean' ? options : !!options?.capture) | ||
| ? documentClickListeners.capture | ||
| : documentClickListeners.bubble; | ||
|
|
||
| // Installed before `instrumentDOM` so these sit underneath the SDK and also see the listeners it attaches itself. | ||
| EventTarget.prototype.addEventListener = function (type, listener, options) { | ||
| if (this === document && type === 'click') { | ||
| phase(options).add(listener); | ||
| } | ||
| return nativeAdd.call(this, type, listener, options); | ||
| }; | ||
|
|
||
| EventTarget.prototype.removeEventListener = function (type, listener, options) { | ||
| if (this === document && type === 'click') { | ||
| phase(options).delete(listener); | ||
| } | ||
| return nativeRemove.call(this, type, listener, options); | ||
| }; | ||
|
|
||
| instrumentDOM(); | ||
|
|
||
| return () => documentClickListeners.capture.size + documentClickListeners.bubble.size; | ||
| } | ||
|
|
||
| it('does not throw if XMLHttpRequest is a key on window but not defined', () => { | ||
| expect(instrumentDOM).not.toThrow(); | ||
| }); | ||
|
|
||
| it('does not leak document click listeners when removeEventListener uses mismatched capture options', () => { | ||
| const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); | ||
|
|
||
| // baseline listenercount is 1 which comes from the SDK's global click handler registered | ||
| // in instrumentDOM(). | ||
| const baseline = countDocumentClickListeners(); | ||
|
|
||
| const never = (): void => {}; | ||
| const onCapture = (): void => {}; | ||
| const onBubble = (): void => {}; | ||
|
|
||
| for (let i = 0; i < 20; i++) { | ||
| document.addEventListener('click', onCapture, true); | ||
| document.addEventListener('click', onBubble); | ||
| document.removeEventListener('click', never); | ||
| document.removeEventListener('click', never); | ||
| document.removeEventListener('click', onCapture, true); | ||
| document.removeEventListener('click', onBubble); | ||
| } | ||
|
|
||
| expect(countDocumentClickListeners() - baseline).toBe(0); | ||
| }); | ||
|
|
||
| it('keeps its handler attached while listeners remain, even after removing listeners that were never added', () => { | ||
| const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); | ||
| const baseline = countDocumentClickListeners(); | ||
|
|
||
| const never = (): void => {}; | ||
| const onClick = (): void => {}; | ||
|
|
||
| document.addEventListener('click', onClick); | ||
| document.removeEventListener('click', never); | ||
| document.removeEventListener('click', onClick, true); | ||
|
|
||
| // `onClick` plus the SDK's handler for it | ||
| expect(countDocumentClickListeners() - baseline).toBe(2); | ||
|
|
||
| document.removeEventListener('click', onClick); | ||
|
|
||
| expect(countDocumentClickListeners() - baseline).toBe(0); | ||
| }); | ||
|
|
||
| it('removes its handler when listeners are added and removed with `null` options', () => { | ||
| const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); | ||
| const baseline = countDocumentClickListeners(); | ||
|
|
||
| const onClick = (): void => {}; | ||
|
|
||
| // @ts-expect-error - `null` is valid at runtime and treated like default options | ||
| document.addEventListener('click', onClick, null); | ||
| // @ts-expect-error - see above | ||
| document.removeEventListener('click', onClick, null); | ||
|
|
||
| expect(countDocumentClickListeners() - baseline).toBe(0); | ||
| }); | ||
|
|
||
| it('does not retain listeners added with `once` or `signal`, which the browser removes on its own', () => { | ||
| instrumentDOM(); | ||
|
|
||
| for (let i = 0; i < 20; i++) { | ||
| const controller = new AbortController(); | ||
| document.addEventListener('click', () => {}, { signal: controller.signal }); | ||
| document.addEventListener('click', () => {}, { once: true, capture: true }); | ||
| controller.abort(); | ||
| } | ||
| document.dispatchEvent(new MouseEvent('click')); | ||
|
|
||
| const clickHandlers = (document as InstrumentedDocument).__sentry_instrumentation_handlers__?.click; | ||
| expect(clickHandlers.listenerCount).toBe(0); | ||
| expect(clickHandlers.handler).toBeDefined(); | ||
| }); | ||
|
|
||
| it('keeps its handler attached after removing tracked listeners if a `once` or `signal` listener was added', () => { | ||
| const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); | ||
| const baseline = countDocumentClickListeners(); | ||
|
|
||
| const onClick = (): void => {}; | ||
| const onceClick = (): void => {}; | ||
|
|
||
| document.addEventListener('click', onClick); | ||
| document.addEventListener('click', onceClick, { once: true }); | ||
| document.removeEventListener('click', onClick); | ||
|
|
||
| // `onceClick` plus the SDK's handler, which must still see the pending `once` listener's event | ||
| expect(countDocumentClickListeners() - baseline).toBe(2); | ||
| }); | ||
|
|
||
| it('counts a listener added twice in the same phase only once', () => { | ||
| const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); | ||
| const baseline = countDocumentClickListeners(); | ||
|
|
||
| const onClick = (): void => {}; | ||
|
|
||
| document.addEventListener('click', onClick); | ||
| document.addEventListener('click', onClick); | ||
| document.removeEventListener('click', onClick); | ||
|
|
||
| expect(countDocumentClickListeners() - baseline).toBe(0); | ||
| }); | ||
|
|
||
| it('removes its handler if a listener is re-added with `once` while already registered', () => { | ||
| const countDocumentClickListeners = instrumentAndTrackDocumentClickListeners(); | ||
| const baseline = countDocumentClickListeners(); | ||
|
|
||
| const onClick = (): void => {}; | ||
|
|
||
| document.addEventListener('click', onClick); | ||
| // no-op in the browser, since `onClick` is already registered for the bubble phase | ||
| document.addEventListener('click', onClick, { once: true }); | ||
| document.removeEventListener('click', onClick); | ||
|
|
||
| expect(countDocumentClickListeners() - baseline).toBe(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.