From 01747fda2662e2dc864c3f9c0fdbe247487ae163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Cort=C3=A9s=20Cano?= <38475681+TrapoSAMA@users.noreply.github.com> Date: Tue, 13 Oct 2026 13:10:21 -0300 Subject: [PATCH] Revoke stale XML-RPC authorization across web login and MFA (#1173) Stop someone sharing a session cookie between xmlrpc and normal logins, and using it to avoid TOTP auth. thanks @TrapoSAMA --- CHANGELOG.TXT | 1 + functions.inc.php | 2 + public/xmlrpc.php | 3 +- tests/XmlRpcSessionHttpTest.php | 189 ++++++++++++++++++ tests/fixtures/xmlrpc-session-http-router.php | 32 +++ 5 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 tests/XmlRpcSessionHttpTest.php create mode 100644 tests/fixtures/xmlrpc-session-http-router.php diff --git a/CHANGELOG.TXT b/CHANGELOG.TXT index d7be49cc..2796560f 100644 --- a/CHANGELOG.TXT +++ b/CHANGELOG.TXT @@ -9,6 +9,7 @@ Changes in 'master' (not in a release yet) (20XX/XX/XX) -------------------------- + - Invalidate XML-RPC authorization when web authentication replaces the session, and reject pending MFA sessions. - Restore SMTP delivery without authentication when admin_smtp_password is empty. - Show the configured TOTP status and Reset TOTP action without reserving space for the hidden form (see #1166, thanks @TrapoSAMA) - Distinguish unknown local alias destinations from external destinations without inspecting other administrators' domains (see discussion #886, thanks @TrapoSAMA) diff --git a/functions.inc.php b/functions.inc.php index 055f6486..3b427274 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -106,6 +106,8 @@ function authentication_require_role(string $role) */ function init_session(string $username, bool $is_admin = false, bool $mfa_complete = false): bool { + // RPC authorization belongs to the previous mailbox session. + unset($_SESSION['authenticated']); $status = session_regenerate_id(true); $_SESSION['sessid'] = array(); $_SESSION['sessid']['roles'] = array(); diff --git a/public/xmlrpc.php b/public/xmlrpc.php index f944c1b0..b7438570 100644 --- a/public/xmlrpc.php +++ b/public/xmlrpc.php @@ -59,7 +59,8 @@ function login($username, $password) return false; } -if (!isset($_SESSION['authenticated'])) { +if (($_SESSION['authenticated'] ?? false) !== true || authentication_mfa_incomplete()) { + unset($_SESSION['authenticated']); $server->addFunction('login', 'login'); } else { $server->setClass('UserProxy', 'user'); diff --git a/tests/XmlRpcSessionHttpTest.php b/tests/XmlRpcSessionHttpTest.php new file mode 100644 index 00000000..cb5b8024 --- /dev/null +++ b/tests/XmlRpcSessionHttpTest.php @@ -0,0 +1,189 @@ +directory = sys_get_temp_dir() . '/pfa-xmlrpc-' . bin2hex(random_bytes(6)); + self::assertTrue(mkdir($this->directory)); + $config = Config::getInstance()->getAll(); + $config['totp'] = 'YES'; + $config['xmlrpc_enabled'] = true; + $config['default_language'] = 'en'; + $config['language_hook'] = ''; + $config['mailbox_post_TOTP_change_secret_script'] = ''; + file_put_contents($this->directory . '/config.json', json_encode($config, JSON_THROW_ON_ERROR)); + self::assertSame(1, db_insert('domain', [ + 'domain' => self::DOMAIN, 'description' => 'MFA HTTP test', 'transport' => 'virtual', + ])); + foreach ([self::USER_A => null, self::USER_B => self::SECRET] as $username => $secret) { + self::assertSame(1, db_insert('mailbox', [ + 'username' => $username, 'password' => pacrypt('fixture-password'), 'name' => 'RPC test', 'maildir' => $username . '/', + 'local_part' => explode('@', $username)[0], 'domain' => self::DOMAIN, 'active' => true, 'totp_secret' => $secret, + ])); + self::assertSame(1, db_insert('alias', [ + 'address' => $username, 'goto' => 'target-' . explode('@', $username)[0] . '@external.example.com', 'domain' => self::DOMAIN, 'active' => true, + ])); + } + $socket = stream_socket_server('tcp://127.0.0.1:0'); + self::assertIsResource($socket); + $address = stream_socket_get_name($socket, false); + fclose($socket); + $this->url = 'http://' . $address; + $command = [PHP_BINARY]; + if (php_ini_loaded_file()) { + array_push($command, '-c', php_ini_loaded_file()); + } + array_push($command, '-d', 'session.save_path=' . $this->directory, '-d', 'log_errors=1', '-d', 'error_log=' . $this->directory . '/error.log', '-S', $address, __DIR__ . '/fixtures/xmlrpc-session-http-router.php'); + $env = getenv(); + $env['PFA_XMLRPC_TEST_CONFIG'] = $this->directory . '/config.json'; + $this->server = proc_open($command, [0 => ['pipe', 'r'], 1 => ['file', $this->directory . '/server.log', 'a'], 2 => ['file', $this->directory . '/server.log', 'a']], $pipes, dirname(__DIR__), $env); + self::assertIsResource($this->server); + fclose($pipes[0]); + for ($attempt = 0; $attempt < 100; $attempt++) { + $ready = @stream_socket_client('tcp://' . $address, $errno, $error, 0.1); + if ($ready !== false) { + fclose($ready); + return; + } + usleep(50000); + } + self::fail('PHP HTTP server did not start: ' . file_get_contents($this->directory . '/server.log')); + } + + protected function tearDown(): void + { + if (is_resource($this->server)) { + proc_terminate($this->server); + proc_close($this->server); + } + foreach ([self::USER_A, self::USER_B] as $username) { + db_execute('DELETE FROM ' . table_by_key('alias') . ' WHERE address = :username', ['username' => $username]); + db_execute('DELETE FROM ' . table_by_key('mailbox') . ' WHERE username = :username', ['username' => $username]); + } + db_execute('DELETE FROM ' . table_by_key('domain') . ' WHERE domain = :domain', ['domain' => self::DOMAIN]); + if (isset($this->directory) && is_dir($this->directory)) { + foreach (new DirectoryIterator($this->directory) as $file) { + if (!$file->isDot()) { + unlink($file->getPathname()); + } + } + rmdir($this->directory); + } + } + + public function testMailboxIdentityChangeRevokesRpcUntilFreshLogin(): void + { + self::assertTrue($this->rpc('login.login', [self::USER_A, 'fixture-password'])); + self::assertSame(['target-a@external.example.com'], $this->rpc('alias.get')); + [$status] = $this->request('/users/login.php', [ + 'CSRF_Token' => $this->token(), 'fUsername' => self::USER_B, 'fPassword' => 'fixture-password', 'lang' => 'en', + ]); + self::assertSame(302, $status); + $state = $this->state(); + self::assertSame(self::USER_B, $state['username']); + self::assertFalse($state['mfa_complete']); + self::assertFalse($state['rpc_authorized']); + $this->assertRpcUnavailable(); + // Finishing MFA must not restore the old RPC authorization either. + if (time() % 30 === 29) { + usleep(1100000); + } + [$status] = $this->request('/users/login-mfa.php', [ + 'CSRF_Token' => $this->token(), 'fTOTP_code' => \OTPHP\TOTP::create(self::SECRET)->now(), + ]); + self::assertSame(302, $status); + self::assertTrue($this->state()['mfa_complete']); + $this->assertRpcUnavailable(); + self::assertFalse($this->rpc('login.login', [self::USER_B, 'fixture-password'])); + self::assertTrue($this->rpc('login.login', [self::USER_A, 'fixture-password'])); + self::assertSame(['target-a@external.example.com'], $this->rpc('alias.get')); + } + + public function testLegacyPendingMfaCookieCannotUseRpcProxies(): void + { + [$status] = $this->request('/__legacy-pending'); + self::assertSame(200, $status); + self::assertTrue($this->state()['rpc_authorized']); + $this->assertRpcUnavailable(); + self::assertFalse($this->state()['rpc_authorized']); + self::assertTrue($this->rpc('login.login', [self::USER_A, 'fixture-password'])); + self::assertSame(['target-a@external.example.com'], $this->rpc('alias.get')); + } + + public function testCompletedWebLoginAlsoRequiresFreshRpcLogin(): void + { + self::assertTrue($this->rpc('login.login', [self::USER_A, 'fixture-password'])); + [$status] = $this->request('/users/login.php', [ + 'CSRF_Token' => $this->token(), 'fUsername' => self::USER_A, 'fPassword' => 'fixture-password', 'lang' => 'en', + ]); + self::assertSame(302, $status); + self::assertTrue($this->state()['mfa_complete']); + self::assertFalse($this->state()['rpc_authorized']); + $this->assertRpcUnavailable(); + self::assertTrue($this->rpc('login.login', [self::USER_A, 'fixture-password'])); + self::assertSame(['target-a@external.example.com'], $this->rpc('alias.get')); + } + + private function assertRpcUnavailable(): void + { + foreach (['alias.get', 'vacation.getDetails', 'user.changePassword'] as $method) { + [$status, $body] = $this->request('/xmlrpc.php', (string)new \Zend_XmlRpc_Request($method, []), 'text/xml'); + self::assertSame(200, $status, $body); + $response = new \Zend_XmlRpc_Response(); + $response->loadXml($body); + self::assertTrue($response->isFault(), $body); + self::assertSame(620, $response->getFault()->getCode(), $body); + } + } + + private function rpc(string $method, array $params = []) + { + [$status, $body] = $this->request('/xmlrpc.php', (string)new \Zend_XmlRpc_Request($method, $params), 'text/xml'); + self::assertSame(200, $status, $body); + $response = new \Zend_XmlRpc_Response(); + $response->loadXml($body); + self::assertFalse($response->isFault(), $body); + return $response->getReturnValue(); + } + + private function state(): array + { + [$status, $body] = $this->request('/__state'); + self::assertSame(200, $status, $body); + return json_decode($body, true, 512, JSON_THROW_ON_ERROR); + } + + private function token(): string + { + return $this->state()['token']; + } + + private function request(string $path, array|string|null $post = null, string $contentType = 'application/x-www-form-urlencoded'): array + { + $options = ['method' => $post === null ? 'GET' : 'POST', 'ignore_errors' => true, 'follow_location' => 0, 'timeout' => 10, + 'header' => "Cookie: {$this->cookie}\r\nContent-Type: {$contentType}\r\n"]; + if ($post !== null) { + $options['content'] = is_array($post) ? http_build_query($post) : $post; + } + $body = file_get_contents($this->url . $path, false, stream_context_create(['http' => $options])); + self::assertNotFalse($body); + foreach ($http_response_header as $header) { + if (preg_match('/^Set-Cookie: (postfixadmin_session=[^;]+)/i', $header, $match)) { + $this->cookie = $match[1]; + } + } + return [(int)explode(' ', $http_response_header[0])[1], $body]; + } +} diff --git a/tests/fixtures/xmlrpc-session-http-router.php b/tests/fixtures/xmlrpc-session-http-router.php new file mode 100644 index 00000000..1056b789 --- /dev/null +++ b/tests/fixtures/xmlrpc-session-http-router.php @@ -0,0 +1,32 @@ + CsrfToken::generate(), + 'username' => $_SESSION['sessid']['username'] ?? null, + 'mfa_complete' => $_SESSION['sessid']['mfa_complete'] ?? null, + 'rpc_authorized' => $_SESSION['authenticated'] ?? false, + ]); +} elseif (in_array($path, ['/xmlrpc.php', '/users/login.php', '/users/login-mfa.php'], true)) { + $page = dirname(__DIR__, 2) . '/public' . $path; + chdir(dirname($page)); + require $page; +} else { + http_response_code(404); +}