Skip to content

Commit

Permalink
fix(browser/v7): Ensure wrap() only returns functions (#13838 backp…
Browse files Browse the repository at this point in the history
…ort) (#13864)


Co-authored-by: Luca Forstner <[email protected]>
  • Loading branch information
legobeat and lforst authored Oct 4, 2024
1 parent 5adcbf9 commit 5a88ff6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

- "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)

Work in this release contributed by @legobeat. Thank you for your contribution!

## 7.119.0

- backport(tracing): Report dropped spans for transactions (#13343)
Expand Down
8 changes: 7 additions & 1 deletion packages/browser/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions packages/browser/test/unit/integrations/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit 5a88ff6

Please sign in to comment.