Unsafe HTML Injection
dangerouslySetInnerHTML, direct innerHTML assignment
criticalSecurity
unsafe-htmlWhy this matters
Injecting unsanitized HTML opens the door to XSS attacks. An attacker can execute JavaScript in your users' browsers, steal sessions, and exfiltrate data.
Example
components/comment.tsxfunction Comment({ html }: { html: string }) {
return (
<div dangerouslySetInnerHTML={{ __html: html }} />
);
}components/comment.tsximport DOMPurify from "dompurify";
function Comment({ html }: { html: string }) {
const clean = DOMPurify.sanitize(html);
return (
<div dangerouslySetInnerHTML={{ __html: clean }} />
);
}How to fix
Sanitize all HTML with DOMPurify or a similar library before rendering. Better yet, use React's built-in text escaping and avoid dangerouslySetInnerHTML entirely.
Reference
- In the output
- Findings carry
"ruleId": "unsafe-html"in--jsonand the same id in SARIF and in the terminal. - Silence one line
// prodlint-disable-next-line unsafe-html- Silence a whole file
// prodlint-disable unsafe-htmlat the top of the file- How it works
- Reads the syntax tree, with a text fallback for files that don't parse. Source on GitHub