8000 fix: Make internal Sentry flags non-enumerable in fill util · productinfo/sentry-javascript@3ddda21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ddda21

Browse files
committed
fix: Make internal Sentry flags non-enumerable in fill util
1 parent 419ef8d commit 3ddda21

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

packages/utils/src/object.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,28 @@ export function fill(source: { [key: string]: any }, name: string, replacement:
6262
}
6363
const original = source[name] as () => any;
6464
const wrapped = replacement(original) as SentryWrappedFunction;
65-
wrapped.__sentry__ = true;
66-
wrapped.__sentry_original__ = original;
67-
wrapped.__sentry_wrapped__ = wrapped;
65+
66+
// Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work
67+
// otherwise it'll throw "TypeError: Object.defineProperties called on non-object"
68+
// tslint:disable-next-line:strict-type-predicates
69+
if (typeof wrapped === 'function') {
70+
wrapped.prototype = {};
71+
Object.defineProperties(wrapped, {
72+
__sentry__: {
73+
enumerable: false,
74+
value: true,
75+
},
76+
__sentry_original__: {
77+
enumerable: false,
78+
value: original,
79+
},
80+
__sentry_wrapped__: {
81+
enumerable: false,
82+
value: wrapped,
83+
},
84+
});
85+
}
86+
6887
source[name] = wrapped;
6988
}
7089

packages/utils/test/object.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,25 @@ describe('fill()', () => {
207207
expect(replacement).toBeCalled();
208208
expect.assertions(3);
209209
});
210+
211+
test('internal flags shouldnt be enumerable', () => {
212+
const source = {
213+
foo: (): number => 42,
214+
};
215+
const name = 'foo';
216+
const replacement = cb => cb;
217+
218+
fill(source, name, replacement);
219+
220+
// Shouldn't show up in iteration
221+
expect(Object.keys(replacement)).not.toContain('__sentry__');
222+
expect(Object.keys(replacement)).not.toContain('__sentry_original__');
223+
expect(Object.keys(replacement)).not.toContain('__sentry_wrapped__');
224+
// But should be accessible directly
225+
expect(source.foo.__sentry__).toBe(true);
226+
expect(source.foo.__sentry_original__).toBe(source.foo);
227+
expect(source.foo.__sentry_wrapped__).toBe(source.foo);
228+
});
210229
});
211230

212231
describe('urlEncode()', () => {

0 commit comments

Comments
 (0)
0