Security operations

Reflected Cross-Site Scripting (XSS): How It Works & How to Stop It

Reflected XSS needs just one crafted link: the victim clicks, the site echoes the script back in its response, and the browser runs it as if the site itself wrote it.

Cysoro Security TeamPublished · 9 min read
SECURITY OPERATIONS Reflected Cross-Site Scripting One bad link, one echoing page, one stolen session — how it works search results GET /search?term=<script> alert('XSS')</script> No results for: <script>alert('XSS')</script> Fixed: output encoded &lt;script&gt; — harmless text Encode output for its context
Quick answer

Reflected cross-site scripting (XSS) is a non-persistent injection attack in which a malicious script hidden in a URL or form input is reflected back by a vulnerable web application in its HTTP response and executed in the victim's browser. The attacker delivers the crafted link by phishing; the script then runs with the victim's session. The fix is context-aware output encoding and a Content Security Policy.

Key takeaways

  • Reflected XSS is non-persistent: the payload lives in the request, not in the database, so the attacker must deliver a crafted link to each victim.
  • It is the most common XSS type in the wild, typically delivered through search pages, error messages and other inputs that echo user data.
  • Output encoding is the primary fix — encode every untrusted value for the exact HTML context where it lands.

Reflected cross-site scripting is the most common kind of XSS — and the easiest to understand. An attacker hides a script in a link, the victim clicks, and the website itself bounces the script back in its response. The victim's browser runs it as if the trusted site wrote it. No database, no persistence: just one bad link and one page that echoes input it should have encoded.

What is reflected cross-site scripting?

Reflected cross-site scripting (XSS) occurs when an attacker injects browser-executable code into a single HTTP response. The injected attack is not stored in the application; it is non-persistent and only affects users who open a maliciously crafted link or third-party page. The attack string travels in the request — usually a URL parameter — is processed improperly by the application, and is returned to the victim in the response.

OWASP's Web Security Testing Guide calls reflected XSS the most frequent type of XSS found in the wild, and notes it is also known as non-persistent, first-order or type 1 XSS. Because the payload is delivered and executed via a single request and response, the attacker must deliver the crafted link to each victim — typically by phishing, a malicious advertisement or a compromised page.

How does a reflected XSS attack work?

Almost every reflected XSS attack follows the same four steps. Knowing them shows you where the chain breaks.

How reflected XSS works, in 4 steps 1Craft the linkattacker hides ascript in a URLparameter 2Deliver itphishing email,message or adcarries the link 3Site reflects itthe app echoes theinput into the pagewithout encoding 4Browser runs itthe script executeswith the victim'ssession Break the chain at step 3: encode output, and the script becomes harmless text.
The four steps of a reflected XSS attack — one request, one response, one victim. Illustration by Cysoro.

1. The attacker crafts a malicious link

The attacker finds an input the site echoes back — a search box, an error page, a comment preview — and builds a URL with a script in the parameter. A classic example is a search page that prints the search term without encoding it.

2. The attacker delivers the link

The link goes out by phishing email, text message, social media post or malicious ad. The victim has to click; reflected XSS cannot spread on its own. Attackers often disguise the link with a URL shortener or a plausible-looking domain.

3. The vulnerable site reflects the script

The victim's browser sends the request to the real, trusted site. The site takes the input and inserts it into the HTML response without encoding it — so the browser cannot tell the attacker's script apart from the site's own code.

4. The browser executes the script

The script runs in the victim's browser with the privileges of the trusted site. It can read the page, steal the session cookie, log keystrokes, change page content, or make requests as the victim. The attacker's code is usually JavaScript, though other scripting languages are also used.

What does a reflected XSS attack look like?

Imagine a search page that echoes the search term when nothing is found. A normal request looks like this: GET /search.php?term=candy, and the page replies No results were found for: candy. Now the attacker sends a victim this link: GET /search.php?term=<script>alert('XSS!');</script>.

If the application inserts the term into the HTML without encoding it, the victim's browser receives a page containing a live script tag — and runs it. In a real attack the script would not show an alert; it would silently send the victim's session cookie to the attacker's server, or replace a download link with a malicious one.

OWASP's testing guide documents exactly this pattern and the filter-evasion variants attackers use when a site tries to block <script> but misses encoded forms such as %3cscript%3e.

The key point: the victim never visited a suspicious site. They visited the real site, over the real domain, with a valid certificate — and the real site delivered the attack. That is why reflected XSS is so effective, and why it is a server-side bug, not a user mistake.

Reflected vs stored vs DOM-based XSS: what's the difference?

All three types share one root cause — untrusted input rendered as code — but they differ in where the payload lives, how the victim is reached, and where you look for evidence.

Reflected XSSStored XSSDOM-based XSS
Where the payload livesIn the request (URL parameter)In the database, saved by the appIn the browser, in client-side code
PersistenceNon-persistentPersistentNon-persistent
How the victim is reachedA crafted link, usually via phishingViewing an infected pageA crafted link or page
Blast radiusOne victim per delivered linkEvery viewer of the pageOne victim per delivered link
Server log traceYes, in request and responseYesOften none
Typical impactModerateSevere — often high or criticalModerate

Stored XSS is the most dangerous of the three because one injection hits every viewer. Reflected XSS is the most common, because so many pages echo input. DOM-based XSS is the hardest to detect, because the payload may never touch the server.

The OWASP Top 10 classified XSS as the second most prevalent issue, found in around two thirds of all applications, and grouped all three types under the same weakness: CWE-79, improper neutralization of input during web page generation.

What can an attacker do with reflected XSS?

The script runs with the victim's session on the trusted site, so the attacker inherits whatever the victim can do. The common impacts are session hijacking, account takeover, credential theft and malware delivery.

  • Session stealing. The script reads document.cookie and sends it to the attacker, who reuses the session. HttpOnly cookies block this specific theft, but the attacker can still act inside the live session.
  • Account takeover. With the session, the attacker can change the victim's email or password, or perform actions as the victim.
  • Credential harvesting. The script can replace the page's login form with a fake one, or inject a convincing "session expired, log in again" prompt.
  • Defacement and content change. The attacker can rewrite download links, inject fake payment pages or redirect the victim to a malicious site.
  • Keylogging and clipboard theft. Scripts can capture keystrokes and clipboard contents while the victim stays on the page.
  • MFA bypass. Because the script runs inside an authenticated session, it can act on behalf of the victim without needing the second factor again.

How do you detect reflected XSS?

Detection starts with finding inputs the application echoes. Test every place user input appears in a response: search boxes, error messages, URL parameters, form fields and headers. OWASP's testing guide recommends a black-box approach in three phases: detect input vectors, test for reflected injection, and test filter bypasses.

  1. Map the inputs. List every parameter, form field and header that appears in a response — including error pages, which are a classic reflection point.
  2. Send a harmless probe. Submit a unique marker such as test123<script> and check whether it appears unencoded in the response.
  3. Test encoding variants. Try encoded forms such as %3cscript%3e, different contexts (HTML body, attribute, JavaScript string, URL) and case variations — filters often block one form and miss another.
  4. Check the logs. Reflected XSS leaves traces in request and response logs, unlike DOM-based XSS. Search logs for script tags and encoded angle brackets in parameters.
  5. Use automated scanners as a starting point, but confirm every finding by hand — scanners produce false positives and miss context-specific issues.

For defenders, the signals in production are unusual parameters containing script tags, encoded HTML in URLs, and spikes in error-page traffic. If you run a site, check your security headers too — our free security headers checker shows whether your site sends the headers that limit XSS damage.

How do you prevent reflected XSS?

Preventing XSS requires separating untrusted data from active browser content. The primary defense, in the OWASP XSS Prevention Cheat Sheet, is context-aware output encoding: encode every untrusted value at the moment it is inserted into a response, choosing the encoding based on where the value lands — HTML body, HTML attribute, JavaScript, CSS or URL.

PortSwigger's Web Security Academy gives the same advice: filter input on arrival, encode data on output for its context, and use a Content Security Policy as a last line of defense.

Five defenses against reflected XSS 1. Output encoding — encode every untrusted value for its exact HTML context 2. Frameworks that escape by default — React, Rails, modern templates 3. Input validation — allowlists for expected formats, reject the rest 4. Content Security Policy — block inline scripts even if one slips through 5. Cookie flags — HttpOnly and Secure limit session theft
Defense in depth against reflected XSS — encoding is the fix; the rest limit the damage. Illustration by Cysoro.

Encode output for its context

Encoding is not one technique — it depends on where the value lands. A value in an HTML body needs HTML entity encoding; a value in an attribute needs attribute encoding; a value in a JavaScript string needs JavaScript encoding. Encoding for the wrong context leaves the hole open. Use your framework's encoding functions rather than writing your own.

Use frameworks that escape by default

Modern frameworks such as React and recent versions of Ruby on Rails escape output by design. If your stack does this, the main risk is the places where you deliberately bypass escaping — dangerouslySetInnerHTML in React, raw HTML helpers, or hand-built templates. Audit those first.

Validate input — but do not rely on it

Validate input against an allowlist of expected formats: numbers where numbers are expected, known values where the choice is fixed. Input validation helps, but it is not a substitute for output encoding — attackers evade input filters with encoding tricks, and legitimate input can still be dangerous in the wrong context.

Add a Content Security Policy

A Content Security Policy (CSP) tells the browser which sources of scripts are allowed, and can block inline scripts entirely. CSP is defense in depth: it limits the damage if an encoding bug slips through, but it is not a fix on its own. Check your headers with the security headers checker.

Set HttpOnly and Secure on cookies

The HttpOnly flag stops JavaScript from reading a session cookie, which blocks the classic cookie-theft payload. The Secure flag ensures cookies are only sent over HTTPS. Neither stops reflected XSS — an attacker can still act inside the victim's session — but both reduce the damage. Our SSL checker confirms your site serves HTTPS correctly.

What should you do if your site has reflected XSS?

Treat it as a real vulnerability, not a cosmetic bug. A reflected XSS hole is a phishing amplifier: it turns any link to your site into a potential account-takeover weapon against your users.

  1. Confirm and scope it. Reproduce the issue, identify every parameter and context affected, and check logs for signs of exploitation.
  2. Fix the encoding first. Apply context-aware output encoding at the vulnerable template or handler, and search the codebase for the same pattern elsewhere.
  3. Add CSP and cookie flags if they are missing, to limit damage while the fix rolls out.
  4. Notify affected users if there is evidence of exploitation, and ask them to review recent account activity and change passwords.
  5. Add regression tests that submit script payloads to every reflected input and assert the response contains no live script.

For users who clicked a suspicious link: close the tab, clear cookies for the site, change your password from a trusted device, and check for unfamiliar account activity. If you received the link by email, report it — our guide to phishing email examples shows what these messages look like.

The bottom line

Reflected XSS is simple, common and dangerous: a script in a link, reflected by a trusted site, executed in a victim's browser. It is the most frequent XSS type in the wild, and it succeeds because one page echoes input it should have encoded.

The fix is equally simple in principle: encode every untrusted value for its exact context, use frameworks that escape by default, and add CSP and HttpOnly cookies as defense in depth. If you run a website, check your headers with our free security headers checker — and fix the reflections before an attacker finds them.

Frequently asked questions

What is reflected cross-site scripting in simple terms?

Reflected XSS is an attack where a malicious script is hidden in a link. When the victim clicks it, the vulnerable website echoes the script back in its response, and the victim's browser runs it as if it came from the trusted site.

What is the difference between reflected and stored XSS?

Reflected XSS is non-persistent: the payload lives in the request and hits one victim per crafted link. Stored XSS is persistent: the payload is saved in the database and runs for every user who views the infected page, so its blast radius is much larger.

What is DOM-based XSS?

DOM-based XSS runs entirely in the browser: client-side JavaScript takes attacker-controlled data and inserts it into the page unsafely. Unlike reflected and stored XSS, it may leave no trace in server logs, which makes it harder to detect.

How do attackers deliver reflected XSS?

Usually by phishing: an email, message or malicious page carries a crafted link with the payload in a URL parameter. The victim clicks, the vulnerable site reflects the script, and it executes in the victim's browser session.

Can reflected XSS steal my session cookie?

Yes. A classic reflected XSS payload reads document.cookie and sends it to the attacker's server, letting the attacker hijack the victim's session. HttpOnly cookies block JavaScript from reading them, which limits this specific theft — but attackers can still act inside the victim's session.

How do you prevent reflected XSS?

Use context-aware output encoding: encode every untrusted value for the HTML context where it lands. Use frameworks that escape by default, validate input, set the HttpOnly and Secure flags on cookies, and add a Content Security Policy as defense in depth.

Is reflected XSS still common in 2026?

Yes. OWASP's testing guide calls reflected XSS the most frequent type of XSS found in the wild, and XSS as a class was the second most prevalent issue in the OWASP Top 10, found in around two thirds of applications.

Can a web application firewall stop reflected XSS?

A WAF can block many known payloads, but it is not a reliable fix: attackers evade filters with encoding tricks, and a WAF cannot know which inputs are safe to echo. Output encoding in the application is the real fix; a WAF is defense in depth.

Sources

  1. OWASP — Testing for Reflected Cross Site Scripting (WSTG-INPV-01)
  2. OWASP Top Ten 2017 — A7: Cross-Site Scripting (XSS)
  3. PortSwigger — What is cross-site scripting (XSS) and how to prevent it?
  4. OWASP — Cross Site Scripting Prevention Cheat Sheet
  5. Trend Micro — What Is Cross-site scripting (XSS)?
Written & fact-checked byCysoro Security Team

The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.

  • Email authentication
  • Phishing & BEC
  • Domain & DNS security
  • Small-business security

Checked against the OWASP Web Security Testing Guide (WSTG-INPV-01), the OWASP Top 10 description of cross-site scripting, the OWASP XSS Prevention Cheat Sheet, and PortSwigger's Web Security Academy.

How we write and fact-check →

Free tools for this topic

No account needed.

See where your domain stands

Free for up to 3 domains. No card, no installs.

Start free