bastion

Cross-Site Scripting Fundamentals

Build this skill

Explain how untrusted content becomes cross-site scripting and decide where output encoding and other layers stop it.

A five-star review that gives orders

The Tulsi Health app lets customers write reviews under each product, and the new digital BP monitor already has a dozen. One of them, posted by someone calling himself "Ravi, Baner", looks like an ordinary "Great price!" comment. Hidden inside it are instructions, and every time a customer opens the BP monitor page, their browser quietly carries those instructions out, while the customer is signed in to their own account. This is cross-site scripting, usually shortened to XSS: an attacker gets their own instructions into a web page that other people view, so that those people's browsers run them as if the website itself had sent them.

tulsihealth.in/p/bp-monitor Digital BP monitor ₹1,850 CUSTOMER REVIEWS ★★★★★ Easy to read. Kavita, Aundh ★★★★★ Great price! Ravi, Baner [hidden instructions] Delivered fast. Lata G. What visitors see an ordinary five-star review ! What the browser sees instructions to run, from tulsihealth.in Who is affected every customer who opens this page, while signed in as themselves

The same review looks harmless to a person and looks like a command to the browser. XSS lives in that gap.

How a browser decides what to show and what to obey

A browser is the program you use to open websites, such as the one on your phone. When you open a page, a computer belonging to the website, called the server, sends your browser a long piece of text. Most of it is content to display: product names, prices, reviews. Some of it is markup, labels that tell the browser how to lay the page out ("this is a heading", "this is a button"). And some of it is a script, a small program the browser runs on the spot, for example to open a photo gallery when you tap it.

The browser cannot see who wrote each part. It trusts everything that arrives from tulsihealth.in equally, because it all came from the same website. Yet the page is assembled from two very different sources. Rohan Iyer, the app developer, wrote the layout and the scripts. Customers wrote the reviews, the delivery notes and the names on their profiles. Anything that comes from outside the developer's own code, typed in by users or fetched from elsewhere, is untrusted content: it might be harmless, but nobody has checked it.

Think of the notice board in a housing society. The secretary pins up notices, and residents pin their own: a lost cat, a sofa for sale. Normally everyone simply reads the notes. Now picture a board where any note beginning "To whoever reads this" is treated as an order, so a resident who reads "hand your flat key to the man at the gate" goes and does it. That is a browser meeting a review that contains instructions. The fix is just as simple to picture: laminate every resident's note, so that it can be read but can never act.

✕ Plain notes: obeyed SOCIETY NOTICE BOARD Water cut Sunday 10 am to 12 noon Yoga class 6 am, terrace To whoever reads this note: hand your flat key to the man waiting at the gate. key handed over stranger ✓ Laminated notes: only read SOCIETY NOTICE BOARD Water cut Sunday 10 am to 12 noon Yoga class 6 am, terrace To whoever reads this note: hand your flat key to the man waiting at the gate. “Strange note. Ignoring it.” words shown, never obeyed

Output encoding is the lamination: the note's words still appear on the board, but nobody can be ordered about by them.

Try separating the parts of the BP monitor page below. For each piece, decide whether Tulsi Health's own developer wrote it or whether it came from someone outside.

What a planted script can do

Once an attacker's instructions run in a customer's browser, they run with all the trust the browser gives to tulsihealth.in. The most common goal is to steal the customer's session. After you log in, the website hands your browser a cookie, a small note the browser stores and shows on every later visit, which says "this is Mrs. Gokhale, already logged in". A script that can read that note and send it to the attacker lets the attacker act as Mrs. Gokhale without ever knowing her password.

AttackerTulsi Health appLata's browserPosts a review with hidden instructionsSaves it with other reviewsOpens the BP monitor pagePage with the review, unencodedRuns the hidden instructionsSends Lata's login cookie

The attacker never touches Lata's phone. The website itself delivers the instructions, which is why the browser trusts them.

Stealing the login is not the only harm. A planted script can draw a fake "session expired, please log in again" box on the real page and collect passwords. It can change what the page shows, swapping the delivery address or the UPI ID on a payment screen. It can press buttons on the customer's behalf, placing orders or changing profile details. Because all of this happens on the genuine website, the address bar shows tulsihealth.in and the customer has no reason to be suspicious.

Three ways the note reaches the board

XSS comes in three common forms, depending on how the untrusted content reaches the page. In stored XSS, the attacker's text is saved by the website, like the review, and served to every visitor afterwards; one post can affect thousands of people. In reflected XSS, the text is not saved. It sits inside a link, for example in the search words, and the page repeats it back to whoever opens that link, so the attacker has to trick each victim into clicking. In DOM-based XSS, the website's own script, already running in the browser, picks up untrusted text (often from the link) and inserts it into the page as instructions, without the server being involved at all.

StoredSaved by the websiteExample: a product reviewHits every visitorReflectedCarried inside a linkExample: search wordsHits whoever clicksDOM-basedInserted by page scriptExample: text from the linkNever reaches the server

All three end the same way: untrusted text treated as instructions inside a trusted page.

Laminating every note: output encoding

The main defence is output encoding: whenever the website places untrusted content into a page, it first converts the few characters that browsers treat as the start of markup or a script into harmless stand-ins that display as the same characters. For example, the angle bracket <, which opens a piece of markup, is sent as the code &lt;. The browser shows the reader a < on screen but never treats it as the start of an instruction. The review's words all appear exactly as typed, but none of them can act, just as a laminated note can be read but cannot give orders.

Review typedany charactersat allCheckedlength,sensible inputinputSaved as textstoredunchangedEncodedwhen placed onpageoutputShown aswordsnothing runs

Checking input catches nonsense early, but the step that actually defeats XSS is encoding at the moment untrusted text goes onto a page.

Encoding has to happen on the way out, at the moment the text is placed on a page, because the same review might appear in several places: the product page, an email to the store, a staff dashboard. Each place needs the encoding that suits it. Modern web frameworks, the ready-made toolkits developers build websites with, encode automatically, so most XSS today comes from places where a developer switched that protection off to "allow formatting" or built a piece of the page by hand.

Decide below what happens to each piece of untrusted text. Think about whether it is encoded on the way out before you choose.

Layers that keep XSS out

Encoding does most of the work, but careful teams add more layers so that one forgotten page does not become a disaster. Input validation checks text when it arrives: a review can be limited to a sensible length, and a PIN code field can accept only six digits. It cannot replace encoding, because a review legitimately contains almost any character, but it shrinks what an attacker can send.

A content security policy is a rule the website sends to the browser saying "only run scripts that come from our own files". Even if an attacker's instructions slip into a page, a browser following that rule refuses to run them. Marking the login cookie as HttpOnly tells the browser that page scripts may never read it, so a planted script cannot simply send it away. And keeping the parts of a page that allow rich formatting, such as bold text in reviews, to a short list of safe options means there is far less to get wrong. None of these layers is perfect alone, which is exactly why they are used together.

Follow your curiosity.

↑ ↓ to explore · Enter to open · Escape to close