Skip to content

Commit 014c45c

Browse files
authored
fix(Cache): set secure directory (0755) and file (0644) permissions in FileVarExportHandler (#10555)
1 parent edf4380 commit 014c45c

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎system/Cache/FactoriesCache/FileVarExportHandler.php‎

Lines changed: 3 additions & 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;
@@ -37,6 +37,8 @@ public function save(string $key, mixed $val): void
3737
return;
3838
}
3939

40+
@chmod($tmp, 0644);
41+
4042
// Another process may have wiped the directory. Clean up on failure.
4143
if (! @rename($tmp, $this->path . "/{$key}")) {
4244
log_message('warning', 'FactoriesCache: failed to commit cache file for key: ' . $key);

‎tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,46 @@
2222
#[Group('Others')]
2323
final class FactoriesCacheFileVarExportHandlerTest extends AbstractFactoriesCacheHandlerTestCase
2424
{
25+
public static function setUpBeforeClass(): void
26+
{
27+
parent::setUpBeforeClass();
28+
29+
helper('filesystem');
30+
}
31+
2532
protected function createFactoriesCache(): void
2633
{
2734
$this->handler = new FileVarExportHandler();
2835
$this->cache = new FactoriesCache($this->handler);
2936
}
37+
38+
public function testSaveCreatesDirectoryAndFileWithCorrectPermissions(): void
39+
{
40+
$dir = WRITEPATH . 'cache_test_dir_' . uniqid('', true);
41+
$oldUmask = umask(0000);
42+
43+
try {
44+
$handler = new FileVarExportHandler();
45+
$this->setPrivateProperty($handler, 'path', $dir);
46+
47+
$handler->save('test_key', ['data']);
48+
49+
$this->assertDirectoryExists($dir);
50+
51+
if (! is_windows()) {
52+
$dirPerms = fileperms($dir) & 0777;
53+
$this->assertSame(0755, $dirPerms);
54+
55+
$filePerms = fileperms($dir . '/test_key') & 0777;
56+
$this->assertSame(0644, $filePerms);
57+
}
58+
} finally {
59+
umask($oldUmask);
60+
61+
if (is_dir($dir)) {
62+
delete_files($dir);
63+
rmdir($dir);
64+
}
65+
}
66+
}
3067
}

0 commit comments

Comments
 (0)