Skip to content

Commit 3cb68b0

Browse files
authored
fix(Cache): reject directory separators and null bytes in cache filenames (#10766)
1 parent 785c820 commit 3cb68b0

5 files changed

Lines changed: 418 additions & 1 deletion

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Release Date: Unreleased
66

77
## BREAKING
88

9+
- **Cache:** `FileHandler::validateKey()` now rejects `/`, `\` and null bytes in the cache item file name, independently of `Config\Cache::$reservedCharacters`, so a cache key can no longer point outside the cache directory when that setting is relaxed. See [File Cache Rejects Directory Separators](../installation/upgrade_477.md#file-cache-rejects-directory-separators) for migration instructions.
10+
911
## Message Changes
1012

1113
- Added the `HTTP.invalidFileName` language string.

‎docs/installation/upgrade_477.md‎

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

1111
## Breaking Changes
1212

13+
### File Cache Rejects Directory Separators
14+
15+
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.
16+
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).
18+
1319
## Breaking Enhancements
1420

1521
## Project Files

‎docs/libraries/caching.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,26 @@ The default number of seconds to save items when none is specified.
5252

5353
WARNING: This is not used by framework handlers where 60 seconds is hard-coded, but may be useful to projects and modules. This will replace the hard-coded value in a future release.
5454

55+
### $reservedCharacters
56+
57+
A string of characters that are not allowed in cache keys or tags. Handlers throw an `InvalidArgumentException` when a key contains one of them. The default, `{}()/\@:`, is required for PSR-6 compliance.
58+
59+
Relaxing `$reservedCharacters` relaxes key validation for every cache handler. The File handler still rejects `/`, `\` and null bytes in the resulting file name, so a cache key cannot point outside the cache directory. The characters left in `$reservedCharacters` still keep keys from being interpreted differently by a handler or by the file system.
60+
61+
On Windows, keep `:` reserved: it separates an NTFS alternate data stream, so a key such as `file:stream` refers to a stream of a file in the cache directory rather than to a new cache item.
62+
63+
Do not use unvalidated user input directly as a cache key. Build keys from identifiers with a controlled format, or hash the input:
64+
65+
```php
66+
$key = 'search_' . hash('sha256', $userInput);
67+
```
68+
5569
### $file
5670

5771
This is an array of settings specific to the **File** handler to determine how it should save the cache files.
5872

73+
Cache files whose names start with a dot are not fully supported by the File handler: `getCacheInfo()` does not report them, `deleteMatching()` wildcard patterns do not match them, and `clean()` / `cache:clear` do not delete them. Avoid keys and prefixes that produce a leading dot.
74+
5975
### $memcached
6076

6177
This is an array of servers that will be used when using the **Memcached** handler.
@@ -310,7 +326,7 @@ var_dump($cache->getMetadata('my_cached_item'));
310326

311327
- **Return type**: `string`
312328

313-
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.
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.
314330

315331
Example:
316332

‎system/Cache/Handlers/FileHandler.php‎

Lines changed: 30 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,35 @@ public function initialize(): void
7677
{
7778
}
7879

80+
/**
81+
* Validates a cache key and returns the file name the item is stored under.
82+
*
83+
* The file name must not contain a directory separator or a null byte, so a
84+
* cache item can never be written outside the cache directory, even when
85+
* `Config\Cache::$reservedCharacters` is relaxed. Other characters are left
86+
* to the configured reserved characters, because rejecting characters that
87+
* are valid in file names on some platforms would break existing keys.
88+
*
89+
* @param mixed $key The key to validate
90+
* @param string $prefix Optional prefix to include in length calculations
91+
*
92+
* @throws InvalidArgumentException When $key is not valid or the resulting
93+
* file name contains a directory separator
94+
* or a null byte
95+
*/
96+
public static function validateKey($key, $prefix = ''): string
97+
{
98+
// Parent validation also hashes keys that exceed MAX_KEY_LENGTH, so
99+
// validate the returned file name and not the raw key.
100+
$key = parent::validateKey($key, $prefix);
101+
102+
if (strpbrk($key, '/\\') !== false || str_contains($key, "\0")) {
103+
throw new InvalidArgumentException('Cache key cannot contain directory separators or null bytes.');
104+
}
105+
106+
return $key;
107+
}
108+
79109
public function get(string $key): mixed
80110
{
81111
$key = static::validateKey($key, $this->prefix);

0 commit comments

Comments
 (0)