Title: FunctionToString integration's patched `Function.prototype.toString` throws SecurityError when its realm's browsing context was navigated cross-origin · Issue #21965 · getsentry/sentry-javascript · GitHub
Open Graph Title: FunctionToString integration's patched `Function.prototype.toString` throws SecurityError when its realm's browsing context was navigated cross-origin · Issue #21965 · getsentry/sentry-javascript
X Title: FunctionToString integration's patched `Function.prototype.toString` throws SecurityError when its realm's browsing context was navigated cross-origin · Issue #21965 · getsentry/sentry-javascript
Description: Is there an existing issue for this? I have checked for existing issues I have reviewed the documentation I am using the latest SDK release or the bug is reproducible on master How do you use Sentry? Sentry Saas (sentry.io) Which SDK are...
Open Graph Description: Is there an existing issue for this? I have checked for existing issues I have reviewed the documentation I am using the latest SDK release or the bug is reproducible on master How do you use Sentr...
X Description: Is there an existing issue for this? I have checked for existing issues I have reviewed the documentation I am using the latest SDK release or the bug is reproducible on master How do you use Sentr...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/21965
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"FunctionToString integration's patched `Function.prototype.toString` throws SecurityError when its realm's browsing context was navigated cross-origin","articleBody":"### Is there an existing issue for this?\n\n- [x] I have checked for existing issues\n- [x] I have reviewed the documentation\n- [x] I am using the latest SDK release or the bug is reproducible on master\n\n### How do you use Sentry?\n\nSentry Saas (sentry.io)\n\n### Which SDK are you using?\n\n@sentry/nuxt (the affected code is in @sentry/core, so all browser SDKs are affected)\n\n### SDK Version\n\n10.34.0 — the affected code is unchanged on current master (`packages/core/src/integrations/functiontostring.ts`)\n\n### Framework Version\n\nNuxt 4 (not framework-specific)\n\n### Description\n\nThe `functionToStringIntegration` (enabled by default) patches `Function.prototype.toString`. The patched function calls `getClient()` → `getCurrentScope()` → `getMainCarrier()` → `getSentryCarrier(GLOBAL_OBJ)` on **every** `.toString()` invocation, which performs a property read of `GLOBAL_OBJ.__SENTRY__`.\n\n`GLOBAL_OBJ` is `globalThis`, which in a `Window` realm is the **WindowProxy** of the realm's browsing context. If that browsing context is later **navigated to a cross-origin document** while code from the old realm can still be invoked (e.g. a still-alive parent window holding references into a same-origin child iframe whose `src` changed to a cross-origin URL), the `__SENTRY__` read on the WindowProxy throws:\n\n```\nSecurityError: Failed to read a named property '__SENTRY__' from 'Window':\nBlocked a frame with origin \"https://app.example.com\" from accessing a cross-origin frame.\n```\n\n(current Chrome wording: `Blocked a restricted frame with origin \"…\" from accessing another frame.`)\n\nBecause the patch is installed on `Function.prototype`, the throw is triggered by *any* third-party script calling `.toString()`. In our production case the chain was: Microsoft Clarity's MutationObserver serialization → Tawk.to's core-js `Function.prototype.toString` wrapper → Facebook Pixel's (`fbevents.js`) core-js `inspectSource` → Sentry's patched `toString` → `SecurityError`, surfacing as `onunhandledrejection` noise (24 events within ~1 second from a single page transition on a page embedding a third-party game iframe with `sandbox=\"... allow-top-navigation\"`).\n\nProduction stack (origins redacted, resolved via source maps):\n\n```\n/0.8.66/clarity.js (async/await machinery, MutationObserver serialization)\n/_s/v4/app/.../twk-chunk-vendors.js (ShadowRoot.toString — core-js toString wrapper)\n/en_US/fbevents.js (core-js inspectSource)\n@sentry/core/build/esm/integrations/functiontostring.js:24 (Function.prototype.toString)\n@sentry/core/build/esm/currentScopes.js:101 (getClient)\n@sentry/core/build/esm/currentScopes.js:10 (getCurrentScope)\n@sentry/core/build/esm/carrier.js:18 (getMainCarrier → getSentryCarrier(GLOBAL_OBJ) → __SENTRY__ read throws)\n```\n\nNote: the native `Function.prototype.toString` never throws for these calls — the throw is introduced solely by the integration's internal carrier access, so the SDK converts harmless third-party introspection into uncatchable (for the app) SecurityError noise, which Sentry itself then reports.\n\n### Steps to Reproduce\n\nSelf-contained repro (Chrome). Serve the two files below from `http://127.0.0.1:8801` and also expose the same directory on `http://127.0.0.1:8802` (different port = cross-origin but same-site, any static server works):\n\n`parent.html`:\n\n```html\n\u003c!doctype html>\n\u003ciframe id=\"f\" src=\"/child.html\">\u003c/iframe>\n\u003cscript>\n const f = document.getElementById('f')\n let done = false\n f.onload = () => {\n if (done) return\n done = true\n const childFn = f.contentWindow.exposedFn // keep a reference into the child realm\n setTimeout(() => { f.src = 'http://127.0.0.1:8802/parent.html' }, 300) // navigate iframe cross-origin\n setTimeout(() => {\n try {\n childFn.toString() // resolves to the child realm's patched Function.prototype.toString\n console.log('ok')\n } catch (e) {\n console.log('THREW', e.name, e.message)\n }\n }, 1500)\n }\n\u003c/script>\n```\n\n`child.html` (stands in for any same-origin frame that initialized a Sentry browser SDK — inlined replica of `functiontostring.ts` + `carrier.ts` so the repro needs no bundler; using the real SDK behaves identically):\n\n```html\n\u003c!doctype html>\n\u003cscript>\n const SDK_VERSION = '10.34.0'\n const GLOBAL_OBJ = globalThis\n function getSentryCarrier(carrier) {\n const __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {})\n __SENTRY__.version = __SENTRY__.version || SDK_VERSION\n return (__SENTRY__[SDK_VERSION] = __SENTRY__[SDK_VERSION] || {})\n }\n function getMainCarrier() { getSentryCarrier(GLOBAL_OBJ); return GLOBAL_OBJ }\n function getClient() { getMainCarrier(); return undefined }\n const SETUP_CLIENTS = new WeakMap()\n const originalFunctionToString = Function.prototype.toString\n Function.prototype.toString = function (...args) {\n const originalFunction = this && this.__sentry_original__\n const context = SETUP_CLIENTS.has(getClient()) && originalFunction !== undefined ? originalFunction : this\n return originalFunctionToString.apply(context, args)\n }\n window.exposedFn = function exposedFn() {}\n\u003c/script>\n```\n\nObserved output:\n\n```\nTHREW SecurityError Failed to read a named property '__SENTRY__' from 'Window': Blocked a restricted frame with origin \"http://127.0.0.1:8801\" from accessing another frame.\n```\n\nThe same read also throws in any other \"realm outlives its document's origin\" situation (Android Chrome same-process cross-origin navigations of the top frame show the same signature).\n\n### Expected Result\n\nThe patched `Function.prototype.toString` should never throw where the native implementation would not. Suggested fix — wrap the patch body, mirroring the defensive style already used around the patch installation:\n\n```ts\nFunction.prototype.toString = function (this: WrappedFunction, ...args: any[]): string {\n try {\n const originalFunction = getOriginalFunction(this);\n const context =\n SETUP_CLIENTS.has(getClient() as Client) && originalFunction !== undefined ? originalFunction : this;\n return originalFunctionToString.apply(context, args);\n } catch {\n // e.g. SecurityError reading __SENTRY__ off a WindowProxy whose browsing context\n // was navigated cross-origin — fall back to native behavior\n return originalFunctionToString.apply(this, args);\n }\n};\n```\n\nWe've verified (against @sentry/core 10.34.0) that this keeps the unwrap behavior intact for live realms and degrades to exact native semantics otherwise. Happy to open a PR if this direction is acceptable.\n\n### Actual Result\n\n`SecurityError: Failed to read a named property '__SENTRY__' from 'Window': Blocked a frame with origin \"…\" from accessing a cross-origin frame.` thrown out of `Function.prototype.toString`, reported as unhandled-rejection error events.\n","author":{"url":"https://github.com/JT-Well","@type":"Person","name":"JT-Well"},"datePublished":"2026-07-05T06:27:48Z","url":"https://github.com/getsentry/sentry-javascript/issues/21965"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:aa9550c3-9e6f-4f89-1be8-20f44c29866f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C472:2E648F:63DF8B:87D220:6B0441A0 |
| html-safe-nonce | 11a57a7b91470e343537ff4cafa199955ffe589d76a8e746f5a878f8c437e428 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNDcyOjJFNjQ4Rjo2M0RGOEI6ODdEMjIwOjZCMDQ0MUEwIiwidmlzaXRvcl9pZCI6IjY4NjY4NzY4MTM2Njc0ODQwNjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fc472fed74c798e0fe501d44fb75f4a2929e684eff683be536071455a1fb98ca |
| hovercard-subject-tag | issue:4811859920 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/21965/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0c3d8d3414247726da850438bdc5b08b5ab8ebd765a916f646a1f3d22d765ca0/getsentry/sentry-javascript/issues/21965 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0c3d8d3414247726da850438bdc5b08b5ab8ebd765a916f646a1f3d22d765ca0/getsentry/sentry-javascript/issues/21965 |
| og:image:alt | Is there an existing issue for this? I have checked for existing issues I have reviewed the documentation I am using the latest SDK release or the bug is reproducible on master How do you use Sentr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JT-Well |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9185ccfefd80c1ff2d9ddcdb0cd2becf46b5a1efbbf30741a895e4bb8de25e64 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 06dc448a76fe4751ac1db26524aefa3aa3e889a8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width