Full Stack Developer

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.

55 min

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 &amp;    < becomes &lt;    > becomes &gt;    " becomes &quot;    ' becomes &#39;

Order matters: replace the ampersand first. If you replace it last, the & in &lt; that you just made would become &amp;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…

🔒 The debrief unlocks when you close the case. Finish the lab above.

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

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

📝

Knowledge Check

1 of 3

Why must the ampersand be replaced first when escaping HTML?

Report a problem

About: /lessons/case-the-safe-page