Skip to content

Commit 6856587

Browse files
committed
revert restriction on dot-only cache key prefixes in FileHandler
1 parent 3657a18 commit 6856587

4 files changed

Lines changed: 28 additions & 62 deletions

File tree

‎docs/installation/upgrade_477.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Please refer to the upgrade instructions corresponding to your installation meth
1010

1111
## Breaking Changes
1212

13-
### File Cache Rejects Directory Separators and Dot-Only Prefixes
13+
### File Cache Rejects Directory Separators
1414

1515
For security reasons, `FileHandler::validateKey()` now rejects `/`, `\` and null bytes in the cache item file name, independently of the `Config\Cache::$reservedCharacters` setting. A cache key can no longer point outside the cache directory when that setting is relaxed.
1616

17-
With the default configuration most applications are unaffected, because `/`, `\` and `:` are already reserved characters. Applications that emptied `$reservedCharacters` to share one key namespace between handlers and used directory separators in File cache keys now get an `InvalidArgumentException`. Build keys from identifiers with a controlled format, or hash the input, instead of relaxing `$reservedCharacters`. See [Caching](../libraries/caching.md). A `$prefix` that consists only of dots and spaces is rejected as well, because it would make every cache file a hidden file that `getCacheInfo()` does not report, `deleteMatching()` does not remove and `clean()` does not delete.
17+
With the default configuration most applications are unaffected, because `/`, `\` and `:` are already reserved characters. Applications that emptied `$reservedCharacters` to share one key namespace between handlers and used directory separators in File cache keys now get an `InvalidArgumentException`. Build keys from identifiers with a controlled format, or hash the input, instead of relaxing `$reservedCharacters`. See [Caching](../libraries/caching.md).
1818

1919
## Breaking Enhancements
2020

‎docs/libraries/caching.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ In the case that the first choice `$handler` is not available, this is the next
4646

4747
If you have more than one application using the same cache storage, you can add a custom prefix string here that is prepended to all key names.
4848

49-
The File handler rejects a prefix that consists only of dots and spaces. Such a prefix would turn every cache file into a hidden file, which `getCacheInfo()` does not report, `deleteMatching()` does not remove and `clean()` does not delete. A prefix that merely starts with a dot, such as `.app`, is still accepted for backward compatibility, even though it also produces hidden files.
50-
5149
### $ttl
5250

5351
The default number of seconds to save items when none is specified.
@@ -326,7 +324,7 @@ var_dump($cache->getMetadata('my_cached_item'));
326324

327325
- **Return type**: `string`
328326

329-
This method is used by handler methods to check that keys are valid. It will throw an `InvalidArgumentException` for non-strings, invalid characters, and empty lengths. The File handler additionally rejects `/`, `\` and null bytes in the resulting file name, even when `Config\Cache::$reservedCharacters` is relaxed. It also rejects a prefix that consists only of dots and spaces.
327+
This method is used by handler methods to check that keys are valid. It will throw an `InvalidArgumentException` for non-strings, invalid characters, and empty lengths. The File handler additionally rejects `/`, `\` and null bytes in the resulting file name, even when `Config\Cache::$reservedCharacters` is relaxed.
330328

331329
Example:
332330

‎system/Cache/Handlers/FileHandler.php‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,19 @@ public function initialize(): void
8686
* to the configured reserved characters, because rejecting characters that
8787
* are valid in file names on some platforms would break existing keys.
8888
*
89-
* A prefix made only of dots and spaces is rejected as well, because it
90-
* would turn every cache file into a hidden file that `getCacheInfo()` does
91-
* not report, `deleteMatching()` does not remove and
92-
* `clean()` does not delete.
93-
*
9489
* @param mixed $key The key to validate
9590
* @param string $prefix Optional prefix to include in length calculations
9691
*
97-
* @throws InvalidArgumentException When $key is not valid, the resulting
92+
* @throws InvalidArgumentException When $key is not valid or the resulting
9893
* file name contains a directory separator
99-
* or a null byte, or $prefix consists only
100-
* of dots and spaces
94+
* or a null byte
10195
*/
10296
public static function validateKey($key, $prefix = ''): string
10397
{
10498
// Parent validation also hashes keys that exceed MAX_KEY_LENGTH, so
10599
// validate the returned file name and not the raw key.
106100
$key = parent::validateKey($key, $prefix);
107101

108-
if ($prefix !== '' && trim($prefix, '. ') === '') {
109-
throw new InvalidArgumentException('Cache key prefix cannot consist only of dots and spaces.');
110-
}
111-
112102
if (strpbrk($key, '/\\') !== false || str_contains($key, "\0")) {
113103
throw new InvalidArgumentException('Cache key cannot contain directory separators or null bytes.');
114104
}

‎tests/system/Cache/Handlers/FileHandlerTest.php‎

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -572,44 +572,9 @@ public static function provideValidateKeyRejectsSeparatorsAndNullBytes(): iterab
572572

573573
yield 'backslash zero prefix' => ['key', '\\0'];
574574

575-
// trim() strips the leading and trailing dots only, so the remaining
576-
// backslashes make this prefix pass the dot-only check.
577575
yield 'escaped traversal in prefix' => ['key', '..\\\\..\\\\'];
578576
}
579577

580-
#[DataProvider('provideValidateKeyRejectsDotOnlyPrefix')]
581-
public function testValidateKeyRejectsDotOnlyPrefix(string $key, string $prefix): void
582-
{
583-
config('Cache')->reservedCharacters = '';
584-
585-
$this->expectException(InvalidArgumentException::class);
586-
$this->expectExceptionMessage('Cache key prefix cannot consist only of dots and spaces.');
587-
588-
FileHandler::validateKey($key, $prefix);
589-
}
590-
591-
/**
592-
* @return iterable<string, array{string, string}>
593-
*/
594-
public static function provideValidateKeyRejectsDotOnlyPrefix(): iterable
595-
{
596-
yield 'dot' => ['key', '.'];
597-
598-
yield 'two dots' => ['key', '..'];
599-
600-
yield 'three dots' => ['key', '...'];
601-
602-
yield 'two dots and a space' => ['key', '.. '];
603-
604-
yield 'spaces around a dot' => ['key', ' . '];
605-
606-
yield 'space only' => ['key', ' '];
607-
608-
yield 'spaces only' => ['key', ' '];
609-
610-
yield 'prefix with dots and plain key' => ['item', '..'];
611-
}
612-
613578
#[DataProvider('provideValidateKeyAcceptsHarmlessNames')]
614579
public function testValidateKeyAcceptsHarmlessNames(string $key, string $prefix, string $expected): void
615580
{
@@ -649,6 +614,20 @@ public static function provideValidateKeyAcceptsHarmlessNames(): iterable
649614

650615
yield 'drive letter' => ['C:file', '', 'C:file'];
651616

617+
yield 'dot prefix' => ['key', '.', '.key'];
618+
619+
yield 'two dots prefix' => ['item', '..', '..item'];
620+
621+
yield 'three dots prefix' => ['key', '...', '...key'];
622+
623+
yield 'two dots and a space prefix' => ['key', '.. ', '.. key'];
624+
625+
yield 'spaces around a dot prefix' => ['key', ' . ', ' . key'];
626+
627+
yield 'space only prefix' => ['key', ' ', ' key'];
628+
629+
yield 'spaces only prefix' => ['key', ' ', ' key'];
630+
652631
yield 'dot but not dot-only prefix' => ['key', '.app', '.appkey'];
653632

654633
yield 'colon in prefix' => ['key', 'app:', 'app:key'];
@@ -702,15 +681,13 @@ public function testValidateKeyRejectsTraversalInPrefixOfHashedKey(): void
702681
FileHandler::validateKey(str_repeat('a', 300), '../');
703682
}
704683

705-
public function testValidateKeyRejectsDotOnlyPrefixOfHashedKey(): void
684+
public function testValidateKeyHashesLongKeyWithDotPrefix(): void
706685
{
707686
config('Cache')->reservedCharacters = '';
708687

709-
$this->expectException(InvalidArgumentException::class);
710-
$this->expectExceptionMessage('Cache key prefix cannot consist only of dots and spaces.');
688+
$key = str_repeat('a', 300);
711689

712-
// Hashing the key must not bypass the prefix check.
713-
FileHandler::validateKey(str_repeat('a', 300), '.');
690+
$this->assertSame('.' . md5($key), FileHandler::validateKey($key, '.'));
714691
}
715692

716693
public function testValidateKeyRejectsEmptyKey(): void
@@ -821,18 +798,19 @@ public function testPublicApiRejectsSeparatorWithDefaultConfig(): void
821798
$this->handler->save('../evil', 'value');
822799
}
823800

824-
public function testDotOnlyPrefixFromConfigIsRejectedByPublicApi(): void
801+
public function testDotPrefixFromConfigIsHandledSafelyByPublicApi(): void
825802
{
803+
config('Cache')->reservedCharacters = '';
804+
826805
$config = new Cache();
827806
$config->prefix = '..';
828807
$config->file['storePath'] = $this->config->file['storePath'];
829808

830809
$handler = CacheFactory::getHandler($config, 'file');
831810

832-
$this->expectException(InvalidArgumentException::class);
833-
$this->expectExceptionMessage('Cache key prefix cannot consist only of dots and spaces.');
834-
835-
$handler->save('key', 'value');
811+
$this->assertTrue($handler->save('key', 'value'));
812+
$this->assertSame('value', $handler->get('key'));
813+
$this->assertTrue($handler->delete('key'));
836814
}
837815

838816
/**

0 commit comments

Comments
 (0)