From 35bdc87dee3498794e34c1ad35dd9927950c8766 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 1 Oct 2024 11:53:53 +0200 Subject: [PATCH] fix(browser): Ensure `wrap()` only returns functions (#13838) --- packages/browser/src/helpers.ts | 8 +++++++- packages/browser/test/integrations/helpers.test.ts | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/browser/src/helpers.ts b/packages/browser/src/helpers.ts index a232d24044dc..702f44e36b7b 100644 --- a/packages/browser/src/helpers.ts +++ b/packages/browser/src/helpers.ts @@ -64,7 +64,13 @@ export function wrap( // the original wrapper. const wrapper = fn.__sentry_wrapped__; if (wrapper) { - return wrapper; + if (typeof wrapper === 'function') { + return wrapper; + } else { + // If we find that the `__sentry_wrapped__` function is not a function at the time of accessing it, it means + // that something messed with it. In that case we want to return the originally passed function. + return fn; + } } // We don't wanna wrap it twice diff --git a/packages/browser/test/integrations/helpers.test.ts b/packages/browser/test/integrations/helpers.test.ts index 37806e06f8a9..ebfabd475e09 100644 --- a/packages/browser/test/integrations/helpers.test.ts +++ b/packages/browser/test/integrations/helpers.test.ts @@ -174,4 +174,17 @@ describe('internal wrap()', () => { expect(wrapped.__sentry_original__).toBe(fn); expect(fn.__sentry_wrapped__).toBe(wrapped); }); + + it('should only return __sentry_wrapped__ when it is a function', () => { + const fn = (() => 1337) as WrappedFunction; + + wrap(fn); + expect(fn).toHaveProperty('__sentry_wrapped__'); + fn.__sentry_wrapped__ = 'something that is not a function' as any; + + const wrapped = wrap(fn); + + expect(wrapped).toBe(fn); + expect(wrapped).not.toBe('something that is not a function'); + }); });