Skip to content

Commit e0d8295

Browse files
committed
fix: Wrap event.target access in try/catch
1 parent 804a311 commit e0d8295

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

packages/browser/src/helpers.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,15 @@ export function breadcrumbEventHandler(eventName: string, debounce: boolean = fa
188188
lastCapturedEvent = event;
189189

190190
const captureBreadcrumb = () => {
191-
const target = htmlTreeAsString(event.target as Node);
191+
let target;
192+
193+
// Accessing event.target can throw (see getsentry/raven-js#838, #768)
194+
try {
195+
target = event.target ? htmlTreeAsString(event.target as Node) : htmlTreeAsString((event as unknown) as Node);
196+
} catch (e) {
197+
target = '<unknown>';
198+
}
199+
192200
if (target.length === 0) {
193201
return;
194202
}

packages/utils/src/misc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Event, Integration, WrappedFunction } from '@sentry/types';
2+
23
import { isString } from './is';
34

45
/** Internal */
@@ -259,7 +260,7 @@ export function htmlTreeAsString(elem: Node): string {
259260
// try/catch both:
260261
// - accessing event.target (see getsentry/raven-js#838, #768)
261262
// - `htmlTreeAsString` because it's complex, and just accessing the DOM incorrectly
262-
// can throw an exception in some circumstances.
263+
// - can throw an exception in some circumstances.
263264
try {
264265
let currentElem: Node | null = elem;
265266
const MAX_TRAVERSE_HEIGHT = 5;

packages/utils/src/object.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ExtendedError, WrappedFunction } from '@sentry/types';
22

3-
import { isError, isPrimitive, isSyntheticEvent, isEvent, isElement } from './is';
3+
import { isElement, isError, isEvent, isPrimitive, isSyntheticEvent } from './is';
44
import { Memo } from './memo';
5-
import { truncate } from './string';
65
import { htmlTreeAsString } from './misc';
6+
import { truncate } from './string';
77

88
/**
99
* Wrap a given object method with a higher-order function
@@ -104,12 +104,23 @@ function getWalkSource(
104104
} = {};
105105

106106
source.type = value.type;
107-
source.target = isElement(value.target)
108-
? htmlTreeAsString(value.target)
109-
: Object.prototype.toString.call(value.target);
110-
source.currentTarget = isElement(value.currentTarget)
111-
? htmlTreeAsString(value.currentTarget)
112-
: Object.prototype.toString.call(value.currentTarget);
107+
108+
// Accessing event.target can throw (see getsentry/raven-js#838, #768)
109+
try {
110+
source.target = isElement(value.target)
111+
? htmlTreeAsString(value.target)
112+
: Object.prototype.toString.call(value.target);
113+
} catch (_oO) {
114+
source.target = '<unknown>';
115+
}
116+
117+
try {
118+
source.currentTarget = isElement(value.currentTarget)
119+
? htmlTreeAsString(value.currentTarget)
120+
: Object.prototype.toString.call(value.currentTarget);
121+
} catch (_oO) {
122+
source.currentTarget = '<unknown>';
123+
}
113124

114125
// tslint:disable-next-line:strict-type-predicates
115126
if (typeof CustomEvent !== 'undefined' && value instanceof CustomEvent) {

0 commit comments

Comments
 (0)