diff --git a/CHANGELOG.TXT b/CHANGELOG.TXT index 236f872c..1d936ece 100644 --- a/CHANGELOG.TXT +++ b/CHANGELOG.TXT @@ -8,6 +8,7 @@ Changes in 'master' (not in a release yet) (20XX/XX/XX) -------------------------- + - Enforce managed-domain authorization for administrator TOTP self-exceptions, including partial edits. - Follow up security search filtering, password-recovery messages and password confirmation for TOTP exception forms. - Populate TOTP actions for each returned administrator or mailbox and handle empty account lists. - Open expanded navigation dropdowns on mouse hover while retaining click, touch and keyboard controls. diff --git a/model/TotpPf.php b/model/TotpPf.php index 09cbc7b8..7b385dc4 100644 --- a/model/TotpPf.php +++ b/model/TotpPf.php @@ -317,7 +317,7 @@ class TotpPf } // Ensure admin has access to the domain of the exception - if ($exception_username != $username && !in_array($Exception_domain, $domains)) { + if (!in_array($Exception_domain, $domains)) { throw new \Exception(Config::Lang('pException_user_entire_domain_error')); } } else { diff --git a/model/TotpexceptionHandler.php b/model/TotpexceptionHandler.php index 11c330d5..1a0c0c68 100644 --- a/model/TotpexceptionHandler.php +++ b/model/TotpexceptionHandler.php @@ -110,6 +110,10 @@ class TotpexceptionHandler extends PFAHandler $password = ''; } $this->checkCurrentPassword($password); + // Partial edits must authorize the stored target as well as submitted targets. + if ($this->is_admin && !$this->new && !isset($values['username'])) { + $this->_validate_username('username', (string)($this->result['username'] ?? '')); + } if (!$this->is_admin) { $this->values['username'] = $this->username; } @@ -186,11 +190,6 @@ class TotpexceptionHandler extends PFAHandler return true; } - // Match TotpPf::addException(): an administrator may exempt their own login. - if ($value === $this->admin_username) { - return true; - } - // Admins can set for users/domains they manage if (strpos($value, '@')) { list($_, $exception_domain) = explode('@', $value); diff --git a/tests/TotpexceptionHandlerTest.php b/tests/TotpexceptionHandlerTest.php index 26d3162c..4760970f 100644 --- a/tests/TotpexceptionHandlerTest.php +++ b/tests/TotpexceptionHandlerTest.php @@ -211,7 +211,7 @@ class TotpexceptionHandlerTest extends \PHPUnit\Framework\TestCase $this->assertSame([], $this->storedUsernames()); } - public function testAdminSelfExceptionDoesNotAuthorizeItsUnmanagedDomain(): void + public function testAdminCannotExemptSelfOutsideManagedDomains(): void { foreach (['foo.com', 'bar.com'] as $domain) { db_insert('domain', ['domain' => $domain, 'description' => 'managed', @@ -220,7 +220,7 @@ class TotpexceptionHandlerTest extends \PHPUnit\Framework\TestCase 'domain' => $domain, 'active' => true], ['created'], true); } $this->asAdmin(); - $targets = ['admin@example.com' => true, 'foo.com' => true, 'user@foo.com' => true, + $targets = ['admin@example.com' => false, 'foo.com' => true, 'user@foo.com' => true, 'user@bar.com' => true, 'example.com' => false, 'other@example.com' => false]; foreach ($targets as $target => $allowed) { $handler = new TotpexceptionHandler(1, 'admin@example.com', 1); @@ -229,9 +229,45 @@ class TotpexceptionHandlerTest extends \PHPUnit\Framework\TestCase 'username' => $target, 'ip' => '192.0.2.20', 'description' => 'scope check']), $target); } $totp = new TotpPf('admin', new Login('admin')); + try { + $totp->addException('admin@example.com', 'admin-password', + '192.0.2.21', 'admin@example.com', 'unmanaged self exception'); + $this->fail('An administrator must not exempt an unmanaged own login'); + } catch (\Exception $e) { + $this->assertSame(Config::lang('pException_user_entire_domain_error'), $e->getMessage()); + } + $this->assertSame([], $this->storedUsernames()); + } + + public function testAdminCanExemptSelfInManagedDomain(): void + { + db_insert('domain_admins', ['username' => 'admin@example.com', + 'domain' => 'example.com', 'active' => true], ['created'], true); + $this->asAdmin(); + $handler = new TotpexceptionHandler(1, 'admin@example.com', 1); + $this->assertTrue($handler->init('')); + $this->assertTrue($handler->set(['password_current' => 'admin-password', + 'username' => 'admin@example.com', 'ip' => '192.0.2.22', 'description' => 'managed self'])); + $this->assertTrue($handler->save()); + $totp = new TotpPf('admin', new Login('admin')); $this->assertTrue($totp->addException('admin@example.com', 'admin-password', - '192.0.2.21', 'admin@example.com', 'existing dedicated-page contract')); - $this->assertSame(['admin@example.com'], $this->storedUsernames()); + '192.0.2.23', 'admin@example.com', 'managed self via dedicated page')); + $this->assertSame(['admin@example.com', 'admin@example.com'], $this->storedUsernames()); + } + + public function testAdminCannotEditUnmanagedSelfExceptionByOmittingTarget(): void + { + db_insert('totp_exception_address', ['username' => 'admin@example.com', + 'ip' => '192.0.2.24', 'description' => 'existing exception'], ['created', 'modified'], true); + $row = db_query_one('SELECT id FROM totp_exception_address'); + $this->asAdmin(); + $handler = new TotpexceptionHandler(0, 'admin@example.com', 1); + $this->assertTrue($handler->init((string)$row['id'])); + foreach ([[], ['username' => null]] as $target) { + $this->assertFalse($handler->set($target + ['password_current' => 'admin-password', 'ip' => '192.0.2.25'])); + $this->assertFalse($handler->save()); + } + $this->assertSame('192.0.2.24', db_query_one('SELECT ip FROM totp_exception_address')['ip']); } }