diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 391c732c6639..dc255486bd11 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -22,6 +22,7 @@ Release Date: Unreleased - **API:** Fixed HTTP 500 errors in `ResponseTrait::paginate()` when using a Query Builder with a very large page number or `$perPage = 0`. - **Cache:** Fixed a bug where `PredisHandler::increment()` and `PredisHandler::decrement()` updated the unused `'data'` hash field instead of `'__ci_value'`, causing `get()` to return stale values for existing counters. +- **Cache:** Fixed a bug where the File handler skipped cache entries whose key begins with a period in `clean()`, `getCacheInfo()` and `deleteMatching()`. Such entries were not listed by `getCacheInfo()` and were not removed by `clean()` or `deleteMatching()`, so `cache:clear` left them in place. `deleteMatching()` wildcard patterns now also match these entries, while the `.htaccess` protection file is still skipped. - **CORS:** Fixed a bug where `Cors::hasResponseHeaders()` matched header substrings via `str_contains()`, causing false positives when existing exposed headers shared a prefix with configured exposed headers. - **Database:** Fixed a bug where `BaseConnection::prepare()` did not reset pretend mode when the closure threw an exception, leaving the connection in pretend mode so that subsequent queries on it were silently discarded. - **Database:** Fixed a bug where `Postgre`, `OCI8`, and `SQLSRV` `Forge::_dropKeyAsConstraint()` interpolated table, constraint, and schema names without escaping. diff --git a/docs/helpers/filesystem_helper.md b/docs/helpers/filesystem_helper.md index 8c1048100748..a917147f2256 100644 --- a/docs/helpers/filesystem_helper.md +++ b/docs/helpers/filesystem_helper.md @@ -216,11 +216,15 @@ Example: $controllers = get_filenames(APPPATH . 'Controllers/'); ``` -#### get_dir_file_info($sourceDir[, $topLevelOnly = true]) +#### get_dir_file_info($sourceDir[, $topLevelOnly = true[, $recursion = false[, $hidden = false]]]) - **Parameters** - **$sourceDir** `string` Directory path - **$topLevelOnly** `bool` Whether to look only at the specified directory (excluding sub-directories) + - **$recursion** `bool` Whether to recurse into sub-directories. For internal use; do not pass it from application code. + - **$hidden** `bool` Whether to include hidden files (files beginning with a period) + + > **Note:** `$recursion` is an internal parameter and should not be passed by callers. When you need hidden files, use the named argument, e.g. `get_dir_file_info($sourceDir, hidden: true)`. - **Returns**: An array containing info on the supplied directory's contents diff --git a/docs/libraries/caching.md b/docs/libraries/caching.md index d89313718864..977998ba478b 100644 --- a/docs/libraries/caching.md +++ b/docs/libraries/caching.md @@ -70,7 +70,7 @@ $key = 'search_' . hash('sha256', $userInput); This is an array of settings specific to the **File** handler to determine how it should save the cache files. -Cache files whose names start with a dot are not fully supported by the File handler: `getCacheInfo()` does not report them, `deleteMatching()` wildcard patterns do not match them, and `clean()` / `cache:clear` do not delete them. Avoid keys and prefixes that produce a leading dot. +Dot-leading cache keys are supported by the File handler: they are reported by `getCacheInfo()`, matched by `deleteMatching()`, and removed by `clean()` / `cache:clear`. The `.htaccess` protection file is never deleted by `deleteMatching()`, and the protection files (`.htaccess`, `index.html`, `index.htm`, `index.php`, `web.config`) are kept by `clean()`; any other dot-leading file in the cache directory is affected. See the notes below `deleteMatching()`, `clean()` and `getCacheInfo()`. ### $memcached @@ -201,7 +201,7 @@ This method will delete multiple items from the cache store at once by matching This method is only implemented for the File, Redis and Predis handlers. Due to limitations, it couldn't be implemented for the Memcached and Wincache handlers. !!! note "Note" - With the File handler, the pattern is matched against cache file names only and cannot match files in subdirectories. A pattern containing a forward slash (`/`) therefore never matches. + With the File handler, the pattern is matched against cache file names only and cannot match files in subdirectories. A pattern containing a forward slash (`/`) therefore never matches. A cache entry whose key begins with a period (a dot-leading file name) is matched by patterns like any other entry, but the `.htaccess` protection file is never deleted. Any other dot-leading file in the cache directory (for example `.gitignore` or `.env`) is matched as well. Example: @@ -276,6 +276,9 @@ Example: $cache->clean(); ``` +!!! note "Note" + The File handler also deletes cache entries whose key begins with a period, while the `.htaccess`, `index.html`, `index.htm`, `index.php` and `web.config` files are kept. Any other dot-leading file in the cache directory or its subdirectories (for example `.gitignore`, `.env` or `.user.ini`) is deleted as well. + #### getCacheInfo() - **Returns**: Information on the entire cache database @@ -294,6 +297,9 @@ var_dump($cache->getCacheInfo()); !!! note "Note" The information returned and the structure of the data are dependent on which adapter is being used. +!!! note "Note" + The File handler also reports dot-leading files in the cache directory, including the `.htaccess` protection file. + #### getMetadata(string $key) - **Parameters** diff --git a/system/Cache/Handlers/FileHandler.php b/system/Cache/Handlers/FileHandler.php index cc24df2c1219..bbb77c4512b4 100644 --- a/system/Cache/Handlers/FileHandler.php +++ b/system/Cache/Handlers/FileHandler.php @@ -162,8 +162,12 @@ public function deleteMatching(string $pattern): int $deleted = 0; foreach ($iterator as $file) { - // FNM_PERIOD keeps glob() semantics: wildcards don't match a leading dot. - if (! fnmatch($pattern, $file->getFilename(), FNM_PERIOD)) { + $filename = $file->getFilename(); + + // A dot-leading file name is a legal cache entry, so wildcards must + // match it. `.htaccess` protects the cache directory and is never a + // cache entry, so it must never be deleted. + if (strcasecmp($filename, '.htaccess') === 0 || ! fnmatch($pattern, $filename)) { continue; } @@ -202,12 +206,12 @@ public function decrement(string $key, int $offset = 1): bool|int public function clean(): bool { - return delete_files($this->path, false, true); + return delete_files($this->path, htdocs: true, hidden: true); } public function getCacheInfo(): array { - return get_dir_file_info($this->path); + return get_dir_file_info($this->path, hidden: true); } public function getMetaData(string $key): ?array diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index 7c42bd5c65f8..afa861cd4621 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -258,6 +258,7 @@ function get_filenames( * @param string $sourceDir Path to source * @param bool $topLevelOnly Look only at the top level directory specified? * @param bool $recursion Internal variable to determine recursion status - do not use in calls + * @param bool $hidden Whether to include hidden files (files beginning with a period) * * @return array */ - function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $recursion = false): array + function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $recursion = false, bool $hidden = false): array { static $fileData = []; $relativePath = $sourceDir; @@ -283,9 +284,17 @@ function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $r // Used to be foreach (scandir($sourceDir, 1) as $file), but scandir() is simply not as fast while (false !== ($file = readdir($fp))) { - if (is_dir($sourceDir . $file) && $file[0] !== '.' && $topLevelOnly === false) { - get_dir_file_info($sourceDir . $file . DIRECTORY_SEPARATOR, $topLevelOnly, true); - } elseif ($file[0] !== '.') { + if ($file === '.' || $file === '..') { + continue; + } + + if (! $hidden && $file[0] === '.') { + continue; + } + + if (is_dir($sourceDir . $file) && $topLevelOnly === false) { + get_dir_file_info($sourceDir . $file . DIRECTORY_SEPARATOR, $topLevelOnly, true, $hidden); + } else { $info = get_file_info($sourceDir . $file); if ($info !== null) { $fileData[$file] = $info; diff --git a/tests/system/Cache/Handlers/FileHandlerTest.php b/tests/system/Cache/Handlers/FileHandlerTest.php index 5a0e057bb349..592d30b6d8fe 100644 --- a/tests/system/Cache/Handlers/FileHandlerTest.php +++ b/tests/system/Cache/Handlers/FileHandlerTest.php @@ -327,24 +327,117 @@ public function testDeleteMatchingTreatsCachePathLiterally(): void } } - public function testDeleteMatchingWildcardDoesNotMatchHiddenFiles(): void + public function testDeleteMatchingWildcardMatchesHiddenEntries(): void { - $hidden = $this->config->file['storePath'] . '/.hidden'; + $this->assertTrue($this->handler->save('.hidden', 'value')); + $this->assertTrue($this->handler->save(self::$key1, 'value')); + + $this->assertSame(2, $this->handler->deleteMatching('*')); + $this->assertNull($this->handler->get('.hidden')); + $this->assertNull($this->handler->get(self::$key1)); + } + + public function testDeleteMatchingMatchesExplicitDotPattern(): void + { + $this->assertTrue($this->handler->save('.hidden', 'value')); + $this->assertTrue($this->handler->save(self::$key1, 'value')); + + $this->assertSame(1, $this->handler->deleteMatching('.hid*')); + $this->assertNull($this->handler->get('.hidden')); + $this->assertSame('value', $this->handler->get(self::$key1)); + } - $this->assertNotFalse(file_put_contents($hidden, 'data')); + #[DataProvider('provideDeleteMatchingDoesNotDeleteHtaccess')] + public function testDeleteMatchingDoesNotDeleteHtaccess(string $protectionFile): void + { + $path = $this->config->file['storePath'] . DIRECTORY_SEPARATOR . $protectionFile; try { - $this->assertTrue($this->handler->save(self::$key1, 'value')); + $this->assertNotFalse(file_put_contents($path, 'Deny from all')); + $this->assertTrue($this->handler->save('.hidden', 'value')); $this->assertSame(1, $this->handler->deleteMatching('*')); - $this->assertFileExists($hidden); + $this->assertFileExists($path); + $this->assertNull($this->handler->get('.hidden')); } finally { - if (is_file($hidden)) { - unlink($hidden); + if (is_file($path)) { + unlink($path); } } } + /** + * @return iterable + */ + public static function provideDeleteMatchingDoesNotDeleteHtaccess(): iterable + { + yield 'lowercase' => ['.htaccess']; + + yield 'uppercase' => ['.HTACCESS']; + + yield 'mixed case' => ['.HtAccess']; + } + + public function testCleanDeletesHiddenEntries(): void + { + $path = $this->config->file['storePath'] . DIRECTORY_SEPARATOR . '.hidden'; + + $this->assertTrue($this->handler->save('.hidden', 'value')); + $this->assertFileExists($path); + + $this->assertTrue($this->handler->clean()); + $this->assertFileDoesNotExist($path); + $this->assertNull($this->handler->get('.hidden')); + } + + public function testCleanKeepsProtectionFiles(): void + { + $protectionFiles = ['.htaccess', 'index.html', 'index.htm', 'index.php', 'web.config']; + + try { + foreach ($protectionFiles as $protectionFile) { + $this->assertNotFalse( + file_put_contents( + $this->config->file['storePath'] . DIRECTORY_SEPARATOR . $protectionFile, + '', + ), + ); + } + + $this->assertTrue($this->handler->save(self::$key1, 'value')); + $this->assertTrue($this->handler->save('.hidden', 'value')); + + $this->assertTrue($this->handler->clean()); + $this->assertNull($this->handler->get(self::$key1)); + $this->assertNull($this->handler->get('.hidden')); + + foreach ($protectionFiles as $protectionFile) { + $this->assertFileExists($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $protectionFile); + } + } finally { + foreach ($protectionFiles as $protectionFile) { + $path = $this->config->file['storePath'] . DIRECTORY_SEPARATOR . $protectionFile; + + if (is_file($path)) { + unlink($path); + } + } + } + } + + public function testGetCacheInfoReportsHiddenEntries(): void + { + $this->assertTrue($this->handler->save('.hidden', 'value')); + + $info = $this->handler->getCacheInfo(); + + $this->assertArrayHasKey('.hidden', $info); + $this->assertSame('.hidden', $info['.hidden']['name']); + $this->assertArrayHasKey('server_path', $info['.hidden']); + $this->assertArrayNotHasKey('.', $info); + $this->assertArrayNotHasKey('..', $info); + } + public function testIncrement(): void { $this->handler->save(self::$key1, 1); diff --git a/tests/system/Commands/Cache/ClearCacheTest.php b/tests/system/Commands/Cache/ClearCacheTest.php index ca93f215bc13..5930f0ad0df2 100644 --- a/tests/system/Commands/Cache/ClearCacheTest.php +++ b/tests/system/Commands/Cache/ClearCacheTest.php @@ -111,6 +111,16 @@ public function testClearCacheWorks(): void $this->assertStringContainsString('Cache cleared.', $this->getStreamFilterBuffer()); } + public function testClearCacheRemovesHiddenEntries(): void + { + cache()->save('.hidden', 'bar'); + $this->assertSame('bar', cache('.hidden')); + + command('cache:clear'); + + $this->assertNull(cache('.hidden')); + } + public function testClearCacheDiscardsSharedLocatorCache(): void { $handler = $this->getIsolatedCacheHandler(); diff --git a/tests/system/Helpers/FilesystemHelperTest.php b/tests/system/Helpers/FilesystemHelperTest.php index f264864ebf9a..e68838fa9f6b 100644 --- a/tests/system/Helpers/FilesystemHelperTest.php +++ b/tests/system/Helpers/FilesystemHelperTest.php @@ -497,6 +497,69 @@ public function testGetDirFileInfoIgnoresDirectoriesWhenTopLevelOnlyIsTrue(): vo $this->assertArrayNotHasKey('subdir', $result); } + public function testGetDirFileInfoSkipsHiddenFilesByDefault(): void + { + $dir = WRITEPATH . 'test_get_dir_file_info_hidden_' . bin2hex(random_bytes(4)); + + $this->assertTrue(mkdir($dir, 0777, true)); + $this->assertNotFalse(file_put_contents($dir . '/file.txt', 'visible')); + $this->assertNotFalse(file_put_contents($dir . '/.hidden', 'hidden')); + + try { + $result = get_dir_file_info($dir, true); + + $this->assertArrayHasKey('file.txt', $result); + $this->assertArrayNotHasKey('.hidden', $result); + } finally { + unlink($dir . '/file.txt'); + unlink($dir . '/.hidden'); + rmdir($dir); + } + } + + public function testGetDirFileInfoIncludesHiddenFiles(): void + { + $dir = WRITEPATH . 'test_get_dir_file_info_hidden_' . bin2hex(random_bytes(4)); + + $this->assertTrue(mkdir($dir, 0777, true)); + $this->assertNotFalse(file_put_contents($dir . '/file.txt', 'visible')); + $this->assertNotFalse(file_put_contents($dir . '/.hidden', 'hidden')); + + try { + $result = get_dir_file_info($dir, true, false, true); + + $this->assertArrayHasKey('file.txt', $result); + $this->assertArrayHasKey('.hidden', $result); + $this->assertSame('.hidden', $result['.hidden']['name']); + $this->assertArrayNotHasKey('.', $result); + $this->assertArrayNotHasKey('..', $result); + } finally { + unlink($dir . '/file.txt'); + unlink($dir . '/.hidden'); + rmdir($dir); + } + } + + public function testGetDirFileInfoIncludesHiddenDirectoryRecursively(): void + { + $dir = WRITEPATH . 'test_get_dir_file_info_hidden_' . bin2hex(random_bytes(4)); + + $this->assertTrue(mkdir($dir . '/.sub', 0777, true)); + $this->assertNotFalse(file_put_contents($dir . '/.sub/inner.txt', 'inner')); + + try { + $withHidden = get_dir_file_info($dir, false, false, true); + $this->assertArrayHasKey('inner.txt', $withHidden); + + $withoutHidden = get_dir_file_info($dir, false); + $this->assertArrayNotHasKey('inner.txt', $withoutHidden); + } finally { + unlink($dir . '/.sub/inner.txt'); + rmdir($dir . '/.sub'); + rmdir($dir); + } + } + public function testGetFileInfo(): void { $file = SUPPORTPATH . 'Files/baker/banana.php';