Repository navigation
fix(core): Avoid stack overflow in FunctionToString on cyclic Proxy prototype chains #26162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,22 +19,29 @@ const _functionToStringIntegration = (() => { | |||||
| const callOriginalFunctionToString = Function.prototype.call.bind(originalFunctionToString) as ( | ||||||
| thisArg: unknown, | ||||||
| ) => string; | ||||||
| // eslint-disable-next-line @typescript-eslint/unbound-method | ||||||
| const hasOwnProperty = Function.prototype.call.bind(Object.prototype.hasOwnProperty) as ( | ||||||
| obj: unknown, | ||||||
| key: PropertyKey, | ||||||
| ) => boolean; | ||||||
|
|
||||||
| // intrinsics (like Function.prototype) might be immutable in some environments | ||||||
| // e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal) | ||||||
| try { | ||||||
| Function.prototype.toString = function (this: WrappedFunction): string { | ||||||
| const originalFunction = getOriginalFunction(this); | ||||||
| let unwrappedFunction: WrappedFunction | undefined; | ||||||
|
|
||||||
| try { | ||||||
| if (SETUP_CLIENTS.has(getClient() as Client) && originalFunction !== undefined) { | ||||||
| unwrappedFunction = originalFunction; | ||||||
| // `markFunctionWrapped` sets the original as an own property. An inherited lookup could walk a | ||||||
| // prototype chain that loops through a Proxy and overflow the stack, which native `toString` never does. | ||||||
| if (SETUP_CLIENTS.has(getClient() as Client) && hasOwnProperty(this, '__sentry_original__')) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This brings back the exact issue we gave as the reason for closing #25964 |
||||||
| unwrappedFunction = getOriginalFunction(this); | ||||||
| } | ||||||
| } catch { | ||||||
| // Reading the Sentry carrier off `getClient()` can throw a `SecurityError` when `this` (or the global | ||||||
| // object) is a `WindowProxy` whose browsing context was navigated cross-origin. The native | ||||||
| // `toString` never throws here, so fall back to it to avoid turning harmless introspection into noise. | ||||||
| // object) is a `WindowProxy` whose browsing context was navigated cross-origin, and Proxy traps on | ||||||
| // `this` can throw too. The native `toString` never throws here, so fall back to it to avoid turning | ||||||
| // harmless introspection into noise. | ||||||
| } | ||||||
|
|
||||||
| return callOriginalFunctionToString(unwrappedFunction ?? this); | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/server-utils/src/integrations/hono/sentryOriginalTraps.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // oxlint-disable-next-line typescript/no-unsafe-function-type | ||
| type AnyFunction = Function; | ||
|
|
||
| /** | ||
| * Proxy traps that expose `original` as `__sentry_original__` on a wrapping Proxy without defining it on the target, | ||
| * which is the original itself. `functionToStringIntegration` only unwraps own properties, so the property has to | ||
| * look like an own one too. | ||
| */ | ||
| export function sentryOriginalTraps<T extends AnyFunction>( | ||
| original: AnyFunction, | ||
| ): Pick<ProxyHandler<T>, 'get' | 'getOwnPropertyDescriptor'> { | ||
| return { | ||
| get(target, prop, receiver) { | ||
| if (prop === '__sentry_original__') { | ||
| return original; | ||
| } | ||
| return Reflect.get(target, prop, receiver); | ||
| }, | ||
| getOwnPropertyDescriptor(target, prop) { | ||
| if (prop === '__sentry_original__') { | ||
| return { value: original, writable: true, enumerable: false, configurable: true }; | ||
| } | ||
| return Reflect.getOwnPropertyDescriptor(target, prop); | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/server-utils/test/integrations/hono/sentryOriginalTraps.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { functionToStringIntegration, getCurrentScope, setCurrentClient } from '@sentry/core'; | ||
| import { afterAll, describe, expect, it } from 'vitest'; | ||
| import { wrapMiddlewareWithSpan } from '../../../src/integrations/hono/wrapMiddlewareSpan'; | ||
| import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; | ||
|
|
||
| describe('sentryOriginalTraps', () => { | ||
| afterAll(() => { | ||
| getCurrentScope().setClient(undefined); | ||
| }); | ||
|
|
||
| it('lets functionToStringIntegration unwrap a wrapped Hono middleware', async () => { | ||
| const client = new TestClient(getDefaultTestClientOptions()); | ||
| setCurrentClient(client); | ||
| client.addIntegration(functionToStringIntegration()); | ||
|
|
||
| const middleware = async (_c: unknown, next: () => Promise<void>): Promise<void> => { | ||
| await next(); | ||
| }; | ||
| const wrapped = wrapMiddlewareWithSpan(middleware); | ||
|
|
||
| expect(wrapped).not.toBe(middleware); | ||
| expect(wrapped.toString()).toBe(middleware.toString()); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.