← Web Application Security (OWASP) learning path
Web Application Security (OWASP) · Module 4 of 10

Output encoding and XSS prevention

Keep pages from running untrusted content as scripts.

📖 Key ideas

  1. Cross-Site Scripting (XSS) risks arise when untrusted data is rendered as HTML/JS.
  2. Encode output for its context (HTML, attribute, URL, JavaScript).
  3. Modern frameworks (React, Angular, Vue) escape by default; avoid bypassing it.
  4. Sanitise rich text with a vetted library (e.g. DOMPurify).
  5. Add a Content Security Policy (CSP) as an extra layer.
💡 Real-life example: Output encoding is putting user text behind glass in a display case: people can read it, but it cannot act.
XSSCross-Site Scripting
Output encodingEscaping data for its context
SanitiserRemoves unsafe HTML
CSPContent Security Policy header