Repository navigation
fix(core): Escape JSON.stringify output that goes into generated code
#26314
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| import MagicString from 'magic-string'; | ||
| import { DEFAULT_EXPORT, type ExportName, type SameWorkerBinding } from './wranglerConfig'; | ||
| import { detectWorkerEntrypointClasses } from './workerEntrypoint'; | ||
| import { stringifyForCodeInjection } from '@sentry/core'; | ||
|
|
||
| const MERGED_OPTIONS_IDENTIFIER = '__SENTRY_OPTIONS__'; | ||
|
|
||
|
|
@@ -284,7 +285,7 @@ function buildMergedOptionsDeclaration( | |
| return `const ${MERGED_OPTIONS_IDENTIFIER} = ${optionsFn};\n`; | ||
| } | ||
|
|
||
| const names = bindingNames.map(name => JSON.stringify(name)).join(', '); | ||
|
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. Missed stringify in generated codeLow Severity This is flagged because the review guideline requires Additional Locations (1)Triggered by project rule: PR Review Guidelines for Cursor Bot Reviewed by Cursor Bugbot for commit cb6d5bd. Configure here. |
||
| const names = bindingNames.map(name => stringifyForCodeInjection(name)).join(', '); | ||
| return ( | ||
| `const ${MERGED_OPTIONS_IDENTIFIER} = (env) => { ` + | ||
| `const opts = (${optionsFn})(env); ` + | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| const UNSAFE_CHARS = /[<>\u2028\u2029]/g; | ||
|
|
||
| const ESCAPED_CHARS: Record<string, string> = { | ||
| '<': '\\u003c', | ||
| '>': '\\u003e', | ||
| '\u2028': '\\u2028', | ||
| '\u2029': '\\u2029', | ||
| }; | ||
|
|
||
| /** | ||
| * A drop-in replacement for `JSON.stringify`, for values that go into generated JavaScript code. | ||
| * | ||
| * `JSON.stringify` output is a valid JavaScript expression, but it does not escape `<` and `>`, so | ||
| * a value with `</script>` or `<!--` breaks out of a `<script>` element when the code is inlined in | ||
| * HTML. It also does not escape U+2028 and U+2029, which end a line in JavaScript before ES2019. | ||
| * These characters only occur inside JSON strings, where their `\u` escapes have the same value. | ||
| * | ||
| * Use this every time `JSON.stringify` output is put into code, for example in a bundler plugin. | ||
| * | ||
| * @internal Only meant for Sentry-internal SDK usage. | ||
| * @hidden | ||
| */ | ||
| export function stringifyForCodeInjection( | ||
| value: unknown, | ||
| replacer?: ((this: unknown, key: string, value: unknown) => unknown) | (number | string)[] | null, | ||
| space?: string | number, | ||
| ): string { | ||
| // Each branch matches one `JSON.stringify` overload. | ||
| const json = | ||
| typeof replacer === 'function' ? JSON.stringify(value, replacer, space) : JSON.stringify(value, replacer, space); | ||
| // `JSON.stringify` returns `undefined` for `undefined` and functions, although its type says `string`. | ||
| return json?.replace(UNSAFE_CHARS, char => ESCAPED_CHARS[char] as string); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { JSDOM } from 'jsdom'; | ||
| import { runInNewContext } from 'node:vm'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { stringifyForCodeInjection } from '../../../src/build-time-plugins/stringifyForCodeInjection'; | ||
|
|
||
| describe('stringifyForCodeInjection', () => { | ||
| it.each([ | ||
| '</script><script>globalThis.injected = true</script>', | ||
| '</ScRiPt \n><script>globalThis.injected = true</script>', | ||
| '<!--<script>', | ||
| '--> after a comment', | ||
| 'quotes "\' and backslashes \\u003c with whitespace \n\r\t\u2028\u2029 and <>&', | ||
| ])('keeps %j inside one script element, with the same value', value => { | ||
| const input = { [value]: [value, { nested: value }] }; | ||
| const code = `globalThis.result = ${stringifyForCodeInjection(input)};`; | ||
|
|
||
| const dom = new JSDOM(`<script>${code}</script>`); | ||
| const scripts = dom.window.document.querySelectorAll('script'); | ||
| dom.window.close(); | ||
|
|
||
| expect(scripts).toHaveLength(1); | ||
| expect(scripts[0]?.textContent).toBe(code); | ||
|
|
||
| const context: { result?: unknown } = {}; | ||
| runInNewContext(code, context); | ||
| expect(context.result).toEqual(input); | ||
| }); | ||
|
|
||
| it('escapes the unsafe characters', () => { | ||
| expect(stringifyForCodeInjection('<>\u2028\u2029')).toBe('"\\u003c\\u003e\\u2028\\u2029"'); | ||
| }); | ||
|
|
||
| it('gives the same result as JSON.stringify for other values', () => { | ||
| const value = { a: [1, 'two', null, true], b: { c: 'd/e\\f' } }; | ||
| expect(stringifyForCodeInjection(value)).toBe(JSON.stringify(value)); | ||
| expect(stringifyForCodeInjection(value, null, 2)).toBe(JSON.stringify(value, null, 2)); | ||
| expect(stringifyForCodeInjection(value, ['a'])).toBe(JSON.stringify(value, ['a'])); | ||
| expect(stringifyForCodeInjection(value, (_key, v) => (v === 'two' ? 2 : v))).toBe( | ||
| JSON.stringify(value, (_key, v) => (v === 'two' ? 2 : v)), | ||
| ); | ||
| }); | ||
|
|
||
| it('returns undefined where JSON.stringify does', () => { | ||
| expect(stringifyForCodeInjection(undefined)).toBeUndefined(); | ||
| expect(stringifyForCodeInjection(() => undefined)).toBeUndefined(); | ||
| }); | ||
| }); |


Uh oh!
There was an error while loading. Please reload this page.