Start learning free

LESSON 56 OF 76 · SECURITY: SECURE THE WEB APP

XSS in Attributes and Links

Practice

You already escape what visitors type with html.escape, so a review can't slip a <script> tag into the page. This page also links each reviewer's name to their website. A value that goes inside an attribute, like href, has two more ways to run code.

No quotes: in <a href=…> without quotes, the value ends at the first space, and the rest becomes new attributes. A website of https://x.example onmouseover=alert(1) adds an onmouseover that runs when the mouse passes over the name. The scheme: a website of javascript:alert(1) has nothing for html.escape to change, yet clicking the link runs it. Either way, the attacker's script runs on the shop's own page in every visitor's browser, where it can read the visitor's session cookie and act as them.

The fix has two parts. First, put every attribute value in double quotes. html.escape already turns a " inside the value into &quot;, so nothing can close your quotes early. Second, keep an allowlist of link schemes. urlparse reads a URL's scheme the way a browser does, in small letters and without leading spaces:

from urllib.parse import urlparse

urlparse("https://shop.example/mugs").scheme    # 'https'
urlparse(" JavaScript:alert(1)").scheme        # 'javascript'

photo = '<img src="/mug.png" alt="' + html.escape(caption) + '">'

The check posts five reviews and reads the product pages. A page comes back as one line. Here it is split up by review:

POST /products/1/reviews  {"name": "Ana", "website": "https://ana.example.com", …}  →  201  {"id": 1}
POST /products/1/reviews  {"name": "Mallory", "website": "https://mallory.example onmouseover=alert(document.cookie)", …}  →  201  {"id": 2}
POST /products/1/reviews  {"name": "Max", "website": "javascript:alert(document.cookie)", …}  →  201  {"id": 3}
POST /products/1/reviews  {"name": "Kim", "website": " JavaScript:alert(document.cookie)", …}  →  201  {"id": 4}
GET /products/1  →  200
    <h1>Mug</h1>
    <p><a href="https://ana.example.com">Ana</a>: Keeps my tea hot.</p>
    <p><a href="https://mallory.example onmouseover=alert(document.cookie)">Mallory</a>: Nice mug</p>
    <p>Max: Click my name for a coupon!</p>
    <p>Kim: Same here</p>
POST /products/6/reviews  {"name": "Ben & Co", "website": "http://ben.example.com/tea?page=2&lang=en", "text": "Pours <b>well</b>"}  →  201  {"id": 5}
GET /products/6  →  200
    <h1>Teapot</h1><p><a href="http://ben.example.com/tea?page=2&amp;lang=en">Ben &amp; Co</a>: Pours &lt;b&gt;well&lt;/b&gt;</p>
GET /products/99  →  404  Product not found

Your task: in link, put the escaped href in double quotes. Then add a list WEB_SCHEMES with "http" and "https" and read the website's scheme with urlparse. When the scheme isn't in the list, return only the escaped name, with no link around it.


Assignment
Follow the Coding Tutorial to quote the link's address and allow only http and https links.


Hint
urlparse(url).scheme is the part before the first :, already in small letters. not in is true when a list doesn't hold a value.


STEP 1 OF 5
    Ctrl + Enter to check