Skip to content

Commit

Permalink
[PasswordHasher] Make bcrypt nul byte hash test tolerant to PHP relat…
Browse files Browse the repository at this point in the history
…ed failures
  • Loading branch information
alexandre-daubois committed May 7, 2024
1 parent 93fcdb8 commit 426de2c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,33 @@ public function testBcryptWithLongPassword()
}

/**
* "password_hash()" does not accept passwords containing NUL bytes prior to PHP 8.2
* and throws a ValueError, thus this test is skipped because `$hasher->verify()` will
* not be executed.
*
* @requires PHP >= 8.2
* @requires PHP < 8.4
*/
public function testBcryptWithNulByte()
{
$hasher = new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT);
$plainPassword = "a\0b";

if (\PHP_VERSION_ID < 80218 || \PHP_VERSION_ID >= 80300 && \PHP_VERSION_ID < 80305) {
// password_hash() does not accept passwords containing NUL bytes since PHP 8.2.18 and 8.3.5
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword));
try {
$hash = password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]);

if (null === $hash) {
// we skip the test in case password_hash() returns null as
// implemented in patches backport
// at https://github.com/shivammathur/php-src-backports/commit/d22d9ebb29dce86edd622205dd1196a2796c08c7
$this->markTestSkipped('password_hash() does not accept passwords containing NUL bytes.');
}
} catch (\Throwable $throwable) {
// we also skip the test in case the PHP version does not support NUL bytes in passwords
// with bcrypt, as introduced in https://github.com/php/php-src/commit/11f2568767660ffe92fbc6799800e01203aad73a
if (false !== strpos($throwable->getMessage(), 'Bcrypt password must not contain null character')) {
$this->markTestSkipped('password_hash() does not accept passwords containing NUL bytes.');
}

throw $throwable;
}

$this->assertTrue($hasher->verify($hasher->hash($plainPassword), $plainPassword));
$this->assertTrue($hasher->verify($hash, $plainPassword));
}

public function testNeedsRehash()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,33 @@ public function testBcryptWithLongPassword()
}

/**
* "password_hash()" does not accept passwords containing NUL bytes prior to PHP 8.2
* and throws a ValueError, thus this test is skipped because `$hasher->verify()` will
* not be executed.
*
* @requires PHP >= 8.2
* @requires PHP < 8.4
*/
public function testBcryptWithNulByte()
{
$hasher = new SodiumPasswordHasher(null, null);
$plainPassword = "a\0b";

if (\PHP_VERSION_ID < 80218 || \PHP_VERSION_ID >= 80300 && \PHP_VERSION_ID < 80305) {
// password_hash() does not accept passwords containing NUL bytes since PHP 8.2.18 and 8.3.5
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword));
try {
$hash = password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]);

if (null === $hash) {
// we skip the test in case password_hash() returns null as
// implemented in patches backport
// at https://github.com/shivammathur/php-src-backports/commit/d22d9ebb29dce86edd622205dd1196a2796c08c7
$this->markTestSkipped('password_hash() does not accept passwords containing NUL bytes.');
}
} catch (\Throwable $throwable) {
// we skip the test in case the PHP version does not support NUL bytes in passwords
// with bcrypt, as introduced in https://github.com/php/php-src/commit/11f2568767660ffe92fbc6799800e01203aad73a
if (false !== strpos($throwable->getMessage(), 'Bcrypt password must not contain null character')) {
$this->markTestSkipped('password_hash() does not accept passwords containing NUL bytes.');
}

throw $throwable;
}

$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT))->hash($plainPassword), $plainPassword));
$this->assertTrue($hasher->verify($hash, $plainPassword));
}

public function testUserProvidedSaltIsNotUsed()
Expand Down

0 comments on commit 426de2c

Please sign in to comment.