Skip to content
Cyber Unboxed
Web Application Attacks

Cross-Site Scripting (XSS): When a Website Runs a Stranger's Code

Cross-site scripting, or XSS, lets an attacker's script run inside a trusted website in your browser. Here is how it works and how developers prevent it.

4 min readIntermediate Dec 4, 2026

Explain Like I'm Not a Hacker

A community noticeboard where the building manager reads out every note as an official instruction. A stranger pins a note saying 'everyone who hears this, hand your keys to the man at the back door', and people comply.

The 30-second explanation

Browsers trust the scripts a website sends them. Cross-site scripting happens when a site lets a stranger slip a script into its pages, for example through a comment box or a search result, and then serves it to everyone else as if it were the site's own. The visitor's browser has no way to tell the difference and runs it. From there the script can act as the visitor on that site: read what is on the page, send requests as them, or steal the login session that proves who they are.

How it works

  1. 1

    1. Plant

    The attacker finds somewhere the site accepts input and shows it back, such as a comment, profile field or search box.

  2. 2

    2. Serve

    The site includes that input in a page without making it safe, so it is delivered as part of the page's code.

  3. 3

    3. Run

    A visitor loads the page, and their browser runs the script because it appears to come from the trusted site.

  4. 4

    4. Abuse

    The script acts as the visitor, for example by reading page content, making requests on their behalf or sending out a session token.

Every web page is a mix of fixed content from the developer and variable content from somewhere else: a username, a comment, a search term. If the developer drops that variable content into the page exactly as it arrived, any code hidden in it comes along. The browser sees one page from one trusted site and runs all of it. That is the flaw. Stored XSS is the worst case, because the bad content sits in the site's database, say in a blog comment, and runs in the browser of everyone who views it. Reflected XSS bounces through a link, where the attacker crafts an address that contains the script and tricks people into opening it. A third kind happens only inside the page's own JavaScript. Because the script runs as the site, it can do whatever the logged-in visitor could do, and it can read data on the page. Compared with SQL injection, which attacks the database, XSS attacks the site's visitors. Prevention is mostly the developer's job. Encode everything that comes from outside before it goes into a page, use frameworks that do this by default, add a Content Security Policy that limits which scripts may run, and mark session cookies so scripts cannot read them.

Real-world example

A blog lets readers post comments and displays them as raw HTML. An attacker posts a comment containing a small script. Every reader who opens that article now runs the script in their browser. Because the script runs as the blog, it can reach whatever the blog can, including the session of a logged-in administrator who happens to read the comments. The fix is small. The developer encodes comments before displaying them, so the script appears as harmless text, and adds cookie settings that stop scripts reading the session.

How to spot it

  • User input shown back unchanged

    Comments, names or search terms appear on a page exactly as typed, including symbols that should have been encoded.

  • HTML allowed where plain text is expected

    Fields that accept formatting but are not filtered by a trusted sanitising library.

  • No Content Security Policy

    The site sets no rules about which scripts the browser may run.

  • Session cookies readable by scripts

    Login cookies missing the HttpOnly flag, so a script can read and send them.

What to do

  1. 1Encode all untrusted data for the place it appears in the page, and use a framework that escapes output by default.
  2. 2Add a strict Content Security Policy and set session cookies with HttpOnly, Secure and SameSite.
  3. 3Sanitise any user-supplied HTML with a well-tested library, and test your own pages for injection before attackers do.

Stay curious. Stay safer.

This is one piece of a bigger picture. Explore more real-world examples, concepts and tips to build your cybersecurity awareness.

Explore More

Keep reading