XSS Defense by Output Context
Escaping rules depend on whether data enters HTML text, an attribute, a URL or JavaScript; validation alone cannot solve every context.
Frame the problem
Security starts with a concrete asset, attacker capability and trust crossing.
Why the system fails
A generic sanitization function is applied without knowing the parser context, or a safe value is later concatenated into a different context.
The important question is not “what is XSS Defense by Output Context?” but “which assumption let untrusted data or an over-scoped identity cross application output → specific browser parser?” Trace the decision at the boundary, then constrain what can happen after the first control fails.
Design the control in layers
Start with the control closest to the interpretation or privilege boundary: Use the framework’s context-aware escaping Then add a control that reduces blast radius and telemetry that proves the decision was enforced.
The resulting design is not labelled secure. Record the identified controls, the known failure paths, the remaining exposure, and the evidence you would need during an incident.
| Prevent | Detect | Recover |
|---|---|---|
| Use the framework’s context-aware escaping · Prefer textContent and typed URL APIs · Deploy nonce/hash-based CSP as defense in depth | CSP reports grouped by sink and route · Security tests for every rendering context | Contain the affected identity or component, scope impact from audit evidence, and preserve a regression test. |
Key points
- Asset: The separation between user data and executable browser behavior.
- Boundary: Application output → specific browser parser
- Primary control: Use the framework’s context-aware escaping
- Detection signal: CSP reports grouped by sink and route
- Always ask what limits damage when the primary control fails.
XSS Context Visualizer
Change the system and observe which assumption moves.
Follow the attack
Safe conceptual simulation: capability → missing control → crossed boundary → asset impact.
- 1Attacker starts with: Anyone who controls a value later rendered into a page.
- 2A generic sanitization function is applied without knowing the parser context, or a safe value is later concatenated into a different context.
- 3The weak or missing boundary control is crossed: Application output → specific browser parser
- 4Impact: Stored or reflected script execution under the application origin.
- Stored or reflected script execution under the application origin.
Defend, detect, recover
One prevention is a single point of security failure. Layer it and make failure observable.
- • Use the framework’s context-aware escaping
- • Prefer textContent and typed URL APIs
- • Deploy nonce/hash-based CSP as defense in depth
- • CSP reports grouped by sink and route
- • Security tests for every rendering context
- • Contain the affected identity or component.
- • Scope access from audit evidence.
- • Fix the boundary and add a regression test.
- • Misconfiguration and new access paths can bypass the intended control.
- • A privileged insider or compromised control plane may still reach the asset.