Skip to content

Commit 23ab8d3

Browse files
committed
fix: correct cache directory permissions mode to 0755 in FileVarExportHandler
1 parent 94036f1 commit 23ab8d3

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎system/Cache/FactoriesCache/FileVarExportHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function save(string $key, mixed $val): void
2323

2424
// Two processes may try to create the directory at the same time.
2525
// is_dir() confirms it exists, so suppressing the warning is safe.
26-
if (! is_dir($this->path) && ! @mkdir($this->path, 0777, true) && ! is_dir($this->path)) {
26+
if (! is_dir($this->path) && ! @mkdir($this->path, 0755, true) && ! is_dir($this->path)) {
2727
log_message('error', 'FactoriesCache: cannot create cache directory: ' . $this->path);
2828

2929
return;

‎tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler;
1717
use PHPUnit\Framework\Attributes\Group;
18+
use ReflectionProperty;
1819

1920
/**
2021
* @internal
@@ -27,4 +28,30 @@ protected function createFactoriesCache(): void
2728
$this->handler = new FileVarExportHandler();
2829
$this->cache = new FactoriesCache($this->handler);
2930
}
31+
32+
public function testSaveCreatesDirectoryWithCorrectPermissions(): void
33+
{
34+
$dir = WRITEPATH . 'cache_test_dir_' . uniqid('', true);
35+
36+
try {
37+
$handler = new FileVarExportHandler();
38+
$ref = new ReflectionProperty(FileVarExportHandler::class, 'path');
39+
$ref->setValue($handler, $dir);
40+
41+
$handler->save('test_key', ['data']);
42+
43+
$this->assertDirectoryExists($dir);
44+
45+
if (! is_windows()) {
46+
$perms = fileperms($dir) & 0777;
47+
$expected = 0755 & ~umask();
48+
$this->assertSame($expected, $perms);
49+
}
50+
} finally {
51+
if (is_dir($dir)) {
52+
array_map('unlink', glob("{$dir}/*") ?: []);
53+
rmdir($dir);
54+
}
55+
}
56+
}
3057
}

0 commit comments

Comments
 (0)