diff --git a/src/Guard.php b/src/Guard.php index 5a4619f..9e3721a 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -81,7 +81,7 @@ class Guard implements MiddlewareInterface * * @var array|null */ - protected $keyPair; + protected $keyPair = null; /** * @param ResponseFactoryInterface $responseFactory @@ -102,20 +102,18 @@ public function __construct( int $strength = 16, bool $persistentTokenMode = false ) { - $this->responseFactory = $responseFactory; - $this->prefix = rtrim($prefix, '_'); - if ($strength < 16) { throw new RuntimeException('CSRF middleware instantiation failed. Minimum strength is 16.'); } + + $this->responseFactory = $responseFactory; + $this->prefix = rtrim($prefix, '_'); $this->strength = $strength; $this->setStorage($storage); $this->setFailureHandler($failureHandler); $this->setStorageLimit($storageLimit); $this->setPersistentTokenMode($persistentTokenMode); - - $this->keyPair = null; } /** @@ -226,19 +224,17 @@ public function generateToken(): array */ public function validateToken(string $name, string $value): bool { - $valid = false; + if (!isset($this->storage[$name])) { + return false; + } - if (isset($this->storage[$name])) { - $token = $this->storage[$name]; + $token = $this->storage[$name]; - if (function_exists('hash_equals')) { - $valid = hash_equals($token, $value); - } else { - $valid = $token === $value; - } + if (function_exists('hash_equals')) { + return hash_equals($token, $value); } - return $valid; + return $token === $value; } /** @@ -347,20 +343,26 @@ public function removeTokenFromStorage(string $name): void */ protected function enforceStorageLimit(): void { - if ($this->storageLimit > 0 - && (is_array($this->storage) - || ($this->storage instanceof Countable && $this->storage instanceof Iterator) + if ($this->storageLimit === 0 + || ( + !is_array($this->storage) + && !($this->storage instanceof Countable && $this->storage instanceof Iterator) ) ) { - if (is_array($this->storage)) { - while (count($this->storage) > $this->storageLimit) { - array_shift($this->storage); - } - } elseif ($this->storage instanceof Iterator) { - while (count($this->storage) > $this->storageLimit) { - $this->storage->rewind(); - unset($this->storage[$this->storage->key()]); - } + return; + } + + if (is_array($this->storage)) { + while (count($this->storage) > $this->storageLimit) { + array_shift($this->storage); + } + return; + } + + if ($this->storage instanceof Iterator) { + while (count($this->storage) > $this->storageLimit) { + $this->storage->rewind(); + unset($this->storage[$this->storage->key()]); } } } diff --git a/tests/GuardTest.php b/tests/GuardTest.php index 6fade8f..1b1f65b 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -244,6 +244,22 @@ public function testEnforceStorageLimitWithArray() $this->assertArrayHasKey('test_name2', $storage); } + public function testNotEnforceStorageLimitWithArrayWhenLimitIsZero() + { + $initial_storage = $storage = [ + 'test_name' => 'value', + 'test_name2' => 'value2', + ]; + $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); + $mw = new Guard($responseFactoryProphecy->reveal(), 'test', $storage, null, 0); + + $enforceStorageLimitMethod = new ReflectionMethod($mw, 'enforceStorageLimit'); + $enforceStorageLimitMethod->setAccessible(true); + $enforceStorageLimitMethod->invoke($mw); + + $this->assertSame($initial_storage, $storage); + } + public function testEnforceStorageLimitWithIterator() { $storage = new ArrayIterator([