Skip to content
cloudgrip

$ man cross-site-scripting

//Attacks and vulnerabilities

What is cross-site scripting (XSS)?

Cross-site scripting (XSS) is a vulnerability where an application puts untrusted input into a web page without encoding it, so a victim's browser runs the attacker's JavaScript as if it came from the site. The script can read data, act as the user and alter the page.

  • Attacks and vulnerabilities
  • 3 min read
  • updated Nov 11, 2026

How it works

XSS happens when data is treated as code. An application takes text that a user controls and places it into a page. If the text contains HTML or JavaScript and the application does not neutralize it, the browser cannot tell it apart from the site's own markup and runs it.

Browsers isolate sites from each other by origin. A script that runs on app.acme.test can read that site's pages, call its API with the user's session and change what the user sees. XSS gives the attacker that position.

There are three main forms:

  • Reflected. The payload sits in the request, for example in a URL parameter, and the server includes it in the page it returns. The attacker must get the victim to follow a prepared link.
  • Stored. The application saves the payload, for example in a comment or a profile field, and later shows it to other users. Simply viewing the page triggers it.
  • DOM-based. The server is not involved. JavaScript in the page reads attacker-controlled data, such as the URL fragment, and writes it into the page unsafely.

What it looks like

A help desk tool lets customers open tickets. The agent dashboard builds its HTML by pasting the ticket subject into a string. A customer submits this:

output
POST /api/tickets HTTP/1.1
Host: support.acme.test
Content-Type: application/json

{"subject": "<svg onload=alert(document.domain)>", "body": "Please help"}

When an agent opens the queue, the browser parses the subject as markup and runs the script in the agent's session. The harmless alert proves that script execution is possible.

The vulnerable code and its fix:

output
// Vulnerable: the subject is placed into HTML as it was received.
res.send(`<h1>${ticket.subject}</h1>`);

// Fixed: the subject is encoded for an HTML context before it is placed.
res.send(`<h1>${escapeHtml(ticket.subject)}</h1>`);

After encoding, < becomes &lt;, and the browser displays the text instead of interpreting it.

How to test for it

  1. Find the inputs. URL parameters, form fields, headers, file names, JSON values and anything stored and shown later.
  2. Find where each one lands. Send a unique marker string and search every response and rendered page for it, including pages that other roles see.
  3. Note the context. The marker may sit between tags, inside an attribute, inside a script block or inside a URL. Each context breaks in a different way.
  4. Try to leave the context. Submit <, ", ' and > and see whether they come back encoded.
  5. Prove execution. Use a harmless payload that shows the origin, in a test account.
  6. Check the client side. Trace what feeds innerHTML and similar calls in the page's JavaScript.

How to fix it

Encode output for the place it lands, and let the framework do the work.

  • Rely on automatic escaping. Modern template engines and front-end frameworks escape values by default. Most XSS in such code comes from the escape hatches, such as dangerouslySetInnerHTML in React and v-html in Vue.
  • Encode by context. HTML body, attribute, JavaScript and URL contexts each need their own encoding.
  • Prefer safe browser APIs. Use textContent in place of innerHTML when inserting text.
  • Sanitize when HTML is required. If users may write rich text, pass it through a maintained sanitizer library that only lets approved tags through.
  • Validate URLs. Accept only http and https schemes for user-supplied links, so javascript: URLs are refused.
  • Add a Content Security Policy (CSP). A strict policy that blocks inline scripts limits the damage when a bug slips through. It is a second layer and not a replacement for encoding.

XSS vs CSRF and SQL injection

XSS and SQL injection are both injection flaws: data ends up interpreted as code. SQL injection targets the database on the server. XSS targets the browser of another user.

Cross-site request forgery forges a request that starts on a different site, and the attacker never sees the reply. XSS runs inside the trusted site and reads whatever the user can see. Finding these patterns before release is a core goal of secure code review.

See what an attacker would find.

Give us a URL and two test logins. If there’s something to find, your first exploited findings land within 24 hours.

You talk to the people who build Hydra and Cerberus. No sales script.

or write to founders@cloudgrip.ai