Hash editor passwords with Argon2id and upgrade old hashes.

Old hashes are replaced at login. Widen the column before deploying:

  ALTER TABLE editors MODIFY password VARCHAR(255) NOT NULL;
This commit is contained in:
tim
2026-09-25 21:41:36 +02:00
parent 15ad07423a
commit dfa6d71198
3 changed files with 66 additions and 5 deletions
+20 -3
View File
@@ -5,6 +5,8 @@ if ( ! isset($_SERVER['HTTPS'])) {
require_once("../session.lib.php");
$added = FALSE;
$csrf_failed = FALSE;
$hash_failed = FALSE;
$exists = FALSE;
if (!isset($_SESSION['editor'])) {
header("Location: https://xxx.org.za/publish/login");
return;
@@ -16,11 +18,20 @@ if (!isset($_SESSION['editor'])) {
$table = "editors";
$username = (string)($_POST['username'] ?? '');
$password = password_hash((string)($_POST['password'] ?? ''), PASSWORD_DEFAULT);
$password = password_hash((string)($_POST['password'] ?? ''), password_algorithm());
$email = (string)($_POST['email'] ?? '');
$found = $db->execute_query("SELECT username FROM $table WHERE username = ?", array($username));
$query = "INSERT INTO $table SET `username` = ?, `password` = ?, `email` = ?";
if ($result = $db->execute_query($query, array($username, $password, $email))) {
$added = TRUE;
if ($found && $found->fetch_assoc()) {
$exists = TRUE;
} else if ($result = $db->execute_query($query, array($username, $password, $email))) {
// If the password column cut the hash short, the editor could never log in
if (password_hash_is_stored($username, $password)) {
$added = TRUE;
} else {
$db->execute_query("DELETE FROM $table WHERE username = ?", array($username));
$hash_failed = TRUE;
}
$is_admin = TRUE;
}
open_db('close');
@@ -54,6 +65,12 @@ if (!isset($_SESSION['editor'])) {
<?php if ($csrf_failed) { ?>
<p><strong>Your session has expired. Please try again.</strong></p>
<?php } ?>
<?php if ($exists) { ?>
<p><strong>An editor with that username already exists.</strong></p>
<?php } ?>
<?php if ($hash_failed) { ?>
<p><strong>The editor was not added: the editors.password column is too short for the password hash. Widen it to VARCHAR(255).</strong></p>
<?php } ?>
<form action="<?= form_value("https://xxx.org.za".$_SERVER['REQUEST_URI']) ?>" method="post">
<?= csrf_field() ?>
+5 -2
View File
@@ -26,12 +26,15 @@ if (isset($_SESSION['editor'])) {
$stored_password = $row['password'];
else $denied = TRUE;
$getid->close();
open_db('close');
$compare_password = $_POST['password'] ?? '';
$compare_password = is_string($_POST['password'] ?? NULL) ? $_POST['password'] : '';
// password_verify() also accepts hashes made by the old crypt() call
if (!password_verify($compare_password, $stored_password)) {
$denied = TRUE;
open_db('close');
} else {
// Move an old hash to the current algorithm while we have the password
rehash_password($username, $compare_password, $stored_password);
open_db('close');
login_editor($username);
// Keep user logged in with cookie, if they request it
if (isset($_POST['remember'])) {