Skip to content
Open redirect Write-up Intermediate

Google.com – Mobile Feedback URL Redirect Regex/Validation Flaw

A mobile feedback page on Google.com parsed a user-supplied URL with a regular expression and then redirected to the original unparsed URL if the parsed protocol was http, https, or empty. By crafting a URL where the regex failed to capture the protocol, an attacker could bypass the check and redirect to a javascript: URI, potentially leading to cross-site scripting.

Brett Buerhaus Follow

Published on buer.haus

Bounty$500 View original

Back in October of last year I discovered a JavaScript flaw on Google.com that bypassed protocol validation by abusing an if check against a URL parsed by regex. I was unable to find a way to attack this vector, but was still rewarded a bounty of $500 due to Google knowing of an active browser vulnerability that…

Brett Buerhaus (@bbuerhaus), on buer.haus

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

In October 2014 I was looking at the mobile versions of Google.com services, on the theory that mobile pages sometimes receive less traffic, less visibility and less engineering attention than their desktop counterparts, and can therefore be more vulnerable. That search led me to the mobile feedback page at google.com/tools/feedback/mobile_feedback, which takes a url request variable and, after the user submits feedback and closes the dialog, sends the browser to that URL. The redirect is performed in JavaScript rather than on the server.

The normal flow of the application is:

  1. Load this URL:
HTTP
https://www.google.com/tools/feedback/mobile_feedback?hl=en&url=http://www.apple.com/&redirect=true&authuser=0&pi=17&hl=en
  1. Write in some feedback and click the submit button.
  2. Click the close button.
  3. You are redirected to apple.com.

Setup

The page is reachable directly with the query string above. The tooling was a JavaScript beautifier and Firebug breakpoints, used against the obfuscated script that implements the flow, mobile_submitter__en.js, served from gstatic.com. After using a beautifier and breakpoints in Firebug, I was able to understand how the script worked.

Finding it

Because the page passes a URL inside a request variable and then redirects to it, it looked like the same class of issue I had recently found elsewhere on Google, so it warranted a closer look. Reading the beautified script gave two important facts:

  1. The URL sent in the url request variable is parsed with a regular expression.
  2. Before redirecting with window.location.href, the code checks whether the protocol section is null, http or https.

Both facts matter. If the protocol is null, the code assumes the redirect target is a relative path. And window.location.href can be vulnerable to Cross-Site Scripting if it is pointed at javascript:.

The regex looked like this:

JavaScript
var Tb = /^(?:([^:/?#.]+):)?(?:\/\/(?:([^/?#]*)@)?([^/#?]*?)(?::([0-9]+))?(?=[/#?]|$))?([^?#]+)?(?:\?([^#]*))?(?:#(.*))?$/;

And the redirect logic like this:

JavaScript
if ("http" == a || "https" == a || "" == a) {
	window.location.href = this.d;
	return
}

Exploitation

Step 1: understand what the regex captures

The URL is parsed and, right before the redirect, the code validates that the protocol is http, https or blank. In the check "http" == a, a is the second position in the match array. In the window.location assignment, d is the first position of the array, which is the original unparsed URI. So the value that is validated and the value that is used for the redirect are not the same string.

A worked example makes the capture groups concrete. Given this request:

HTTP
https://www.google.com/tools/feedback/mobile_feedback?hl=en&url=http://login:[redacted]:80/1/2%3F3=4&5=6%237=8&redirect=true&authuser=0&pi=17&hl=en

the redirect URI is:

Text
http://login:[redacted]:80/1/2?3=4&5=6#7=8

and the regex results are:

Text
0: "http://login:[redacted]:80/1/2?3=4&5=6#7=8" [0, 51]
1: "http" [0, 4]
2: "login:pass" [7, 17]
3: "www.google.com" [18, 32]
4: "80" [33, 35]
5: "/1/2" [35, 39]
6: "3=4&5=6" [40, 47]
7: "7=8" [48, 51]

There are many parts in a URL that the regex looks for, and not every URL has all of them, so the capture groups are made optional with question marks. That optionality is the initial flaw.

Step 2: make the protocol group empty

The second position in the array (index 1, since the array starts at 0) is parsed out as http in the example above. The first thing the regex checks for is any character except :, /, ?, # and ., up until the first : character. But the last part of the regex contains a greedy check for any characters up until the first # or ?. By placing a period before the first :, the first capture group is ignored and the match is instead encapsulated in index position 5. A # or a ? can be put in place of the . to illustrate the capture match even more clearly.

With a # character in the URL protocol, index 1 comes out empty. Because the protocol is null, the if check passes (it accepts ""), and the browser is allowed to redirect.

Step 3: the payload

This was as close as I could get to a malicious payload:

HTTP
https://www.google.com/tools/feedback/mobile_feedback?hl=en&url=javascript.:alert(1);&redirect=true&authuser=0&pi=17&hl=en

The regex match results for it:

Text
Successful match! Groups:
0: "javascript.:alert(1);" [0, 23]
1: undefined
2: undefined
3: undefined
4: undefined
5: "javascript.:alert(1);" [0, 23]
6: undefined
7: undefined

Group 1, the protocol, is undefined, so the validation treats it as blank and lets the redirect proceed. The browser is then redirected to:

Text
javascript.:alert(1);

Root cause

The flaw is a mismatch between what is validated and what is used. The regex parses the URL into groups, the if check inspects the protocol group (index 1), and the redirect uses the original unparsed URI (index 0). Because the protocol group is optional and the trailing part of the regex greedily matches up to the first # or ?, a period (or #, or ?) placed before the first : causes the protocol group to be skipped and the whole string to land in group 5 instead. The check then sees an empty protocol, which it explicitly permits on the assumption that an empty protocol means a relative path, and the redirect fires with the original string.

The check validated the parsed protocol group, while the redirect used the original unparsed URI. Using a regex to parse URLs is common, but this flow would have been better served by the secure URL validation methods Google uses on its backend elsewhere. Mobile sites need to be faster and simpler, and that convenience over security can produce insecure practices.

Impact

The redirect target is controlled through the url parameter, and the validation can be bypassed so that a javascript: URI reaches window.location.href, which the report notes can be vulnerable to Cross-Site Scripting. In my own testing I could not find a way to exploit it, because I am not aware of any protocol that contains a period, nor of a way to make the browser ignore the period in the protocol. Google nonetheless awarded a $500 bounty, because it knew of an active browser vulnerability that allowed the issue to be exploited successfully.

Fix

The new version of mobile_submitter__en.js changed the check to:

JavaScript
if ("http" == a || "https" == a) {
	window.location.href = this.d;
	return
}

The blank-protocol case is no longer accepted. This did not change the regex flaw, but it requires that the request variable contain a full path URL with a protocol of http or https.

Variants to test

  • Other optional protocol groups. Look for URL-parsing regexes where the scheme group is optional and the redirect or navigation uses the raw input rather than the parsed scheme. A positive result is a match where the scheme group is undefined while the raw string still reaches a sink such as window.location.href.
  • Separator characters before the first colon. Try ., # and ? immediately before the : in a scheme-like prefix, as done here. A positive result is the same shift of the whole string into a later capture group with the scheme group empty.
  • Blank-protocol assumptions. Find code that treats an empty scheme as "relative path" and then navigates with the original string. A positive result is a javascript: or other dangerous scheme passing the blank check.
  • Mobile and less-travelled endpoints. The original lead was the mobile feedback page; the same pattern is worth checking on mobile variants of other services, where traffic and maintenance may be lower.
  • Obfuscated JavaScript. Beautify and set breakpoints in heavily obfuscated bundles, as here, and watch the parsed values at the point of the check and at the point of navigation. A positive result is any divergence between the value validated and the value used.

Timeline and bounty

  • Reported: 10/6/14
  • Fixed: 11/26/14
  • Bounty: $500

Eduardo Vela (@sirdarckcat) later noted that the URL validation regex used here comes from Google's Closure Library, with more information in the utils.js file. After writing the article I donated the $500 to St. Jude Children's Research Hospital.

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. 12 min read.

What happened, in plain words

In October 2014 I found a security flaw in a page on Google.com. The page was a feedback form for mobile users. It had a feature that sent you to another website after you finished giving feedback. The page decided whether that destination was safe by checking it with a pattern-matching tool called a regular expression, or regex for short. The check could be fooled, and the browser could be told to go to a javascript: address instead of a normal web address. That kind of address runs code in the page, which is the basis of a class of attack called Cross-Site Scripting, or XSS.

I could not find a way to actually make the attack work in a browser myself. Google still paid a $500 bounty, because Google knew of a browser bug that would let the flaw be exploited. A bounty is a reward a company pays to someone who reports a security problem responsibly instead of abusing it.

This article explains the whole thing from the beginning, assuming you can use a browser and a computer and nothing more.

Background ideas you need first

Before the details, a few everyday ideas.

  • A URL is the address of something on the web, like https://www.apple.com/. It has parts: the https at the front is the protocol (the rules for how to talk), then the site name, then an optional path and query.
  • A query string is the part of a URL after a ?. It carries little labelled values, like ?colour=blue&size=large. Each label is a parameter (also called a request variable). The page reads these values and acts on them.
  • JavaScript is the programming language that runs inside your browser and makes pages interactive. When a page needs to move you to another address, JavaScript can set window.location.href to that address, and the browser follows it.
  • A regular expression (regex) is a pattern for matching text. Think of it like a very precise search rule: "find a word that starts with a capital letter and ends with a digit". Programmers use regexes to pull a URL apart into its pieces.
  • Capture groups are the pieces a regex pulls out. If a regex has brackets in it, each bracketed part captures one piece of the matched text, and the pieces are numbered starting from 0. Group 0 is always the whole match; groups 1, 2 and so on are the individual pieces.
  • Obfuscated code is code deliberately made hard to read, often by shortening names and removing spaces. Companies do it to make files smaller and harder to copy. It also makes life harder for anyone reading the code, including security researchers.
  • Cross-Site Scripting (XSS) is when an attacker gets their own code to run inside someone else's web page. If a page takes a value from the URL and later uses it as an address to navigate to, and that value can be a javascript: address, the attacker's code can run.
  • DOM-based means the problem happens entirely inside the browser's copy of the page (the DOM, or Document Object Model), not on the server. The server never sees the dangerous value.
  • A beautifier is a tool that reformats code to make it readable again, adding spaces and line breaks.
  • Firebug was a browser add-on for inspecting and debugging web pages. A breakpoint is a marker you put on a line of code so the browser pauses there, letting you look at the values in memory at that moment.

The page and its normal behaviour

I was looking at the mobile versions of Google.com services. My reasoning was that mobile pages sometimes get less traffic, less attention and less maintenance than their desktop versions, so they can be more vulnerable. That search led me to the mobile feedback page at google.com/tools/feedback/mobile_feedback. It takes a url request variable and, after you submit feedback and close the dialog, sends your browser to that URL. The redirect is done in JavaScript in the browser, not on the server.

The normal flow of the application is:

  1. Load this URL:
HTTP
https://www.google.com/tools/feedback/mobile_feedback?hl=en&url=http://www.apple.com/&redirect=true&authuser=0&pi=17&hl=en
  1. Write in some feedback and click the submit button.
  2. Click the close button.
  3. You are redirected to apple.com.

The important one for us is url, because it is a value I control and the page acts on it.

Setting up

The page is reachable directly with the query string above. The tools were a JavaScript beautifier and Firebug breakpoints, used against the obfuscated script that implements the flow, mobile_submitter__en.js, served from gstatic.com. After using a beautifier and breakpoints in Firebug, I was able to understand how the script worked.

Finding the flaw

Because the page passes a URL inside a request variable and then redirects to it, it looked like the same class of issue I had recently found elsewhere on Google, so it warranted a closer look. Reading the beautified script gave two important facts:

  1. The URL sent in the url request variable is parsed with a regular expression.
  2. Before redirecting with window.location.href, the code checks whether the protocol section is null, http or https.

Both facts matter. If the protocol is null, the code assumes the redirect target is a relative path, meaning a path on the same site rather than a full address elsewhere. And window.location.href can be vulnerable to Cross-Site Scripting if it is pointed at javascript:.

The regex looked like this:

JavaScript
var Tb = /^(?:([^:/?#.]+):)?(?:\/\/(?:([^/?#]*)@)?([^/#?]*?)(?::([0-9]+))?(?=[/#?]|$))?([^?#]+)?(?:\?([^#]*))?(?:#(.*))?$/;

Line by line, in plain words:

  • var Tb = stores the pattern in a variable named Tb.
  • /^(?:([^:/?#.]+):)?/ looks at the start of the string for an optional piece ending in a colon. The [^:/?#.]+ means "one or more characters that are not :, /, ?, # or .". This is the protocol group, group 1. The ? at the end makes the whole thing optional.
  • (?:\/\/(?:([^/?#]*)@)?([^/#?]*?)(?::([0-9]+))?(?=[/#?]|$))? handles the // part of a URL, an optional username before an @, the host name, and an optional port number. These become groups 2, 3 and 4.
  • ([^?#]+)? captures the path, group 5.
  • (?:\?([^#]*))? captures the query string after a ?, group 6.
  • (?:#(.*))?$/ captures anything after a #, group 7, and anchors the match to the end of the string.

And the redirect logic like this:

JavaScript
if ("http" == a || "https" == a || "" == a) {
	window.location.href = this.d;
	return
}

Line by line:

  • if ("http" == a || "https" == a || "" == a) { checks whether the value in a is http, https, or an empty string. The || means "or".
  • window.location.href = this.d; sends the browser to the value in this.d.
  • return stops the function here.

Exploitation

Step 1: understand what the regex captures

The URL is parsed and, right before the redirect, the code validates that the protocol is http, https or blank. In the check "http" == a, a is the second position in the match array. In the window.location assignment, d is the first position of the array, which is the original unparsed URI. So the value that is validated and the value that is used for the redirect are not the same string. That gap is the heart of the problem.

A worked example makes the capture groups concrete. Given this request:

HTTP
https://www.google.com/tools/feedback/mobile_feedback?hl=en&url=http://login:[redacted]:80/1/2%3F3=4&5=6%237=8&redirect=true&authuser=0&pi=17&hl=en

the redirect URI is:

Text
http://login:[redacted]:80/1/2?3=4&5=6#7=8

and the regex results are:

Text
0: "http://login:[redacted]:80/1/2?3=4&5=6#7=8" [0, 51]
1: "http" [0, 4]
2: "login:pass" [7, 17]
3: "www.google.com" [18, 32]
4: "80" [33, 35]
5: "/1/2" [35, 39]
6: "3=4&5=6" [40, 47]
7: "7=8" [48, 51]

What this shows: group 0 is the whole string. Group 1 is the protocol, http. Group 2 is the username and password part before the @. Group 3 is the site name. Group 4 is the port number. Group 5 is the path. Group 6 is the query string. Group 7 is the part after the #. The numbers in square brackets are the positions in the string where each piece starts and ends.

There are many parts in a URL that the regex looks for, and not every URL has all of them, so the capture groups are made optional with question marks. That optionality is the initial flaw.

Step 2: make the protocol group empty

The second position in the array (index 1, since the array starts at 0) is parsed out as http in the example above. The first thing the regex checks for is any character except :, /, ?, # and ., up until the first : character. But the last part of the regex contains a greedy check for any characters up until the first # or ?. "Greedy" means it takes as much as it can. By placing a period before the first :, the first capture group is ignored and the match is instead encapsulated in index position 5. A # or a ? can be put in place of the . to illustrate the capture match even more clearly.

With a # character in the URL protocol, index 1 comes out empty. Because the protocol is null, the if check passes (it accepts ""), and the browser is allowed to redirect.

Step 3: the payload

This was as close as I could get to a malicious payload:

HTTP
https://www.google.com/tools/feedback/mobile_feedback?hl=en&url=javascript.:alert(1);&redirect=true&authuser=0&pi=17&hl=en

Here the url value is javascript.:alert(1);.

The regex match results for it:

Text
Successful match! Groups:
0: "javascript.:alert(1);" [0, 23]
1: undefined
2: undefined
3: undefined
4: undefined
5: "javascript.:alert(1);" [0, 23]
6: undefined
7: undefined

Group 1, the protocol, is undefined, so the validation treats it as blank and lets the redirect proceed. The browser is then redirected to:

Text
javascript.:alert(1);

Root cause

The flaw is a mismatch between what is validated and what is used. The regex parses the URL into groups, the if check inspects the protocol group (index 1), and the redirect uses the original unparsed URI (index 0). Because the protocol group is optional and the trailing part of the regex greedily matches up to the first # or ?, a period (or #, or ?) placed before the first : causes the protocol group to be skipped and the whole string to land in group 5 instead. The check then sees an empty protocol, which it explicitly permits on the assumption that an empty protocol means a relative path, and the redirect fires with the original string.

The check validated the parsed protocol group, while the redirect used the original unparsed URI. Using a regex to parse URLs is common, but this flow would have been better served by the secure URL validation methods Google uses on its backend elsewhere. Mobile sites need to be faster and simpler, and that convenience over security can produce insecure practices.

Impact

The redirect target is controlled through the url parameter, and the validation can be bypassed so that a javascript: URI reaches window.location.href, which the report notes can be vulnerable to Cross-Site Scripting. In my own testing I could not find a way to exploit it, because I am not aware of any protocol that contains a period, nor of a way to make the browser ignore the period in the protocol. Google nonetheless awarded a $500 bounty, because it knew of an active browser vulnerability that allowed the issue to be exploited successfully.

Fix

The new version of mobile_submitter__en.js changed the check to:

JavaScript
if ("http" == a || "https" == a) {
	window.location.href = this.d;
	return
}

Line by line: the if now accepts only http or https. The || "" == a part is gone, so a blank protocol no longer passes. The redirect line and the return are unchanged.

The blank-protocol case is no longer accepted. This did not change the regex flaw, but it requires that the request variable contain a full path URL with a protocol of http or https.

Variants to test

  • Other optional protocol groups. Look for URL-parsing regexes where the scheme group is optional and the redirect or navigation uses the raw input rather than the parsed scheme. A positive result is a match where the scheme group is undefined while the raw string still reaches a sink such as window.location.href.
  • Separator characters before the first colon. Try ., # and ? immediately before the : in a scheme-like prefix, as done here. A positive result is the same shift of the whole string into a later capture group with the scheme group empty.
  • Blank-protocol assumptions. Find code that treats an empty scheme as "relative path" and then navigates with the original string. A positive result is a javascript: or other dangerous scheme passing the blank check.
  • Mobile and less-travelled endpoints. The original lead was the mobile feedback page; the same pattern is worth checking on mobile variants of other services, where traffic and maintenance may be lower.
  • Obfuscated JavaScript. Beautify and set breakpoints in heavily obfuscated bundles, as here, and watch the parsed values at the point of the check and at the point of navigation. A positive result is any divergence between the value validated and the value used.

Timeline and bounty

  • Reported: 10/6/14
  • Fixed: 11/26/14
  • Bounty: $500

Eduardo Vela (@sirdarckcat) later noted that the URL validation regex used here comes from Google's Closure Library, with more information in the utils.js file. After writing the article I donated the $500 to St. Jude Children's Research Hospital.

Glossary

  • Bounty: a reward a company pays to someone who reports a security problem responsibly.
  • Browser: the program you use to view web pages, such as Chrome or Firefox.
  • Capture group: a bracketed piece of a regex that pulls out one part of the matched text; groups are numbered from 0, where 0 is the whole match.
  • Closure Library: a set of JavaScript tools developed by Google, which includes the URL-parsing regex used here.
  • Cross-Site Scripting (XSS): when an attacker gets their own code to run inside someone else's web page.
  • DOM (Document Object Model): the browser's in-memory copy of a page, which JavaScript can read and change.
  • DOM-based: a problem that happens entirely inside the browser's copy of the page, not on the server.
  • Firebug: a browser add-on for inspecting and debugging web pages.
  • Greedy: a regex behaviour where a pattern takes as much text as it can rather than as little as possible.
  • JavaScript: the programming language that runs inside your browser and makes pages interactive.
  • Mobile version: a version of a website designed for phones and tablets, often simpler and sometimes less maintained than the desktop version.
  • Obfuscated code: code deliberately made hard to read, often by shortening names and removing spaces.
  • Parameter (request variable): a labelled value in a URL's query string, like url=....
  • Payload: the specific value an attacker sends to try to trigger a flaw.
  • Protocol: the rules for how to talk to an address, such as http, https or javascript.
  • Query string: the part of a URL after a ?, carrying labelled values.
  • Redirect: sending the browser from one address to another.
  • Regex (regular expression): a pattern for matching text, used to pull a URL apart into its pieces.
  • Relative path: a path on the same site rather than a full address elsewhere.
  • Sink: a place in code where a value is used in a way that can be dangerous, such as navigating the browser.
  • URL: the address of something on the web.
  • window.location.href: the browser property that holds the current address; setting it makes the browser navigate.
  • XSS: see Cross-Site Scripting.

More by Brett Buerhaus

View all

8 more disclosed reports, best first.

More from Google

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Open redirect

The lesson behind this report.

A trusted site forwards users to any address an attacker picks; rarely paid alone, often the missing link in a chain.