Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

Harden OIDC state validation and session rotation (#1071)

AmarBego57a02c610b6f
diff --git a/includes/checksession.php b/includes/checksession.php
index cfa0caa..bd18ec8 100644
--- a/includes/checksession.php
+++ b/includes/checksession.php
@@ -14,6 +14,21 @@ if (isset($_GET['code']) && isset($_GET['state'])) {
// This request is coming from the OIDC login flow
$code = $_GET['code'];
$state = $_GET['state'];
+ $expectedState = $_SESSION['oidc_state'] ?? null;
+
+ if (
+ !is_string($code) || $code === '' ||
+ !is_string($state) || $state === '' ||
+ !is_string($expectedState) || $expectedState === '' ||
+ !hash_equals($expectedState, $state)
+ ) {
+ unset($_SESSION['oidc_state']);
+ $db->close();
+ header("Location: login.php?error=oidc_invalid_state");
+ exit();
+ }
+
+ unset($_SESSION['oidc_state']);
if (!isset($_SESSION['oidc_state']) || !hash_equals($_SESSION['oidc_state'], $state)) {
header("Location: login.php");
@@ -88,6 +103,7 @@ if (isset($_GET['code']) && isset($_GET['state'])) {
$row = $result->fetchArray(SQLITE3_ASSOC);
if ($row != false) {
+ session_regenerate_id(true);
$_SESSION['username'] = $username;
$_SESSION['token'] = $token;
$_SESSION['loggedin'] = true;
diff --git a/includes/oidc/oidc_login.php b/includes/oidc/oidc_login.php
index 104d4b1..82a4662 100644
--- a/includes/oidc/oidc_login.php
+++ b/includes/oidc/oidc_login.php
@@ -9,6 +9,7 @@ $username = $userData['username'];
$language = $userData['language'];
$main_currency = $userData['main_currency'];
+session_regenerate_id(true);
$_SESSION['username'] = $username;
$_SESSION['loggedin'] = true;
$_SESSION['main_currency'] = $main_currency;
diff --git a/login.php b/login.php
index fb58a73..7e7888a 100644
--- a/login.php
+++ b/login.php
@@ -58,6 +58,7 @@ if ($adminRow['login_disabled'] == 1) {
$username = $row['username'];
$language = $row['language'];
+ session_regenerate_id(true);
$_SESSION['username'] = $username;
$_SESSION['loggedin'] = true;
$_SESSION['main_currency'] = $main_currency;
@@ -229,6 +230,7 @@ if (isset($_POST['username']) && isset($_POST['password'])) {
]);
}
+ session_regenerate_id(true);
$_SESSION['username'] = $username;
$_SESSION['loggedin'] = true;
$_SESSION['main_currency'] = $main_currency;
@@ -296,7 +298,7 @@ if (!$password_login_disabled) {
}
-if (isset($_GET['error']) && $_GET['error'] == "oidc_user_not_found") {
+if (isset($_GET['error']) && in_array($_GET['error'], ["oidc_user_not_found", "oidc_invalid_state"], true)) {
$loginFailed = true;
}
diff --git a/totp.php b/totp.php
index a9aa492..c3b8caf 100644
--- a/totp.php
+++ b/totp.php
@@ -104,6 +104,7 @@ if (isset($_POST['one-time-code'])) {
$result = $stmt->execute();
$user = $result->fetchArray(SQLITE3_ASSOC);
+ session_regenerate_id(true);
$_SESSION['username'] = $user['username'];
$_SESSION['loggedin'] = true;
$_SESSION['main_currency'] = $user['main_currency'];