Skip to content

Commit 5b662e5

Browse files
caseysoftwareclaude
andcommitted
Verify the current password with password_verify() when changing it
CUser::validatePassword() hashed the submitted password again and looked for a users row with that exact hash. Passwords are stored with password_hash(), which uses a random salt, so the new hash never matched and every self-service password change failed with "You have entered the wrong old password". Load the stored hash for the user and check it with password_verify(). The unit test previously passed only because the mock returns staged rows regardless of the WHERE clause; it now stages a real bcrypt hash and also asserts that a wrong password is rejected. Refs #386 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016CP89WVmGZ1VVCCZMS6ntN
1 parent 0db81ed commit 5b662e5

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎modules/users/users.class.php‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,23 @@ public function hook_cron()
230230
return true;
231231
}
232232

233+
/**
234+
* Checks a plain-text password against the user's stored hash.
235+
*
236+
* The stored value is a salted password_hash() result, so it has to be
237+
* verified with password_verify(); hashing the input again never matches.
238+
*
239+
* @param int $userId
240+
* @param string $password
241+
*
242+
* @return bool
243+
*/
233244
public function validatePassword($userId, $password)
234245
{
235-
$hash = $this->authenticator->hashPassword($password);
236-
237-
$users = $this->loadAll('user_id', 'user_password = \'' . $hash . '\' AND user_id = ' . (int) $userId);
246+
$users = $this->loadAll('user_id', 'user_id = ' . (int) $userId);
247+
$hash = (string) ($users[$userId]['user_password'] ?? '');
238248

239-
return isset($users[$userId]);
249+
return '' !== $hash && password_verify((string) $password, $hash);
240250
}
241251

242252
public function getIdByToken($token)

‎unit_tests/modules/users/CUsersTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,14 @@ public function testValidatePassword()
137137
$result = $this->obj->validatePassword(1, 'password');
138138
$this->assertFalse($result);
139139

140+
$this->post_data['user_password'] = password_hash('password', PASSWORD_BCRYPT);
140141
$this->mockDB->stageHashList(1, $this->post_data);
141142
$result = $this->obj->validatePassword(1, 'password');
142143
$this->assertTrue($result);
144+
145+
// The wrong password must not validate against the stored hash.
146+
$result = $this->obj->validatePassword(1, 'not-the-password');
147+
$this->assertFalse($result);
143148
}
144149

145150
/*

0 commit comments

Comments
 (0)