further Davidson CI testing
pauljd/davidson-ci: Davidson CI / quality (push) Successful in 13s
pauljd/davidson-ci: Davidson CI / ai-review (push) Successful in 1m0s

This commit is contained in:
2026-09-09 22:55:59 +01:00
parent 9805d5056d
commit 35d187b4c2
13 changed files with 64 additions and 11 deletions
+24
View File
@@ -56,3 +56,27 @@ function csrf_verify(?string $token): bool
{
return is_string($token) && $token !== '' && isset($_SESSION['csrf_token']) && hash_equals($_SESSION['csrf_token'], $token);
}
// Logs the session out if the user's session_version has moved on since login (e.g. a
// password reset bumped it), so a stolen/old session cookie stops working immediately
// instead of staying valid until it naturally expires. Call this right after
// start_app_session() + getDbConnection() on any page that trusts $_SESSION['user_id'].
function enforce_session_version(PDO $pdo): void
{
if (!isset($_SESSION['user_id'])) {
return;
}
$stmt = $pdo->prepare('SELECT session_version FROM users WHERE id = :id');
$stmt->execute(['id' => $_SESSION['user_id']]);
$currentVersion = $stmt->fetchColumn();
$sessionVersionMatches = $currentVersion !== false
&& isset($_SESSION['session_version'])
&& (int) $currentVersion === (int) $_SESSION['session_version'];
if (!$sessionVersionMatches) {
$_SESSION = [];
session_destroy();
}
}