CVE-2022-1850 (published 2022-05-24T15:15Z)

Path Traversal in GitHub repository filegator/filegator prior to 7.8.0.

Severity: MEDIUM
CVSS v2 base score: 5.5 (AV:N/AC:L/Au:S/C:P/I:P/A:N)
CVSS v3 base score: 8.1, HIGH (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/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/filegator/filegator
commit_details: repo_url: https://github.com/filegator/filegator
author: Milos Stojanovic
author_date: 2022-05-24 12:24:30+02:00
msg: path traversal fix
repository: repo_url: https://github.com/filegator/filegator
repo_name: filegator/filegator
description: Powerful Multi-User File Manager
repo_language: PHP
owner: filegator
file_changes: filename: Filesystem.php
change_type: ModificationType.MODIFY
diff: @@ -241,6 +241,7 @@ class Filesystem implements Service
         ) {
             $path = $this->separator;
         }
+
         return $this->joinPaths($this->getPathPrefix(), $path);
     }
 
@@ -266,6 +267,9 @@ class Filesystem implements Service
 
     private function joinPaths(string $path1, string $path2): string
     {
+        $path1 = $this->escapeDots($path1);
+        $path2 = $this->escapeDots($path2);
+
         if (! $path2 || ! trim($path2, $this->separator)) {
             return $this->addSeparators($path1);
         }
@@ -295,4 +299,14 @@ class Filesystem implements Service
 
         return  (string) array_pop($tmp);
     }
+
+    private function escapeDots(string $path): string
+    {
+        $path = preg_replace('/\\\+\.{2,}/', '', $path);
+        $path = preg_replace('/\.{2,}\\\+/', '', $path);
+        $path = preg_replace('/\/+\.{2,}/', '', $path);
+        $path = preg_replace('/\.{2,}\/+/', '', $path);
+
+        return $path;
+    }
 }

programming_language: PHP
filename: FilesystemTest.php
change_type: ModificationType.MODIFY
diff: @@ -461,6 +461,18 @@ class FilesystemTest extends TestCase
         $this->assertEquals('/john/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['..']));
         $this->assertEquals('/john/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['../']));
         $this->assertEquals('/john/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['/sub/../../']));
+        $this->assertEquals('/john/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['..\\']));
+        $this->assertEquals('/john/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['..\\\\']));
+        $this->assertEquals('/john/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['..\\..\\']));
+        $this->assertEquals('/john/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['\\\\..']));
+        $this->assertEquals('/john/', $this->invokeMethod($this->storage, 'applyPathPrefix', ['\\..\\..']));
+        $this->assertEquals('/john/\\.', $this->invokeMethod($this->storage, 'applyPathPrefix', ['\\.\\...']));
+        $this->assertEquals('/john/\\.', $this->invokeMethod($this->storage, 'applyPathPrefix', ['\\.\\....']));
+        $this->assertEquals('/john/.\\.', $this->invokeMethod($this->storage, 'applyPathPrefix', ['.\\.\\...']));
+        $this->assertEquals('/john/.', $this->invokeMethod($this->storage, 'applyPathPrefix', ['..\\.\\...']));
+        $this->assertEquals('/john/.', $this->invokeMethod($this->storage, 'applyPathPrefix', ['..\\.\\...']));
+        $this->assertEquals('/john/.', $this->invokeMethod($this->storage, 'applyPathPrefix', ['..\\.\\......']));
+        $this->assertEquals('/john/.\\', $this->invokeMethod($this->storage, 'applyPathPrefix', ['...\\.\\......\\']));
     }
 
     public function testStripPathPrefix()

programming_language: PHP