Browse Source

fix: validate filesystem comment filenames

Jose Tejera 1 month ago
parent
commit
fb5da18316
2 changed files with 19 additions and 1 deletions
  1. 7 1
      lib/Data/Filesystem.php
  2. 12 0
      tst/Data/FilesystemTest.php

+ 7 - 1
lib/Data/Filesystem.php

@@ -215,7 +215,13 @@ class Filesystem extends AbstractData
                 // - parentid is the comment this comment replies to (It can be pasteid)
                 // - parentid is the comment this comment replies to (It can be pasteid)
                 if ($file->isFile()) {
                 if ($file->isFile()) {
                     $items = explode('.', $file->getBasename('.php'));
                     $items = explode('.', $file->getBasename('.php'));
-                    if (count($items) !== 3) {
+                    if (
+                        $file->getExtension() !== 'php' ||
+                        count($items) !== 3 ||
+                        $items[0] !== $pasteid ||
+                        preg_match('/\A[a-f0-9]{16}\z/', $items[1]) !== 1 ||
+                        preg_match('/\A[a-f0-9]{16}\z/', $items[2]) !== 1
+                    ) {
                         continue;
                         continue;
                     }
                     }
                     $comment = $this->_get($file->getPathname());
                     $comment = $this->_get($file->getPathname());

+ 12 - 0
tst/Data/FilesystemTest.php

@@ -89,6 +89,18 @@ class FilesystemTest extends TestCase
             $discussionPath . $pasteid . '.ffffffffffffffff.' . $pasteid . '.php',
             $discussionPath . $pasteid . '.ffffffffffffffff.' . $pasteid . '.php',
             Filesystem::PROTECTION_LINE . PHP_EOL . '{'
             Filesystem::PROTECTION_LINE . PHP_EOL . '{'
         );
         );
+        file_put_contents(
+            $discussionPath . $pasteid . '.invalid.invalid.php',
+            Filesystem::PROTECTION_LINE . PHP_EOL . json_encode($comment)
+        );
+        file_put_contents(
+            $discussionPath . 'ffffffffffffffff.eeeeeeeeeeeeeeee.' . $pasteid . '.php',
+            Filesystem::PROTECTION_LINE . PHP_EOL . json_encode($comment)
+        );
+        file_put_contents(
+            $discussionPath . $pasteid . '.dddddddddddddddd.' . $pasteid,
+            Filesystem::PROTECTION_LINE . PHP_EOL . json_encode($comment)
+        );
 
 
         $errorLog = ini_get('error_log');
         $errorLog = ini_get('error_log');
         ini_set('error_log', '/dev/null');
         ini_set('error_log', '/dev/null');