Resolve hash_equals() error w/ expired csrf_token, add login redirect

This commit is contained in:
billz 2025-01-21 00:33:24 -08:00
parent 6cb0be96b4
commit 111c9581a3

View File

@ -336,23 +336,26 @@ function CSRFMetaTag()
*/ */
function CSRFValidate() function CSRFValidate()
{ {
if(isset($_POST['csrf_token'])) { if (empty($_SESSION['csrf_token']) || !is_string($_SESSION['csrf_token'])) {
$post_token = $_POST['csrf_token']; error_log('Session expired or CSRF token is missing.');
$header_token = $_SERVER['HTTP_X_CSRF_TOKEN'] ?? null; header('Location: /login');
exit;
}
if (empty($post_token) && is_null($header_token)) { $post_token = $_POST['csrf_token'] ?? null;
return false; $header_token = $_SERVER['HTTP_X_CSRF_TOKEN'] ?? null;
}
$request_token = $post_token; if (empty($post_token) && is_null($header_token)) {
if (empty($post_token)) { error_log('CSRF token missing in the request');
$request_token = $header_token; return false;
} }
if (hash_equals($_SESSION['csrf_token'], $request_token)) { $request_token = $post_token ?: $header_token;
return true;
} else { if (hash_equals($_SESSION['csrf_token'], $request_token)) {
error_log('CSRF violation'); return true;
return false; } else {
} error_log('CSRF token mismatch');
return false;
} }
} }