Skip to content

Commit

Permalink
Merge pull request #5008 from MGatner/cache-config
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Aug 19, 2021
2 parents 795df09 + d2fe271 commit dd63db0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
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
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:

0 comments on commit dd63db0

Please sign in to comment.