Skip to content

fix(core): Escape JSON.stringify output that goes into generated code - #26314

Open
isaacs wants to merge 1 commit into
developfrom
feat/escape-json-stringify-for-js-injection
Open

isaacs wants to merge 1 commit into
developfrom
feat/escape-json-stringify-for-js-injection

Conversation

@isaacs

@isaacs isaacs commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

JSON.stringify output is a valid JavaScript expression, but it does not escape < and >. When a value with </script> or <!-- goes into generated code and the bundle is inlined in HTML, it breaks out of the <script> element.

CodeQL flags this pattern (js/bad-code-sanitization). This helper avoids the issue.

Two packages had their own private escape helpers, and they did different things: bundler-plugins escaped <, and Remix escaped </ and <!--. The other SDKs did not escape at all. Now stringifyForCodeInjection in @sentry/core is the one helper. It is a drop-in replacement for JSON.stringify that also escapes <, >, U+2028 and U+2029. These characters only occur inside JSON strings, where their \u escapes have the same value. JSON.stringify already escapes the other characters in the CodeQL example.

Build-time code in astro, bundler-plugins, cloudflare, nextjs, nuxt, remix, server-utils, solidstart, svelte, sveltekit and tanstackstart-react now uses it for code, virtual modules, loaders and Vite define values. AGENTS.md points to it.

These uses of JSON.stringify stay as they are:

  • log and warning messages
  • boolean define and DefinePlugin values, and npm package names in __SENTRY_SERVER_MODULES__
  • the node local variables worker, which sends code to the V8 inspector, not to HTML
  • the Node ESM loader in server-runtime-injection, which is URL-encoded into a data: URL
  • base64-encoded source maps

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: 3562cb3 (PR test merge using develop at c9e61e3)

Against: f49009b (develop) · base run #65896

⚠️ Baseline mismatch: 4 develop commit(s) in the PR build are missing from the baseline. Their size changes also affect this comparison. Compare commits. Re-run once develop's baseline build for c9e61e3 finishes.

Path Size % Change Change
@sentry/browser 30.04 kB - -
@sentry/browser - with treeshaking flags 28.23 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.12 kB - -
@sentry/browser (incl. Tracing) 52.2 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 52.2 kB - -
@sentry/browser (incl. Tracing, Profiling) 55.17 kB - -
@sentry/browser (incl. Tracing, Replay) 92.73 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 81.52 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 97.08 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 110.51 kB - -
@sentry/browser (incl. Feedback) 47.66 kB - -
@sentry/browser (incl. sendFeedback) 30.62 kB - -
@sentry/browser (incl. FeedbackAsync) 35.62 kB - -
@sentry/browser (incl. Metrics) 31.36 kB - -
@sentry/browser (incl. Logs) 31.64 kB - -
@sentry/browser (incl. Metrics & Logs) 32.3 kB - -
@sentry/react 32.04 kB - -
@sentry/react (incl. Tracing) 54.86 kB - -
@sentry/vue 38.21 kB - -
@sentry/vue (incl. Tracing) 55.14 kB - -
@sentry/svelte 30.05 kB - -
@sentry/remix (Remix 3 client bundle) 57.96 kB - -
CDN Bundle 31.79 kB - -
CDN Bundle (incl. Tracing) 52.58 kB - -
CDN Bundle (incl. Logs, Metrics) 34.58 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 55.14 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 75.61 kB - -
CDN Bundle (incl. Tracing, Replay) 90.63 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 93.25 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 96 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 98.6 kB - -
CDN Bundle - uncompressed 92.69 kB - -
CDN Bundle (incl. Tracing) - uncompressed 155.13 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 101.03 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 162.76 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 231.11 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 275.27 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 282.9 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 287.07 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 294.69 kB - -
@sentry/nextjs (client) 58.23 kB - -
@sentry/sveltekit (client) 53.26 kB - -
@sentry/core/server 44.53 kB - -
@sentry/core/browser 13.99 kB - -
@sentry/node 149.35 kB -0.01% -4 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.18 kB - -
@sentry/node - without tracing 88.27 kB -0.01% -4 B 🔽
@sentry/node - without channel injection 138.45 kB -0.01% -2 B 🔽
@sentry/aws-serverless 97.19 kB -0.01% -6 B 🔽
@sentry/cloudflare (withSentry) - minified 204.35 kB - -
@sentry/cloudflare (withSentry) 474.32 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 229.66 kB - -

View base workflow run

@github-actions

Copy link
Copy Markdown
Contributor

One or more gzipped bundles increased by more than 500 bytes. If this increase is intentional, add the Accept Bundlesize Increase label to this PR to rerun and accept the check.

@isaacs
isaacs added this pull request to stack #26315 November 12, 2026 10:28
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 4 not successful, 55 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E node-eve Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E node-eve (latest) Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E nextjs-16-cacheComponents (cloudflare) Test (optional) Failing after 10m Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E cloudflare-think Test (optional) Successful in 56s Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry-static Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (latest) Test (optional) Successful in 54s Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable Test (optional) Successful in 49s Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (deno) Test (optional) Successful in 53s Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (cloudflare) Test (optional) Successful in 30s Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-14 (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (cloudflare) Test (optional) Successful in 37s Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (latest) Test (optional) Successful in 53s Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (cloudflare, latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 41s Details
✅ CI: Optional E2E Tests / E2E astro-7-static Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-mastra Test (optional) Successful in 54s Details
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable (latest) Test (optional) Successful in 42s Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries Test (optional) Successful in 44s Details
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 39s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (bun) Test (optional) Successful in 52s Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 58s Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 (tracer provider) Test (optional) Successful in 42s Details
✅ CI: Optional E2E Tests / E2E nextjs-orpc Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-mastra Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 34s Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 33s Details
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) Test (optional) Successful in 56s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 43s Details
✅ CI: Optional E2E Tests / E2E node-flue Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 41s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 38s Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 35s Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Successful in 34s Details
✅ CI: Optional E2E Tests / E2E supabase-nextjs Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 26s Details
✅ CI: Optional E2E Tests / E2E node-pi-durable Test (optional) Successful in 34s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-cloudflare Test (optional) Successful in 27s Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions Test (optional) Successful in 28s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 35s Details
✅ CI: Optional E2E Tests / E2E react-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 54s Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (tunnel-dev) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 40s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E vue-3 (no Options API) Test (optional) Successful in 1m Details

@isaacs
isaacs force-pushed the feat/escape-json-stringify-for-js-injection branch from 1d4e4d8 to 97cab5a Compare November 12, 2026 11:19
@isaacs
isaacs requested a review from a team November 12, 2026 11:19

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 97cab5a. Configure here.

): string {
// `JSON.stringify` returns `undefined` for `undefined` and functions, although its type says `string`.
// oxlint-disable-next-line typescript/no-explicit-any
return JSON.stringify(value, replacer as any, space)?.replace(UNSAFE_CHARS, char => ESCAPED_CHARS[char] as string);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uncommented any casts in helper

Low Severity

This was flagged because the review rules require a comment on new any and as any in SDK source explaining why a safer type is not possible. The replacer parameter and the replacer as any pass-through have oxlint disables, but those comments do not explain the unsafe typing.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 97cab5a. Configure here.

@JPeer264 JPeer264 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one

Comment thread AGENTS.md

- Follow existing conventions — check neighboring files
- Reach for existing utils before writing a new one. Most shared helpers live in `@sentry/core` (`packages/core/src/utils/`), with browser helpers in `packages/browser-utils/`. Search first (LSP `workspaceSymbol` or grep) for common needs (type guards in `is.ts`, object/array helpers, `normalize`, `dsn`, `merge`, string/url helpers). Reuse or extend the existing util rather than adding a near-duplicate; only introduce a new util when nothing fits.
- When `JSON.stringify` output goes into generated code (bundler plugins, loaders, virtual modules, `define`), use `stringifyForCodeInjection` from `@sentry/core` instead. It escapes `<`, `>`, U+2028 and U+2029, so the code stays safe when inlined in a `<script>` element.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Even though we might retire it, but adding it to BUGBOT.md wouldn't harm. Especially if we at some point migrate that somehow to Warden

@JPeer264
JPeer264 force-pushed the feat/escape-json-stringify-for-js-injection branch from 97cab5a to ecc2738 Compare November 12, 2026 13:28
Base automatically changed from jp/nextjs-deno-entry to develop November 13, 2026 06:40
`JSON.stringify` output is a valid JavaScript expression, but it does
not escape `<` and `>`. When a value with `</script>` or `<!--` goes
into generated code and the bundle is inlined in HTML, it breaks out of
the `<script>` element.

CodeQL flags this pattern (js/bad-code-sanitization). This helper avoids
the issue.

Two packages had their own private escape helpers, and they did
different things: `bundler-plugins` escaped `<`, and Remix escaped `</`
and `<!--`. The other SDKs did not escape at all. Now
`stringifyForCodeInjection` in `@sentry/core` is the one helper. It is
a drop-in replacement for `JSON.stringify` that also escapes `<`, `>`,
U+2028 and U+2029. These characters only occur inside JSON strings,
where their `\u` escapes have the same value. `JSON.stringify` already
escapes the other characters in the CodeQL example.

Build-time code in astro, bundler-plugins, cloudflare, nextjs, nuxt,
remix, server-utils, solidstart, svelte, sveltekit and
tanstackstart-react now uses it for code, virtual modules, loaders and
Vite `define` values. `AGENTS.md` points to it.

These uses of `JSON.stringify` stay as they are:

- log and warning messages
- boolean `define` and `DefinePlugin` values, and npm package names in
  `__SENTRY_SERVER_MODULES__`
- the `node` local variables worker, which sends code to the V8
  inspector, not to HTML
- the Node ESM loader in `server-runtime-injection`, which is
  URL-encoded into a `data:` URL
- base64-encoded source maps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JPeer264
JPeer264 force-pushed the feat/escape-json-stringify-for-js-injection branch from ecc2738 to 4d40b75 Compare November 13, 2026 06:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants