You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/AccountPasswordSecurityTest...

40 lines
1.8 KiB
PHTML

<?php
require dirname(__DIR__) . '/app/Utils/Hash.php';
use App\Utils\Hash;
$assert = static function ($condition, $message) {
if (!$condition) {
throw new RuntimeException($message);
}
};
$password = 'StrongAccount!123';
$first = Hash::passwordHash($password);
$second = Hash::passwordHash($password);
$info = password_get_info($first);
$assert(strlen($first) === 60 && $info['algoName'] === 'bcrypt' && $info['options']['cost'] === 12,
'New hashes must be bcrypt cost 12 and fit the existing 64-character column.');
$assert($first !== $second && Hash::checkPassword($first, $password), 'Passwords must use independent random salts.');
$assert(!Hash::checkPassword($first, 'WrongAccount!123'), 'Wrong password was accepted.');
$assert(!Hash::checkPassword(md5($password), $password), 'Legacy MD5 must not authenticate fresh-install accounts.');
$assert(!Hash::checkPassword($first, ['invalid']), 'Array password was accepted.');
foreach (['shortA!1', 'alllowercase!123', 'ALLUPPERCASE!123', 'NoSymbol123456', 'NoDigitsLong!Aa',
str_repeat('a', 73) . 'A!1', "ValidLong!A1\0suffix"] as $invalid) {
$assert(Hash::newPasswordError($invalid) !== null, 'Invalid new password was accepted.');
try {
Hash::passwordHash($invalid);
throw new RuntimeException('Weak new password was hashed.');
} catch (InvalidArgumentException $exception) {
// Expected.
}
}
$maximum = 'Aa!1' . str_repeat('x', 68);
$assert(Hash::newPasswordError($maximum) === null, 'A strong 72-byte password was rejected.');
$weakExistingHash = password_hash('short', PASSWORD_BCRYPT);
$assert(Hash::checkPassword($weakExistingHash, 'short'), 'Login must not apply the new-password composition policy.');
echo 'Account password security checks passed.' . PHP_EOL;