Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion docs/helpers/filesystem_helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions docs/libraries/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand All @@ -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**
Expand Down
12 changes: 8 additions & 4 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions system/Helpers/filesystem_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{
* name: string,
Expand All @@ -267,7 +268,7 @@ function get_filenames(
* relative_path: string,
* }>
*/
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;
Expand All @@ -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;
Expand Down
107 changes: 100 additions & 7 deletions tests/system/Cache/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{string}>
*/
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);
Expand Down
10 changes: 10 additions & 0 deletions tests/system/Commands/Cache/ClearCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
63 changes: 63 additions & 0 deletions tests/system/Helpers/FilesystemHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading