Skip to content

Commit 9a085a0

Browse files
committed
fix(Cache): reject directory traversal sequences and separators in FileHandler::validateKey()
The file handler concatenates the cache key with the cache store path, so a key that contains a directory separator or a traversal sequence can address files outside the cache directory. BaseHandler::validateKey() only rejects such characters when Config\Cache::$reservedCharacters happens to contain them, and that setting is commonly emptied when several handlers share the same keys (for example file storage next to Redis). FileHandler::validateKey() now always rejects "/", "\", a null byte, and traversal sequences ("..") in both the key and the prefix, independently of the configured reservedCharacters, throwing CodeIgniter\Exceptions\InvalidArgumentException. Valid keys keep the existing PSR-6 validation and hashing behaviour.
1 parent 59e5eea commit 9a085a0

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎system/Cache/Handlers/FileHandler.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Cache\Handlers;
1515

1616
use CodeIgniter\Cache\Exceptions\CacheException;
17+
use CodeIgniter\Exceptions\InvalidArgumentException;
1718
use CodeIgniter\I18n\Time;
1819
use Config\Cache;
1920
use FilesystemIterator;
@@ -76,6 +77,32 @@ public function initialize(): void
7677
{
7778
}
7879

80+
/**
81+
* Validates a cache key, ensuring it contains no directory traversal sequences
82+
* or path separators, regardless of the configured reservedCharacters setting.
83+
*
84+
* @param mixed $key The key to validate
85+
* @param string $prefix Optional prefix to include in length calculations
86+
*
87+
* @throws InvalidArgumentException When $key is not valid or contains path separators
88+
*/
89+
public static function validateKey($key, $prefix = ''): string
90+
{
91+
if (! is_string($key)) {
92+
throw new InvalidArgumentException('Cache key must be a string');
93+
}
94+
95+
if (strpbrk($key, "/\\\0") !== false || str_contains($key, '..')) {
96+
throw new InvalidArgumentException('Cache key cannot contain directory separators or traversal sequences.');
97+
}
98+
99+
if (strpbrk($prefix, "/\\\0") !== false || str_contains($prefix, '..')) {
100+
throw new InvalidArgumentException('Cache prefix cannot contain directory separators or traversal sequences.');
101+
}
102+
103+
return parent::validateKey($key, $prefix);
104+
}
105+
79106
public function get(string $key): mixed
80107
{
81108
$key = static::validateKey($key, $this->prefix);

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Cache\CacheFactory;
1717
use CodeIgniter\Cache\Exceptions\CacheException;
1818
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\Exceptions\InvalidArgumentException;
1920
use CodeIgniter\I18n\Time;
2021
use Config\Cache;
2122
use PHPUnit\Framework\Attributes\DataProvider;
@@ -504,4 +505,55 @@ public function testReconnect(): void
504505
{
505506
$this->assertTrue($this->handler->reconnect());
506507
}
508+
509+
#[DataProvider('provideValidateKeyRejectsTraversalAndSeparatorsEvenWithEmptyReservedCharacters')]
510+
public function testValidateKeyRejectsTraversalAndSeparatorsEvenWithEmptyReservedCharacters(string $key): void
511+
{
512+
$config = config('Cache');
513+
$originalReservedCharacters = $config->reservedCharacters;
514+
515+
// Multi-store setups often empty this setting to share keys between
516+
// handlers, which must not weaken the file handler's own validation.
517+
$config->reservedCharacters = '';
518+
519+
try {
520+
$this->expectException(InvalidArgumentException::class);
521+
$this->expectExceptionMessage('Cache key cannot contain directory separators or traversal sequences.');
522+
523+
FileHandler::validateKey($key);
524+
} finally {
525+
$config->reservedCharacters = $originalReservedCharacters;
526+
}
527+
}
528+
529+
/**
530+
* @return iterable<string, array{string}>
531+
*/
532+
public static function provideValidateKeyRejectsTraversalAndSeparatorsEvenWithEmptyReservedCharacters(): iterable
533+
{
534+
yield 'parent directory' => ['../evil'];
535+
536+
yield 'nested traversal' => ['sub/../../evil'];
537+
538+
yield 'forward slash separator' => ['sub/key'];
539+
540+
yield 'backslash separator' => ['sub\\key'];
541+
542+
yield 'parent traversal only' => ['..'];
543+
544+
yield 'null byte' => ["sub\0key"];
545+
}
546+
547+
public function testValidateKeyRejectsTraversalInPrefix(): void
548+
{
549+
$this->expectException(InvalidArgumentException::class);
550+
$this->expectExceptionMessage('Cache prefix cannot contain directory separators or traversal sequences.');
551+
552+
FileHandler::validateKey('valid_key', '../');
553+
}
554+
555+
public function testValidateKeySuccess(): void
556+
{
557+
$this->assertSame('valid_key', FileHandler::validateKey('valid_key'));
558+
}
507559
}

0 commit comments

Comments
 (0)