From 2c8a8893e9274a965a29743749a1da908cc736a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Cort=C3=A9s=20Cano?= <38475681+TrapoSAMA@users.noreply.github.com> Date: Sat, 29 Aug 2026 15:07:23 -0400 Subject: [PATCH] Enforce TOTP policy for XML-RPC login (#1131) if a user/mailbox has TOTP enabled, block access via XMLRPC (if it's enabled) as the two aren't compatible. Part of #1128. thanks @TrapoSAMA --- config.inc.php | 2 ++ model/TotpPf.php | 12 ++++++++++++ public/xmlrpc.php | 3 ++- tests/TotpPfTest.php | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/config.inc.php b/config.inc.php index ce99aaf5..6ca3cf2e 100644 --- a/config.inc.php +++ b/config.inc.php @@ -790,6 +790,8 @@ $CONF['theme_custom_css'] = ''; // XMLRPC Interface. // This should be only of use if you wish to use e.g the // Postfixadmin-Squirrelmail package +// XML-RPC has no TOTP challenge flow. Mailbox accounts with a TOTP +// secret cannot authenticate through this interface. // change to boolean true to enable xmlrpc $CONF['xmlrpc_enabled'] = false; diff --git a/model/TotpPf.php b/model/TotpPf.php index 278bd9f1..498b48cf 100644 --- a/model/TotpPf.php +++ b/model/TotpPf.php @@ -133,6 +133,18 @@ class TotpPf return (is_array($result) && isset($result['totp_secret']) && !empty($result['totp_secret'])); } + /** + * Authenticate an interface that cannot complete a TOTP challenge. + */ + public function passwordOnlyLogin(string $username, string $password): bool + { + if (!$this->login->login($username, $password)) { + return false; + } + + return !$this->usesTOTP($username); + } + /** * Check if a TOTP code is valid for a user */ diff --git a/public/xmlrpc.php b/public/xmlrpc.php index dd1d5647..f944c1b0 100644 --- a/public/xmlrpc.php +++ b/public/xmlrpc.php @@ -48,7 +48,8 @@ $server = new Zend_XmlRpc_Server(); function login($username, $password) { $login = new Login('mailbox'); - if ($login->login($username, $password)) { + $totp = new TotpPf('mailbox', $login); + if ($totp->passwordOnlyLogin($username, $password)) { session_regenerate_id(); $_SESSION['authenticated'] = true; $_SESSION['sessid'] = array(); diff --git a/tests/TotpPfTest.php b/tests/TotpPfTest.php index 0f639e6c..9b3ef3b1 100644 --- a/tests/TotpPfTest.php +++ b/tests/TotpPfTest.php @@ -76,6 +76,24 @@ class TotpPfTest extends TestCase $this->assertNotEmpty($currentCode); } + public function testPasswordOnlyLoginRejectsAccountsUsingTotp(): void + { + global $CONF; + Config::write('totp', 'YES'); + $CONF['totp'] = 'YES'; + + $totp = new TotpPf('mailbox', new Login('mailbox')); + $this->assertTrue($totp->passwordOnlyLogin('test@example.com', 'foobar')); + + db_execute( + 'UPDATE mailbox SET totp_secret = :secret WHERE username = :username', + ['username' => 'test@example.com', 'secret' => 'existing-secret'] + ); + + $this->assertFalse($totp->passwordOnlyLogin('test@example.com', 'foobar')); + $this->assertFalse($totp->passwordOnlyLogin('test@example.com', 'incorrect-password')); + } + public function testDbOperations() { $x = new TotpPf('mailbox', new Login('mailbox'));