UNPKG

@clerk/shared

Version:

Internal package utils used by the Clerk SDKs

1 lines 1.25 kB
{"version":3,"file":"htmlSafeJson.mjs","names":[],"sources":["../src/htmlSafeJson.ts"],"sourcesContent":["// `<`, `>`, `/` (to neutralize `</script>` breakouts) plus the U+2028/U+2029 line\n// terminators, which are valid in JSON strings but break executable script contexts.\nconst ESCAPE_REGEX = /[<>/\\u2028\\u2029]/g;\n\n/**\n * `JSON.stringify` that is safe to embed directly inside an HTML `<script>` element.\n *\n * `JSON.stringify` leaves `<`, `>` and `/` untouched, so a `</script>` substring in any\n * string value would break out of the surrounding script block (XSS). Escaping those\n * characters to their `\\uXXXX` forms keeps the value byte-identical after `JSON.parse`\n * while preventing the HTML parser from terminating the element early.\n */\nexport function htmlSafeJson(value: unknown): string {\n const json = JSON.stringify(value);\n if (json === undefined) {\n return 'undefined';\n }\n return json.replace(ESCAPE_REGEX, ch => `\\\\u${ch.charCodeAt(0).toString(16).padStart(4, '0')}`);\n}\n"],"mappings":";AAEA,MAAM,eAAe;;;;;;;;;AAUrB,SAAgB,aAAa,OAAwB;CACnD,MAAM,OAAO,KAAK,UAAU,KAAK;CACjC,IAAI,SAAS,QACX,OAAO;CAET,OAAO,KAAK,QAAQ,eAAc,OAAM,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG;AAChG"}