Skip to content
XSS Write-up Intermediate

Chaining tricky ouath exploitation to stored XSS

Rohan aggarwal Follow

Published on medium.com

#### Rohan Aggarwal

Rohan Aggarwal, on medium.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. 5 min read.

Context

redacted.com is a file storage service in the same category as Google Drive or Dropbox: users upload, download and share files from the platform. The researcher found the bug while working through a private program on HackerOne. Two low-impact issues were present: a Self-XSS in the form of AngularJS template injection, and a misconfigured OAuth implementation. Neither is very useful alone, but chained together they produce a stored XSS that runs in other users' sessions.

The OAuth piece is the Dropbox import feature in the account settings. To import files from Dropbox, a user connects the application to their Dropbox account through OAuth. The normal flow is:

  1. The user clicks the connect Dropbox button, which initiates a GET request to Dropbox:
HTTP
https://dropbox.com/oauth2/authorize?client_id=***********&response_type=code&state=****************&redirect_uri=https%3A%2F%2Fwww.redacted.com%2Faccount%2Fsettings%2Fdropbox-callback
  1. The user is redirected to Dropbox, logs in and clicks Allow.
Image for post
Image for post From the original on medium.com
  1. After Allow, a GET request carrying the state parameter and the auth code is sent to redacted.com, visible in the Location header.

  2. Once that GET request executes on redacted.com, Dropbox is synced with the current user session and all files from it can be imported.

Setup

The work was done against a private HackerOne program, with the application referred to as redacted.com. The researcher had an account on redacted.com and a Dropbox account, and used the Dropbox import feature in the account settings. No specific tools or versions are described in the report.

Finding it

The XSS was found in the name of an uploaded file. A file named like this:

Text
{{constructor.constructor('alert(1)')()}}.jpg

results in XSS on the uploaded files dashboard. On its own this is Self-XSS. The researcher also found an easy way to make it stored for other users by sharing the link to the file: the file gets imported into the recipient's uploaded files dashboard with the name unchanged, which triggers the stored XSS. The report then presents a second, more interesting route through the OAuth flow.

Testing the OAuth flow, the goal was to connect the researcher's Dropbox account to other redacted.com users. The obvious defences were in place: the redirect_uri was properly whitelisted, the state parameter existed, and the auth code could not be used twice. The researcher also checked whether the application verified the state parameter against the current user session, and it did. That means the final URL coming back from Dropbox could not simply be replayed on another user's account:

HTTP
https://www.redacted.com/account/settings/dropbox-callback?state=********code=***********

So the question became what was wrong with the implementation.

Exploitation

Step 1: Remove the state parameter

Out of curiosity the researcher removed the state parameter from the URL and used the resulting request on a different user account:

HTTP
https://www.redacted.com/account/settings/dropbox-callback?code=***********

Surprisingly, the researcher's Dropbox account was connected to that user. In other words, a single GET request can connect the attacker's Dropbox account to anyone's redacted.com account. There is no option to log in to redacted.com using a Dropbox account, so this is not an account takeover by itself. But combined with the file name XSS, it becomes useful.

Step 2: Upload the payload file to Dropbox

The researcher uploaded a file named:

Text
{{constructor.constructor('alert(1)')()}}.jpg

to Dropbox. The report notes that, strangely, this was allowed.

Step 3: Send the OAuth URL without state to the victim

The final OAuth URL from Dropbox, with the state parameter removed, is given to the victim:

HTTP
https://www.redacted.com/account/settings/dropbox-callback?code=***********

Step 4: The victim imports the attacker's files

The victim's account gets connected to the attacker's Dropbox account and imports all the files from it, including the malicious file.

Step 5: The payload executes

When the user visits the Uploaded File Dashboard, the XSS payload in the file name is executed.

Root cause

The server was verifying the state parameter against the current user session, but it was not verifying that the parameter was present at all. The researcher models the server logic as something like:

Text
if(isset($_GET['state'])){    if($_GET['state'] != current_user_state)        ACCESS DENIED        exit()}ACCESS GRANTED

Because the check only runs when state is set, omitting it skips the comparison entirely and the callback is accepted. The whitelisted redirect_uri, the presence of state in the normal flow and the single-use auth code did not help, since the bypass does not touch any of them. The XSS side is AngularJS template injection in the file name, which executes on the uploaded files dashboard.

Impact

An attacker can connect their own Dropbox account to any redacted.com user's account with a single GET request, and the victim then imports the attacker's files, including a file whose name carries the AngularJS payload. When the victim visits the Uploaded File Dashboard, the payload executes in their session. The report does not describe any account takeover via Dropbox login, and states that this is not possible because there is no such login option.

Fix

The report does not describe the fix that was applied. The change needed is to reject callbacks where state is missing, not only callbacks where it does not match the session, so that the comparison always runs.

Variants to test

  • Omit the state parameter entirely on OAuth callbacks of other applications and see whether the account still gets linked. A positive result is a successful link with no state value supplied.
  • Send an empty state value (state=) and see whether it is treated as present but unverified.
  • Try the callback with a state value from a different session and confirm it is rejected, to establish that the comparison itself works.
  • Look for other parameters whose presence is checked but whose absence skips validation, such as CSRF tokens or nonces.
  • Test whether file names are rendered through a client-side template engine on dashboards and import views, using a harmless AngularJS expression as the file name.

Timeline

Date Event
Jan 1, 2019 Report submitted
Jan 2, 2019 Triaged
Jan 8, 2019 Resolved
Jan 15, 2019 Rewarded

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

A security researcher was testing a website that stores files for you, the same kind of service as Google Drive or Dropbox. We will call it redacted.com, because the real name was kept private. On this site you can upload files, download them again and share them with other people.

The researcher found two small problems. On their own, neither was very dangerous. But when you put them together, one after the other, they became a serious attack: a piece of code written by the attacker would run inside another person's browser, in that person's logged-in session.

This kind of attack is called stored XSS (cross-site scripting). Imagine a noticeboard where visitors pin notes. If someone can pin a note that says "whoever reads this, do what I wrote", and the board actually carries out the instruction for every reader, that is stored XSS: the harmful content is saved on the site and runs for whoever views it later.

The two small problems were:

  1. A Self-XSS: harmful code that only runs in your own browser when you type it yourself. It is called "self" because the victim and the attacker are the same person, so it is usually considered low impact.
  2. A misconfigured OAuth implementation: a mistake in the way the site lets you connect another service (Dropbox) to your account. OAuth is the mechanism behind all those "Log in with Google" or "Connect to Facebook" buttons. A mistake there can let an attacker link their own account to someone else's.

Neither bug alone would have mattered much. Chained together, they gave a perfect stored XSS. The rest of this article walks through how, step by step, assuming you know nothing about security beyond using a browser.

Background: what the site does and what OAuth is

redacted.com is a file storage service. You upload files, you download them, you share them. The researcher found the bug while working through a private program on HackerOne, a platform where companies invite researchers to test their systems and report problems.

One feature in the account settings lets you import your files from Dropbox. To do that, you have to connect the application to your Dropbox account. That connection is made with OAuth.

A simple comparison: OAuth is like a hotel giving a plumber a temporary key to one room, instead of your master key. You (the user) tell Dropbox (the hotel) "this application may access my files", Dropbox asks you to confirm, and then it hands the application a short-lived pass (a code) instead of your password. The application uses that pass to get access. You never give your Dropbox password to redacted.com.

Two words appear in every OAuth flow and matter later:

  • client_id: a public identifier for the application, like a name badge. It is not secret.
  • state: a random value the application makes up and sends along with the request. When Dropbox sends the user back, it includes the same state value. The application checks that the value matches the one it created for this particular user session. This is a defence against CSRF (cross-site request forgery), an attack where a victim's browser is tricked into sending a request the victim did not intend. The state value proves the callback belongs to a flow this user actually started.

A session is how a website remembers who you are between page loads. When you log in, the server gives your browser a small piece of data (often a cookie) that it checks on every request. If an attacker can make your browser send a request while you are logged in, the server treats it as coming from you.

A GET request is the simplest kind of web request: your browser asks a server for a page or resource by visiting a URL. You can trigger one just by clicking a link or pasting an address into the address bar. That matters here, because the whole attack is delivered as a link.

The normal OAuth flow

Here is how the Dropbox import feature is supposed to work.

  1. The user clicks the connect Dropbox button, and the browser sends a GET request to Dropbox:
HTTP
https://dropbox.com/oauth2/authorize?client_id=***********&response_type=code&state=****************&redirect_uri=https%3A%2F%2Fwww.redacted.com%2Faccount%2Fsettings%2Fdropbox-callback

Line by line:

  • https://dropbox.com/oauth2/authorize is the Dropbox address that starts the permission process.
  • client_id=*********** identifies which application is asking. The stars are where the real value was, hidden in the report.
  • response_type=code says "when you are done, give me a code".
  • state=**************** is the random value the application generated for this user session. Dropbox will echo it back.
  • redirect_uri=https%3A%2F%2Fwww.redacted.com%2Faccount%2Fsettings%2Fdropbox-callback is where Dropbox should send the user afterwards. The %3A and %2F are just the encoded forms of : and /, because URLs cannot contain those characters raw in a parameter value. Decoded, it is https://www.redacted.com/account/settings/dropbox-callback.
  1. The user is redirected to Dropbox, logs in and clicks Allow.
Image for post
Image for post From the original on medium.com

This is the consent screen: Dropbox asks the user to confirm that redacted.com may access their files.

  1. After Allow, a GET request carrying the state parameter and the auth code is sent to redacted.com, visible in the Location header.

A Location header is part of the server's reply that tells the browser "go to this address next". Dropbox replies to the browser with a redirect to redacted.com, and the address it redirects to contains the state value and the auth code. The auth code is the short-lived pass mentioned earlier: redacted.com will exchange it for access to the user's Dropbox files.

  1. Once that GET request executes on redacted.com, Dropbox is synced with the current user session and all files from it can be imported.

"Synced" here means the two accounts are now linked: redacted.com has stored the permission, and from then on it can list and copy the user's Dropbox files into the user's redacted.com account.

The first bug: a file name that runs code

The researcher found XSS in the name of an uploaded file. A file named like this:

Text
{{constructor.constructor('alert(1)')()}}.jpg

results in XSS on the uploaded files dashboard.

What is going on in that name? The site is built with AngularJS, a JavaScript framework for building web pages. AngularJS has a feature called templates: the page can contain placeholders written between double curly braces, like {{name}}, and the framework replaces them with real values when the page loads. That is convenient for developers. But if a value that a user controls, such as a file name, is inserted into the page and AngularJS treats it as a template rather than as plain text, then the user's text is not just displayed: it is evaluated as an expression.

The expression constructor.constructor('alert(1)')() is a known way to reach the JavaScript Function constructor from inside an AngularJS expression and run arbitrary code. alert(1) is the classic harmless test: it pops up a box with the number 1. If that box appears, the code ran. This class of bug is called AngularJS template injection, or client-side template injection.

On its own this is Self-XSS: it only fires when the person who uploaded the file opens the dashboard. An attacker cannot make a victim type a file name into their own account, so by itself it is low impact.

The researcher also found an easy way to make it stored for other users by sharing the link to the file: the file gets imported into the recipient's uploaded files dashboard with the name unchanged, which triggers the stored XSS. The report then presents a second, more interesting route through the OAuth flow.

Testing the OAuth flow

Testing the OAuth flow, the goal was to connect the researcher's Dropbox account to other redacted.com users. Why would an attacker want that? Because if the attacker's Dropbox is linked to a victim's account, the victim's account will import the attacker's files, and one of those files can carry the harmful name from the previous section.

The obvious defences were in place:

  • the redirect_uri was properly whitelisted, meaning Dropbox only sends users back to addresses the application registered in advance, so an attacker cannot redirect the code to their own server;
  • the state parameter existed;
  • the auth code could not be used twice.

The researcher also checked whether the application verified the state parameter against the current user session, and it did. That means the final URL coming back from Dropbox could not simply be replayed on another user's account:

HTTP
https://www.redacted.com/account/settings/dropbox-callback?state=********code=***********

Line by line:

  • https://www.redacted.com/account/settings/dropbox-callback is the address on redacted.com that finishes the connection. "Callback" means "call me back here when you are done".
  • state=******** is the random value, hidden in the report.
  • code=*********** is the auth code, also hidden.

If you pasted this URL into another user's browser, the server would compare the state value with the one stored for that user's session, find they do not match, and refuse. So the question became: what was wrong with the implementation?

Exploitation

Step 1: Remove the state parameter

Out of curiosity the researcher removed the state parameter from the URL and used the resulting request on a different user account:

HTTP
https://www.redacted.com/account/settings/dropbox-callback?code=***********

Line by line:

  • The address is the same callback as before.
  • code=*********** is still the auth code from the attacker's Dropbox authorisation.
  • There is no state parameter at all now. The ? is followed directly by code.

Surprisingly, the researcher's Dropbox account was connected to that user. In other words, a single GET request can connect the attacker's Dropbox account to anyone's redacted.com account. There is no option to log in to redacted.com using a Dropbox account, so this is not an account takeover by itself. But combined with the file name XSS, it becomes useful.

Why does removing a parameter help the attacker? Because the server's check was written as "if a state value is present, compare it". When the value is absent, the comparison never runs, and the request is accepted. The defence was there, but it could be skipped by simply not sending the thing it protects.

Step 2: Upload the payload file to Dropbox

The researcher uploaded a file named:

Text
{{constructor.constructor('alert(1)')()}}.jpg

to Dropbox. The report notes that, strangely, this was allowed.

Step 3: Send the OAuth URL without state to the victim

The final OAuth URL from Dropbox, with the state parameter removed, is given to the victim:

HTTP
https://www.redacted.com/account/settings/dropbox-callback?code=***********

This is the same request as in step 1, now aimed at someone else. The attacker only needs the victim to open this link while logged in to redacted.com. A link can be sent by email, chat or any other channel; the report does not describe how it was delivered.

Step 4: The victim imports the attacker's files

The victim's account gets connected to the attacker's Dropbox account and imports all the files from it, including the malicious file.

Step 5: The payload executes

When the user visits the Uploaded File Dashboard, the XSS payload in the file name is executed. The dashboard lists the imported files, including the one whose name is the AngularJS expression. The framework evaluates the name as a template and the alert(1) runs.

Root cause

The server was verifying the state parameter against the current user session, but it was not verifying that the parameter was present at all. The researcher models the server logic as something like:

Text
if(isset($_GET['state'])){    if($_GET['state'] != current_user_state)        ACCESS DENIED        exit()}ACCESS GRANTED

Line by line, in plain words:

  • if(isset($_GET['state'])){ means "if the request contains a state value, then do the following". $_GET is the collection of parameters that arrived in the URL.
  • if($_GET['state'] != current_user_state) means "if the value sent does not equal the value stored for this user's session".
  • ACCESS DENIED and exit() mean "refuse and stop".
  • The closing } ends the outer check.
  • ACCESS GRANTED is what happens when the outer check is skipped, that is, when no state was sent at all.

The flaw is the shape of the logic: the comparison sits inside the "is it present" check, so absence bypasses the comparison instead of failing it. The correct shape would be to refuse when state is missing, and only then compare values.

The whitelisted redirect_uri, the presence of state in the normal flow and the single-use auth code did not help, since the bypass does not touch any of them. The XSS side is AngularJS template injection in the file name, which executes on the uploaded files dashboard.

Impact

An attacker can connect their own Dropbox account to any redacted.com user's account with a single GET request, and the victim then imports the attacker's files, including a file whose name carries the AngularJS payload. When the victim visits the Uploaded File Dashboard, the payload executes in their session. The report does not describe any account takeover via Dropbox login, and states that this is not possible because there is no such login option.

Fix

The change needed is to reject callbacks where state is missing, not only callbacks where it does not match the session, so that the comparison always runs.

Variants to test

These are the follow-up checks suggested by the same reasoning, useful if you want to look for similar bugs elsewhere:

  • Omit the state parameter entirely on OAuth callbacks of other applications and see whether the account still gets linked. A positive result is a successful link with no state value supplied.
  • Send an empty state value (state=) and see whether it is treated as present but unverified.
  • Try the callback with a state value from a different session and confirm it is rejected, to establish that the comparison itself works.
  • Look for other parameters whose presence is checked but whose absence skips validation, such as CSRF tokens or nonces. A nonce is a one-time random value used to prove a request is fresh.
  • Test whether file names are rendered through a client-side template engine on dashboards and import views, using a harmless AngularJS expression as the file name.

Timeline

Date Event
Jan 1, 2019 Report submitted
Jan 2, 2019 Triaged
Jan 8, 2019 Resolved
Jan 15, 2019 Rewarded

"Triaged" means the company accepted the report as a real issue and put it in its queue. "Resolved" means the problem was fixed.

Glossary

  • API: a defined way for one piece of software to talk to another, like a menu of requests a service accepts.
  • Auth code: a short-lived pass that an OAuth provider gives an application so it can obtain access, instead of the user's password.
  • Callback: the address an external service sends the user back to after they approve something.
  • Client-side template injection: a bug where user-controlled text is evaluated as a template expression by a framework running in the browser.
  • client_id: a public identifier for an application in OAuth, like a name badge.
  • Cookie: a small piece of data a site stores in your browser so it can recognise your session on later requests.
  • CSRF (cross-site request forgery): tricking a logged-in user's browser into sending a request the user did not intend.
  • GET request: the simplest web request, made by visiting a URL; it can be triggered by a link.
  • HackerOne: a platform where companies invite researchers to test their systems and report vulnerabilities.
  • Location header: part of a server reply that tells the browser to go to another address.
  • Nonce: a one-time random value used to prove a request is fresh and not replayed.
  • OAuth: a standard that lets an application access another service on a user's behalf without seeing the user's password.
  • redirect_uri: the address an OAuth provider is allowed to send the user back to; it is registered in advance (whitelisted).
  • Self-XSS: harmful code that only runs when the victim themselves enters it, so the attacker and victim are the same person.
  • Session: how a site remembers who you are between requests, usually via a cookie.
  • State parameter: a random value an application sends in an OAuth request and checks on return, to confirm the callback belongs to a flow this user started.
  • Stored XSS: harmful code saved on a site that runs in the browser of anyone who later views the affected page.
  • Synced: in this context, two accounts linked so that files from one can be listed and copied into the other.
  • Triaged: a report accepted by the company as a genuine issue and queued for handling.
  • Whitelisted: explicitly allowed in advance, as with a list of permitted redirect addresses.
  • XSS (cross-site scripting): a bug where an attacker's code runs in another user's browser inside a trusted site.

More by Rohan aggarwal

View all

3 more disclosed reports, best first.

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.