Self-XSS to rXSS via Uploaded File Name
The other day I found a self XSS that could be escalated to a reflected XSS. The method for doing so used a Javascript object I’d never heard of before and I didn’t find any examples specifically referencing bug bounty so I thought I’d share in case it helps anyone in the future.
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 upload feature on a web application. The upload page contained a form that posted a file to an upload endpoint using multipart/form-data:
<html>
<body>
<form enctype="multipart/form-data" action="upload" method="POST">
Upload file<input name="theFile" type="file">
<input type="submit" value="Upload file now">
</form>
</body>
</html>
After the form was posted, the application displayed the name of the uploaded file on the resulting page. Uploading RCE_Please.php, for example, produced a page that read "Thanks for uploading RCE_Please.php". The file name was displayed back on the resulting page.
Setup
No accounts, roles, tools or versions are described. The report describes no accounts, roles, tools or versions. The researcher's own framing is that the technique is worth sharing because the JavaScript object involved was unfamiliar and no bug bounty examples referencing it were found.
Finding it
The self-XSS was straightforward: the uploaded file name was rendered into the page without adequate handling, so a file named with a script tag executed in the uploader's own browser.
<script>alert(document.domain)</script>
On its own this is only self-XSS, because the payload runs in the session of the person who uploaded the file. The interesting question was whether the same reflection could be triggered from a page the attacker controls, which would turn it into a reflected XSS against any victim who visited that page.
Exploitation
Step 1: try a GET request
The first attempt was to see whether the reflected parameter could be supplied through a GET request. It could not.
Step 2: cross-origin POST with XMLHttpRequest
The second attempt was to use an XMLHTTPRequest from an attacker-controlled page to POST the required payload to the target. This failed because of CORS.
Step 3: build the form and file in JavaScript
The third attempt was to use JavaScript to populate the form and set the name of the uploaded file. This was harder than expected: browsers place protections on modifying an input's file array, specifically to stop attackers uploading arbitrary files from a victim's computer. The researcher notes that defaults cannot even be set in HTML. The workaround came from a Stack Overflow answer, which pointed to the DataTransfer object.
The malicious page below can be hosted by an attacker; when a victim navigates to it, the reflected XSS fires on the target domain.
<html>
<body>
<form id="theForm" enctype="multipart/form-data" action="https://target.domain/upload" method="POST">
<input id="theInput" name="theFile" type="file">
<input type="submit" value="Upload file now">
</form>
<script>
setTimeout(() => {
const f = document.getElementById("theForm");
const i = document.getElementById("theInput");
const dt = new DataTransfer()
const files = [
new File(['content'], 'maliciousfilename.txt<img src=x onerror=alert(document.domain)>')
];
files.forEach(f => dt.items.add(f));
i.files = dt.files;
f.submit();
}, 1000);
</script>
</body>
</html>
The important parts:
- The form's
actionpoints at the target's upload endpoint, so the browser submits the multipart request to the target rather than to the attacker's origin. - A
DataTransferobject is created and aFileis added to it. The file's name is the payload:maliciousfilename.txt<img src=x onerror=alert(document.domain)>. Theimgtag with a brokensrcfiresonerror, which runsalert(document.domain). i.files = dt.filesassigns the constructed file list to the file input, bypassing the restriction on setting file contents directly.f.submit()posts the form. The target reflects the file name into its response, and the injectedimgtag executes in the target's origin.- The
setTimeoutof 1000 ms exists only so a triager can see the attacker's page before the browser is redirected to the target.
Root cause
The application echoes the uploaded file name into the response page without encoding it, so markup in the name is interpreted by the browser. The self-XSS is the direct consequence. The escalation works because the upload endpoint accepts a cross-origin form submission: a form POST is not subject to the same CORS restrictions that blocked the XMLHTTPRequest attempt, and the browser-level protection on file inputs can be sidestepped with DataTransfer. The report does not describe any server-side validation or output encoding that would have prevented the reflection.
Impact
An attacker who can host a page and persuade a victim to visit it can cause script to execute in the target's origin in the victim's browser, using the victim's session against the application. The report does not state which actions that script could perform or what data it could reach; the demonstrated effect is alert(document.domain). The attack requires the victim to navigate to the attacker-controlled page, and the target upload endpoint must be reachable from that page.
Fix
A fix would be to HTML-encode the file name before rendering it into the response.
Variants to test
- Upload forms that echo the file name: submit a file named with a harmless marker such as
<b>test</b>and check whether the response contains literal markup or encoded text. A positive result is the marker being rendered as HTML. - GET-based reflections of the same parameter: try supplying the value in the query string. A positive result is the payload reflected and executed without a POST.
- Cross-origin form submission: host a page that posts to the upload endpoint and check whether the response is processed by the browser. A positive result is the reflected payload executing on the target origin.
- File input manipulation: on any page that uploads files, try assigning a
DataTransfer-built file list to the input and submitting. A positive result is the constructed file name reaching the server. - Other reflected fields in upload responses, such as content type or size labels: inject a marker and check for unencoded output. A positive result is execution of the marker in the target's origin.
What happened, in plain words
A website had a page where you could upload a file. After the upload finished, the site showed a thank-you message that included the name of the file you had just uploaded. For example, if you uploaded a file called RCE_Please.php, the page said "Thanks for uploading RCE_Please.php".
The researcher noticed that the file name was placed into the page as-is, without being cleaned up. That means if the file name contained something that looks like HTML or JavaScript, the browser would treat it as code rather than as plain text. A file named <script>alert(document.domain)</script> made the browser run that script.
On its own, that is only a problem for the person who uploaded the file: they are attacking themselves. This is called self-XSS. The interesting part is whether an attacker could make someone else's browser upload such a file, so that the script runs in the victim's browser on the target site. That would be a reflected XSS, a much more serious bug.
The researcher tried three approaches. The first two failed. The third worked, using a browser feature called DataTransfer that most people have never heard of. This article walks through the whole story, explaining each concept from scratch.
The upload page
The page contained a form. A form is the part of a web page that lets you type things or choose files and then send them to the server, usually by clicking a button. Here is the code of the page:
<html>
<body>
<form enctype="multipart/form-data" action="upload" method="POST">
Upload file<input name="theFile" type="file">
<input type="submit" value="Upload file now">
</form>
</body>
</html>
Line by line:
<html>and<body>are the outer wrappers of every web page. They tell the browser that what follows is a page and its visible content.<form ...>starts the form. The attributes on it describe how the form will send its data.enctype="multipart/form-data"tells the browser how to package the data. When a form includes a file, the browser cannot send it as ordinary text; it uses a special format called multipart/form-data, which bundles each field (including the file's contents and its name) as a separate part. Think of it like putting each item in its own labelled envelope inside one big parcel.action="upload"is the address the form sends its data to. Here it is a relative address, meaning "theuploadpath on the same site".method="POST"is the way the browser talks to the server. GET is used when you are just asking for a page (like typing an address in the address bar). POST is used when you are sending data that changes something on the server, such as an upload. The difference matters later in the story.<input name="theFile" type="file">is the field where the user picks a file from their computer. Thenameis the label the server uses to recognise this field.<input type="submit" value="Upload file now">is the button that sends the form.
When the form was submitted, the server responded with a page that said something like "Thanks for uploading RCE_Please.php". The file name had been copied from the upload into the response page. In security language, the name was reflected: it went out from the user and came straight back in the page.
The self-XSS
The researcher uploaded a file whose name was itself a piece of JavaScript wrapped in an HTML tag:
<script>alert(document.domain)</script>
What this does:
<script>is an HTML tag that tells the browser "everything until</script>is JavaScript code, please run it".alert(...)is a JavaScript function that pops up a dialog box with a message.document.domainis a JavaScript expression that returns the domain name of the page currently open, for exampleexample.com.
So the whole thing means: "show a pop-up containing the domain of the page I am on". If the pop-up appears, the browser has run the attacker's code as part of the target site. That is the proof that the site is vulnerable.
Why is this called self-XSS? Because the only person affected is the one who uploaded the file. The script runs in the uploader's own browser, in their own session. A session is the state of being logged in: the server remembers who you are, usually by giving your browser a small piece of data called a cookie that it sends back with every request. If you attack yourself, you gain nothing. The bug only becomes dangerous if an attacker can make another person's browser perform the same upload.
Trying to make someone else trigger it
The goal now is to build a page, hosted by the attacker, that a victim might visit. When the victim opens it, their browser should upload a file with a malicious name to the target site, and the target site should then show the reflected script, which runs in the victim's browser. This is the step from self-XSS to reflected XSS.
First attempt: can the value come in a GET request?
The first idea was to see whether the file name could be supplied through a GET request, that is, by putting the value in the URL's query string (the part after the ?). If the site accepted it that way, the attacker could simply send the victim a link. It did not work.
Second attempt: send the POST from JavaScript
The second idea was to use JavaScript on the attacker's page to send a POST request to the target. The tool for this in a browser is XMLHttpRequest, an older API for making requests from JavaScript. An API is simply a set of functions a program offers so that other programs can use it; here, the browser offers JavaScript a way to talk to servers.
This failed because of CORS. CORS stands for Cross-Origin Resource Sharing. Browsers have a rule called the same-origin policy: a page from one site (one origin) is not allowed to read responses from another site. An origin is the combination of protocol, host and port, for example https://example.com. The rule exists so that a malicious page cannot quietly fetch your bank's pages using your logged-in session. CORS is the mechanism by which a server can deliberately relax that rule and say "other sites may read my responses". The report says the attempt failed because of CORS.
Third attempt: build the form and the file in JavaScript
The third idea was to use JavaScript to fill in the form and set the file name, then submit the form normally. This turned out to be harder than expected, because browsers deliberately protect the file input. You cannot simply tell a file input "pretend the user chose a file called X". The browser enforces this so that a malicious page cannot silently grab files from your computer and upload them. The researcher notes that you cannot even set a default file in the HTML itself.
After some searching, the researcher found a Stack Overflow answer that pointed to a browser object called DataTransfer. DataTransfer is normally used behind the scenes when you drag and drop files in a browser: it is the container that holds the files being dragged. Because it is a general-purpose container, JavaScript is allowed to create one and put files into it. And a file input will accept a file list that comes from a DataTransfer. That is the loophole.
Here is the malicious page the attacker can host:
<html>
<body>
<form id="theForm" enctype="multipart/form-data" action="https://target.domain/upload" method="POST">
<input id="theInput" name="theFile" type="file">
<input type="submit" value="Upload file now">
</form>
<script>
setTimeout(() => {
const f = document.getElementById("theForm");
const i = document.getElementById("theInput");
const dt = new DataTransfer()
const files = [
new File(['content'], 'maliciousfilename.txt<img src=x onerror=alert(document.domain)>')
];
files.forEach(f => dt.items.add(f));
i.files = dt.files;
f.submit();
}, 1000);
</script>
</body>
</html>
Line by line:
- The
<form>is almost the same as the original upload form, but nowaction="https://target.domain/upload"points at the target site's upload address. Theid="theForm"gives the form a name so JavaScript can find it later. - The file input has
id="theInput"for the same reason. <script>starts the JavaScript block.setTimeout(() => { ... }, 1000)means "wait 1000 milliseconds (one second), then run the code inside the braces". The researcher explains the delay is only there so that a triager (the person reviewing the bug report) can see the attacker's page before the browser moves on to the target.const f = document.getElementById("theForm");finds the form on the page and stores it in a variable calledf.const i = document.getElementById("theInput");does the same for the file input, stored ini.const dt = new DataTransfer()creates an empty DataTransfer container, the same kind of object the browser uses for drag and drop.const files = [ ... ]creates a list containing one file.new File(['content'], 'maliciousfilename.txt<img src=x onerror=alert(document.domain)>')builds that file:'content'is its contents (any text will do), and the second argument is its name. The name is the payload.files.forEach(f => dt.items.add(f));goes through the list and adds each file to the DataTransfer container.i.files = dt.files;takes the file list out of the DataTransfer and assigns it to the file input. This is the step that gets around the browser's protection: the input now believes a file has been chosen.f.submit();sends the form, exactly as if the user had clicked the button. The browser posts the multipart request tohttps://target.domain/upload.
Why does the payload work? The file name is:
maliciousfilename.txt<img src=x onerror=alert(document.domain)>
maliciousfilename.txtis ordinary text, so the file looks like a normal upload.<img src=x onerror=...>is an HTML tag for an image.src=xtells the browser to load an image from the addressx, which does not exist.onerror=...is an attribute that tells the browser what to do if the image fails to load. Sincexwill always fail, the browser runs the code inonerror, which isalert(document.domain).
So when the target site reflects the file name into its response page, the browser sees the img tag, tries to load the image, fails, and runs the script. The script runs in the target site's origin, in the victim's browser.
Why it worked
There are two separate problems that combine:
- The site copies the file name into its response page without encoding it. Encoding means converting special characters into a safe form. For example,
<becomes<, which the browser displays as the character<instead of treating it as the start of a tag. Because the site did not encode, the browser interpreted the name as HTML. - The browser's protection on file inputs can be bypassed with DataTransfer.
The report does not describe any server-side validation or output encoding that would have prevented the reflection.
What an attacker could do
An attacker who can host a page and persuade a victim to visit it can make script run in the target site's origin, in the victim's browser, using the victim's session. The report does not say which actions that script could perform or what data it could reach; the demonstrated effect is the pop-up showing document.domain. The attack requires the victim to visit the attacker's page, and the target's upload endpoint must be reachable from that page.
How to fix it
A fix would be to HTML-encode the file name before putting it into the response page.
Things to try on other sites
- Upload forms that echo the file name: upload a file named with a harmless marker such as
<b>test</b>and check whether the response shows literal markup or encoded text. If the marker is rendered as bold text, the site is reflecting unencoded input. - GET-based reflections of the same value: try putting the value in the query string. If the payload is reflected and runs without a POST, that is a finding.
- Cross-origin form submission: host a page that posts to the upload endpoint and check whether the browser processes the response. If the reflected payload runs on the target origin, that is a finding.
- File input manipulation: on any page that uploads files, try assigning a DataTransfer-built file list to the input and submitting. If the constructed file name reaches the server, that is a finding.
- Other reflected fields in upload responses, such as content type or size labels: inject a marker and check for unencoded output. If the marker runs in the target's origin, that is a finding.
Glossary
- API: a set of functions a program offers so that other programs can use it; here, the browser's built-in functions that JavaScript can call.
- CORS (Cross-Origin Resource Sharing): a way for a server to tell browsers that pages from other sites are allowed to read its responses.
- Cookie: a small piece of data a site stores in your browser and sends back with each request, often used to remember that you are logged in.
- Cross-origin: belonging to a different origin (protocol, host and port) than the page making the request.
- DataTransfer: a browser object normally used to hold files during drag and drop; JavaScript can create one and put files into it.
- Encoding (HTML encoding): converting special characters such as
<into safe text such as<so the browser displays them instead of treating them as markup. - Form: the part of a web page that collects input and sends it to a server.
- GET: a request that asks for a resource, with data in the URL; used when you type an address or click a link.
- HTML: the language that describes the structure and content of web pages.
- JavaScript: the programming language that runs in browsers and makes pages interactive.
- multipart/form-data: a format for sending form data that includes files, bundling each field as a separate part.
- Origin: the combination of protocol, host and port, for example
https://example.com. - POST: a request that sends data to a server, typically to create or change something, such as an upload.
- Reflected XSS: a cross-site scripting bug where input from the attacker is echoed back in a page and runs as code in the victim's browser.
- Same-origin policy: the browser rule that a page from one origin cannot read responses from another origin.
- Self-XSS: a cross-site scripting bug where the payload only runs in the browser of the person who entered it, so the attacker is attacking themselves.
- Session: the state of being logged in, which the server tracks, usually with a cookie.
- setTimeout: a JavaScript function that runs code after a delay, measured in milliseconds.
- Stack Overflow: a question-and-answer website for programmers.
- Triager: the person who reviews a bug report and decides whether it is valid.
- XMLHttpRequest: an older browser API that lets JavaScript send requests to a server.
- XSS (cross-site scripting): a class of bug where an attacker's code runs in another user's browser as part of a website.