Learning center

An XSS comment on raw HTML: trace the data before you sanitize

Decide by where the string comes from and which context it lands in. Text your framework escapes on output needs no change; a raw-HTML escape hatch fed by users or third parties needs a sanitizer, and data inside a <script> tag needs escaping for that context even when your own repository wrote it.

Find the escape hatch and the data behind it#

Modern frameworks escape by default. React renders {value} as text, and "By default in Django, every template automatically escapes the output of every variable tag." Django automatic HTML escaping An XSS comment is worth reading closely only where code steps around that default: React's dangerouslySetInnerHTML, the DOM's innerHTML, Django's |safe filter or a Jinja environment with autoescaping off.

React's documentation states the condition plainly: "If the HTML inside isn't trusted (for example, if it's based on user data), you risk introducing an XSS vulnerability." React dangerouslySetInnerHTML So the first question is the data's origin, and the second is the context. OWASP notes that "The appropriate protection depends on how the data is used": user-authored HTML needs "a maintained HTML sanitizer", while text placed in a context that interprets syntax needs that context's encoding. OWASP XSS Prevention Cheat Sheet MDN on innerHTML gives the same warning for the DOM API.

JSON-LD written from repository files#

In April 2026, a pull request to vLLM's recipes site added structured data for search engines. Each recipe page rendered a JSON-LD block with dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}, filled from the recipe's title, description and provider. Two AI reviewers flagged it.

Gemini Code Assist wrote: "Injecting JSON-LD using dangerouslySetInnerHTML without escaping can lead to Cross-Site Scripting (XSS)" "if any field in the metadata (like the title or description) contains a closing </script> tag." It then named the data's origin itself: "Although the data currently comes from the repository, it is a security best practice to escape the JSON string" "to prevent potential injection from malicious contributions." Gemini's comment Codex added a comment titled "Escape JSON-LD before injecting into script tag": "JSON.stringify(jsonLd) is injected with dangerouslySetInnerHTML without escaping <," "so any recipe field containing </script> can terminate the JSON-LD block and execute arbitrary JS." Codex's comment

The pull request merged on 27 April 2026 without a reply or a change. Pull request

Both comments were technically right about the context. JSON.stringify escapes quotes and backslashes for JSON, but leaves < alone, and the HTML parser ends a script element at </script> whatever JSON thinks. In Node:

> JSON.stringify({ name: "a</script>b" })
'{"name":"a</script>b"}'
> JSON.stringify({ name: "a</script>b" }).replace(/</g, "\\u003c")
'{"name":"a\\u003c/script>b"}'

The escaped form parses back to the same string, so the structured data is unchanged. Next.js's JSON-LD guide recommends exactly this replacement, noting that JSON.stringify "does not sanitize malicious strings used in XSS injection". Next.js JSON-LD

The origin lowers the severity without changing the fix. The recipe data comes from YAML files under models/ in the same repository, loaded with js-yaml. A malicious title has to get through a pull request review first. The risk was a careless or hostile contribution, and the practical failure was more likely a broken page than an attack on readers.

The project made the change later anyway. A pull request merged on 15 September 2026 added a jsonLdScript helper that applies .replace(/</g, "\\u003c"), with a comment that "a stray </script> inside a stringified value would break the document." Later pull request The escape is one line, it fixes correctness as well as security, and it keeps working if the data later comes from somewhere less reviewed. Reviewers sometimes suggest a sanitizer here, but a sanitizer suits HTML fragments, not JSON inside a script element, where it would strip or mangle data instead of escaping it.

Dismiss when the framework escapes it#

The common false positive is a comment on ordinary interpolation, <p>{comment.body}</p> in React or {{ comment.body }} in a Django template. Both escape on output, so the reply can cite the framework's documentation and close the thread. Escaping does not cover a URL's scheme, though: a user-supplied href or src can still carry javascript:, and OWASP lists unvalidated URL values among the gaps frameworks leave, so check the scheme before dismissing a comment on one. OWASP framework security If you are unsure, render a value containing <b> and look at the page: escaped output shows the tag as text.

The opposite case needs no debate. HTML from users, from a CMS, from a third-party API or from a model's output, written through dangerouslySetInnerHTML, innerHTML or |safe, needs a maintained sanitizer such as DOMPurify at the point of rendering, or a change to render it as text.

Decide from origin and context#

What is renderedEvidence to checkResponse
A value through the framework's default escapingThe template or JSX renders it as textDismiss, citing the framework
HTML from users, a CMS, an external API or a modelWhere the string is producedChange: sanitize it, or render it as text
Data inside a <script> tag, such as JSON-LDWhether < is escapedChange: escape < as \u003c, whatever the origin
HTML built by your own code from escaped partsEvery interpolation inside the builderTest with <b> in each input, then dismiss or fix
Repository-authored HTML, such as rendered Markdown docsWho can merge contentDismiss with the trust boundary, or sanitize if contributions are open

When the reviewer's suggested fix does not suit the context, such as a sanitizer for JSON, accept the finding and use the right fix. The guide to accepted findings covers how to record a finding that was right with a different fix.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading