Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Cybersecurity Essentials — Alpha School

Lesson 14: Cross-Site Scripting (XSS) and Cross-Site Request Forgery

Two of the most common web attacks do not break into a server at all: they trick a visitor's browser into doing something the visitor did not intend. Cross-site scripting, or XSS, smuggles a script into a page so that it runs in other visitors' browsers, and cross-site request forgery, or CSRF, tricks a visitor's browser into making a request they did not intend, such as changing their email address. Both attacks exploit trust: the browser trusts the site, and the site trusts the browser, and the attacker stands in the middle. This lesson explains how each one works at a level that lets you defend against it, and the defences are straightforward once you see the trick.

XSS: a script that is not yours

XSS happens when a site shows visitor input back to other visitors without escaping it. A worked example on a fictional practice site: a comment box lets a visitor post a comment, and the site displays it by inserting the text directly into the page. An attacker posts a comment containing a script element, and when other visitors load the page, their browsers run that script, because it came from a site they trust. The script runs with the visitor's permissions, so it could read the page, send data to the attacker, or perform actions as the visitor. The fix is to escape output, so the browser shows the text as text, or better, to use a framework that escapes by default, and to set a Content Security Policy, a header that tells the browser which sources scripts may come from, so an injected script from an unexpected source is blocked even if it gets into the page.

Stored, reflected and DOM-based XSS

XSS comes in three flavours, and it helps to know them apart. Stored XSS is the example above: the malicious input is saved, in a comment or a profile, and runs every time someone views it, which makes it the most dangerous kind. Reflected XSS is not saved: the input is reflected straight back in a response, such as in a search results page that says “Results for: ...” with the search text unescaped, and it only works if the victim clicks a crafted link. DOM-based XSS happens entirely in the browser, when JavaScript takes untrusted data, such as part of the URL, and inserts it into the page unsafely. The defence is the same in all three cases: treat untrusted data as text, never as code, escape it on output, and use a Content Security Policy as a safety net, because a defence in depth, with more than one layer, survives a single mistake.

CSRF: a request you did not make

CSRF works differently. Imagine you are logged into a fictional practice bank site, and you visit a malicious page in another tab. That page contains a hidden form that submits a request to the bank, such as changing your email address, and your browser sends it along with your login cookie, because that is what browsers do: they attach cookies to requests for a site, whatever page the request came from. The bank sees a valid login and a well-formed request, and it obeys, even though you never intended it. The fix is a CSRF token: a secret, unpredictable value that the server puts into each form and checks on submission, which the attacker's page cannot know, so its forged request fails. Modern browsers also help with the SameSite attribute on cookies, which tells the browser not to send a cookie with requests from other sites, and both defences together make CSRF very hard.

Defence in depth for the web

The defences in this lesson and the last one share a pattern worth naming: defence in depth, which means layering several protections so that no single failure is enough. For XSS, the layers are escaping output, a Content Security Policy, and keeping frameworks updated. For CSRF, the layers are CSRF tokens and SameSite cookies. For injection, the layers are parameterised queries, validation and least privilege. A site with one layer can be broken by one mistake, while a site with several layers needs an attacker to defeat them all at once, which is far harder. When you build the web applications in the web development course, apply these layers from the start, because adding security after a site is built is slower, more expensive, and more likely to miss something than building it in from the beginning.

Key Terms

Cross-site scripting (XSS)
Smuggling a script into a page so it runs in other visitors' browsers.
Stored XSS
XSS where the malicious input is saved, such as in a comment, and runs for every viewer.
Reflected XSS
XSS where the input is reflected straight back in a response, and only works if the victim clicks a crafted link.
Cross-site request forgery (CSRF)
Tricking a visitor's browser into making a request they did not intend, such as changing their email.
CSRF token
A secret, unpredictable value in each form, checked on submission, which a forged request cannot provide.
Content Security Policy
A header telling the browser which sources scripts may come from, blocking injected scripts.
SameSite
A cookie attribute telling the browser not to send the cookie with requests from other sites.

Discussion Questions

  1. Why does escaping output stop XSS? What would happen if a comment containing a script element were shown unescaped?
  2. How does a CSRF token prove a request really came from the site's own form?
  3. Why is defence in depth better than relying on a single protection?

Practice Questions

  1. What is cross-site scripting?
  2. What is the difference between stored and reflected XSS?
  3. How does a CSRF attack work?
  4. What is a CSRF token, and how does it stop the attack?
  5. What does a Content Security Policy do?
Show answers
  1. Smuggling a script into a page so it runs in other visitors' browsers. The script runs with the visitor's permissions because it came from a site they trust.
  2. Stored XSS is saved and runs for every viewer; reflected XSS is not saved and needs the victim to click a crafted link. Stored XSS is the more dangerous kind.
  3. A malicious page makes your browser send a request to a site you are logged into. The browser attaches your login cookie, so the site obeys a request you never intended.
  4. It is a secret, unpredictable value in each form, checked on submission. An attacker's page cannot know the token, so its forged request fails the check.
  5. It tells the browser which sources scripts may come from. An injected script from an unexpected source is blocked, even if it gets into the page.
Keep reading to complete this lesson