Lesson 5 of 7
Cross-site scripting
Listen to an AI summary of Cross-site scripting
A review on the sports shop
The sports shop website lets a customer write a review of a product. The shop saves the review and shows the review to every later visitor.
A tag is a word in angle brackets in HTML that says what one piece of the page is. The tag <p> starts a paragraph of text, and </p> ends the paragraph.
The server builds the product page with four lines of Python. Look at how line 3 builds the HTML.
Line 3 joins three strings with +. The programmer wrote the two tags, and a customer wrote the review in the middle. For the review Warm and a good fit, the browser draws one paragraph with those words.
A review that contains a script
A script is a piece of JavaScript inside a page, which the browser runs when the page loads. In HTML, a script goes between the tags <script> and </script>.
An attacker writes the review Great hoodie <script>alert(1)</script>. The JavaScript alert(1) opens a small box that shows the number 1.
<p>Great hoodie <script>alert(1)</script></p>
- <p>the programmer wrote
- Great hoodie <script>alert(1)</script>the attacker's review
- </p>the programmer wrote
The browser cannot tell which tags the programmer wrote. The browser shows the words Great hoodie, reads <script> as a tag and runs the JavaScript, for every visitor to the product page.
A real attacker's script can run without showing anything. The script can read the page, change what the visitor sees, and send requests as the signed-in visitor.
What is cross-site scripting
Cross-site scripting (XSS, CWE-79) is untrusted data written into a page so that the browser runs the data as a script.
Stored and reflected
Stored cross-site scripting is a script that is saved and then written into the page for every later visitor. The review on the sports shop is stored cross-site scripting.
Attacker
writes a review that contains a script
Shop's server
saves the review in the database
Product page
line 3 joins the review into the HTML
Every visitor
the browser runs the script
Reflected cross-site scripting is a script that arrives in a request and is written straight back into the response.
The search page of the sports shop writes the search word into the page, as in Results for hoodie. A link to the search page holds the search word, as in /search?name=hoodie. An attacker makes a link with a script in place of hoodie and sends the link to a victim.
Attacker
sends the victim a link with a script in it
Victim's browser
the victim clicks the link
Shop's server
saves nothing, and writes the script into the response
Victim's browser
runs the script, for this one victim
A third form, where the script never reaches the server, is taught in the next lesson.
What is output encoding
In HTML, the characters < and > mark the start and the end of a tag. Output encoding is replacing the characters that have a meaning in HTML, so that the browser shows the characters as text. The text < replaces <, and the text > replaces >.
The Python function html.escape does output encoding on a string. The safer product page changes line 3:
page = "<p>" + html.escape(review) + "</p>"The browser draws < as the character < and never reads < as the start of a tag.
Output encoding switched off
Many frameworks do output encoding on all data written into a page. Each framework also has a switch that turns output encoding off.
React is a framework for building pages with JavaScript. The switch in React is named dangerouslySetInnerHTML, as in the line of React code:
<div dangerouslySetInnerHTML={{ __html: review }} /><div> is a tag for one block of a page, and the part in curly brackets gives React the string review to write. React writes the string into the block as HTML, with no output encoding. The word dangerously in the name warns the programmer and the reviewer.
Every framework has a name of its own for the switch:
- React
dangerouslySetInnerHTML- Vue
v-html- Angular
bypassSecurityTrustHtml- Razor
Html.Raw- Jinja2
| safeand{% autoescape false %}
Some pages have to show HTML that a user wrote, such as bold words in a review. Safer code first passes the HTML through a sanitiser, a step that removes the dangerous parts. DOMPurify is a sanitiser written in JavaScript.
Where the data is written
Replacing < and > protects text between two tags. Other places in a page need a different defence.
An attribute is a named setting inside a tag, such as href, which holds the address inside a link. The tag <input value="hoodie"> draws the search box of the sports shop with the word hoodie in the box. value is an attribute, and a pair of " characters marks the start and the end of the attribute.
The search page writes each search word into the attribute. An attacker searches for "><script>alert(1)</script>.
<input value=""><script>alert(1)</script>">
- <input value="the programmer wrote
- "><script>alert(1)</script>the search word from the attacker
- ">the programmer wrote
The " from the attacker ends the attribute, and the > ends the tag. The browser then runs the script. The function html.escape also replaces " with the text ", so the attacker's " cannot end the attribute. Safer code also keeps " characters around every attribute, because a space in the data ends an attribute that has none.
Untrusted data between <script> tags becomes part of the JavaScript, and HTML output encoding does not protect the data. An address in a link that starts with javascript: runs a script, as the next lesson shows.
What is a Content Security Policy
A Content Security Policy is a header in a response that tells the browser which scripts are allowed to run.
A script typed into a review is not a file from the shop's website. The browser refuses to run the script. The policy limits the damage where a line of code has no output encoding.
Examples of what to look for
- A value from a request or the database joined into HTML with no output encoding.
- A name that switches output encoding off, such as
dangerouslySetInnerHTML,v-html,Html.Rawor| safe. - Untrusted data in an attribute with no
"characters around the attribute. - Untrusted data between
<script>tags. - HTML that a user wrote shown as HTML, with no sanitiser.
Check your understanding
State the difference between stored and reflected cross-site scripting.
Show the answer
A stored script is saved and written into the page for every later visitor. A reflected script arrives in a request and is written straight back into the response.
Identify the line of the product page that joins the review into the HTML. State what the browser does with a
<script>tag in the review.Show the answer
Line 3. The browser reads
<script>as a tag and runs the JavaScript between the two tags.Give one reason why React named a switch
dangerouslySetInnerHTML.Show the answer
The switch writes a string into the page as HTML with no output encoding. The word
dangerouslyis a warning.
Practise it
Challenges with a weakness this lesson teaches.
- Gift photo uploadsGiftwrap, BeginnerOpen
- Product ratingsSwiftcart, IntermediateOpen
- Post and renderChirp, IntermediateOpen
- Link previewsChirp, IntermediateOpen
- Avatar uploadChirp, IntermediateOpen
- Mentions and notificationsChirp, IntermediateOpen
- Comments on homeworkGradebook, BeginnerOpen