Skip to content
XSS Disclosed report Beginner CVE-2018-16484

[m-server] HTML Injection in filenames displayed as directory listing in the browser allows to embed iframe with malicious JavaScript code

The m-server module builds an HTML directory listing without escaping filenames, so a file with a crafted name containing HTML is rendered as markup. An attacker who can place such a file on the server can cause JavaScript from a second file to run in a visitor's browser when the listing is viewed.

bl4de Follow

Originally disclosed on HackerOne

Medium 6.0 View original
  • Fixed in 29 days
  • Disclosed after 4 months

I would like to report Stored XSS vulnerability in m-server module.

m-server displays content of selected directory as HTML in the browser. However, no escape is implemented which allows malicious user to embed executable JavaScript or HTML code (eg. to load HTML document into iframe element and execute JavaScript from within loaded file).

Module

module name: m-server version: 1.4.0 npm page: https://www.npmjs.com/package/m-server

Module Description

M-Server is a mini http static server that without any dependencies;

Module Stats

Stats 6 downloads in the last day 68 downloads in the last week 180 downloads in the last month

~2200 estimated downloads per year

Vulnerability Description

No HTML special characters escape is implememtned in the function which constructs and returns HTML with current directory listing:

JavaScript
// node_modules/m-server/lib/utils.js, line 57

    dirs.sort(sort).forEach(function (val) {
        html.push('<li><a href="' + path + '/' + val + '">' + val + '</a></li>');
    });
    files.sort(sort).forEach(function (val) {
        html.push('<li><a download href="' + path + '/' + val + '">' + val + '</a></li>');
    });
    html.push('</ul>');
    return html.join('');

Steps To Reproduce:

  • install m-server module:
$ npm install m-server
  • create malware_frame.html file with following content:
HTML
<html>

<head>
    <meta charset="utf8" />
    <title>Frame embeded with malware :P</title>
</head>

<body>
    <p>iframe element with malicious code</p>
    <script>
        alert('Uh oh, I am bad, bad malware!!!')
    </script>
</body>

</html>
  • in the same directory, create another file with following name:
"><iframe src="malware_frame.html">
  • run m-server in the same directory, where two above files exist:
$ ./node_modules/m-server/index.js -p 8080
-------------------------------------------------------------
	Mini http server running on port 8080 !
	You can open the floowing urls to view files.
	127.0.0.1:8080
	10.235.1.22:8080
	10.235.4.26:8080
	Have fun ^_^
-------------------------------------------------------------

  • malicious frame is embedded and JavaScript code from malware_frame.html executed immediately:
1.jpg
1.jpg From the original on HackerOne

Both files can be uploaded by malicious user if eg. other vunerabilities in other applications exist on the same server (eg. upload file feature) or if attacker gains an access to the server using poorly secured remote access.

Patch

escape-html module can be used to prevent this attack (https://www.npmjs.com/package/escape-html):

JavaScript
const escapeHtml = require('escape-html')

(...)

// vulnerable part of node_modules/m-server/lib/utils.js:
dirs.sort(sort).forEach(function (val) {
    html.push('<li><a href="' + path + '/' + escape(val) + '">' + escape(val) + '</a></li>');
});
files.sort(sort).forEach(function (val) {
    html.push('<li><a download href="' + path + '/' + escape(val) + '">' + escape(val) + '</a></li>');
});
html.push('</ul>');
return html.join('');

Supporting Material/References:

  • Operating system: Ubuntu 16.04
  • Node.js 8.9.4
  • npm v. 5.6.0
  • curl 7.47.0

Wrap up

  • 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 filename when directory listing is displayed in the browser

The hacker selected the Cross-site Scripting (XSS) - Stored weakness. This vulnerability type requires contextual information from the hacker. They provided the following answers:

URL http://localhost:8080

Verified Yes

The original report as published on HackerOne, by bl4de. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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-listing HTML is built by concatenating raw filenames into markup without HTML-escaping them.

Impact

A malicious user could inject an iframe with JavaScript through a crafted filename when the directory listing is displayed, and the script runs in the browser of anyone viewing that listing.

What the attacker needed

Ability to place files in the served directory; victim opens the directory listing

The fix

The report suggests escaping filenames with the escape-html module before inserting them into the generated HTML.

How it was found

  1. Install the m-server module.
  2. Create an HTML file containing a script that shows an alert.
  3. Create another file whose name contains an iframe tag pointing at the first file.
  4. Start m-server in that directory.
  5. Open the directory listing in a browser; the iframe is embedded and the script runs.

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 in a folder as a web page. This server puts each filename straight into the page's HTML without cleaning it, so a filename that looks like HTML tags is treated as real tags. That lets someone who can add files to the folder make the page load and run their own script in the browser of anyone who opens the listing.

What is XSS?

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.

More by bl4de

View all

42 more disclosed reports, best first.

More from Node.js third-party modules

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.