Use this when user content could run as script in other users' browsers.
XSS
XSS (cross-site scripting) injects malicious script into pages viewed by other users, stealing sessions or acting as them. Context-aware output encoding, framework auto-escaping and a strict CSP prevent it.
1. XSS injects scripts into pages.
2. Context-aware output encoding (frameworks auto-escape).
3. Sanitise rich HTML (DOMPurify).
4. CSP as defence in depth.
5. Avoid innerHTML/dangerous sinks.
untrusted input → encoded output → no script execution
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Common mistakes Narrow the fault
- Output not encoded by context.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Encode output for its context (HTML, attribute, JS, URL).
- Use framework auto-escaping.
- A strict CSP limits damage.
- DAST XSS tests (ZAP) — findings
curl -sI https://<site> | grep -i content-security-policy— CSP header- Output encoding in templates — framework auto-escaping
ReferenceWhy it matters1 section · depth on demand
Types and defences
| Type | Where the script comes from |
|---|---|
| Stored | Saved in the database and shown to other users |
| Reflected | Part of the request, echoed in the response |
| DOM-based | Client-side JavaScript writes untrusted data into the page |
Defences: context-aware output encoding (frameworks do this by default; avoid raw HTML helpers), a strict Content-Security-Policy, HttpOnly cookies, and sanitising any HTML users are allowed to submit (DOMPurify).
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Cross-Site Scripting (XSS) happens when an application includes untrusted data in a page without proper encoding, so the browser runs attacker-controlled script in the site's context. The script can steal data, perform actions as the user or alter the page.
| Type | Source |
|---|---|
| Reflected | Input echoed back in the response |
| Stored | Input saved and shown to other users |
| DOM-based | Client-side JavaScript writes untrusted data into the page |
Prevention:
- Context-aware output encoding (HTML, attribute, JavaScript, URL) — templating engines with auto-escaping.
- Avoid dangerous sinks (
innerHTML,eval); use safe APIs (textContent). - Sanitise rich HTML with a vetted library.
- Content Security Policy as a second layer.
HttpOnlycookies to limit session theft.
Output encoding by context
| Context | Encoding |
|---|---|
| HTML body | HTML entity encode < > & " ' |
| HTML attribute | Attribute encode, always quote attributes |
| JavaScript | Avoid; if needed, JSON-encode and use textContent |
| URL parameter | URL encode, and allow only http(s): schemes |
| CSS | Avoid user input |
Template engines (Twig, Jinja2, React JSX) encode by default — dangerous APIs are |raw, innerHTML, dangerouslySetInnerHTML.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Web Security → XSS. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://owasp.org/www-project-application-security-verification-standard/ · confidence 94%