What it means
A browser trusts a page according to where it came from. Cross-site scripting, or XSS, abuses that trust. If a site places data from a user into a page as it is, any markup in the data becomes part of the page. A script in that data runs with the site's own standing in the viewer's browser. It can read what the viewer sees and send requests in their name. The fix is output encoding, which turns special characters into a form the browser displays as text. The encoding must suit the place the data lands. A Content Security Policy adds a second layer.
See the idea / Applications
Unencoded, a saved note runs as the site’s own script.
Open the same saved note in two versions of a page. One inserts the note as it is. The other encodes it for HTML first.
Meridian’s booking console shows each booking’s note to the operator on duty. A tenant saves a note on booking P-0412 that contains a script element. Dax, an operator, is signed in and opens that booking. Compare a page that inserts the note as it is with a page that encodes it first.
01 / 04
Save
A tenant saves a booking note that contains a script element. The database stores it as plain text. Nothing has run.
More about this step
Storing the note is not the flaw. A database column can hold any characters. The question is what the console does when it puts the note into a page.
Step through at your own pace.
Keep in mindOne note in one place, the body of an HTML element. Encoding must match the place the data lands. Attributes, links and script blocks each need their own encoding, and some places are unsafe for untrusted data whatever the encoding. A Content Security Policy is a second layer, not the fix. This is stored cross-site scripting. Reflected cross-site scripting, which arrives in a link, and DOM-based cross-site scripting, which page code builds, are not shown. This case extends the lesson’s booking system.
Read the visual, step by step
Inserted as is
- Save. A tenant saves a booking note that contains a script element. The database stores it as plain text. Nothing has run. Storing the note is not the flaw. A database column can hold any characters. The question is what the console does when it puts the note into a page.
- Build. Dax, the operator on duty, opens the booking. The server puts the note into the page exactly as saved, so the page now contains a real script element. The server builds the page by joining the note into its HTML. The angle brackets in the note arrive in the browser as markup, next to the markup the developer wrote.
- Run. Dax’s browser parses the page and runs the script. It cannot tell this script from the console’s own code. A browser trusts a page by its origin. This script arrived from the console’s origin inside the console’s page, so it gets the same trust as the console’s own scripts.
- Act. The script runs as Dax inside the console site. It can read what he can see and send requests that he could send. The tenant never signed in as Dax and never learned his password. The script borrows the session that Dax already has open. Marking the session cookie so that scripts cannot read it limits one kind of theft. It does not stop the script from sending requests.
Encoded for HTML
- Save. A tenant saves a booking note that contains a script element. The database stores it as plain text. Nothing has run. Storing the note is not the flaw. A database column can hold any characters. The question is what the console does when it puts the note into a page.
- Encode. Dax, the operator on duty, opens the booking. The server encodes the note for HTML first, so each angle bracket is sent as < or >. Output encoding replaces the characters that HTML treats as markup. The opening of the note is sent as <script>, which a browser displays as text. The saved note in the database is not changed.
- Show. The browser displays the note as text. The page contains no script element from the note, so nothing runs. Dax sees the note’s characters on screen, angle brackets included. The encoding matched the place the data landed, which here is the body of an HTML element.
- Read. Dax reads an odd-looking note and reports it. No script ran, and his session was not used. The attempt is still worth reporting, because someone tried it. The fix sits where the note meets the page, not in a filter on what tenants may type.
One note in one place, the body of an HTML element. Encoding must match the place the data lands. Attributes, links and script blocks each need their own encoding, and some places are unsafe for untrusted data whatever the encoding. A Content Security Policy is a second layer, not the fix. This is stored cross-site scripting. Reflected cross-site scripting, which arrives in a link, and DOM-based cross-site scripting, which page code builds, are not shown. This case extends the lesson’s booking system.
Flow references: OWASP: Cross Site Scripting Prevention Cheat Sheet
AN ILLUSTRATIVE SCENARIO
A note that runs in the operator's browser
Meridian's booking console shows each booking's note to the operator on duty. A tenant saves a note containing a script element. When Dax opens the booking, the page includes the note as saved, and his browser runs the script as if the console had sent it. With the note encoded for HTML first, Dax sees the characters on screen and nothing runs.
Put it to work
- Find every place where data from users or other systems is written into a page, including values that pass through a database first.
- Use your framework's default escaping, write text through APIs that do not parse markup, and treat any raw HTML insertion as needing review.
- Add a Content Security Policy that blocks inline script, and mark session cookies so that page scripts cannot read them.
How to check your work
Save a harmless marker made of angle brackets, quotes and an ampersand in each input field of a test environment. Open every page that displays it, whether the server or the browser builds the page. The marker must show on screen as typed, with its markup visible, and no test script may run. In pages the server builds, the source must show the angle brackets as < and >. Check that each response carries a Content-Security-Policy header.
Connect the ideas
- SQL injection
A flaw in which untrusted input is joined into the text of a database query, so the database reads part of the input as SQL. A parameterized query removes it by keeping input as a value.
- Session
State linking multiple requests; an authenticated session associates later requests with a previously authenticated identity until it expires or is revoked.
- WAF
A control that inspects web application traffic and applies rules to detect or block unwanted requests, often including known attack patterns.
- Trust boundary
A place where identity, network, or data assumptions change and a fresh check is required.