Pcornel
Browse topics
Options
Recent
Favorites
Security · Technical reference

XSS

Direct answer

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.

When to use this

Use this when user content could run as script in other users' browsers.

First 30 seconds

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.

Operational verification

untrusted input → encoded output → no script execution

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
Security architecture and operations
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformSecurity architecture and operations
Scope / prerequisiteUse this when user content could run as script in other users' browsers.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

1 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 references

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.

observation · 1
Key points
  • Encode output for its context (HTML, attribute, JS, URL).
  • Use framework auto-escaping.
  • A strict CSP limits damage.
Key checks
  • 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

TypeWhere the script comes from
StoredSaved in the database and shown to other users
ReflectedPart of the request, echoed in the response
DOM-basedClient-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.

TypeSource
ReflectedInput echoed back in the response
StoredInput saved and shown to other users
DOM-basedClient-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.
  • HttpOnly cookies to limit session theft.

Output encoding by context

ContextEncoding
HTML bodyHTML entity encode < > & " '
HTML attributeAttribute encode, always quote attributes
JavaScriptAvoid; if needed, JSON-encode and use textContent
URL parameterURL encode, and allow only http(s): schemes
CSSAvoid user input

Template engines (Twig, Jinja2, React JSX) encode by default — dangerous APIs are |raw, innerHTML, dangerouslySetInnerHTML.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docSecurity architecture and operations current

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%
Confirmation

Confirm action

Continue?