The Quote That Escaped the Link

Easy

· Frontend

Incident

SEV-1

BUG-HOVER

Priority: Critical (XSS) · reported by security

Time to fix: 10 min

Failing checks on the shipped build

  • a double quote cannot close the href

    expected "<a href=\"http://t.co/@&quot;onmouseover=&quot;x&quot;\">http://t.co/@&quot;onmouseover=&quot;x&quot;</a>" but got "<a href=\"http://t.co/@\"onmouseover=\"x\"\">http://t.co/@\"onmouseover=\"x\"</a>"

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the value the screen shows back through the state and the pure helpers that compute it; the view is usually only the messenger.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

Twitter · 2010

Modelled on the Twitter "onMouseOver" worm (21 September 2010): a tweet containing a link followed by an @ and a double quote broke out of the href attribute Twitter's auto-linker generated, adding an attribute of the author's choosing to the link. Merely hovering over such a tweet ran script, and self-retweeting versions spread across the site within hours. Twitter said it had fixed the bug the month before, and a later site update had brought it back.

linkify.js turns the URLs in a tweet into links and escapes everything else — but its escaping was written for text between tags, not for attribute values.

Fix linkify so neither the text nor a link can break out of its context.

Code map

var URL_RE = /https?:\/\/\S+/g;

function escapeHtml(s) {
  return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

exports.linkify = function (text) {
  var out = "";
  var last = 0;
  var m;
  URL_RE.lastIndex = 0;
  while ((m = URL_RE.exec(text)) !== null) {
    out += escapeHtml(text.slice(last, m.index));
    var url = escapeHtml(m[0]);
    out += '<a href="' + url + '">' + url + "</a>";
    last = m.index + m[0].length;
  }
  return out + escapeHtml(text.slice(last));
};

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

The Quote That Escaped the Link — JavaScript Bug Hunt

Modelled on the Twitter "onMouseOver" worm (21 September 2010): a tweet containing a link followed by an @ and a double quote broke out of the href…

  • Language: JavaScript
  • Layer: Frontend
  • Difficulty: Easy
  • Concepts: Security, Parsing
  • Modelled on: Twitter · 2010
  • Visible tests: a plain link is linkified; a double quote cannot close the href
  • Reward: 50 XP for a complete fix

Incident

  • Severity: SEV-1 (priority: Critical (XSS))
  • Ticket: BUG-HOVER
  • Reported by: security
  • Time to fix: 10 minutes, the target for an easy incident

Failing checks on the shipped build

  • a double quote cannot close the href — expected "<a href=\"http://t.co/@&quot;onmouseover=&quot;x&quot;\">http://t.co/@&quot;onmouseover=&quot;x&quot;</a>" but got "<a href=\"http://t.co/@\"onmouseover=\"x\"\">http://t.co/@\"onmouseover=\"x\"</a>"

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the value the screen shows back through the state and the pure helpers that compute it; the view is usually only the messenger.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Briefing

Modelled on the Twitter "onMouseOver" worm (21 September 2010): a tweet containing a link followed by an @ and a double quote broke out of the href attribute Twitter's auto-linker generated, adding an attribute of the author's choosing to the link. Merely hovering over such a tweet ran script, and self-retweeting versions spread across the site within hours. Twitter said it had fixed the bug the month before, and a later site update had brought it back.

linkify.js turns the URLs in a tweet into links and escapes everything else — but its escaping was written for text between tags, not for attribute values.

Fix linkify so neither the text nor a link can break out of its context.

Bug report

BUG-HOVER · Priority: Critical (XSS) · Reported by: security

linkify(text) returns HTML:

  • a URL is "http://" or "https://" followed by non-whitespace characters; each becomes <a href="E">E</a>, where E is the escaped URL
  • all other text is escaped
  • escaping is the same everywhere: & -> &amp; < -> &lt; > -> &gt; " -> &quot; ' -> &#39;
  • so no URL or text can ever close the href attribute or open a tag

Observed: a URL containing a double quote ends the href value early and the rest of the URL is parsed as new attributes on the <a>.

Logs

[render] tweet 25094329857 -> <a href="http://t.co/@"onmouseover="...">
[csp-report] inline event handler executed on twitter.com

The code as shipped

src/tweets/linkify.js (editable)

var URL_RE = /https?:\/\/\S+/g;

function escapeHtml(s) {
  return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

exports.linkify = function (text) {
  var out = "";
  var last = 0;
  var m;
  URL_RE.lastIndex = 0;
  while ((m = URL_RE.exec(text)) !== null) {
    out += escapeHtml(text.slice(last, m.index));
    var url = escapeHtml(m[0]);
    out += '<a href="' + url + '">' + url + "</a>";
    last = m.index + m[0].length;
  }
  return out + escapeHtml(text.slice(last));
};

Read-only context: src/tweets/CONTEXTS.js.

Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.