How I Bypassed CORS Using XSS and Stole API Data
The application stored an authentication token in localStorage and had a DOM XSS vulnerability, allowing an attacker to run JavaScript inside the app. The attacker used this to read the token and call an internal API that returned sensitive user data, then sent that data to an external server.
medium.com
Published on medium.com
The original lives on medium.com
We do not hold its text, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the combination of storing an authentication token in localStorage, a DOM XSS vulnerability that allowed arbitrary script execution, and the absence of a Content Security Policy that would have blocked external scripts.
Impact
An attacker could steal authentication tokens, exfiltrate sensitive API data such as names and phone numbers, enumerate users, and abuse permissions.
What the attacker needed
Victim visits a page with the XSS payload; attacker controls an external script host; attacker controls a server to receive data
The fix
The application should store tokens in HttpOnly cookies, implement a Content Security Policy, avoid eval() and dynamic script injection, and sanitise all DOM inputs.
How it was found
- The tester opened the browser console and noticed an API request that used a token from localStorage.
- The tester tried to send data to an external server but the browser blocked it with a CORS error.
- The tester injected a script tag that loaded an external JavaScript file from GitHub.
- The external script read the token from localStorage and used it to call the internal API.
- The script sent the API response to an external webhook server.
- The tester checked the webhook server and saw the stolen user data.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
A website can keep a login token in a place called localStorage, which is readable by any JavaScript running on that page. If an attacker can inject their own JavaScript into the page (a cross-site scripting, or XSS, bug), they can read that token and use it to fetch private data from the site's own API. The browser's same-origin policy normally stops other websites from reading that data, but here the malicious code ran inside the trusted site, so the policy did not help.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.