seriesoftubes/scripts/lib/js-literal.js
Frederic Jung dd6e4f4f8a
Fix CodeQL code-scanning alerts
- Values pasted into eval'd or inline-script code go through lit() (scripts/lib/js-literal.js), which escapes <, >, /, U+2028 and U+2029 after JSON.stringify
- build test and i18n-check read script tags with a DOM parser or a plain search, not HTML regexes
- mcp.js parses the Authorization header without a backtracking regex
2026-11-05 21:24:26 +01:00

5 lines
458 B
JavaScript

// A value as a JavaScript literal, safe to paste into code that is eval'd or into an inline <script>: JSON, with the
// characters that could end a script tag or break a string (<, >, /, U+2028, U+2029) written as \u escapes.
const ESC = /[<>\/\u2028\u2029]/g;
const lit = (v) => { const s = JSON.stringify(v); return s === undefined ? 'undefined' : s.replace(ESC, (c) => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')); };
module.exports = { lit };