Case File: The Safe Page
Customers type text, and pages are built from text, so the two can be confused. Write the small set of functions that turn parcels into HTML safely: escape what users typed, keep what you wrote, build a whole page and handle the empty list. The real TypeScript compiler checks your types and real inputs, including attacks, run through your code. Generated for you alone.
Sabi Logistics · Launch 1
The Safe Page
A customer typed a script into the note field, and every dispatcher who opened the tracking page ran it
Amara New hire · Phone call
Amara. Something odd happened this morning. A dispatcher opened the tracking page and a pop-up told her to “click here to claim a prize”. Ngozi found the cause in a parcel’s note field: a customer had typed a few lines of JavaScript into it, our page printed that text straight into the HTML, and the browser did what browsers do with HTML: it ran it.
Ngozi calls this cross-site scripting. The page that shows parcels is built by gluing strings together, and I have been asked to fix it before the rest of the app follows the same pattern. I need four small functions that build the page, and I need them to treat everything a person typed as text to show, never as code to run.
The brief
What you need to know first.
- A web page is HTML text. A browser reads it and treats anything between angle brackets as structure (a tag), and certain tags (
<script>) or attributes (onerror=) as code to run. If you build the page by joining strings, any text a user typed that contains<,>,"or&can change the structure of your page. That is cross-site scripting (XSS), one of the most common ways websites are attacked. - The fix is to escape user text when you put it into HTML: replace the characters that mean something with their entities, which display as the character but are not interpreted:
& becomes & < becomes < > becomes > " becomes " ' becomes 'Order matters: replace the ampersand first. If you replace it last, the & in < that you just made would become &lt;. Escaping a text twice shows the escapes to the user, escaping once is right, and escaping never is the bug.
- Escape at the point of output, into the context where the text lands. This lab is about HTML text and HTML attributes (which is why the quotation mark is escaped too: an id such as
"><img onerror=...>would otherwise close the attribute and add a new tag). Other contexts (a URL, a piece of JavaScript, CSS) need different rules. - Not everything is escaped. The parts you wrote (the tags) and the numbers you formatted stay as they are. A body that is already HTML (built by your own functions) must go in untouched, or it would be escaped into visible text. Knowing which strings are text and which are already HTML is the whole discipline: nothing user-typed ever reaches the page without passing through
escapeHtml. - A page function: a complete document needs a doctype, a language, a character set and a title. The title is user-influenced text, so it is escaped too (and appears twice in this lab).
- Handle the empty case deliberately: a list with no parcels shows a friendly message, not an empty
<ul>. - How the lab checks you. The file must compile with no
any; every function must have the exact signature; and a long list of real inputs, including attack strings, is run through your code and the reference solution: the results must match character for character. The exact format of each piece is in the starter’s comments.
The starter is generated for you, with your own field names and site name. When every check passes the lab shows a reference starting with GT{. Submit it at the bottom of the page.
Preparing your code…
Amara: “Your escaping stops the script in the note. But the same customer’s name appears inside a link: href="/customers/NAME". Does escapeHtml make that safe? What else must you think about when text goes into a URL?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Amara: “So a page is a string with two kinds of text in it, and the rule is that the user’s kind never gets to speak in the page’s voice.”
What a senior developer would do next. Do not rely on remembering. Use a template layer that escapes by default (most frameworks do) and make the unsafe path loud (innerHTML, {@html ...}, dangerouslySetInnerHTML are named to be noticed in code review). Add a Content-Security-Policy header that blocks inline scripts, as a second layer, so a missed escape does less damage. Test with a list of attack strings in every page that shows user text. And remember that escaping is for output: validate input as well (the next cases), but never treat validation as a replacement for escaping.
The traps. Escaping the ampersand last; escaping in the wrong context (a URL needs encoding, not HTML entities); escaping text that is already HTML; and trusting “it is only an id” for a value that came from a request.
Write the security fix note
Write the note describing the fix: what went wrong with the note field (how text became code), what escapeHtml replaces and why the ampersand is first, which strings are escaped and which are left alone (and why), how the page handles the empty list, and two more layers of defence you would add (such as a template layer that escapes by default and a Content-Security-Policy).
🧪 Want to practise this on your own computer? See how to set up your lab →