Cross-Site Scripting (XSS)
Chapters and transcript
Cross-Site Scripting What if a website could be tricked into running an attacker's code in your browser? That is cross-site scripting, or XSS. The attacker never touches your laptop. The website delivers the code itself. Let us see how it works on our practice guestbook, and how developers stop it.
How XSS happens XSS happens when a website takes something a user typed, and puts it into a page as HTML, without making it safe. A user posts a message. The website saves it, and later puts it into the page for every visitor. If that message contains JavaScript, the visitor's browser runs it.
A simple example Compare two messages. A normal message is just text, wrapped in a div, and shown as it is. Now an attacker posts an image tag instead. The browser tries to load an image called x, fails, and runs the onerror code. The attacker's JavaScript is now running on the website.
XSS in action Now the laptop, on our practice guestbook. I type an image tag that points to a missing image, with an onerror test. I click Post. The browser cannot load the image, runs the onerror code, and a pop-up appears. That pop-up is harmless. It only proves that script runs.
Why it is serious Real attackers do much more than a pop-up. The script runs inside the website, in the victim's browser. It can steal session cookies that are not HttpOnly, act as the user, like changing their email, or show a fake login form to collect passwords.
Three types There are three types of XSS. Stored XSS is saved on the site, in comments, profiles or messages, and shown to everyone. Reflected XSS is sent in a link, and reflected back in the page. And DOM-based XSS happens when the site's own JavaScript puts untrusted data into the page, for example with innerHTML.
Quick check Quick check. Think back to the guestbook demo. In stored XSS, where does the script live? Take a moment, then choose.
How testers look Testers work step by step. First, find every place where input is shown back on a page. Next, enter harmless HTML, like bold tags, and see if the text turns bold. Only if HTML is rendered, try a test payload, like the image tag you saw. And only on systems you are allowed to test.
The fix The main defence is to encode output. Here is the fixed guestbook, with the same message. The less-than sign is sent as ampersand, l, t, semicolon. The browser displays it as text, so nothing runs. Modern frameworks such as React do this by default.
How to defend Add more layers. Encode output, as you just saw. Avoid dangerous functions like innerHTML with user input. If you must allow some HTML, sanitise it with a trusted library such as DOMPurify. Add a Content Security Policy header to limit which scripts can run. And mark session cookies as HttpOnly.
Putting it together Let us put it together. XSS happens when input is put into a page as HTML without being made safe. The script runs in the victim's browser, inside the website. It can be stored, reflected or DOM-based. The main defence is output encoding, with sanitising, CSP and HttpOnly cookies as extra layers. Now read the lesson below, and try the lab.
Cross-Site Scripting (XSS) happens when a website takes something a user typed and puts it into a page as HTML, without making it safe. If that input contains JavaScript, the browser runs it.
A simple example
A guestbook shows each message like this:
<div class="message">Great service, thank you!</div>
An attacker posts this instead:
<img src=x onerror="alert('XSS')">
The website puts it into the page as it is. The browser tries to load an image called x, fails, and runs the onerror code. The attacker's JavaScript is now running on the website.
Why this is serious
The script runs inside the website, in the victim's browser. Depending on how the site is built, an attacker could:
- Steal session cookies (if they are not marked
HttpOnly) and take over accounts - Perform actions as the user: change their email, send messages, make purchases
- Show a fake login form to collect passwords
- Change what the page says
alert() is only used by testers to prove that script runs. Real attackers do much more.
Three types of XSS
| Type | Where the script comes from |
|---|---|
| Stored | Saved on the site (comments, profiles, messages) and shown to everyone |
| Reflected | Sent in a link, for example /search?q=<script>..., and reflected back in the page |
| DOM-based | The site's own JavaScript puts untrusted data into the page, for example with innerHTML |
How testers look for XSS
- Find every place where input is shown back on a page
- Enter harmless HTML such as
<b>test</b>and see if it turns bold - If HTML is rendered, try a test payload such as
<img src=x onerror=alert(1)>
How to defend
- Encode output: show
<as<so the browser displays it as text. Modern frameworks such as React do this by default. - Avoid dangerous functions such as
innerHTMLand React'sdangerouslySetInnerHTMLwith user input - If you must allow some HTML, sanitise it with a trusted library such as DOMPurify
- Add a Content Security Policy (CSP) header to limit which scripts can run
- Mark session cookies as HttpOnly
In the lab, you will make a guestbook run your code.
HANDS-ON LAB
Guestbook XSS
The guestbook shows your message as HTML. Make it run JavaScript.
Check your understanding
Answer every question correctly to complete this lesson. If you miss one, try the quiz again.
1. In stored XSS, where does the malicious script live?
2. What can an attacker do with XSS?
3. What is the main defence against XSS?
Locked: answer every quiz question above correctly to complete this lesson.
Spotted a mistake or something unclear? Report a mistake