Browse Source

Fix `FileCacheEngine` removing files in parent of configured path.

Processing dot paths (specifically `..`) will expand to the parent of
the configured base path, causing unrelated files to be deleted.
ndm2 7 years ago
parent
commit
c42affa606
2 changed files with 31 additions and 1 deletions
  1. 4 1
      src/Cache/Engine/FileEngine.php
  2. 27 0
      tests/TestCase/Cache/Engine/FileEngineTest.php

+ 4 - 1
src/Cache/Engine/FileEngine.php

@@ -269,7 +269,10 @@ class FileEngine extends CacheEngine
 
         $this->_clearDirectory($this->_config['path'], $now, $threshold);
 
-        $directory = new RecursiveDirectoryIterator($this->_config['path']);
+        $directory = new RecursiveDirectoryIterator(
+            $this->_config['path'],
+            \FilesystemIterator::SKIP_DOTS
+        );
         $contents = new RecursiveIteratorIterator(
             $directory,
             RecursiveIteratorIterator::SELF_FIRST

+ 27 - 0
tests/TestCase/Cache/Engine/FileEngineTest.php

@@ -641,4 +641,31 @@ class FileEngineTest extends TestCase
         $result = Cache::add('test_add_key', 'test data 2', 'file_test');
         $this->assertFalse($result);
     }
+
+    /**
+     * Tests that only files inside of the configured path are being deleted.
+     *
+     * @return void
+     */
+    public function testClearIsRestrictedToConfiguredPath()
+    {
+        $this->_configCache([
+            'prefix' => '',
+            'path' => TMP . 'tests',
+        ]);
+
+        $unrelatedFile = tempnam(TMP, 'file_test');
+        file_put_contents($unrelatedFile, 'data');
+        $this->assertFileExists($unrelatedFile);
+
+        Cache::write('key', 'data', 'file_test');
+        $this->assertFileExists(TMP . 'tests/key');
+
+        $result = Cache::clear(false, 'file_test');
+        $this->assertTrue($result);
+        $this->assertFileNotExists(TMP . 'tests/key');
+
+        $this->assertFileExists($unrelatedFile);
+        $this->assertTrue(unlink($unrelatedFile));
+    }
 }