# 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.

- Type: term
- Domain: Engineering & architecture
- Level: Intermediate
- Last reviewed: 2026-09-23
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/cross-site-scripting/>)

## What 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.

## Why 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.

## When 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.

## How 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.

## A practical 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.

## Where 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.

## Visualization

[View the visualization on the website](<https://wiki.soroush.uk/topic/cross-site-scripting/#visualization-cross-site-scripting>)

## Sources

1. [Cross Site Scripting Prevention Cheat Sheet](<https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html>) — OWASP Foundation · Security practice guidance
2. [Cross-site Scripting \(XSS\)](<https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/XSS>) — MDN Web Docs · Official technical documentation

## Related topics

- [Same-Origin Policy](<https://wiki.soroush.uk/topic/same-origin-policy/>)
- [Cross-Site Request Forgery \(CSRF\)](<https://wiki.soroush.uk/topic/cross-site-request-forgery/>)
- [Frontend and Backend](<https://wiki.soroush.uk/topic/frontend-and-backend/>)
