Skip to content

Commit 1450b1d

Browse files
authored
Passwords: hash(): Password can not be empty. (#47)
1 parent 2eacede commit 1450b1d

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/Security/Passwords.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function __construct($algo = PASSWORD_DEFAULT, array $options = [])
4141
*/
4242
public function hash(string $password): string
4343
{
44+
if ($password === '') {
45+
throw new Nette\InvalidArgumentException('Password can not be empty.');
46+
}
47+
4448
$hash = isset($this)
4549
? @password_hash($password, $this->algo, $this->options) // @ is escalated to exception
4650
: @password_hash($password, PASSWORD_BCRYPT, func_get_args()[1] ?? []); // back compatibility with v2.x

tests/Security/Passwords.hash().phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require __DIR__ . '/../bootstrap.php';
1414

1515

1616
Assert::truthy(
17-
preg_match('#^\$.{50,}\z#', (new Passwords)->hash(''))
17+
preg_match('#^\$.{50,}\z#', (new Passwords)->hash('my-password'))
1818
);
1919

2020
Assert::truthy(
@@ -27,3 +27,7 @@ Assert::same($hash, crypt('dg', $hash));
2727
Assert::exception(function () {
2828
(new Passwords(PASSWORD_BCRYPT, ['cost' => 3]))->hash('dg');
2929
}, Nette\InvalidStateException::class, 'Computed hash is invalid. password_hash(): Invalid bcrypt cost parameter specified: 3');
30+
31+
Assert::exception(function () {
32+
(new Passwords)->hash('');
33+
}, Nette\InvalidArgumentException::class, 'Password can not be empty.');

tests/Security/Passwords.static.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php';
1515

1616
// deprecated static usage
1717
Assert::error(function () {
18-
Passwords::hash('');
18+
Passwords::hash('my-password');
1919
}, E_DEPRECATED, 'Non-static method Nette\Security\Passwords::hash() should not be called statically');
2020

2121
Assert::truthy(

0 commit comments

Comments
 (0)