termEngineering & architecture

Cross-Site Scripting (XSS)

A web vulnerability in which attacker-controlled content is interpreted as executable script or active markup within another site's trusted origin.

Download Markdown (.md)

01What it means

Cross-Site Scripting (XSS) occurs when untrusted data reaches a browser execution context without the context-appropriate safety treatment. Reflected and stored XSS involve server-produced output, while DOM-based XSS arises in client-side processing. The resulting code runs with the origin's access and user context.[1][2]

Untrusted data reaching executable context Trace input to its browser sink, then apply context-appropriate safety
  1. 01Untrusted inputRequest, storage, external data
  2. 02Transform + storePreserve trust context
  3. 03Output contextHTML, attribute, URL, JavaScript, CSS
  4. 04Safe treatmentEncode, safe sink, or sanitize
  5. 05BrowserRender data without unintended execution

Framework escaping, safe text sinks, HTML sanitization, and CSP address different parts of the risk.

02Why it matters

XSS can read or change visible data, perform authenticated actions, capture input, alter interfaces, and bypass many CSRF defenses. A single vulnerable rendering path can affect every user who views stored content.

03When to use it

Apply XSS prevention anywhere untrusted or mixed-trust data enters HTML, attributes, URLs, styles, scripts, or DOM APIs. Rich-text features need a maintained sanitizer; ordinary text should use safe templating and text-oriented APIs.

04How it works

  1. Trace untrusted input through storage, transformation, and every browser output context.
  2. Use framework auto-escaping and context-appropriate output encoding by default.
  3. Place plain content through safe sinks such as textContent rather than HTML parsing APIs.
  4. Sanitize intentionally allowed HTML with a maintained allowlist-based library.
  5. Restrict dangerous capabilities with Content Security Policy and test bypass-prone rendering paths.

05A practical example

Example

A support portal displays customer names using textContent rather than innerHTML. Its separate rich-text field is sanitized, and a restrictive CSP limits impact if a rendering mistake remains.

06Where it breaks

No single encoder works for every browser context, sanitizers require updates, and CSP is defense in depth rather than a substitute for safe rendering. Third-party scripts and template escape hatches expand the attack surface.

Common mistakes

  • Using HTML encoding inside JavaScript, CSS, or URL contexts as if contexts were interchangeable.
  • Sending untrusted strings to innerHTML or similar parsing sinks.
  • Disabling framework escaping for convenience without sanitization.
  • Assuming HttpOnly cookies make XSS harmless.

07Sources

  1. Cross Site Scripting Prevention Cheat SheetOWASP Foundation · Security practice guidanceCross Site Scripting Prevention Cheat Sheet
  2. Cross-site Scripting (XSS)MDN Web Docs · Official technical documentationCross-site Scripting (XSS)
View saved topics

Start typing to search titles, definitions, categories, and frameworks.