diff --git a/app/Config/Cache.php b/app/Config/Cache.php index c82e3cbe646d..7dbe30f37b52 100644 --- a/app/Config/Cache.php +++ b/app/Config/Cache.php @@ -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 diff --git a/system/Cache/Handlers/BaseHandler.php b/system/Cache/Handlers/BaseHandler.php index ffd031b00134..340100e1838c 100644 --- a/system/Cache/Handlers/BaseHandler.php +++ b/system/Cache/Handlers/BaseHandler.php @@ -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 = '{}()/\@:'; @@ -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 diff --git a/tests/system/Cache/Handlers/BaseHandlerTest.php b/tests/system/Cache/Handlers/BaseHandlerTest.php index 133c644f1b9e..aa95da40e83e 100644 --- a/tests/system/Cache/Handlers/BaseHandlerTest.php +++ b/tests/system/Cache/Handlers/BaseHandlerTest.php @@ -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'; diff --git a/user_guide_src/source/changelogs/index.rst b/user_guide_src/source/changelogs/index.rst index 038d259e4189..a997a57f0c3f 100644 --- a/user_guide_src/source/changelogs/index.rst +++ b/user_guide_src/source/changelogs/index.rst @@ -12,6 +12,7 @@ See all the changes. .. toctree:: :titlesonly: + v4.2.0 v4.1.4 v4.1.3 v4.1.2 diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst new file mode 100644 index 000000000000..c1a7d2673ab1 --- /dev/null +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -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: