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 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
10 changes: 10 additions & 0 deletions packages/utils/src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,13 @@ export function isInstanceOf(wat: any, base: any): boolean {
return false;
}
}

/**
* 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 {
return isPlainObject(wat) && !!(wat._isVue || wat.__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
17 changes: 17 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,19 @@ 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(isNaN(null)).toEqual(false);
expect(isNaN(true)).toEqual(false);
expect(isNaN('foo')).toEqual(false);
expect(isNaN(42)).toEqual(false);
expect(isNaN({})).toEqual(false);
expect(isNaN([])).toEqual(false);
expect(isNaN(new Error('foo'))).toEqual(false);
expect(isNaN(new Date())).toEqual(false);
Duncanxyz marked this conversation as resolved.
Show resolved Hide resolved
});
});
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