Skip to content
Path traversal Disclosed report Intermediate

Path Transversal inside saveContracts.js

The saveContracts.js script concatenates filenames from a directory listing directly into a file path without sanitisation, so a filename containing directory traversal sequences could cause the script to read files outside the intended directory. The author claims this would let an attacker read arbitrary files on the server.

caon Follow

Originally disclosed on HackerOne

Informational View original
  • Disclosed after 1 hour

Reference: https://portswigger.net/web-security/file-path-traversal

Directory traversal (also known as file path traversal) is a web security vulnerability that allows an attacker to read arbitrary files on the server that is running an application. This might include application code and data, credentials for back-end systems, and sensitive operating system files. In some cases, an attacker might be able to write to arbitrary files on the server, allowing them to modify application data or behavior, and ultimately take full control of the server.

Inside https://github.com/Sifchain/sifnode/blob/develop/smart-contracts/scripts/saveContracts.js there's a part of the code which is not sanitized; meaning it could allow a path transversal to happen.

JavaScript
function readFiles(dirname, onFileContent, onError) {
  fs.readdir(dirname, function(err, filenames) {
    if (err) {
      onError("The build/contracts directory does not exist.\n\nMake sure the build directory exists before running this script.\n\nTo create build directory run `truffle deploy --network develop`\n\n");
      return;
    }
    filenames.forEach(function(filename) {
      fs.readFile(dirname + filename, 'utf-8', function(err, content) {  <<<<<< VULNERABLE
        if (err) {
          onError(filename, err);
          return;
        }
        onFileContent(filename, content);
      });
    });
  });
}

Caller:

JavaScript
readFiles("build/contracts/", handleFileContents, handleError);

Explanation:

readFiles() function calls the build/contracts/ path, let' says a file named ../../../../etc/passwd exists inside the folder. Inside readFiles the first part of the script will grab all filenames, meaning it will grab the ../../../../etc/passwdfile. After grabbing the filenames It will proceed to call fs.readFile to each of the files. When fs.readFile happens to ../../../../etc/passwd the call will be something like that:

build/contracts/../../../../etc/passwd

Once executed it will show the passwd file containing all users and password of the machine because /../../../../ will force the path to root.

Impact

This transversal allows an attacker to read arbitrary files on the server.

#Fix

Sanitize the inputs in the filename variable If you are using node or express it is a good idea to follow this https://stackoverflow.com/questions/46718772/how-i-can-sanitize-my-input-values-in-node-js/46719000

Kind regards Caon

The original report as published on HackerOne, by caon. 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

Likely the script builds a file path by concatenating a directory prefix with an unsanitised filename taken from the directory listing, without checking that the resulting path stays inside the intended directory.

Impact

The author claims this could allow an attacker to read arbitrary files on the server, but the report does not show a realistic way for an attacker to control filenames in that directory.

What the attacker needed

Ability to place a crafted filename in build/contracts

The fix

The filename variable should be sanitised before being used to build the file path.

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 program that reads files from a folder should only read files inside that folder. Here the script trusts whatever name it finds and sticks it onto a folder path, so a name like '../../etc/passwd' can climb out of the folder and reach other files on the machine. This is called path traversal.

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 Sifchain

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.