Skip to content

Commit 936cd5a

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 936cd5a

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

‎system/Cache/Handlers/FileHandler.php‎

Lines changed: 25 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,30 @@ 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 (strpbrk($target, "/\\\0") !== false || rtrim($target, ' .') === '') {
98+
throw new InvalidArgumentException('Cache key cannot contain directory separators or traversal sequences.');
99+
}
100+
101+
return parent::validateKey($key, $prefix);
102+
}
103+
79104
public function get(string $key): mixed
80105
{
81106
$key = static::validateKey($key, $this->prefix);

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

Lines changed: 64 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,67 @@ 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+
553+
public function testValidateKeyRejectsTraversalInPrefix(): void
554+
{
555+
$this->expectException(InvalidArgumentException::class);
556+
$this->expectExceptionMessage('Cache key cannot contain directory separators or traversal sequences.');
557+
558+
FileHandler::validateKey('valid_key', '../');
559+
}
560+
561+
public function testValidateKeySuccess(): void
562+
{
563+
$this->assertSame('valid_key', FileHandler::validateKey('valid_key'));
564+
$this->assertSame('foo..json', FileHandler::validateKey('foo..json'));
565+
$this->assertSame('user..settings', FileHandler::validateKey('user..settings'));
566+
$this->assertSame('..item', FileHandler::validateKey('..item'));
567+
$this->assertSame('..item', FileHandler::validateKey('item', '..'));
568+
$this->assertSame('item..', FileHandler::validateKey('item..'));
569+
$this->assertSame('..foo', FileHandler::validateKey('..foo'));
570+
}
507571
}

0 commit comments

Comments
 (0)