-
-
Notifications
You must be signed in to change notification settings - Fork 44
fix(FileStorage): harden GC against non-cache files (#45) #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OndraDol
wants to merge
2
commits into
nette:master
Choose a base branch
from
OndraDol:fix/issue-45-gc-hardening-atomic-write
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Test: Nette\Caching\Storages\FileStorage handles corrupted cache files gracefully. | ||
| * | ||
| * Verifies that reading a corrupted cache file returns null instead of | ||
| * throwing unserialize errors. Covers both corrupted metadata and | ||
| * corrupted data payloads. | ||
| */ | ||
|
|
||
| use Nette\Caching\Cache; | ||
| use Nette\Caching\Storages\FileStorage; | ||
| use Tester\Assert; | ||
|
|
||
|
|
||
| require __DIR__ . '/../bootstrap.php'; | ||
|
|
||
|
|
||
| $dir = getTempDir(); | ||
| $storage = new FileStorage($dir); | ||
| $cache = new Cache($storage); | ||
|
|
||
|
|
||
| // --- Corrupted metadata --- | ||
|
|
||
| $cache->save('meta-test', 'hello'); | ||
| Assert::same('hello', $cache->load('meta-test')); | ||
|
|
||
| // Find the cache file and corrupt its metadata | ||
| $files = glob($dir . '/_*'); | ||
| Assert::truthy($files); | ||
|
|
||
| foreach ($files as $file) { | ||
| if (is_file($file)) { | ||
| // Overwrite with a valid-looking size header but garbage meta. | ||
| // "000010" = meta size of 10, followed by non-serialized data. | ||
| file_put_contents($file, '000010not-serial'); | ||
| } | ||
| } | ||
|
|
||
| // Reading corrupted meta should return null, not crash | ||
| Assert::null($cache->load('meta-test')); | ||
|
|
||
|
|
||
| // --- Corrupted serialized data --- | ||
|
|
||
| // Use a fresh storage to avoid interference | ||
| $dir2 = getTempDir() . '/data-test'; | ||
| @mkdir($dir2, 0777, true); | ||
| $storage2 = new FileStorage($dir2); | ||
| $cache2 = new Cache($storage2); | ||
|
|
||
| $cache2->save('data-test', ['complex' => 'array', 'with' => [1, 2, 3]]); | ||
| Assert::type('array', $cache2->load('data-test')); | ||
|
|
||
| // Find the cache file | ||
| $files2 = glob($dir2 . '/_*'); | ||
| Assert::truthy($files2); | ||
|
|
||
| foreach ($files2 as $file) { | ||
| if (is_file($file)) { | ||
| $content = file_get_contents($file); | ||
| $size = (int) substr($content, 0, 6); | ||
| $headerAndMeta = substr($content, 0, 6 + $size); | ||
| // Keep valid header+meta but replace data with garbage | ||
| file_put_contents($file, $headerAndMeta . 'CORRUPTED-DATA-NOT-SERIALIZABLE'); | ||
| } | ||
| } | ||
|
|
||
| // Reading corrupted data should not crash. | ||
| // The result may be false (failed unserialize) but must not throw. | ||
| Assert::noError(function () use ($cache2) { | ||
| $cache2->load('data-test'); | ||
| }); | ||
|
|
||
|
|
||
| // --- Truncated file --- | ||
|
|
||
| $dir3 = getTempDir() . '/truncated-test'; | ||
| @mkdir($dir3, 0777, true); | ||
| $storage3 = new FileStorage($dir3); | ||
| $cache3 = new Cache($storage3); | ||
|
|
||
| $cache3->save('trunc-test', str_repeat('x', 10000)); | ||
| Assert::same(str_repeat('x', 10000), $cache3->load('trunc-test')); | ||
|
|
||
| // Truncate the file mid-data | ||
| $files3 = glob($dir3 . '/_*'); | ||
| foreach ($files3 as $file) { | ||
| if (is_file($file)) { | ||
| $handle = fopen($file, 'r+b'); | ||
| ftruncate($handle, 50); // keep header + partial meta | ||
| fclose($handle); | ||
| } | ||
| } | ||
|
|
||
| // Should return null, not crash | ||
| Assert::null($cache3->load('trunc-test')); | ||
|
|
||
|
|
||
| // --- File with all zero bytes --- | ||
|
|
||
| $dir4 = getTempDir() . '/zeros-test'; | ||
| @mkdir($dir4, 0777, true); | ||
| $storage4 = new FileStorage($dir4); | ||
| $cache4 = new Cache($storage4); | ||
|
|
||
| $cache4->save('zero-test', 'data'); | ||
|
|
||
| $files4 = glob($dir4 . '/_*'); | ||
| foreach ($files4 as $file) { | ||
| if (is_file($file)) { | ||
| file_put_contents($file, str_repeat("\x00", 100)); | ||
| } | ||
| } | ||
|
|
||
| // All-zero header = size 0, should return null | ||
| Assert::null($cache4->load('zero-test')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Test: Nette\Caching\Storages\FileStorage clean() skips non-cache files. | ||
| * | ||
| * Regression test for GH#45: FileStorage GC crashes with "unserialize(): Error at offset" | ||
| * when non-cache files (e.g. Latte lock files) matching the _* pattern exist | ||
| * in the cache directory. | ||
| */ | ||
|
|
||
| use Nette\Caching\Cache; | ||
| use Nette\Caching\Storages\FileStorage; | ||
| use Tester\Assert; | ||
|
|
||
|
|
||
| require __DIR__ . '/../bootstrap.php'; | ||
|
|
||
|
|
||
| $dir = getTempDir(); | ||
| $storage = new FileStorage($dir); | ||
| $cache = new Cache($storage); | ||
|
|
||
|
|
||
| // Write valid cache entries | ||
| $cache->save('key1', 'value1'); | ||
| $cache->save('key2', 'value2'); | ||
|
|
||
| Assert::same('value1', $cache->load('key1')); | ||
| Assert::same('value2', $cache->load('key2')); | ||
|
|
||
|
|
||
| // Create foreign files that match the _* pattern used by GC. | ||
| // These simulate Latte lock files and other non-cache files that may | ||
| // coexist in the same temp directory tree. | ||
|
|
||
| // Case 1: Content starting with digits — triggers (int) cast to non-zero size | ||
| // in readMetaAndLock(), then garbage gets passed to unserialize(). | ||
| // This is the exact scenario from GH#45 diagnosis by @martinbohmcz: | ||
| // PHP 8 interprets "0942e7" as float 942e7, (int) cast = 9420000. | ||
| file_put_contents($dir . '/_includes-template.php.lock', '0942e7deadbeef'); | ||
|
|
||
| // Case 2: Binary content | ||
| file_put_contents($dir . '/_session.lock', "\xff\xfe\x00\x01\x80\x90binary"); | ||
|
|
||
| // Case 3: Empty file | ||
| file_put_contents($dir . '/_empty.lock', ''); | ||
|
|
||
| // Case 4: File with zero-like header (6 bytes of zeros = size 0, skip path) | ||
| file_put_contents($dir . '/_zero-header.tmp', '000000some-data'); | ||
|
|
||
| // Case 5: File with valid-looking size but garbage meta | ||
| file_put_contents($dir . '/_fake-meta.dat', '000010not-a-serialized-array'); | ||
|
|
||
| // Case 6: File in a subdirectory matching _* pattern | ||
| @mkdir($dir . '/_subdir', 0777, true); | ||
| file_put_contents($dir . '/_subdir/_nested.lock', 'nested-lock-data'); | ||
|
|
||
|
|
||
| // GC (collector mode) must not crash on foreign files | ||
| Assert::noError(function () use ($storage) { | ||
| $storage->clean([]); | ||
| }); | ||
|
|
||
|
|
||
| // Valid cache entries must still be readable | ||
| Assert::same('value1', $cache->load('key1')); | ||
| Assert::same('value2', $cache->load('key2')); | ||
|
|
||
|
|
||
| // Foreign files must be untouched (GC should skip, not delete them) | ||
| Assert::true(file_exists($dir . '/_includes-template.php.lock')); | ||
| Assert::true(file_exists($dir . '/_session.lock')); | ||
| Assert::true(file_exists($dir . '/_empty.lock')); | ||
| Assert::true(file_exists($dir . '/_zero-header.tmp')); | ||
| Assert::true(file_exists($dir . '/_fake-meta.dat')); | ||
| Assert::true(file_exists($dir . '/_subdir/_nested.lock')); | ||
|
|
||
|
|
||
| // Cache::All mode deletes all _* files including foreign ones — expected behavior | ||
| $storage->clean([Cache::All => true]); | ||
|
|
||
| Assert::null($cache->load('key1')); | ||
| Assert::null($cache->load('key2')); | ||
| Assert::false(file_exists($dir . '/_includes-template.php.lock')); | ||
| Assert::false(file_exists($dir . '/_session.lock')); | ||
| Assert::false(file_exists($dir . '/_empty.lock')); | ||
| Assert::false(file_exists($dir . '/_zero-header.tmp')); | ||
| Assert::false(file_exists($dir . '/_fake-meta.dat')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
MetaSerializedis set,readData()now returns@unserialize($data)directly, so malformed payloads resolve tofalseinstead of a miss. InCache::load(), onlynullis treated as missing ($data === null), so a corrupted file can become a persistent cache hit with valuefalse, skipping regeneration and returning incorrect data to callers. Please map unserialize failure tonull(while preserving legitimate serializedfalse, e.g.b:0;).Useful? React with 👍 / 👎.