directory = sys_get_temp_dir() . '/pfa-totp-' . bin2hex(random_bytes(6)); self::assertTrue(mkdir($this->directory)); $config = Config::getInstance()->getAll(); $config['totp'] = 'YES'; $config['page_size'] = 2; $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', ])); self::assertSame(1, db_insert('admin', [ 'username' => self::USERNAME, 'password' => pacrypt('fixture-password'), 'active' => true, 'totp_secret' => null, ])); self::assertSame(1, db_insert('mailbox', [ 'username' => self::USERNAME, 'password' => pacrypt('fixture-password'), 'name' => 'MFA test', 'maildir' => 'mfa-http/', 'local_part' => 'test', 'domain' => self::DOMAIN, 'active' => true, 'totp_secret' => null, ])); $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/totp-http-router.php'); $env = getenv(); $env['PFA_TOTP_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); } db_execute('DELETE FROM ' . table_by_key('alias') . ' WHERE address = :address', ['address' => self::USERNAME]); foreach (['admin', 'mailbox'] as $table) { db_execute('DELETE FROM ' . table_by_key($table) . ' WHERE username = :username', ['username' => self::USERNAME]); } db_delete('domain_admins', 'domain', self::DOMAIN); db_delete('mailbox', 'username', 'plain@' . self::DOMAIN); db_delete('mailbox', 'username', 'zz@' . self::DOMAIN); 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 static function accountTypes(): array { return ['admin' => ['admin', 'admin'], 'mailbox' => ['mailbox', 'user']]; } #[DataProvider('accountTypes')] public function testActivationRendersConfirmationAndResetAction(string $table, string $role): void { $token = $this->startSession($role); [$status, $body] = $this->request('/users/totp.php', [ 'CSRF_Token' => $token, 'fPassword_current' => 'fixture-password', 'fTOTP_secret' => self::SECRET, 'fTOTP_code' => $this->currentCode(self::SECRET), ]); self::assertSame(200, $status, $body); self::assertStringContainsString(Config::lang('pTotp_stored'), $body); self::assertStringContainsString('TOTP is enabled.', $body); self::assertStringContainsString('Reset TOTP', $body); self::assertMatchesRegularExpression('/id="edit_form"[^>]*style="display:none"/', $body); self::assertSame(self::SECRET, $this->storedSecret($table)); [$status, $body] = $this->request('/users/totp.php'); self::assertSame(200, $status, $body); self::assertStringContainsString('TOTP is enabled.', $body); } #[DataProvider('accountTypes')] public function testRejectedReplacementPreservesExistingSecret(string $table, string $role): void { db_update($table, 'username', self::USERNAME, ['totp_secret' => self::SECRET]); $token = $this->startSession($role); $newSecret = 'JBSWY3DPEHPK3PXQ'; foreach ([ ['wrong-password', $this->currentCode($newSecret)], ['fixture-password', 'invalid'], ] as [$password, $code]) { $token = $this->startSession($role); [$status, $body] = $this->request('/users/totp.php', [ 'CSRF_Token' => $token, 'fPassword_current' => $password, 'fTOTP_secret' => $newSecret, 'fTOTP_code' => $code, ]); self::assertSame(200, $status, $body); self::assertSame(self::SECRET, $this->storedSecret($table)); self::assertStringContainsString('TOTP is enabled.', $body); } $token = $this->startSession($role); [$status] = $this->request('/users/totp.php', [ 'CSRF_Token' => $token, 'fPassword_current' => 'fixture-password', 'fTOTP_secret' => $newSecret, 'fTOTP_code' => $this->currentCode($newSecret), ]); self::assertSame(200, $status); self::assertSame($newSecret, $this->storedSecret($table)); } #[DataProvider('accountTypes')] public function testAdministrativeWebResetRetainsPermissionsAndCsrf(string $table, string $role): void { db_update($table, 'username', self::USERNAME, ['totp_secret' => self::SECRET]); db_insert('domain_admins', ['username' => self::USERNAME, 'domain' => self::DOMAIN, 'active' => true], ['created'], true); $token = $this->startSession('global-admin'); $path = '/edit.php?table=' . $table . '&edit=' . urlencode(self::USERNAME); [$status, $body] = $this->request($path); self::assertSame(200, $status, $body); self::assertStringContainsString('name="reset_totp"', $body); [$status] = $this->request($path, ['CSRF_Token' => 'invalid', 'reset_totp' => '1']); self::assertSame(419, $status); self::assertSame(self::SECRET, $this->storedSecret($table)); $token = $this->startSession('admin'); [$status] = $this->request($path, ['CSRF_Token' => $token, 'reset_totp' => '1']); if ($table === 'admin') { self::assertSame(302, $status); self::assertSame(self::SECRET, $this->storedSecret($table)); $token = $this->startSession('global-admin'); [$status] = $this->request($path, ['CSRF_Token' => $token, 'reset_totp' => '1']); } self::assertSame(302, $status); self::assertNull($this->storedSecret($table)); } public function testDomainAdminCannotResetAnUnmanagedMailbox(): void { db_update('mailbox', 'username', self::USERNAME, ['totp_secret' => self::SECRET]); $token = $this->startSession('admin'); [$status] = $this->request('/edit.php?table=mailbox&edit=' . urlencode(self::USERNAME), ['CSRF_Token' => $token, 'reset_totp' => '1']); self::assertSame(302, $status); self::assertSame(self::SECRET, $this->storedSecret('mailbox')); } public function testMailboxMfaVisibilityUsesTheCompleteAuthorizedSearchResult(): void { db_insert('domain_admins', ['username' => self::USERNAME, 'domain' => self::DOMAIN, 'active' => true], ['created'], true); foreach (['plain' => null, 'zz' => self::SECRET] as $name => $secret) { db_insert('mailbox', ['username' => $name . '@' . self::DOMAIN, 'password' => 'unused', 'name' => $name, 'maildir' => '', 'local_part' => $name, 'domain' => self::DOMAIN, 'active' => true, 'totp_secret' => $secret], ['created', 'modified'], true); } $this->startSession('admin'); $path = '/list-virtual.php?domain=' . self::DOMAIN; [$status, $body] = $this->request($path . '&limit=0'); self::assertSame(200, $status, $body); self::assertStringContainsString('