From 60f0499e7e4bde213294458ea8b9fa3e3a04c170 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Thu, 3 Oct 2024 22:08:48 +0000 Subject: [PATCH 1/3] fix(browser): Ensure `wrap()` only returns functions (#13838) --- packages/browser/src/helpers.ts | 8 +++++++- .../browser/test/unit/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 35930167672d..da707cb4e437 100644 --- a/packages/browser/src/helpers.ts +++ b/packages/browser/src/helpers.ts @@ -66,7 +66,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/unit/integrations/helpers.test.ts b/packages/browser/test/unit/integrations/helpers.test.ts index 34420a6d30bc..8311e10ba41f 100644 --- a/packages/browser/test/unit/integrations/helpers.test.ts +++ b/packages/browser/test/unit/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'); + }); }); From e497c06597f2f7c72ac3cf3301a8f39e6f3e6552 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Fri, 4 Oct 2024 08:27:58 +0000 Subject: [PATCH 2/3] Add changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f166186ce49..b263add1a70f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott +## 7.119.1 + +- fix(browser/v7): Ensure wrap() only returns functions (#13838 backport) + ## 7.119.0 - backport(tracing): Report dropped spans for transactions (#13343) From ba21c28dd1b03c5bb394a5d5ec6f8c91fa7e1456 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Fri, 4 Oct 2024 08:28:53 +0000 Subject: [PATCH 3/3] Add attribution --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b263add1a70f..2cfe028a75f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ - fix(browser/v7): Ensure wrap() only returns functions (#13838 backport) +Work in this release contributed by @legobeat. Thank you for your contribution! + ## 7.119.0 - backport(tracing): Report dropped spans for transactions (#13343)