Skip to content

Commit 66cf611

Browse files
lforstlegobeat
authored andcommitted
fix(browser): Ensure wrap() only returns functions (getsentry#13838)
1 parent 5adcbf9 commit 66cf611

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/browser/src/helpers.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ export function wrap(
6666
// the original wrapper.
6767
const wrapper = fn.__sentry_wrapped__;
6868
if (wrapper) {
69-
return wrapper;
69+
if (typeof wrapper === 'function') {
70+
return wrapper;
71+
} else {
72+
// If we find that the `__sentry_wrapped__` function is not a function at the time of accessing it, it means
73+
// that something messed with it. In that case we want to return the originally passed function.
74+
return fn;
75+
}
7076
}
7177

7278
// We don't wanna wrap it twice

packages/browser/test/unit/integrations/helpers.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,17 @@ describe('internal wrap()', () => {
174174
expect(wrapped.__sentry_original__).toBe(fn);
175175
expect(fn.__sentry_wrapped__).toBe(wrapped);
176176
});
177+
178+
it('should only return __sentry_wrapped__ when it is a function', () => {
179+
const fn = (() => 1337) as WrappedFunction;
180+
181+
wrap(fn);
182+
expect(fn).toHaveProperty('__sentry_wrapped__');
183+
fn.__sentry_wrapped__ = 'something that is not a function' as any;
184+
185+
const wrapped = wrap(fn);
186+
187+
expect(wrapped).toBe(fn);
188+
expect(wrapped).not.toBe('something that is not a function');
189+
});
177190
});

0 commit comments

Comments
 (0)