Skip to content
XSS Write-up Intermediate

XSS on Google using Acunetix

A Google page took the URL fragment and used it as the form's action, allowing JavaScript execution when the fragment began with 'javascript:'. An attacker could craft a link that, when clicked, would run arbitrary JavaScript in the victim's browser on the Google domain.

Andrey Leonov Follow

Published on acunetix.com

Bounty$5,000 View original

You have to be a very lazy hacker not to try to find issues in Google. Link and I are not lazy but we may be a bit luckier than most.

Andrey Leonov, on acunetix.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

The target here is Google, and the specific feature is an endpoint on google.ws (and, as it turned out, on google.com) that lives under the path /ajax/pi/fbfr. The response from that endpoint is a small HTML page containing an empty form and a short inline script. The script takes the fragment (the part of the URL after #), uses its first &-separated component as the form's action, turns the remaining components into hidden inputs, and then submits the form automatically. The script takes the fragment, uses its first &-separated component as the form's action, turns the remaining components into hidden inputs, and then submits the form automatically.

The write-up is by Andrey Leonov (@4lemon), working with his colleague Link, and it describes a cross-site scripting issue found in this helper. The bounty was USD 5000, reported on 27 December 2019 and fixed on 8 January 2020.

Setup

The researchers did not set up accounts or roles for this. The work started from routine scanning: they keep a long list of Google targets and run various tools against them, including the Invicti vulnerability scanner (the same scanner that had earlier turned up an XSS in Google Cloud). One of those scans, run in December 2019, flagged an XSS on google.ws. The researchers looked deeper because the target was Google.

Finding it

The scanner reported an XSS with this payload:

Text
https://google.ws/ajax/pi/fbfr?wvstest=javascript:domxssExecutionSink(1,%22%27%5C%22%3E%3Cxsstag%3E()locxss%22)

Scanner output like this is often a false positive, and the researchers say they do not react to every such report. This one was on Google, so they looked deeper. Note that the scanner's payload sits in the query string (?wvstest=...), while the actual mechanism, as they worked out next, is driven by the fragment.

Exploitation

Step 1: read the response

The first step was to examine the HTTP response in detail:

HTTP
HTTP/1.1 200 OK
...
<!doctype html><div style="display:none"> <form method="post"> </form> <script nonce="+ao+4Egc+7YExl3qyyWMJg==">(function(){var a=window.document.forms[0],b=location.hash.substr(1);b||window.close();var c=b.split("&"),d=decodeURIComponent(c[0]);a.action=d;for(var e=1;e<c.length;e++){var f=c[e].split("="),g=document.createElement("input");g.type="hidden";g.name=f[0];g.value=decodeURIComponent(f[1]);a.appendChild(g)}a.submit();}).call(this);</script> </div>

The body is an empty form inside a hidden div, followed by an inline script carrying a nonce attribute. The script is minified; the researchers reformatted it to read it:

JavaScript
(function() {
    var a = window.document.forms[0],
        b = location.hash.substr(1);
    b || window.close();
    var c = b.split("&"),
        d = decodeURIComponent(c[0]);
    a.action = d;
    for (var e = 1; e < c.length; e++) {
        var f = c[e].split("="),
            g = document.createElement("input");
        g.type = "hidden";
        g.name = f[0];
        g.value = decodeURIComponent(f[1]);
        a.appendChild(g)
    }
    a.submit();
}).call(this);

They then walked through it line by line, with comments:

JavaScript
(function() {
// Function that is going to be auto-executed
}).call(this);
JavaScript
(function() {
  // The variable “a” points to a form that is empty right now
    var a = window.document.forms[0],
    // The variable b is a location hash without the # character
        b = location.hash.substr(1);
  // If there is no b (no hash in the location URI), try to self-close
    b || window.close();
  // Split the location hash using the & character
    var c = b.split("&"),
    // And decode the first (zero) element
        d = decodeURIComponent(c[0]);
  // The hash value becomes the action of the form
    a.action = d;
// The below content is not important in the context of the issue
    for (var e = 1; e < c.length; e++) {
        var f = c[e].split("="),
            g = document.createElement("input");
        g.type = "hidden";
        g.name = f[0];
        g.value = decodeURIComponent(f[1]);
        a.appendChild(g)
    }
  // The form is auto-submitted
    a.submit();
}).call(this);

The important part is the assignment a.action = d: the first component of the fragment, after decodeURIComponent, becomes the form's action URL with no scheme check. The rest of the fragment is only used to build hidden inputs, which the researchers note is not important for the issue. The nonce on the script is a Content Security Policy mechanism, but it does not help here, because the script that runs is the page's own trusted script; the attacker only controls the value it is fed.

Step 2: build the payload

Once the behaviour of the function was clear, the researchers needed a fragment whose first component is a javascript: URL. They used:

Text
https://google.ws/ajax/pi/fbfr#javascript:alert(document.cookie)

When the page loads, the script reads the fragment, sets the form action to javascript:alert(document.cookie), and calls a.submit(). The script sets the form action to javascript:alert(document.cookie) and calls a.submit().

Step 3: check other domains

They then checked whether the same endpoint on other Google domains was affected:

Text
https://google.com/ajax/pi/fbfr#javascript:alert(document.cookie)

They also tested the same path on google.com.

Root cause

The page takes a value that the attacker fully controls through the URL fragment and assigns it directly to the action of a form that it then submits, without validating the scheme. Any URL scheme accepted by the browser as a form action is therefore allowed, including javascript:. The page's own CSP nonce does not prevent this, because the executing script is the legitimate inline script of the page; the injection is in the data it consumes, not in the script itself.

Impact

The report demonstrates alert(document.cookie) on google.ws and google.com. The report demonstrates this with alert(document.cookie) on google.ws and google.com. The write-up does not go further into what an attacker could reach beyond the demonstrated cookie access, and it does not describe any additional preconditions beyond the victim following the link.

Fix

Google's fix was small: one line of code changed to check that the fragment begins with http before using it as the form action. The patched script, as shown in the write-up:

JavaScript
(function() {
    var a = window.document.forms[0],
        b = location.hash.substr(1);
    b || window.close();
    var c = b.split("&"),
        d = decodeURIComponent(c[0]);
  // Only the below line needed to be changed
  // to check if the location hash begins with http:
    0 != d.indexOf("http") && window.close();
    a.action = d;
    for (var e = 1; e < c.length; e++) {
        var f = c[e].split("="),
            g = document.createElement("input");
        g.type = "hidden";
        g.name = f[0];
        g.value = decodeURIComponent(f[1]);
        a.appendChild(g)
    }
    a.submit();
}).call(this);

The changed line checks whether the location hash begins with http.

Timeline

Event Date
Vulnerability reported 27 December 2019, 01:01 AM
Vulnerability triaged 27 December 2019, 08:32 PM
Issue fixed by Google 8 January 2020
Bounty paid 8 January 2020
Bounty amount USD 5000

Variants to test

  • Look for redirect or form-forwarding helpers that read location.hash and assign it to form.action, location, window.open or similar sinks. A positive result is a fragment such as #javascript:alert(document.domain) executing in the page's origin.
  • Try the same fragment on sibling domains and paths of the same product, as the researchers did with google.com versus google.ws. A positive result is the same execution on a domain that was not the one originally reported.
  • Check whether the scheme check is done before or after decodeURIComponent. A positive result is a payload that decodes into a javascript: URL after the check has already passed.
  • Test whether the check accepts other schemes that browsers treat as script or as navigable, for example variants that begin with http but are not plain HTTP URLs. A positive result is execution or navigation that the fix was meant to block.
  • Where the page relies on a CSP nonce for its inline script, confirm whether the nonce protects the sink at all. A positive result is execution through the page's own trusted script, as here.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

Google runs a lot of small helper pages that do simple jobs behind the scenes. One of them, on the address google.ws, took a destination from the part of the link after the # symbol and used it as a form's action. The researchers Andrey Leonov (@4lemon) and his colleague Link found that this helper trusted that destination too much. The researchers showed that a javascript: value in the fragment became the form's action. That is what security people call cross-site scripting, or XSS: tricking a website into running someone else's code inside its own pages.

The bug was found by a scanner, reported to Google on 27 December 2019, fixed on 8 January 2020, and paid a bounty of USD 5000. The rest of this article walks through the same steps the researchers took, explaining each idea from scratch.

Background ideas you need first

Before the steps make sense, here are a few everyday ideas.

  • A URL and its fragment. A web address like https://example.com/page#section has parts. The #section bit at the end is called the fragment (or hash). Your browser keeps it locally: when you open such a link, the fragment is not sent to the server at all. It is only read by code running in the page. Think of it as a note you write on the back of an envelope: the postal service never sees it, only the person who opens the envelope does.
  • A form and its action. A form is the part of a web page with boxes you fill in and a button you press to send the answers somewhere. The place the answers are sent to is called the form's action. Normally the action is a web address, so pressing the button sends your data to that address.
  • JavaScript. The programming language that runs inside your browser and makes pages interactive. A link that starts with javascript: instead of http: or https: is not a web address at all: it is a small instruction telling the browser to run the code that follows. For example, javascript:alert(1) tells the browser to pop up a box saying 1.
  • Cookies. Small pieces of text that a website stores in your browser so it can recognise you on the next visit, for example to keep you logged in. If someone can read a site's cookies, they may be able to act as you on that site. That is why alert(document.cookie), which displays the current site's cookies in a pop-up, is a classic way to demonstrate that a script is running with dangerous access.
  • XSS (cross-site scripting). A class of bug where a website ends up running code that an attacker supplied, inside the website's own pages. Because the code appears to come from the site, the browser gives it the site's privileges: it can read the site's cookies and do things as the logged-in user.
  • A vulnerability scanner. A program that automatically visits many addresses and tries many test inputs, looking for signs of common bugs. It is like a robot inspector walking around a building tapping walls. It is fast but noisy: it sometimes reports a problem that is not real, which is called a false positive.
  • An endpoint. A specific address on a server that does one particular job, like a single counter in a shop. Here the endpoint is the path /ajax/pi/fbfr on Google's servers.
  • CSP and nonce. Content Security Policy is a rule a website can set telling the browser which scripts it is allowed to run. A nonce is a one-time random code stamped onto a script tag; the browser only runs scripts carrying the expected stamp. It is like a doorman checking a wristband: only people with the right wristband get in. As you will see, this doorman did not help here, because the script that ran was the page's own trusted script.

Step 1. A report from the vulnerability scanner

The researchers keep a long list of Google targets and regularly run various tools against them, including the Invicti vulnerability scanner. This is routine work: point the scanner at a list of addresses and read what comes back. One scan in December 2019 flagged an XSS on google.ws with this payload:

Text
https://google.ws/ajax/pi/fbfr?wvstest=javascript:domxssExecutionSink(1,%22%27%5C%22%3E%3Cxsstag%3E()locxss%22)

Line by line, in plain words:

  • https://google.ws/ajax/pi/fbfr is the address of the helper page the scanner was testing.
  • ?wvstest=... is the query string, the part of a URL after the question mark that carries extra information to the server. Here the scanner has put its test value under the name wvstest.
  • The value itself is a deliberately strange string full of encoded characters (%22 is a quote mark, %3E is >, and so on). Scanners use strings like this to see whether their input comes back into the page in a dangerous place.

Scanner reports are often false positives, and the researchers say they do not react to every one. This report was about Google, so it was worth a deeper look. Notice one detail that matters later: the scanner put its test value in the query string, but the real problem, as they worked out next, lives in the fragment, the part after #.

Step 2. Reading the response

The next step was to look at what the server actually sends back. You can see this yourself in a browser's developer tools, on the Network tab. The response was:

HTTP
HTTP/1.1 200 OK
...
<!doctype html><div style="display:none"> <form method="post"> </form> <script nonce="+ao+4Egc+7YExl3qyyWMJg==">(function(){var a=window.document.forms[0],b=location.hash.substr(1);b||window.close();var c=b.split("&"),d=decodeURIComponent(c[0]);a.action=d;for(var e=1;e<c.length;e++){var f=c[e].split("="),g=document.createElement("input");g.type="hidden";g.name=f[0];g.value=decodeURIComponent(f[1]);a.appendChild(g)}a.submit();}).call(this);</script> </div>

Line by line:

  • HTTP/1.1 200 OK is the server's status line: the request succeeded.
  • <!doctype html> says this is an HTML page.
  • <div style="display:none"> starts a container that is hidden from view.
  • <form method="post"> </form> is an empty form. It has no fields yet and no action yet; both will be filled in by the script.
  • <script nonce="+ao+4Egc+7YExl3qyyWMJg=="> starts a script block carrying a one-time stamp, the nonce, so the browser's CSP rules will allow this particular script to run.
  • The long line inside the script is the page's own code, written in a compressed style with very short variable names. The researchers reformatted it to read it comfortably.

The reformatted version:

JavaScript
(function() {
    var a = window.document.forms[0],
        b = location.hash.substr(1);
    b || window.close();
    var c = b.split("&"),
        d = decodeURIComponent(c[0]);
    a.action = d;
    for (var e = 1; e < c.length; e++) {
        var f = c[e].split("="),
            g = document.createElement("input");
        g.type = "hidden";
        g.name = f[0];
        g.value = decodeURIComponent(f[1]);
        a.appendChild(g)
    }
    a.submit();
}).call(this);

Then they walked through it with comments explaining each part:

JavaScript
(function() {
// Function that is going to be auto-executed
}).call(this);

This pattern wraps the code in a function and immediately calls it, so it runs as soon as the page loads, with no button press needed.

JavaScript
(function() {
  // The variable “a” points to a form that is empty right now
    var a = window.document.forms[0],
    // The variable b is a location hash without the # character
        b = location.hash.substr(1);
  // If there is no b (no hash in the location URI), try to self-close
    b || window.close();
  // Split the location hash using the & character
    var c = b.split("&"),
    // And decode the first (zero) element
        d = decodeURIComponent(c[0]);
  // The hash value becomes the action of the form
    a.action = d;
// The below content is not important in the context of the issue
    for (var e = 1; e < c.length; e++) {
        var f = c[e].split("="),
            g = document.createElement("input");
        g.type = "hidden";
        g.name = f[0];
        g.value = decodeURIComponent(f[1]);
        a.appendChild(g)
    }
  // The form is auto-submitted
    a.submit();
}).call(this);

Line by line, in plain words:

  • var a = window.document.forms[0] picks the first form on the page, the empty one, and calls it a.
  • b = location.hash.substr(1) takes the fragment from the address bar and drops the leading #. If the address ends in #hello, b becomes hello.
  • b || window.close() means: if b is empty, close the window. So a link with no fragment does nothing.
  • var c = b.split("&") cuts the fragment into pieces wherever there is an &, the same separator used in query strings. Each piece is one instruction.
  • d = decodeURIComponent(c[0]) takes the first piece and turns encoded characters back into normal ones, so %20 becomes a space and so on.
  • a.action = d sets the form's destination to that first piece. This is the crucial line: whatever the visitor put after # becomes the place the form will be sent to, with no check on what kind of address it is.
  • The for loop goes through the remaining pieces, splits each at =, and creates a hidden input for each one, so extra values in the fragment become extra form fields. The researchers note this part is not important for the bug.
  • a.submit() sends the form immediately, without the visitor pressing anything.

The page takes a fragment saying where to go and what to send, and posts there automatically. The problem is that the destination is taken from the visitor with no check on its type. The nonce on the script does not help, because the script that runs is the page's own trusted script; the attacker only controls the value it is fed.

Step 3. Building the proper payload

With the behaviour understood, the researchers needed a fragment whose first piece is a javascript: instruction rather than a web address. They used:

Text
https://google.ws/ajax/pi/fbfr#javascript:alert(document.cookie)

Line by line:

  • https://google.ws/ajax/pi/fbfr is the same helper page.
  • #javascript:alert(document.cookie) is the fragment. The browser keeps it local and the page's script reads it.
  • The script sets the form's action to javascript:alert(document.cookie) and submits the form.

They then checked whether the same endpoint on other Google domains was affected:

Text
https://google.com/ajax/pi/fbfr#javascript:alert(document.cookie)

Line by line:

  • This is the same path, /ajax/pi/fbfr, but on google.com instead of google.ws.
  • The same fragment is used. The same fragment was tested on google.com.

Why it worked

The page takes a value the visitor fully controls through the fragment and puts it straight into the form's action, then submits the form, without checking what kind of address it is. Any kind of address the browser accepts as a form action is therefore allowed, including javascript:. The page's CSP nonce does not prevent this, because the script doing the running is the page's legitimate script; the injection is in the data it consumes, not in the script itself.

What an attacker could do

The report demonstrates alert(document.cookie) on google.ws and google.com. The report demonstrates this with alert(document.cookie) on google.ws and google.com. The write-up does not go further into what an attacker could reach beyond the demonstrated cookie access, and it describes no additional preconditions beyond the victim following the link.

The fix

Google's fix was small: one line changed to check that the fragment begins with http before using it as the form action. The patched script, as shown in the write-up:

JavaScript
(function() {
    var a = window.document.forms[0],
        b = location.hash.substr(1);
    b || window.close();
    var c = b.split("&"),
        d = decodeURIComponent(c[0]);
  // Only the below line needed to be changed
  // to check if the location hash begins with http:
    0 != d.indexOf("http") && window.close();
    a.action = d;
    for (var e = 1; e < c.length; e++) {
        var f = c[e].split("="),
            g = document.createElement("input");
        g.type = "hidden";
        g.name = f[0];
        g.value = decodeURIComponent(f[1]);
        a.appendChild(g)
    }
    a.submit();
}).call(this);

Line by line, focusing on the new line:

  • d.indexOf("http") looks for the text http inside the decoded first piece and returns where it first appears, or -1 if it is not there at all.
  • 0 != d.indexOf("http") is true when http is not at the very start of the value. In that case window.close() runs and the page closes itself.
  • The changed line checks whether the location hash begins with http.

Timeline

Event Date
Vulnerability reported 27 December 2019, 01:01 AM
Vulnerability triaged 27 December 2019, 08:32 PM
Issue fixed by Google 8 January 2020
Bounty paid 8 January 2020
Bounty amount USD 5000

Things worth testing elsewhere

These are the kinds of follow-up checks the same pattern invites, useful if you ever meet a similar helper page:

  1. Look for redirect or form-forwarding helpers that read location.hash and put it into form.action, location, window.open or similar places. A positive result is a fragment such as #javascript:alert(document.domain) running in the page's context.
  2. Try the same fragment on sibling domains and paths of the same product, as the researchers did with google.com versus google.ws. A positive result is the same execution on a domain that was not the one originally reported.
  3. Check whether the scheme check happens before or after decodeURIComponent. A positive result is a payload that decodes into a javascript: address after the check has already passed.
  4. Test whether the check accepts other schemes that browsers treat as script or as navigable, for example variants that begin with http but are not plain HTTP addresses. A positive result is execution or navigation that the fix was meant to block.
  5. Where the page relies on a CSP nonce for its inline script, confirm whether the nonce protects the dangerous line at all. A positive result is execution through the page's own trusted script, as here.

Glossary

  • API: a defined way for one piece of software to ask another to do something, like a service counter with set requests you can make.
  • Bounty: money a company pays a researcher who reports a security bug responsibly.
  • Browser: the program you use to view web pages, such as Chrome or Firefox.
  • Cookie: a small piece of text a website stores in your browser to recognise you later, often used to keep you logged in.
  • CSP (Content Security Policy): rules a website gives the browser about which scripts it may run.
  • Cross-site scripting (XSS): a bug where a website runs code supplied by an attacker inside its own pages, giving that code the site's privileges.
  • Endpoint: a specific address on a server that performs one particular job.
  • False positive: a scanner report of a problem that turns out not to be real.
  • Form: the part of a web page with fields you fill in and a button that sends them somewhere.
  • Form action: the destination a form's data is sent to when it is submitted.
  • Fragment (hash): the part of a URL after #, kept in the browser and not sent to the server.
  • HTTP response: what a server sends back after a browser asks for a page, including a status line and the page content.
  • Invicti: the vulnerability scanner the researchers used.
  • JavaScript: the programming language that runs in the browser and makes pages interactive.
  • javascript: URL: a link that tells the browser to run the code that follows instead of visiting a web address.
  • Nonce: a one-time random stamp on a script tag that the browser checks against the site's CSP rules before running the script.
  • Origin: the combination of scheme, host and port that a page belongs to; code from one origin is not supposed to read another origin's data.
  • Payload: the crafted input a researcher sends to test for a bug.
  • Query string: the part of a URL after ? that carries extra information to the server.
  • Scanner: a program that automatically tests many addresses for common bugs.
  • Triaged: reviewed and confirmed as a real issue by the receiving team.
  • URL: a web address.
  • Vulnerability: a weakness in software that someone could abuse to do something harmful.

More from Google

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.