Skip to content

Commit acb5470

Browse files
authored
fix: add error handling for corrupted cache files in FileHandler (#9586)
1 parent 63ab555 commit acb5470

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

system/Cache/Handlers/FileHandler.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function isSupported(): bool
217217

218218
/**
219219
* Does the heavy lifting of actually retrieving the file and
220-
* verifying it's age.
220+
* verifying its age.
221221
*
222222
* @return array{data: mixed, ttl: int, time: int}|false
223223
*/
@@ -227,7 +227,17 @@ protected function getItem(string $filename)
227227
return false;
228228
}
229229

230-
$data = @unserialize(file_get_contents($this->path . $filename));
230+
$content = @file_get_contents($this->path . $filename);
231+
232+
if ($content === false) {
233+
return false;
234+
}
235+
236+
try {
237+
$data = unserialize($content);
238+
} catch (Throwable) {
239+
return false;
240+
}
231241

232242
if (! is_array($data)) {
233243
return false;

tests/system/Cache/Handlers/FileHandlerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,30 @@ public function testGetMetaDataMiss(): void
363363
{
364364
$this->assertFalse($this->handler->getMetaData(self::$dummy));
365365
}
366+
367+
#[RequiresOperatingSystem('Linux|Darwin')]
368+
public function testGetUnreadableFile(): void
369+
{
370+
$this->handler->save(self::$key1, 'value');
371+
372+
$filePath = $this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . self::$key1;
373+
374+
// Make the file unreadable
375+
chmod($filePath, 0000);
376+
377+
$this->assertNull($this->handler->get(self::$key1));
378+
}
379+
380+
public function testGetItemWithCorruptedData(): void
381+
{
382+
$filePath = $this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . self::$key1;
383+
384+
file_put_contents($filePath, 'corrupted_serialized_data_that_cannot_be_unserialized');
385+
386+
$this->assertFileExists($filePath);
387+
388+
$this->assertNull($this->handler->get(self::$key1));
389+
}
366390
}
367391

368392
final class BaseTestFileHandler extends FileHandler

user_guide_src/source/changelogs/v4.6.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Deprecations
3535
Bugs Fixed
3636
**********
3737

38+
- **Cache:** Fixed a bug where a corrupted or unreadable cache file could cause an unhandled exception in ``FileHandler::getItem()``.
3839
- **Database:** Fixed a bug where ``when()`` and ``whenNot()`` in ``ConditionalTrait`` incorrectly evaluated certain falsy values (such as ``[]``, ``0``, ``0.0``, and ``'0'``) as truthy, causing callbacks to be executed unexpectedly. These methods now cast the condition to a boolean using ``(bool)`` to ensure consistent behavior with PHP's native truthiness.
3940
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
4041
- **Session:** Fixed a bug where using the ``DatabaseHandler`` with an unsupported database driver (such as ``SQLSRV``, ``OCI8``, or ``SQLite3``) did not throw an appropriate error.

0 commit comments

Comments
 (0)