Skip to content
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

fix(utils): Prevent iterating over VueViewModel #8981

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 0 additions & 12 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,6 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: HandlerDa
* Creates breadcrumbs from console API calls
*/
function _consoleBreadcrumb(handlerData: HandlerData & { args: unknown[]; level: string }): void {
// This is a hack to fix a Vue3-specific bug that causes an infinite loop of
// console warnings. This happens when a Vue template is rendered with
// an undeclared variable, which we try to stringify, ultimately causing
// Vue to issue another warning which repeats indefinitely.
// see: https://github.com/getsentry/sentry-javascript/pull/6010
// see: https://github.com/getsentry/sentry-javascript/issues/5916
for (let i = 0; i < handlerData.args.length; i++) {
if (handlerData.args[i] === 'ref=Ref<') {
handlerData.args[i + 1] = 'viewRef';
break;
}
}
const breadcrumb = {
category: 'console',
data: {
Expand Down
17 changes: 17 additions & 0 deletions packages/utils/src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,20 @@ export function isInstanceOf(wat: any, base: any): boolean {
return false;
}
}

interface VueViewModel {
// Vue3
__isVue?: boolean;
// Vue2
_isVue?: boolean;
}
/**
* Checks whether given value's type is a Vue ViewModel.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isVueViewModel(wat: unknown): boolean {
// Not using Object.prototype.toString because in Vue 3 it would read the instance's Symbol(Symbol.toStringTag) property.
return !!(typeof wat === 'object' && wat !== null && ((wat as VueViewModel).__isVue || (wat as VueViewModel)._isVue));
}
6 changes: 5 additions & 1 deletion packages/utils/src/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Primitive } from '@sentry/types';

import { isNaN, isSyntheticEvent } from './is';
import { isNaN, isSyntheticEvent, isVueViewModel } from './is';
import type { MemoFunc } from './memo';
import { memoBuilder } from './memo';
import { convertToPlainObject } from './object';
Expand Down Expand Up @@ -214,6 +214,10 @@ function stringifyValue(
return '[Document]';
}

if (isVueViewModel(value)) {
return '[VueViewModel]';
}

// React's SyntheticEvent thingy
if (isSyntheticEvent(value)) {
return '[SyntheticEvent]';
Comment on lines +220 to 223
Copy link
Member

Choose a reason for hiding this comment

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

well I guess there's precedence for handling this stuff like this in a general package of ours 😅

Copy link
Member

Choose a reason for hiding this comment

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

the tricky thing is that some people end up just using @sentry/browser even when using a framework like React or Vue.

but I think we should re-examine this in v8 and see if we can somehow inject in framework specific processing.

Expand Down
13 changes: 11 additions & 2 deletions packages/utils/src/string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isRegExp, isString } from './is';
import { isRegExp, isString, isVueViewModel } from './is';

export { escapeStringForRegex } from './vendor/escapeStringForRegex';

Expand Down Expand Up @@ -76,7 +76,16 @@ export function safeJoin(input: any[], delimiter?: string): string {
for (let i = 0; i < input.length; i++) {
const value = input[i];
try {
output.push(String(value));
// This is a hack to fix a Vue3-specific bug that causes an infinite loop of
// console warnings. This happens when a Vue template is rendered with
// an undeclared variable, which we try to stringify, ultimately causing
// Vue to issue another warning which repeats indefinitely.
// see: https://github.com/getsentry/sentry-javascript/pull/8981
if (isVueViewModel(value)) {
output.push('[VueViewModel]');
} else {
output.push(String(value));
}
} catch (e) {
output.push('[value cannot be serialized]');
}
Expand Down
10 changes: 10 additions & 0 deletions packages/utils/test/is.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isNaN,
isPrimitive,
isThenable,
isVueViewModel,
} from '../src/is';
import { supportsDOMError, supportsDOMException, supportsErrorEvent } from '../src/supports';
import { resolvedSyncPromise } from '../src/syncpromise';
Expand Down Expand Up @@ -134,3 +135,12 @@ describe('isNaN()', () => {
expect(isNaN(new Date())).toEqual(false);
});
});

describe('isVueViewModel()', () => {
test('should work as advertised', () => {
expect(isVueViewModel({ _isVue: true })).toEqual(true);
expect(isVueViewModel({ __isVue: true })).toEqual(true);
Duncanxyz marked this conversation as resolved.
Show resolved Hide resolved

expect(isVueViewModel({ foo: true })).toEqual(false);
});
});
29 changes: 29 additions & 0 deletions packages/utils/test/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,17 @@ describe('normalize()', () => {
foo: '[SyntheticEvent]',
});
});

test('known classes like `VueViewModel`', () => {
const obj = {
foo: {
_isVue: true,
},
};
expect(normalize(obj)).toEqual({
foo: '[VueViewModel]',
});
});
});

describe('can limit object to depth', () => {
Expand Down Expand Up @@ -618,6 +629,24 @@ describe('normalize()', () => {
});
});

test('normalizes value on every iteration of decycle and takes care of things like `VueViewModel`', () => {
const obj = {
foo: {
_isVue: true,
},
baz: NaN,
qux: function qux(): void {
/* no-empty */
},
};
const result = normalize(obj);
expect(result).toEqual({
foo: '[VueViewModel]',
baz: '[NaN]',
qux: '[Function: qux]',
});
});

describe('skips normalizing objects marked with a non-enumerable property __sentry_skip_normalization__', () => {
test('by leaving non-serializable values intact', () => {
const someFun = () => undefined;
Expand Down