From 77ccefdbf257307eb0dd51d80155b20cc9bb71c4 Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Mon, 21 Sep 2026 09:39:45 +0100 Subject: [PATCH] fix incomplete MFA logins having the ability to change state when a user is part way through an MFA login (username/password auth done, but not the MFA bit), authentication_get_username() will return their current username. This function is used in various other places and historically it was a suitable check to determine if someone was also authenticated. Commit updates authentication_get_username() to include a call to authentication_mfa_incomplete() by default, the only exclusion being on the login-mfa.php pages. Thanks to Wenhao Wu (Southeast University) for the security report "MFA-incomplete (password-only) mailbox session can rewrite its own alias forwarding via /edit.php (CWE-308)" --- functions.inc.php | 18 ++++++++++++++---- public/login-mfa.php | 8 ++++---- public/users/login-mfa.php | 6 +++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/functions.inc.php b/functions.inc.php index c86830fd..470f3db5 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -35,12 +35,12 @@ function authentication_mfa_incomplete(): bool } /** - * check_session - * Action: Check if a session already exists, if not redirect to login.php - * Call: check_session () + * Action: Check if a session already exists, if not redirect to login.php. Returns current user's login. + * + * @param bool $requireMfaComplete - false only for login-mfa.php * @return string username (e.g. foo@example.com) or CLI or SETUP.PHP */ -function authentication_get_username() +function authentication_get_username(bool $requireMfaComplete = true) { if (defined('POSTFIXADMIN_CLI')) { return 'CLI'; @@ -54,6 +54,16 @@ function authentication_get_username() header("Location: login.php"); exit(0); } + + /** + * when an mfa user logs in (username+password) but haven't yet done the MFA code, $_SESSION['sessid']['username'] will be populated + * but the user hasn't finished authentication, so we should not treat them as having logged in. + */ + if ($requireMfaComplete && authentication_mfa_incomplete()) { + header("Location: login-mfa.php"); + exit(0); + } + return $_SESSION['sessid']['username']; } diff --git a/public/login-mfa.php b/public/login-mfa.php index 9e06b018..81b1287b 100644 --- a/public/login-mfa.php +++ b/public/login-mfa.php @@ -66,18 +66,18 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") { $fTotp = safepost('fTOTP_code'); $h = new AdminHandler(); - if (authentication_mfa_incomplete() && $totppf->checkUserTOTP(authentication_get_username(), $fTotp)) { - init_session(authentication_get_username(), true, true); + if (authentication_mfa_incomplete() && $totppf->checkUserTOTP(authentication_get_username(false), $fTotp)) { + init_session(authentication_get_username(false), true, true); // get superadmin status and store in session - $h->init(authentication_get_username()); + $h->init(authentication_get_username(false)); if ($h->result()['superadmin'] == 1) { $_SESSION['sessid']['roles'][] = 'global-admin'; } header("Location: main.php"); exit(0); } else { - error_log("PostfixAdmin admin second factor login failed (username: " . authentication_get_username() . ", ip_address: {$_SERVER['REMOTE_ADDR']})"); + error_log("PostfixAdmin admin second factor login failed (username: " . authentication_get_username(false) . ", ip_address: {$_SERVER['REMOTE_ADDR']})"); $error = $PALANG['pTotp_failed']; flash_error($PALANG['pTotp_failed']); } diff --git a/public/users/login-mfa.php b/public/users/login-mfa.php index 9524bdc6..4bc7bc8c 100644 --- a/public/users/login-mfa.php +++ b/public/users/login-mfa.php @@ -60,12 +60,12 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") { $totppf = new TotpPf('mailbox', new Login('mailbox')); $fTotp = safepost('fTOTP_code'); - if (authentication_mfa_incomplete() && $totppf->checkUserTOTP(authentication_get_username(), $fTotp)) { - init_session(authentication_get_username(), false, true); + if (authentication_mfa_incomplete() && $totppf->checkUserTOTP(authentication_get_username(false), $fTotp)) { + init_session(authentication_get_username(false), false, true); header("Location: main.php"); exit(0); } else { # $h->login failed - error_log("PostfixAdmin admin second factor login failed (username: " . authentication_get_username() . ", ip_address: {$_SERVER['REMOTE_ADDR']})"); + error_log("PostfixAdmin admin second factor login failed (username: " . authentication_get_username(false) . ", ip_address: {$_SERVER['REMOTE_ADDR']})"); flash_error($PALANG['pTotp_failed']); } }