-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from boesing/feature/psr-6-maximum-cache-key-…
…length-validation PSR-6 maximum cache key length validation
- Loading branch information
Showing
10 changed files
with
241 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\Cache\Psr; | ||
|
||
use Laminas\Cache\Psr\SimpleCache\SimpleCacheInvalidArgumentException; | ||
use Laminas\Cache\Storage\Capabilities; | ||
use Laminas\Cache\Storage\StorageInterface; | ||
|
||
use function get_class; | ||
use function min; | ||
use function preg_match; | ||
use function sprintf; | ||
|
||
/** | ||
* Provides memoizing of maximum key length for a storage adapter. | ||
* | ||
* @internal | ||
*/ | ||
trait MaximumKeyLengthTrait | ||
{ | ||
/** | ||
* PCRE runs into a compilation error if the quantifier exceeds this limit | ||
* | ||
* @internal | ||
* | ||
* @readonly | ||
* @var positive-int | ||
*/ | ||
public static $pcreMaximumQuantifierLength = 65535; | ||
|
||
/** | ||
* @var int | ||
* @psalm-var 0|positive-int | ||
*/ | ||
private $maximumKeyLength; | ||
|
||
private function memoizeMaximumKeyLengthCapability(StorageInterface $storage, Capabilities $capabilities): void | ||
{ | ||
$maximumKeyLength = $capabilities->getMaxKeyLength(); | ||
|
||
if ($maximumKeyLength === Capabilities::UNLIMITED_KEY_LENGTH) { | ||
$this->maximumKeyLength = Capabilities::UNLIMITED_KEY_LENGTH; | ||
return; | ||
} | ||
|
||
if ($maximumKeyLength === Capabilities::UNKNOWN_KEY_LENGTH) { | ||
// For backward compatibility, assume adapters which do not provide a maximum key length do support 64 chars | ||
$maximumKeyLength = 64; | ||
} | ||
|
||
if ($maximumKeyLength < 64) { | ||
throw new SimpleCacheInvalidArgumentException(sprintf( | ||
'The storage adapter "%s" does not fulfill the minimum requirements for PSR-6/PSR-16:' | ||
. ' The maximum key length capability must allow at least 64 characters.', | ||
get_class($storage) | ||
)); | ||
} | ||
|
||
/** @psalm-suppress PropertyTypeCoercion The result of this will always be > 0 */ | ||
$this->maximumKeyLength = min($maximumKeyLength, self::$pcreMaximumQuantifierLength - 1); | ||
} | ||
|
||
private function exceedsMaximumKeyLength(string $key): bool | ||
{ | ||
return $this->maximumKeyLength !== Capabilities::UNLIMITED_KEY_LENGTH | ||
&& preg_match('/^.{' . ($this->maximumKeyLength + 1) . ',}/u', $key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.