From 23ab8d36e9479946e76fe0ae4158631493edca19 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sat, 12 Sep 2026 22:08:50 +0200 Subject: [PATCH 1/2] fix: correct cache directory permissions mode to 0755 in FileVarExportHandler --- .../FactoriesCache/FileVarExportHandler.php | 2 +- ...FactoriesCacheFileVarExportHandlerTest.php | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/system/Cache/FactoriesCache/FileVarExportHandler.php b/system/Cache/FactoriesCache/FileVarExportHandler.php index 023910c4b17d..453e06be9e70 100644 --- a/system/Cache/FactoriesCache/FileVarExportHandler.php +++ b/system/Cache/FactoriesCache/FileVarExportHandler.php @@ -23,7 +23,7 @@ public function save(string $key, mixed $val): void // Two processes may try to create the directory at the same time. // is_dir() confirms it exists, so suppressing the warning is safe. - if (! is_dir($this->path) && ! @mkdir($this->path, 0777, true) && ! is_dir($this->path)) { + if (! is_dir($this->path) && ! @mkdir($this->path, 0755, true) && ! is_dir($this->path)) { log_message('error', 'FactoriesCache: cannot create cache directory: ' . $this->path); return; diff --git a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php index c71c756f5cdd..2db93be22779 100644 --- a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php +++ b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php @@ -15,6 +15,7 @@ use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; use PHPUnit\Framework\Attributes\Group; +use ReflectionProperty; /** * @internal @@ -27,4 +28,30 @@ protected function createFactoriesCache(): void $this->handler = new FileVarExportHandler(); $this->cache = new FactoriesCache($this->handler); } + + public function testSaveCreatesDirectoryWithCorrectPermissions(): void + { + $dir = WRITEPATH . 'cache_test_dir_' . uniqid('', true); + + try { + $handler = new FileVarExportHandler(); + $ref = new ReflectionProperty(FileVarExportHandler::class, 'path'); + $ref->setValue($handler, $dir); + + $handler->save('test_key', ['data']); + + $this->assertDirectoryExists($dir); + + if (! is_windows()) { + $perms = fileperms($dir) & 0777; + $expected = 0755 & ~umask(); + $this->assertSame($expected, $perms); + } + } finally { + if (is_dir($dir)) { + array_map('unlink', glob("{$dir}/*") ?: []); + rmdir($dir); + } + } + } } From 3b9452e603f69dd4dda73307fc7d6c6d47c2afa3 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sat, 12 Sep 2026 22:27:45 +0200 Subject: [PATCH 2/2] fix: handle false return value from glob in FactoriesCacheFileVarExportHandlerTest --- .../Cache/FactoriesCacheFileVarExportHandlerTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php index 2db93be22779..2616e73f1875 100644 --- a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php +++ b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php @@ -49,7 +49,12 @@ public function testSaveCreatesDirectoryWithCorrectPermissions(): void } } finally { if (is_dir($dir)) { - array_map('unlink', glob("{$dir}/*") ?: []); + $files = glob("{$dir}/*"); + + if ($files !== false) { + array_map(unlink(...), $files); + } + rmdir($dir); } }