What Is XSS and How Do You Prevent It in Web Applications?
Cross-site scripting (XSS) is a flaw where a web application includes untrusted data in a page without proper encoding, so an attacker's script runs in another user's browser. It can steal sessions, change page content or perform actions as the victim. Developers prevent XSS mainly through context-aware output encoding, which modern frameworks such as React, Angular, Vue and Django apply by default.
Topic: Web & API Security in Cybersecurity & Ethical Hacking · Tutorial
Quick answer: Cross-site scripting (XSS) is a flaw where a web application includes untrusted data in a page without proper encoding, so an attacker's script runs in another user's browser. It can steal sessions, change page content or perform actions as the victim. Developers prevent XSS mainly through context-aware output encoding, which modern frameworks such as React, Angular, Vue and Django apply by default. The biggest risks come from escape hatches like dangerouslySetInnerHTML, v-html, bypassSecurityTrust methods and the safe filter. Sanitise any HTML you must render with a library such as DOMPurify, and add a strict Content Security Policy and Trusted Types as extra layers.
Key takeaways
- XSS is tracked as CWE-79 and sits in the OWASP Top 10 injection category.
- Encode output for its context: HTML, attribute, JavaScript or URL.
- Trust your framework's default escaping and audit every bypass.
- Sanitise rich HTML; never build HTML strings from user input.
- CSP and Trusted Types limit damage if a bug slips through.
Table of Contents
- What Is XSS and Why Should Developers Care?
- How Do Popular Frameworks Protect Against XSS?
- How Do You Render User HTML Safely?
- How Do CSP and Trusted Types Add Defence in Depth?
- What Should a Code Review Check for XSS?
- References
What Is XSS and Why Should Developers Care?
CWE-79 defines XSS as improper neutralisation of input during web page generation. There are three main types: stored (saved in a database and shown to others), reflected (bounced back from a request) and DOM-based (created by client-side JavaScript). For a fuller conceptual guide, read our sibling article what is cross-site scripting and how can it be prevented. This post focuses on what you change in your code.
How Do Popular Frameworks Protect Against XSS?
| Framework | Safe default | Escape hatch to audit |
|---|---|---|
| React | JSX escapes values in text and attributes | dangerouslySetInnerHTML, javascript: URLs in href |
| Angular | Treats values as untrusted and sanitises them | DomSanitizer bypassSecurityTrust methods |
| Vue | Mustache syntax escapes HTML | v-html, dynamic URL bindings |
| Django templates | Auto-escaping on by default | safe filter, mark_safe, autoescape off |
| Jinja2 (Flask) | Auto-escaping for HTML templates in Flask | safe filter, Markup() |
See the official guides from React, Angular, Vue and Django. In plain JavaScript, prefer textContent over innerHTML:
// Safer: the browser treats the value as text, not markup
element.textContent = userComment;
How Do You Render User HTML Safely?
If users must submit formatted content, sanitise it with a maintained allow-list library such as DOMPurify before rendering. A React example:
import DOMPurify from 'dompurify';
function Comment({ html }) {
const clean = DOMPurify.sanitize(html);
return <div dangerouslySetInnerHTML={{ __html: clean }} />;
}
Also validate URLs so only http and https schemes are allowed in links, and keep the sanitiser updated. The OWASP XSS Prevention Cheat Sheet lists encoding rules per context, and the DOM-based cheat sheet covers risky browser APIs.
How Do CSP and Trusted Types Add Defence in Depth?
A Content Security Policy tells the browser which scripts may run. A nonce-based policy blocks injected inline scripts:
Content-Security-Policy: script-src 'nonce-RANDOM' 'strict-dynamic'; object-src 'none'; base-uri 'none'
Generate a fresh random nonce per response. Trusted Types go further by making dangerous DOM sinks such as innerHTML reject plain strings in supporting browsers. Also set session cookies as HttpOnly and Secure.
| Layer | What it stops |
|---|---|
| Output encoding | Most XSS at the source |
| HTML sanitising | Malicious markup in rich content |
| CSP | Execution of injected scripts |
| Trusted Types | Unsafe writes to DOM sinks |
| HttpOnly cookies | Session theft through script |
What Should a Code Review Check for XSS?
- Search the codebase for innerHTML, dangerouslySetInnerHTML, v-html, bypassSecurityTrust, mark_safe and the safe filter.
- Confirm every match uses sanitised or trusted data.
- Check that user-controlled URLs are validated.
- Run SAST in CI and a DAST tool such as OWASP ZAP on staging.
- Test only applications you own or are authorised to test, as required by India's IT Act 2000.
XSS is one of several common risks covered in our guide to OWASP Top 10 vulnerabilities. Learn secure coding hands-on in our web application security training.
References
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0