Skip to content

Lesson 5 of 7

Cross-site scripting

Listen to an AI summary of Cross-site scripting

0:00 / 0:34

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.

  1. def product_page():
  2. review = load_review()# loads a review that a customer sent in and the shop saved
  3. page = "<p>" + review + "</p>"# joins the review into the HTML
  4. return page# returns the page, and the server sends the page to the browser
Line 3 joins the review into the HTML of the page.

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>

  1. <p>the programmer wrote
  2. Great hoodie <script>alert(1)</script>the attacker's review
  3. </p>the programmer wrote
The HTML that line 3 builds from the attacker's review.

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.

  1. Attacker

    writes a review that contains a script

  2. Shop's server

    saves the review in the database

  3. Product page

    line 3 joins the review into the HTML

  4. Every visitor

    the browser runs the script

The path of a stored 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.

  1. Attacker

    sends the victim a link with a script in it

  2. Victim's browser

    the victim clicks the link

  3. Shop's server

    saves nothing, and writes the script into the response

  4. Victim's browser

    runs the script, for this one victim

The path of a reflected script.

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 &lt; replaces <, and the text &gt; 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 &lt; as the character < and never reads &lt; as the start of a tag.

  1. <p>Great hoodie &lt;script&gt;alert(1)&lt;/script&gt;</p><!-- the HTML the server sends -->
  2. Great hoodie <script>alert(1)</script><!-- the text the browser shows on the page -->
The attacker's review after output encoding. No script runs.

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
| safe and {% 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>">

  1. <input value="the programmer wrote
  2. "><script>alert(1)</script>the search word from the attacker
  3. ">the programmer wrote
The HTML for the search box after the attacker's search.

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 &quot;, 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.

  1. Content-Security-Policy: script-src 'self'run only the scripts in files from the shop's own website
A Content Security Policy in a response from the sports shop.

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.Raw or | 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

  1. 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.

  2. 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.

  3. 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 dangerously is a warning.

Practise it

Challenges with a weakness this lesson teaches.