What Is XSS (Cross-Site Scripting)?
Cross-site scripting is a vulnerability where an attacker injects malicious script into a web page viewed by other users, letting that script run in their br...
Published November 16, 2026
Cross-site scripting (XSS) is an attack where an attacker gets malicious JavaScript to run in other users' browsers via your site, letting them steal sessions, act as the user or deface pages. Prevent it by escaping output and using a Content Security Policy.
The concept
Stored XSS saves the payload on the server (comments); reflected XSS echoes it from the URL; DOM XSS happens in client-side code that writes untrusted data into the page.
The root cause is treating user input as HTML or code. Escape on output for the right context (HTML, attribute, JS, URL).
Stored XSS flow
Practical examples
Vulnerable
el.innerHTML = comment // <img src=x onerror=alert(1)> runsSafe
el.textContent = commentEscape in templates (Blade auto-escapes)
{{ $comment }} {{-- escaped --}}
{!! $comment !!} {{-- raw: dangerous --}}CSP header
Content-Security-Policy: default-src 'self'; script-src 'self'Common mistakes
- Using innerHTML or dangerouslySetInnerHTML with user data.
- Trying to blacklist <script> instead of escaping.
- Storing tokens in localStorage reachable by injected scripts.
Key takeaways
- Escape output by context.
- Prefer textContent and framework auto-escaping.
- Add CSP and HttpOnly cookies.
FAQ
Does React prevent XSS?
Mostly, as it escapes by default, except dangerouslySetInnerHTML and unsafe URLs.
Sanitising HTML?
Use a vetted library such as DOMPurify.