Skip to content
FrontDepth, Home

Back to the topic

Practice: Cross-site scripting (XSS)

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

3 predict-output · 4 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    An HTML-escaping function is used in five places, once with the replacements in the wrong order. What does the code log, and which outputs are still dangerous?

    JavaScript
    function escapeHtml(text) {
      return text
        .replaceAll("&", "&")
        .replaceAll("<", "&lt;")
        .replaceAll(">", "&gt;")
        .replaceAll('"', "&quot;")
        .replaceAll("'", "&#x27;");
    }
    
    function escapeWrongOrder(text) {
      return text.replaceAll("<", "&lt;").replaceAll("&", "&amp;");
    }
    
    const review = `Great lamp <b>5/5</b> & "bright"`;
    console.log(escapeHtml(review));
    console.log(escapeWrongOrder("<b>"));
    
    const title = `x" onmouseover="steal()`;
    console.log(`<p title="${escapeHtml(title)}">`);
    console.log(`<p title=${escapeHtml("x onmouseover=steal()")}>`);
    console.log(`<a href="${escapeHtml("javascript:steal()")}">`);
  2. Open questionJunior

    What is XSS, and what can an attacker do once their script runs on your page?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  3. Predict the outputMiddle

    A "sanitiser" removes <script> elements with a regular expression. What does it return for each input, and which results would still run code if inserted with innerHTML?

    JavaScript
    function stripScripts(html) {
      return html.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, "");
    }
    
    console.log(stripScripts("<b>bold</b><script>steal()</script>"));
    console.log(stripScripts("<SCRIPT src=//evil.example/x.js></SCRIPT>ok"));
    console.log(stripScripts('<img src=x onerror="steal()">'));
    console.log(stripScripts("<scr<script></script>ipt>steal()</script>"));
    console.log(stripScripts('<a href="javascript:steal()">win</a>'));
  4. Open questionMiddle

    User comments should support bold text, line breaks and links to other pages of the site. How would you render them safely in React?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  5. Predict the outputSenior

    safeHref checks the scheme with the URL API and an allow-list; naive checks the string prefix. What does the code log?

    JavaScript
    const ALLOWED = new Set(["http:", "https:", "mailto:"]);
    
    function safeHref(input) {
      let url;
      try {
        url = new URL(input, "https://shop.example/product/lamp-l3");
      } catch {
        return "about:blank";
      }
      return ALLOWED.has(url.protocol) ? url.href : "about:blank";
    }
    
    const naive = (input) => (input.startsWith("javascript:") ? "about:blank" : input);
    
    console.log(safeHref("https://example.org/review"));
    console.log(safeHref("/help#returns"));
    console.log(safeHref("JavaScript:alert(1)"));
    console.log(safeHref(" java\tscript:alert(1)"));
    console.log(JSON.stringify(naive(" java\tscript:alert(1)")));
    console.log(safeHref("data:text/html,<b>hi</b>"));
    console.log(new URL(" java\tscript:alert(1)").protocol);
  6. Open questionSenior

    How would you introduce a strict Content Security Policy into an existing app with inline scripts, inline event handlers and third-party tags?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  7. Open questionSenior

    A report-only CSP suddenly reports an inline event handler and requests to an unknown host, all from one page that shows user-generated content, and a user says their account details changed without their action. Walk through what you do.

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.