CVE-2022-25936 (published 2023-01-30T05:15Z)

Versions of the package servst before 2.0.3 are vulnerable to Directory Traversal due to improper sanitization of the filePath variable.\r\r

CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/andrepolischuk/servst
commit_details: repo_url: https://github.com/andrepolischuk/servst
author: Andrey Polischuk
author_date: 2023-01-24 13:17:02+03:00
msg: fix: fix limitation of a file path

https://cwe.mitre.org/data/definitions/22.html
repository: repo_url: https://github.com/andrepolischuk/servst
repo_name: andrepolischuk/servst
description: Simple file server
repo_language: JavaScript
owner: andrepolischuk
file_changes: filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -6,7 +6,7 @@ var mime = require('mime');
 
 module.exports = function(root) {
   if (typeof root !== 'string') return;
-  root = path.normalize(root);
+  root = path.normalize(root + '/');
 
   return function(req, res, fn) {
     if (!/^(GET|HEAD)$/.test(req.method)) return fn();

programming_language: JavaScript
filename: test.js
change_type: ModificationType.MODIFY
diff: @@ -3,7 +3,7 @@ var test = require('ava');
 var http = require('http');
 var request = require('supertest');
 var servst = require('./');
-var statics = servst(__dirname);
+var statics = servst(__dirname + '/fixture/public');
 
 var app = http.createServer(function (req, res) {
   statics(req, res, function (err) {
@@ -29,9 +29,32 @@ test.cb('return 200 for GET /', function (t) {
     });
 });
 
-test.cb('return 200 for GET /test.js', function (t) {
+test.cb('return 200 for GET /index.js', function (t) {
+  request(app)
+    .get('/index.js')
+    .expect(200)
+    .expect('Content-Type', /javascript/)
+    .end(function (err) {
+      t.ifError(err);
+      t.end();
+    });
+});
+
+test.cb('return 404 for GET /test.js', function (t) {
   request(app)
     .get('/test.js')
+    .expect(404)
+    .expect('Content-Type', /text/)
+    .expect('Not found')
+    .end(function (err) {
+      t.ifError(err);
+      t.end();
+    });
+});
+
+test.cb('return 200 for GET relative /public/index.js', function (t) {
+  request(app)
+    .get('/../public/index.js')
     .expect(200)
     .expect('Content-Type', /javascript/)
     .end(function (err) {
@@ -40,9 +63,33 @@ test.cb('return 200 for GET /test.js', function (t) {
     });
 });
 
-test.cb('return 404 GET /test2.js', function (t) {
+test.cb('return 404 for GET relative /public/test.js', function (t) {
+  request(app)
+    .get('/../public/test.js')
+    .expect(404)
+    .expect('Content-Type', /text/)
+    .expect('Not found')
+    .end(function (err) {
+      t.ifError(err);
+      t.end();
+    });
+});
+
+test.cb('return 404 for GET relative /private/index.js', function (t) {
+  request(app)
+    .get('/../private/index.js')
+    .expect(404)
+    .expect('Content-Type', /text/)
+    .expect('Not found')
+    .end(function (err) {
+      t.ifError(err);
+      t.end();
+    });
+});
+
+test.cb('return 404 for GET relative /public-isprivate/index.js', function (t) {
   request(app)
-    .get('/test2.js')
+    .get('/../public-isprivate/index.js')
     .expect(404)
     .expect('Content-Type', /text/)
     .expect('Not found')

programming_language: JavaScript