Injection & Input · 8 min read · Intermediate
Cross-Site Scripting (XSS) & CSP
Understand Reflected, Stored, and DOM XSS and defend with output escaping and Content-Security-Policy.
What you'll learn
- Distinguish Stored, Reflected, and DOM-based XSS
- Use `textContent` and auto-escaping templates by default
- Add a strict `Content-Security-Policy` header as a second layer of defense
Overview
Understand Reflected, Stored, and DOM XSS and defend with output escaping and Content-Security-Policy.
Code Example
// VULNERABLE: executes attacker script tags
// el.innerHTML = userComment;
// SAFE: treats input strictly as plain text
el.textContent = userComment;
// Defense-in-depth HTTP header:
// Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none';
Key Takeaways
- Distinguish Stored, Reflected, and DOM-based XSS
- Use
textContentand auto-escaping templates by default - Add a strict
Content-Security-Policyheader as a second layer of defense
Quick Check
Check Your Understanding
Score: 0 / 1
1. Which DOM property safely inserts untrusted user text without parsing HTML or executing scripts?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.