Skip to content

Commit db02754

Browse files
committed
fix(cache): set secure 0755 permissions mode when creating directory in FileVarExportHandler
1 parent de66f3f commit db02754

2 files changed

Lines changed: 33 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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,36 @@ protected function createFactoriesCache(): void
2727
$this->handler = new FileVarExportHandler();
2828
$this->cache = new FactoriesCache($this->handler);
2929
}
30+
31+
public function testSaveCreatesDirectoryWithCorrectPermissions(): void
32+
{
33+
$dir = WRITEPATH . 'cache_test_dir_' . uniqid('', true);
34+
$oldUmask = umask(0000);
35+
36+
try {
37+
$handler = new FileVarExportHandler();
38+
$this->setPrivateProperty($handler, 'path', $dir);
39+
40+
$handler->save('test_key', ['data']);
41+
42+
$this->assertDirectoryExists($dir);
43+
44+
if (! is_windows()) {
45+
$perms = fileperms($dir) & 0777;
46+
$this->assertSame(0755, $perms);
47+
}
48+
} finally {
49+
umask($oldUmask);
50+
51+
if (is_dir($dir)) {
52+
$files = glob("{$dir}/*");
53+
54+
if ($files !== false) {
55+
array_map(unlink(...), $files);
56+
}
57+
58+
rmdir($dir);
59+
}
60+
}
61+
}
3062
}

0 commit comments

Comments
 (0)