CVE-2022-25937 (published 2023-02-13T05:15Z)

Versions of the package glance before 3.0.9 are vulnerable to Directory Traversal that allows users to read files outside the public root directory. This is related to but distinct from the vulnerability reported in [CVE-2018-3715](https://security.snyk.io/vuln/npm:glance:20180129).\r\r

CVSS v3 base score: 6.5, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:L/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/jarofghosts/glance
commit_details: repo_url: https://github.com/jarofghosts/glance
author: Jesse Keane
author_date: 2023-02-01 21:08:38-08:00
msg: Fix path traversal vulnerability
repository: repo_url: https://github.com/jarofghosts/glance
repo_name: jarofghosts/glance
description: provide a quick peek
repo_language: JavaScript
owner: jarofghosts
file_changes: filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -24,7 +24,7 @@ function Glance(options) {
   this.port = options.port
   this.hideindex = options.hideindex
   this.indices = options.indices
-  this.dir = path.normalize(options.dir)
+  this.dir = path.resolve(options.dir)
   this.nodot = options.nodot
 
   return this
@@ -72,7 +72,7 @@ Glance.prototype.serveRequest = function Glance$serveRequest(req, res) {
   request.response = res
 
   // prevent traversing directories that are parents of the root
-  if (request.fullPath.slice(0, self.dir.length) !== self.dir) {
+  if (path.relative(self.dir, request.fullPath).startsWith('..')) {
     return self.emit('error', 403, request, res)
   }
 
@@ -193,10 +193,10 @@ function renderPage(title, body, res) {
 
 function errorTitle(errorCode) {
   var mappings = {
-    '404': 'File Not Found',
-    '403': 'Forbidden',
-    '405': 'Method Not Allowed',
-    '500': 'Internal Server Error',
+    404: 'File Not Found',
+    403: 'Forbidden',
+    405: 'Method Not Allowed',
+    500: 'Internal Server Error',
   }
   return mappings[errorCode.toString()]
 }

programming_language: JavaScript
filename: secret.txt
change_type: ModificationType.ADD
diff: @@ -0,0 +1 @@
+wee
\ No newline at end of file

programming_language: SQL
filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -1,4 +1,5 @@
 var http = require('http')
+var net = require('net')
 
 var test = require('tape')
 
@@ -74,8 +75,38 @@ test('403s on dir list if configured', function (t) {
 test('fails if path traversal is attempted', function (t) {
   t.plan(1)
 
-  http.get('http://localhost:1666/../index.js', function (res) {
-    t.notStrictEqual(res.statusCode, 200)
+  var socket = new net.Socket()
+  socket.connect(1666, 'localhost', function () {
+    socket.on('data', function (data) {
+      var result = data.toString().split('\n')[0]
+      t.equals(result.trim(), 'HTTP/1.1 403 Forbidden')
+      socket.end()
+    })
+    socket.write(`GET /../index.js HTTP/1.1
+Host: localhost
+user-agent: test/1.2.3
+accept: */*
+
+`)
+  })
+})
+
+test('fails if path traversal with conveniently-named directory is attempted', function (t) {
+  t.plan(1)
+
+  var socket = new net.Socket()
+  socket.connect(1666, 'localhost', function () {
+    socket.on('data', function (data) {
+      var result = data.toString().split('\n')[0]
+      t.equals(result.trim(), 'HTTP/1.1 403 Forbidden')
+      socket.end()
+    })
+    socket.write(`GET /../glance-test-exploit/secret.txt HTTP/1.1
+Host: localhost
+user-agent: test/1.2.3
+accept: */*
+
+`)
   })
 })
 

programming_language: JavaScript