Davidson CI testing & AI refinement
pauljd/davidson-ci: Davidson CI / quality (push) Successful in 20s
pauljd/davidson-ci: Davidson CI / ai-review (push) Successful in 1m14s

This commit is contained in:
2026-09-11 10:41:47 +01:00
parent 8ccbb22034
commit f033480104
4 changed files with 89 additions and 23 deletions
+28 -7
View File
@@ -45,12 +45,31 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pdo->exec('CREATE INDEX IF NOT EXISTS idx_registration_attempts_identifier ON registration_attempts (identifier)');
$ipIdentifier = 'ip:' . ($_SERVER['REMOTE_ADDR'] ?? 'unknown');
$registrationAttemptId = null;
if (!csrf_verify($_POST['csrf_token'] ?? null)) {
$errors['csrf'] = 'Your session has expired. Please refresh the page and try again.';
} elseif (registrationAttemptCount($pdo, $ipIdentifier, REGISTRATION_WINDOW_SECONDS) >= MAX_REGISTRATIONS_PER_IP) {
$errors['rate_limit'] = 'Too many accounts have been created from this network. Please try again later.';
} else {
// Check the per-IP limit and reserve a slot atomically, so concurrent requests
// can't all read the same stale count before any of them commits. The reservation
// is released below if this request doesn't end up creating an account, so only
// actual registrations count against the limit — matching the previous behavior.
$pdo->exec('BEGIN IMMEDIATE');
try {
if (registrationAttemptCount($pdo, $ipIdentifier, REGISTRATION_WINDOW_SECONDS) >= MAX_REGISTRATIONS_PER_IP) {
$errors['rate_limit'] = 'Too many accounts have been created from this network. Please try again later.';
} else {
recordRegistrationAttempt($pdo, $ipIdentifier);
$registrationAttemptId = (int) $pdo->lastInsertId();
}
$pdo->exec('COMMIT');
} catch (Throwable $e) {
$pdo->exec('ROLLBACK');
throw $e;
}
}
if (empty($errors)) {
if ($username === '') {
$errors['username'] = 'Username is required.';
} elseif (strlen($username) < 3 || strlen($username) > 12) {
@@ -97,12 +116,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
'email' => $email,
'password' => $hashedPassword,
]);
// Capture the new user's id before any other insert runs on this connection —
// SQLite's lastInsertId() returns the most recent rowid inserted on the
// connection regardless of table, so recordRegistrationAttempt()'s own insert
// would otherwise clobber it and log the session in as the wrong user.
$newUserId = (int) $pdo->lastInsertId();
recordRegistrationAttempt($pdo, $ipIdentifier);
} catch (PDOException $e) {
if (str_contains($e->getMessage(), 'UNIQUE constraint failed: users.username')) {
$errors['username'] = 'That username is not available.';
@@ -130,6 +144,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
}
}
// Reaching here means this request didn't end in a created account (validation
// failed, a username/email collision was caught, or the rate limit itself
// rejected it) — release any reserved slot so only actual registrations count.
if ($registrationAttemptId !== null) {
$pdo->prepare('DELETE FROM registration_attempts WHERE id = :id')->execute(['id' => $registrationAttemptId]);
}
}
$pageTitle = 'Register';