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
2 changes: 1 addition & 1 deletion system/Cache/FactoriesCache/FileVarExportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler;
use PHPUnit\Framework\Attributes\Group;
use ReflectionProperty;

/**
* @internal
Expand All @@ -27,4 +28,35 @@ 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)) {
$files = glob("{$dir}/*");

if ($files !== false) {
array_map(unlink(...), $files);
}

rmdir($dir);
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test also passes without the fix. With the usual umask of 0022, mkdir with 0777 and with 0755 both give you a 0755 directory, so the assertion can't tell the two apart. Try umask(0000) before the save (restore the old mask in finally) and then assert 0755. That's the permissive-umask case, and it will actually fail if someone reverts the change.

Also, please use ReflectionHelper instead.

}
Loading