Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/Config/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ class Cache extends BaseConfig
*/
public $ttl = 60;

/**
* --------------------------------------------------------------------------
* Reserved Characters
* --------------------------------------------------------------------------
*
* A string of reserved characters that will not be allowed in keys or tags.
* Strings that violate this restriction will cause handlers to throw.
* Default: {}()/\@:
* Note: The default set is required for PSR-6 compliance.
*
* @var string
*/
public $reservedCharacters = '{}()/\@:';

/**
* --------------------------------------------------------------------------
* File settings
Expand Down
10 changes: 7 additions & 3 deletions system/Cache/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
abstract class BaseHandler implements CacheInterface
{
/**
* Reserved characters that cannot be used in a key or tag.
* Reserved characters that cannot be used in a key or tag. May be overridden by the config.
* From https://github.com/symfony/cache-contracts/blob/c0446463729b89dd4fa62e9aeecc80287323615d/ItemInterface.php#L43
*
* @deprecated in favor of the Cache config
*/
public const RESERVED_CHARACTERS = '{}()/\@:';

Expand Down Expand Up @@ -58,8 +60,10 @@ public static function validateKey($key, $prefix = ''): string
if ($key === '') {
throw new InvalidArgumentException('Cache key cannot be empty.');
}
if (strpbrk($key, self::RESERVED_CHARACTERS) !== false) {
throw new InvalidArgumentException('Cache key contains reserved characters ' . self::RESERVED_CHARACTERS);

$reserved = config('Cache')->reservedCharacters ?? self::RESERVED_CHARACTERS;
if ($reserved && strpbrk($key, $reserved) !== false) {
throw new InvalidArgumentException('Cache key contains reserved characters ' . $reserved);
}

// If the key with prefix exceeds the length then return the hashed version
Expand Down
10 changes: 10 additions & 0 deletions tests/system/Cache/Handlers/BaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public function invalidTypeProvider(): array
];
}

public function testValidateKeyUsesConfig()
{
config('Cache')->reservedCharacters = 'b';

$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Cache key contains reserved characters b');

BaseHandler::validateKey('banana');
}

public function testValidateKeySuccess()
{
$string = 'banana';
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ See all the changes.
.. toctree::
:titlesonly:

v4.2.0
v4.1.4
v4.1.3
v4.1.2
Expand Down
18 changes: 18 additions & 0 deletions user_guide_src/source/changelogs/v4.2.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Version 4.2.0
=============

Release Date: Not released

**4.2.0 release of CodeIgniter4**

Enhancements:

- Added Cache config for reserved characters

Changes:

Deprecations:

- Deprecated ``CodeIgniter\\Cache\\Handlers\\BaseHandler::RESERVED_CHARACTERS`` in favor of the new config property

Bugs Fixed: