Course contentsShow
← Web Development
Lesson 2893 of 2,98252. Frontend SecurityFree lesson

Reflected XSS Attacks

How malicious scripts are injected through URL parameters and immediately reflected back to users.

Reflected XSS Attacks

What you'll learn: How attackers exploit URL parameters to inject malicious scripts that execute immediately in a victim's browser.

The Attack Vector

Reflected XSS (Cross-Site Scripting) is like a poisoned letter that gets immediately read back to you. An attacker crafts a malicious URL containing script code in a parameter, tricks a user into clicking it, and the vulnerable website reflects that script back in the response—where the browser executes it.

How It Works

Imagine a search feature that displays your query back to you:

https://example.com/search?q=<user input here>

If the server naively reflects the q parameter without sanitization:

<!-- Vulnerable page output -->
<p>You searched for: <script>alert('XSS!')</script></p>

The browser sees a legitimate script tag from example.com and executes it. Attackers replace that harmless alert with code that steals cookies, captures keystrokes, or redirects users to phishing sites.

The Attack Chain

  1. Attacker crafts URL: https://site.com/search?q=<script>/* malicious code */</script>
  2. Victim clicks link (via email, social media, or ad)
  3. Server reflects input in the HTML response without encoding
  4. Browser executes the injected script immediately
  5. Attacker gains access to cookies, tokens, or performs actions as the victim

Why It's Called "Reflected"

Unlike stored XSS (where malicious code lives in a database), reflected XSS bounces the attack payload off the server in real-time. It's not persistent—it only affects users who click the poisoned link.

Key Takeaway: Reflected XSS occurs when user input from URLs is echoed back into HTML without proper encoding, allowing attackers to inject scripts that execute immediately when victims click malicious links—CSP and input sanitization are your primary defenses.