Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor improvements in code complexity with early returns and early validation #116

Merged
merged 4 commits into from
May 6, 2020
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
56 changes: 29 additions & 27 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Guard implements MiddlewareInterface
*
* @var array|null
*/
protected $keyPair;
protected $keyPair = null;

/**
* @param ResponseFactoryInterface $responseFactory
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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()]);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down