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 259547a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,18 @@ public function testBcryptWithLongPassword()
$this->assertTrue($hasher->verify($hasher->hash($plainPassword), $plainPassword));
}

/**
* "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
*/
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 {
$verified = $hasher->verify($hasher->hash($plainPassword), $plainPassword);

$this->assertTrue($hasher->verify($hasher->hash($plainPassword), $plainPassword));
$this->assertTrue($verified);
} catch (\ValueError $error) {
$this->markTestSkipped(sprintf('password_hash() does not accept passwords containing NUL bytes on PHP %s. ', PHP_VERSION));
}
}

public function testNeedsRehash()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,18 @@ public function testBcryptWithLongPassword()
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT))->hash($plainPassword), $plainPassword));
}

/**
* "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
*/
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 {
$verified = $hasher->verify((new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT))->hash($plainPassword), $plainPassword);

$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT))->hash($plainPassword), $plainPassword));
$this->assertTrue($verified);
} catch (\ValueError $error) {
$this->markTestSkipped(sprintf('password_hash() does not accept passwords containing NUL bytes on PHP %s. ', PHP_VERSION));
}
}

public function testUserProvidedSaltIsNotUsed()
Expand Down

0 comments on commit 259547a

Please sign in to comment.