Skip to content

Commit 33e6dad

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 6f92d1f commit 33e6dad

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎system/Cache/Handlers/FileHandler.php‎

Lines changed: 29 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,34 @@ 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+
$target = $prefix . $key;
96+
97+
if (
98+
$target === ''
99+
|| strpbrk($target, "/\\:\0") !== false
100+
|| trim($target, '. ') === ''
101+
) {
102+
throw new InvalidArgumentException('Cache key cannot contain directory separators or traversal sequences.');
103+
}
104+
105+
return parent::validateKey($key, $prefix);
106+
}
107+
79108
public function get(string $key): mixed
80109
{
81110
$key = static::validateKey($key, $this->prefix);

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

Lines changed: 70 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,73 @@ 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 traversal only' => ['..'];
535+
536+
yield 'current directory only' => ['.'];
537+
538+
yield 'multiple dots' => ['...'];
539+
540+
yield 'dots with trailing space' => ['.. '];
541+
542+
yield 'parent directory' => ['../evil'];
543+
544+
yield 'nested traversal' => ['sub/../../evil'];
545+
546+
yield 'forward slash separator' => ['sub/key'];
547+
548+
yield 'backslash separator' => ['sub\\key'];
549+
550+
yield 'null byte' => ["sub\0key"];
551+
552+
yield 'colon character' => ['test:stream'];
553+
554+
yield 'drive letter' => ['C:file'];
555+
556+
yield 'spaces and dots' => [' . '];
557+
}
558+
559+
public function testValidateKeyRejectsTraversalInPrefix(): void
560+
{
561+
$this->expectException(InvalidArgumentException::class);
562+
$this->expectExceptionMessage('Cache key cannot contain directory separators or traversal sequences.');
563+
564+
FileHandler::validateKey('valid_key', '../');
565+
}
566+
567+
public function testValidateKeySuccess(): void
568+
{
569+
$this->assertSame('valid_key', FileHandler::validateKey('valid_key'));
570+
$this->assertSame('foo..json', FileHandler::validateKey('foo..json'));
571+
$this->assertSame('user..settings', FileHandler::validateKey('user..settings'));
572+
$this->assertSame('..item', FileHandler::validateKey('..item'));
573+
$this->assertSame('..item', FileHandler::validateKey('item', '..'));
574+
$this->assertSame('item..', FileHandler::validateKey('item..'));
575+
$this->assertSame('..foo', FileHandler::validateKey('..foo'));
576+
}
507577
}

0 commit comments

Comments
 (0)