Lesson 6 of 7
Unsafe sinks in the browser
Listen to an AI summary of Unsafe sinks in the browser
What is the DOM
A tag, a word in angle brackets such as <p>, marks each piece of the page.
The browser reads the HTML and builds the DOM, the browser's own copy of the page. Code in the page can read and change the DOM. DOM is short for Document Object Model.
JavaScript in a page can change the DOM, and the screen shows the change.
A greeting on the sports shop
The welcome page of the sports shop greets each visitor by name. The name is in the URL, as shop.example/welcome?name=Sam. The part after the question mark, name=Sam, is a query parameter.
Four lines of JavaScript in the welcome page write the greeting. Follow the name from line 1 to line 4:
var params = new URLSearchParams(location.search); var name = params.get("name"); var box = document.getElementById("greeting"); box.innerHTML = "Hello " + name;
The word var makes a variable. Line 1 uses URLSearchParams to read the query parameters from location.search, the part of the URL after the question mark. The part of a URL after a #, read with location.hash, is a source as well. Line 2 stores the text of the query parameter name in the variable name.
document is the JavaScript name for the DOM. Line 3 uses getElementById to find the piece with the id greeting, and stores the piece in box. Line 4 joins Hello and the name, and writes the string to box.innerHTML, explained next.
What innerHTML does
innerHTML is the JavaScript name for the contents of one piece of a page. The browser reads a string written to innerHTML as HTML. A tag in the string becomes a new piece of the page. outerHTML, insertAdjacentHTML and document.write read a string as HTML in the same way.
textContent is a second way to set the same contents. The browser shows a string written to textContent as text.
A name that contains code
The name in the URL is untrusted data. An attacker sends a customer a link to the real sports shop, with an image tag as the name:
shop.example/welcome?name=<img src=x onerror=alert(1)>The customer opens the link, and line 4 writes the tag to innerHTML. The tag <img> tells the browser to load an image, and no image named x exists. The part onerror holds JavaScript that the browser runs when an image fails to load.
alert(1) shows a box with the number 1, so the attacker's code has run. A real attack can read the customer's orders or change the page. A <script> tag, the usual tag for JavaScript, does not run when written through innerHTML. The attacker uses an image tag instead.
The source and the sink
Line 1 is the source and line 4 is the sink.
In the greeting, the whole path from the source to the sink is inside the browser.
DOM-based cross-site scripting (CWE-79) is JavaScript in the page reading untrusted data and writing the data into the DOM as HTML. The lesson before covers the same attack on a page that a server builds.
No code on the server writes the name into the page, so the fix belongs in the JavaScript.
The safer line
The safer greeting changes one word in line 4:
box.textContent = "Hello " + name;The browser now shows the attacker's tag as text, and no image loads.
A page that has to show HTML a person wrote passes the HTML through a sanitiser first, such as DOMPurify. The sanitiser is the filter between the tap and the drain.
Strings that run as code
eval in JavaScript runs a string as program code, as eval in Python does. The ticket page of the sports shop reads a quantity from the URL and works out a price:
var quantity = params.get("quantity"); var price = eval("4 * " + quantity);
A quantity of 2 gives a price of 8. A quantity of alert(1) runs the attacker's code, so eval is a sink.
Output encoding replaces characters that have a meaning in HTML, such as <, so the browser shows the characters as text. eval reads the string as JavaScript, and alert(1) has no such character, so output encoding changes nothing.
Safer code turns the string into a number, as in 4 * Number(quantity). setTimeout, setInterval and new Function can also run a string as JavaScript.
Links that run code
A link holds its address in a part named href. A javascript: URL is a link address that makes the browser run JavaScript in place of opening a page. A click on a link with the address javascript:alert(1) runs alert(1).
Safer code checks that an address from untrusted data starts with http:// or https:// before writing the address to href.
Examples of what to look for
- A new line of JavaScript that writes a string built from the URL to
innerHTML,outerHTML,insertAdjacentHTMLordocument.write. - A string from the URL passed to
eval,setTimeout,setIntervalornew Function. - An address from untrusted data written to
hrefwith no check of how the address starts. - A hand-written check that removes
<script>tags, in place of a sanitiser.
Three example searches for JavaScript in the folder shop:
Check your understanding
State what the DOM is.
Show the answer
The browser's own copy of a page, which code in the page can read and change.
Identify the source and the sink in the four lines of the greeting.
Show the answer
Line 1 is the source, which reads the URL. Line 4 is the sink, which writes to
innerHTML.Give one reason why output encoding does not help when a string is passed to
eval.Show the answer
evalreads the string as JavaScript, and an attack such asalert(1)has no character that output encoding changes.
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