Unescaped JSON in a <script> tag

next/json-ld-xsserrorsecurityseo

Why it matters

JSON.stringify does not escape </script>: any string in the data containing it ends the script element and the rest runs as HTML (stored XSS through JSON-LD).

Fix

Escape <: JSON.stringify(data).replace(/</g, "\\u003c").

Credit

  • From the Next.js team

    The JSON-LD guide shows sanitizing JSON.stringify output by replacing < with \u003c.

  • From OWASP

    Output encoding for the HTML context is the core defence against cross-site scripting.

References

See it