Preventing XSS in Modern Frameworks: React, Vue, Angular and Templates
Where XSS still happens in modern frontends: dangerouslySetInnerHTML, v-html, URL injection, DOM sinks, markdown rendering.
Published December 6, 2026
Modern frameworks escape output by default, which eliminated the most common cross-site scripting bugs. XSS has not disappeared; it has moved to the escape hatches: rendering raw HTML, building URLs from user input, writing to DOM sinks directly, and rendering markdown or rich text. Knowing these spots is enough to prevent most XSS in a React, Vue or Angular codebase.
The concept
React escapes values interpolated in JSX, Vue escapes {{ }} interpolation and Angular sanitises bindings. The dangerous APIs are explicit: React's dangerouslySetInnerHTML, Vue's v-html, Angular's bypassSecurityTrust* functions, and direct DOM assignments like element.innerHTML or document.write. Another frequent source is href or src attributes built from user input, which can carry javascript: URLs.
When you must render user-provided HTML (rich text editors, markdown, CMS content), sanitise it with a well-maintained library such as DOMPurify, ideally on both server and client, and keep the allowed tags and attributes minimal. Trusted Types, a browser feature enforced via CSP, makes DOM sinks reject raw strings unless they pass through an approved sanitiser policy, catching mistakes at runtime.
Where XSS still happens
| Escape hatch | Framework | Safer approach |
|---|---|---|
| dangerouslySetInnerHTML | React | Render text normally; sanitise with DOMPurify if HTML is required |
| v-html | Vue | Use interpolation; sanitise when unavoidable |
| bypassSecurityTrustHtml | Angular | Avoid; rely on built-in sanitizer |
| element.innerHTML / insertAdjacentHTML | Vanilla DOM | textContent, or sanitise |
| href={userUrl} | All | Allow only http(s)/mailto schemes |
| Markdown → HTML | All | Sanitise rendered HTML |
Rendering rich content safely
The javascript: URL trap
Practical examples
Sanitise HTML in React
Only when you genuinely need to render HTML.
import DOMPurify from 'dompurify'
function Comment({ html }) {
const clean = DOMPurify.sanitize(html, { ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a', 'p', 'ul', 'li', 'code'],
ALLOWED_ATTR: ['href'] })
return <div dangerouslySetInnerHTML={{ __html: clean }} />
}Validate user-supplied URLs
Block javascript:, data: and other dangerous schemes.
function safeHref(input) {
try {
const url = new URL(input, window.location.origin)
return ['http:', 'https:', 'mailto:'].includes(url.protocol) ? url.href : '#'
} catch { return '#' }
}
<a href={safeHref(user.website)} rel="noopener noreferrer">Website</a>Enforce Trusted Types with CSP
DOM sinks reject plain strings.
Content-Security-Policy: require-trusted-types-for 'script'; trusted-types dompurify defaultCommon mistakes
- Assuming frameworks make XSS impossible.
- Rendering markdown or CMS HTML without sanitisation.
- Building href/src from user input without scheme checks.
- Writing custom regex-based HTML sanitisers.
Key takeaways
- Frameworks escape by default; XSS lives in the escape hatches.
- Sanitise any user HTML with DOMPurify and minimal allow-lists.
- Validate URL schemes for href and src.
- Add CSP and Trusted Types as defence in depth.
FAQ
Is server-side rendering safer?
The same rules apply; template engines escape by default, and raw-output helpers are the risk.
Does sanitising on the server suffice?
It helps, but browser parsing differences mean client-side sanitisation right before insertion is also recommended for rich content.
What about JSON in script tags?
Embedding JSON in HTML needs escaping of </script> and other sequences; use framework helpers designed for this.