[sexstatic] HTML injection in directory name(s) leads to Stored XSS when malicious file is embed with <iframe> element used in directory name
The sexstatic static file server did not sanitise the directory path when generating the HTML index page, allowing a directory name containing HTML to be injected into the page. An attacker could create a directory with a crafted name and embed an iframe that executes JavaScript when a user clicks the directory link.
- Triaged in 9 days
- Fixed in 2 months
- Disclosed after 2 months
I would like to report HTML Injection vulnerability in sexstatic module. It is possible to use HTML in directory names, which might lead to run arbitrary JavaScript code in the browser.
Module
module name: sexstatic version: 0.6.2 npm page: https://www.npmjs.com/package/sexstatic
Module Description
A simple static file server middleware. Use it with a raw http server, express/connect, or flatiron/union!
Also adds the ability to arbitrarily modify output HTML for whatever reason. And adding additional: simple url -> string handlers simple url -> function handlers
see opts.extras if you wanna get what i'm talking about
I may or may not extend this with further addons in the future to suit my needs.
Module Stats
Stats 4 downloads in the last day 29 downloads in the last week 158 downloads in the last month
Vulnerability Description
sexstatic can be used as static file server and presents directory index in the browser. Output is correctly sanitized against any attempts of HTML or JavaScript injection, due to sanitization done in sexstatic/lib/sexstatic/showdir.js:
// sexstatic/lib/sexstatic/showdir.js, line 132
var displayName = he.encode(file[0]) + ((isDir)? '/':'');
// TODO: use stylessheets?
html += '<tr>' +
'<td><code>(' + permsToString(file[1]) + ')</code></td>' +
'<td style="text-align: right; padding-left: 1em"><code>' + sizeToString(file[1], humanReadable, si) + '</code></td>' +
'<td style="padding-left: 1em"><a href="' + href + '">' + displayName + '</a></td>' +
'</tr>\n';
However, the same sanitization is missing from this part:
// sexstatic/lib/sexstatic/showdir.js, line 106
var html = [
'<!doctype html>',
'<html>',
' <head>',
' <meta charset="utf-8">',
' <title>Index of ' + pathname +'</title>',
' </head>',
' <body>',
'<h1>Index of ' + pathname + '</h1>'
].join('\n') + '\n';
pathname variable is used without sanitization.
This allows malicious user to use valid HTML code as directory name.
Below I present complete attack scenario with this vulnerability.
Steps To Reproduce:
- install
sexstaticmodule:
$ npm install sexstatic
- in the directory which will be used as root for
sexstatic, create directory with following name:"><iframe src="malware_frame.html">/ - in created directory, create file
malware_frame.htmlwith following content:
<!-- malware_frame.html -->
<html>
<head>
<meta charset="utf8" />
<title>Frame embeded with malware downloader :P</title>
</head>
<body>
<p>iframe element with malicious code</p>
<script>
alert('Uh oh, I am bad, bad malware!!!')
</script>
</body>
</html>
- run
sexstatic:
$ ./node_modules/sexstatic/lib/sexstatic.js -p 8080
sexstatic serving /home/rafal.janicki/playground/hackerone/Node at http://0.0.0.0:8080
- go to
http://localhost:8080to see directory index:

-
now, click on
"><iframe src="malware_frame.html">/directory name on the files list -
malicious JavaScript code from
malware_frame.htmlfile is executed immediately:

Patch
pathname used in HTML output can be sanitized with the same he.encode() function, as it is already done for every filename.
Supporting Material/References:
- Operating system: Ubuntu 16.04
- Node.js 8.9.4
- npm v. 5.6.0
- Chromium 67.0.3367.0 (Developer Build) (64-bit)
Wrap up
I hope my report will help keeping Node.js Ecosystem users safe :)
- I contacted the maintainer to let him know: [N]
- I opened an issue in the related repository: [N]
Regards,
Rafal 'bl4de' Janicki
Impact
Malicious user is able to inject iframe element with malicious JavaScript code via crafted directory name and trick users to open this directory in the browser.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The directory path (pathname) was inserted into the HTML index page without HTML encoding, unlike file names which were encoded.
Impact
A malicious user could inject an iframe with JavaScript via a crafted directory name and trick users into opening that directory, leading to script execution in the browser.
What the attacker needed
Attacker can create a directory with a crafted name; victim clicks the crafted directory link
The fix
The pathname should be sanitised with the same HTML encoding function used for file names.
How it was found
- Install the sexstatic module.
- Create a directory with a name containing HTML that includes an iframe pointing to a malicious file.
- Place a malicious HTML file inside that directory.
- Start the sexstatic server.
- Visit the server root to see the directory index.
- Click the crafted directory name to trigger the stored script.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A static file server shows a list of files and folders. The folder name was inserted into the web page without cleaning, so a folder named with HTML code could run a script. An attacker who can create folders could trick someone into clicking the folder and running malicious code.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.