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

fix: Security::derandomize() may cause hex2bin() error #6292

Merged
merged 1 commit into from
Jul 25, 2022
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
20 changes: 17 additions & 3 deletions system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Config\Cookie as CookieConfig;
use Config\Security as SecurityConfig;
use Config\Services;
use ErrorException;
use InvalidArgumentException;
use LogicException;

/**
Expand Down Expand Up @@ -278,8 +280,13 @@ public function verify(RequestInterface $request)
}

$postedToken = $this->getPostedToken($request);
$token = ($postedToken !== null && $this->tokenRandomize)
? $this->derandomize($postedToken) : $postedToken;

try {
$token = ($postedToken !== null && $this->tokenRandomize)
? $this->derandomize($postedToken) : $postedToken;
} catch (InvalidArgumentException $e) {
$token = null;
}

// Do the tokens match?
if (! isset($token, $this->hash) || ! hash_equals($this->hash, $token)) {
Expand Down Expand Up @@ -359,13 +366,20 @@ protected function randomize(string $hash): string

/**
* Derandomize the token.
*
* @throws InvalidArgumentException "hex2bin(): Hexadecimal input string must have an even length"
*/
protected function derandomize(string $token): string
{
$key = substr($token, -static::CSRF_HASH_BYTES * 2);
$value = substr($token, 0, static::CSRF_HASH_BYTES * 2);

return bin2hex(hex2bin($value) ^ hex2bin($key));
try {
return bin2hex(hex2bin($value) ^ hex2bin($key));
} catch (ErrorException $e) {
// "hex2bin(): Hexadecimal input string must have an even length"
throw new InvalidArgumentException($e->getMessage());
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/system/Security/SecurityCSRFSessionRandomizeTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ public function testCSRFVerifyPostThrowsExceptionOnNoMatch()
$security->verify($request);
}

public function testCSRFVerifyPostInvalidToken()
{
$this->expectException(SecurityException::class);
$this->expectExceptionMessage('The action you requested is not allowed.');

$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['csrf_test_name'] = '!';

$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());

$security = new Security(new MockAppConfig());

$security->verify($request);
}

public function testCSRFVerifyPostReturnsSelfOnMatch()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
Expand Down