. When other users view the page with this comment, the JavaScript executes in their browsers, sending their session cookies to the attacker's server. The attacker then uses stolen cookies to impersonate victims and hijack their accounts." } },{ "@type": "Question", "name": "How do you prevent XSS attacks?", "acceptedAnswer": { "@type": "Answer", "text": "Primary XSS prevention methods: Encode/escape all user-generated content before displaying it (convert < to < etc.), implement Content Security Policy (CSP) headers restricting script sources, validate and sanitize all input on server-side, use modern frameworks with automatic XSS protection (React, Angular, Vue), set HTTPOnly flag on cookies preventing JavaScript access, avoid innerHTML and use textContent/innerText, and regularly test applications with XSS scanning tools." } }] }
Blog

XSS Attack Guide 2024: Cross-Site Scripting Examples, Types & Prevention

JP
John Price
January 27, 2024
Share

Cross-Site Scripting (XSS) ranks among the most common yet dangerous web application vulnerabilities, consistently appearing in OWASP Top 10 lists and affecting millions of websites annually. Despite straightforward prevention techniques, XSS vulnerabilities persist due to developer oversight, complex application architectures, and the challenge of sanitizing user input across diverse contexts. This comprehensive guide explains what XSS is, the three main types with practical examples, real-world attack scenarios, complete prevention strategies, testing methodologies, and best practices for eliminating XSS vulnerabilities from web applications.

What is XSS (Cross-Site Scripting)? Clear Definition

Cross-Site Scripting (XSS) is a web security vulnerability where attackers inject malicious JavaScript code into web pages viewed by other users. When victims visit compromised pages, the malicious script executes in their browsers with the application's privileges, potentially stealing session cookies, capturing keystrokes, redirecting to phishing sites, or modifying page content.

Why it's called "Cross-Site": The attack crosses from the attacker's injected code into the victim's browser session on the targeted site, executing in the context of that site's domain and privileges.

Why it's abbreviated "XSS" not "CSS": To avoid confusion with Cascading Style Sheets, the vulnerability is abbreviated XSS (Cross-Site Scripting).

How XSS Works: Simple Example

Vulnerable Application

A website's search feature displays your search term:

```php // VULNERABLE CODE ```

Normal Usage

URL: `https://site.com/search?q=cybersecurity`

Page displays: "You searched for: cybersecurity"

XSS Attack

URL: `https://site.com/search?q=`

Page HTML becomes:

```html You searched for: ```

Result: JavaScript executes in victim's browser showing alert. More dangerous payloads could steal cookies or perform actions as the victim.

Three Types of XSS Attacks

1. Reflected XSS (Non-Persistent)

How it works: Malicious script reflects off web server in response (URL parameters, form submissions)

Attack scenario:

  1. Attacker crafts malicious URL with XSS payload
  2. Sends URL to victim via email, social media, or phishing
  3. Victim clicks link visiting vulnerable site
  4. Site reflects payload in response; script executes in victim's browser
  5. Script steals session cookie and sends to attacker

Example payload stealing cookies:

```html ```

2. Stored XSS (Persistent)

How it works: Malicious script stored in database/server (comments, profile fields, forum posts) and executed whenever users view that content

Attack scenario:

  1. Attacker submits malicious comment/post with XSS payload
  2. Application stores payload in database without sanitization
  3. ANY user viewing that page executes the malicious script
  4. Script can affect hundreds or thousands of users

Example: Social media profile XSS

```html ```

Why stored XSS is most dangerous: Affects all users automatically without requiring social engineering; persistent until removed

3. DOM-Based XSS

How it works: Vulnerability exists in client-side JavaScript code manipulating DOM based on user input

Vulnerable client-side code:

```javascript // VULNERABLE var search = window.location.hash.substring(1); document.getElementById('results').innerHTML = "You searched for: " + search; ```

Attack: `https://site.com/#`

Key difference: Payload never sent to server—entirely client-side, making detection harder

XSS Type Where Payload Stored Server Involvement Danger Level
Reflected Not stored (URL/request) Server reflects payload Medium (requires victim click)
Stored Database/server Server stores and serves payload High (affects all viewers)
DOM-Based Not stored (URL fragment) Server never sees payload Medium (harder to detect)

Real-World XSS Attack Scenarios

Scenario 1: Session Hijacking via Cookie Theft

```javascript // Malicious payload injected into vulnerable site ```

Attack flow:

  1. Victim views page containing this script
  2. Script reads session cookie from browser
  3. Sends cookie to attacker's server
  4. Attacker uses stolen cookie to impersonate victim

Scenario 2: Keylogging Capturing Credentials

```javascript // Keylogger injected via XSS ```

Impact: Every keystroke sent to attacker—capturing passwords, credit cards, personal information

Scenario 3: Phishing via Page Modification

```javascript // Inject fake login form ```

Impact: Users see realistic fake login form; submit credentials directly to attacker

Famous XSS Attacks and Breaches

Samy Worm - MySpace (2005)

Twitter XSS Worm (2010)

eBay Persistent XSS (2016)

Complete XSS Prevention Guide

1. Output Encoding/Escaping (Primary Defense)

Principle: Encode user-generated content before displaying in HTML

HTML Context Encoding:

```php // SECURE - Encode HTML special characters echo "You searched for: " . htmlspecialchars($search, ENT_QUOTES, 'UTF-8'); // Converts dangerous characters: // < becomes < // > becomes > // " becomes " // ' becomes ' ```

JavaScript Context Encoding:

```javascript // SECURE - Encode for JavaScript strings var search = ""; ```

URL Context Encoding:

```php // SECURE - URL encode parameters $url = "http://example.com/search?q=" . urlencode($search); ```

2. Content Security Policy (CSP)

What it is: HTTP header instructing browsers which scripts are allowed to execute

Basic CSP Header:

``` Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted-cdn.com; object-src 'none' ```

What this does:

Strict CSP for maximum protection:

``` Content-Security-Policy: default-src 'none'; script-src 'nonce-random123' 'strict-dynamic'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self' ```

3. Input Validation

Whitelist approach: Only allow expected input formats

```javascript // Validate email format function validateEmail(email) { const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; return regex.test(email); } // Reject obviously malicious input function containsXSS(input) { const dangerous = /4. HTTPOnly Cookie Flag

Purpose: Prevent JavaScript from accessing cookies

```php // Set session cookie with HTTPOnly flag setcookie('session', $session_id, [ 'httponly' => true, // JavaScript cannot access 'secure' => true, // Only sent over HTTPS 'samesite' => 'Strict' // CSRF protection ]); ```

Impact: Even if XSS exists, attackers cannot steal session cookies via `document.cookie`

5. Use Modern Frameworks with Built-in Protection

Frameworks automatically encoding output:

  • React: JSX automatically escapes values preventing XSS
  • Angular: Templates sanitize bindings by default
  • Vue.js: Interpolation escapes HTML
  • Django: Template auto-escaping enabled by default

React example (secure by default):

```javascript // SECURE - React automatically escapes function SearchResults({searchTerm}) { return
You searched for: {searchTerm}
; } // Even if searchTerm contains javascript:alert('XSS') "> '> ```

Testing methodology:

  1. Test EVERY input field, URL parameter, header
  2. Try payloads in different contexts (HTML, JavaScript, URL)
  3. Check if special characters encoded or stripped
  4. Observe application response and page source
  5. Test with browser developer tools open

Automated XSS Testing Tools

XSStrike:

```bash # Advanced XSS scanner python3 xsstrike.py -u "http://target.com/search?q=test" ```

Burp Suite Scanner:

  • Automated crawling and XSS testing
  • Context-aware payload generation
  • DOM-based XSS detection

OWASP ZAP:

  • Free open-source scanner
  • Active and passive XSS detection
  • Automated and manual testing modes

XSS Testing Checklist

  • ☐ Test all input fields with XSS payloads
  • ☐ Test URL parameters and fragments (hash)
  • ☐ Test HTTP headers (User-Agent, Referer, X-Forwarded-For)
  • ☐ Test file upload functionality (SVG, HTML uploads)
  • ☐ Test stored content (comments, profiles, posts)
  • ☐ Check for DOM-based XSS in JavaScript
  • ☐ Verify CSP headers present and restrictive
  • ☐ Confirm HTTPOnly flag on session cookies

Advanced XSS Attack Vectors

Filter Bypass Techniques

Attackers use various methods to bypass basic XSS filters:

Case variation and encoding:

```html <script>alert('XSS')</script> ```

Event handlers:

```html ```

JavaScript pseudo-protocol:

```html Click me