Skip to content
Open
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
3 changes: 2 additions & 1 deletion system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ErrorException;
use JsonException;
use SensitiveParameter;
use ValueError;

/**
* Class Security
Expand Down Expand Up @@ -419,7 +420,7 @@ protected function derandomize(#[SensitiveParameter] string $token): string

try {
return bin2hex((string) hex2bin($value) ^ (string) hex2bin($key));
} catch (ErrorException $e) {
} catch (ErrorException|ValueError $e) {
// "hex2bin(): Hexadecimal input string must have an even length"
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/system/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CodeIgniter\Config\Factories;
use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
Expand Down Expand Up @@ -419,4 +420,15 @@ public static function provideGetPostedTokenReturnsNullForInvalidInputs(): itera

yield 'invalid_form_data' => [self::createIncomingRequest()->setBody('csrf_test_name[]=invalid')];
}

public function testDerandomizeThrowsInvalidArgumentExceptionOnInvalidHex(): void
{
$security = $this->createMockSecurity();
$derandomize = self::getPrivateMethodInvoker($security, 'derandomize');

$this->expectException(InvalidArgumentException::class);

// 'G' is not a valid hexadecimal character, which will trigger hex2bin's ValueError/Warning
$derandomize(str_repeat('G', 64));
}
}
Loading