Cross-site scripting (XSS): script that runs as your site

Cross-site scripting (XSS) is an attack in which a website sends a script written by an attacker to other users' browsers. The browser runs it as if it came from the site, so it can read what the page shows, act as the signed-in user, or change what they see. It happens when user input is placed into a page without being encoded for where it lands. A cross site scripting attack is aimed at the browsers of a site's users, and a page that allows one has an XSS vulnerability.

Section Web attacks Updated Weakness CWE-79

Quick facts

Weakness
CWE-79: improper neutralization of input during web page generation
OWASP category
A05:2025 Injection
Types
Reflected, stored and DOM-based
Main fix
Encode output for the context it lands in; add a Content Security Policy
Does a WAF stop it?
Partly. Script and markup in a request are recognisable; DOM-based XSS may never reach the server

Definitions follow OWASP and CWE-79.

How an XSS attack works

A page includes something a user supplied: a search term, a name, a comment. If the page inserts it as markup instead of as text, an attacker can supply markup that contains a script. Every browser that loads the page runs the script with the site's identity.

From there the script can do whatever the page can: read data shown to the user, send requests with their session, change the page to ask for a password. The site's own trust becomes the attacker's tool, which is why XSS on a trusted site is serious even when it looks small. When the flaw lives in browser code, see DOM-based XSS. A script that skims form fields is described under formjacking.

What are XSS attacks used for? Reading what a page shows, acting as the signed-in user, and changing what the visitor sees. What are cross site scripting attacks built from? A string of input that the page treats as code. A page that does this is vulnerable to XSS, and an application with several XSS vulnerabilities gives an attacker several places to try.

In Spanish the same attack is called secuencia de comandos en sitios cruzados, and the abbreviation XSS comes from the English name.

XSS examples

The classic test is a search box that shows the term back: <script>alert(1)</script> as the query, and a dialog box if the page inserts it as markup. Real attacks avoid script tags because they are filtered first, and use attributes that run code instead, such as an image whose onerror handler runs when the image fails to load.

Other places carry the same risk without looking like markup: a link whose address starts with javascript:, user input placed inside an existing script block, or a value written into a page attribute without quotes. Each context needs its own encoding, which is why a single escaping function used everywhere cannot cover them all. File names built from input carry a similar flaw on the server; see path traversal.

The OWASP XSS Filter Evasion Cheat Sheet is a guide to XSS testing for application security professionals, and it shows that input filtering is an incomplete defense: it supplies a series of attacks that can bypass certain XSS filters. The list began as RSnake's XSS Cheat Sheet, once published at ha.ckers.org/xss.html; OWASP now keeps an updated, maintained version. This is why the fixes below start with encoding output, not with filtering input.

Reflected, stored and DOM-based XSS

  • Reflected XSS. The script travels in the request, usually a link, and the page reflects it straight back. In reflected cross site scripting the victim has to open a crafted link.
  • Stored XSS. The script is saved, for example in a comment or profile, and served to everyone who views it. No crafted link is needed.
  • DOM-based XSS. The page's own JavaScript takes data from the URL or another source and writes it into the page unsafely, so the server may never see the script.

The difference decides who can see the attack. Reflected and stored XSS pass through the server; DOM-based XSS can live entirely in the browser.

HOW THE THREE TYPES REACH A VICTIM PASSES THE SERVER NEEDS A CRAFTED LINK HITS EVERY VIEWER REFLECTED XSS STORED XSS DOM-BASED XSS // only the first two can be seen by a filter on the server
// three types, and which of them a server-side filter can see

XSS and CSRF: what the difference is

Both use the victim's browser, but in opposite directions. XSS runs attacker script inside your site, so it can read and do anything the page can. CSRF makes the victim's browser send a request to your site from somewhere else; it can make requests but cannot read the responses.

An XSS flaw also defeats most CSRF protection, because script running on the site can read the anti-forgery token like the page does.

What a WAF catches, and what it misses

A request filter matches script tags, event handlers and other markup in parameters, and refuses or records them. That covers many reflected and stored attempts, because the script has to arrive in a request at some point.

It misses DOM-based XSS that never reaches the server, payloads encoded or split in ways the rules do not decode, and stored script that arrived through a channel the filter does not read. Fields where users legitimately write markup produce false positives. SQL injection is the server-side counterpart: input is joined into a query instead of into a page; prompt injection does the same with a model's instructions.

Filter evasion is the reason a request filter is one layer and not the fix: encoding output in the application code deals with the flaw itself.

How to prevent XSS attacks

  1. Encode output for its context. HTML body, attribute, JavaScript and URL contexts each need their own encoding; the OWASP XSS Prevention Cheat Sheet lists the rules.
  2. Use a framework that encodes by default. And treat every place that bypasses it as a review point.
  3. Avoid writing untrusted data into the page with script. Use APIs that set text, not markup.
  4. Add a Content Security Policy. A CSP limits which scripts the browser will run, so an injected script is refused even if it gets into the page; see Content Security Policy; the other browser-side headers are covered under security headers.
  5. Mark session cookies HttpOnly. Script cannot read them, which limits what a successful XSS can steal; see session hijacking.

XSS in modern front-end frameworks

Current frameworks encode text by default, which has made XSS rarer. Each also keeps a way around that default for inserting raw markup: dangerouslySetInnerHTML in React, v-html in Vue, and the bypass functions of Angular's sanitizer. Most XSS in such applications sits in those few lines.

The other gaps are outside the framework: code that writes to innerHTML directly, third-party widgets, server-rendered templates around the application, and links built from user data. A search of the codebase for the escape hatches is a quick first audit.

Questions

What is cross-site scripting?

Cross-site scripting (XSS) is an attack in which a website sends a script written by an attacker to other users' browsers, where it runs with the site's identity.

What are the types of XSS?

Reflected XSS, where the script comes in the request and is sent straight back; stored XSS, where it is saved and served to every viewer; and DOM-based XSS, where the page's own script writes untrusted data into the page.

How do you prevent XSS?

Encode output for the context it is inserted into, use a framework that does this by default, avoid writing untrusted data as markup, add a Content Security Policy, and mark session cookies HttpOnly.

What is the difference between XSS and CSRF?

XSS runs attacker script inside your site, so it can read and act. CSRF makes a victim's browser send a request to your site from elsewhere; it can act but cannot read the response.

What is the XSS filter evasion cheat sheet?

It is an OWASP guide to XSS testing for application security professionals, based on RSnake's XSS Cheat Sheet from ha.ckers.org. It lists attacks that can bypass certain XSS filters, which shows that input filtering alone is an incomplete defense.