diff --git a/lib/Horde.php b/lib/Horde.php index 1753421a..cb768257 100644 --- a/lib/Horde.php +++ b/lib/Horde.php @@ -13,6 +13,7 @@ * @category Horde * @license http://www.horde.org/licenses/lgpl21 LGPL * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ /** diff --git a/lib/Horde/Core/Auth/Application.php b/lib/Horde/Core/Auth/Application.php index 3393ac6b..9d447b15 100644 --- a/lib/Horde/Core/Auth/Application.php +++ b/lib/Horde/Core/Auth/Application.php @@ -11,6 +11,7 @@ * @category Horde * @license http://opensource.org/licenses/lgpl-2.1.php LGPL * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ use Horde\Core\Session\HordeSession; diff --git a/lib/Horde/Core/Block/Layout/View.php b/lib/Horde/Core/Block/Layout/View.php index 0fc43d68..91b42991 100644 --- a/lib/Horde/Core/Block/Layout/View.php +++ b/lib/Horde/Core/Block/Layout/View.php @@ -13,6 +13,7 @@ * @category Horde * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ class Horde_Core_Block_Layout_View extends Horde_Core_Block_Layout { diff --git a/lib/Horde/Core/Browser.php b/lib/Horde/Core/Browser.php index e29a3a1a..aa19dbc3 100644 --- a/lib/Horde/Core/Browser.php +++ b/lib/Horde/Core/Browser.php @@ -13,6 +13,7 @@ * @category Horde * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ class Horde_Core_Browser extends Horde_Browser { diff --git a/lib/Horde/Core/Factory/Browser.php b/lib/Horde/Core/Factory/Browser.php index f8a2ba11..af163539 100644 --- a/lib/Horde/Core/Factory/Browser.php +++ b/lib/Horde/Core/Factory/Browser.php @@ -5,6 +5,7 @@ /** * @category Horde * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ class Horde_Core_Factory_Browser extends Horde_Core_Factory_Injector { diff --git a/lib/Horde/Core/Factory/Data.php b/lib/Horde/Core/Factory/Data.php index fe21dc8f..9337963b 100644 --- a/lib/Horde/Core/Factory/Data.php +++ b/lib/Horde/Core/Factory/Data.php @@ -7,6 +7,7 @@ * * @category Horde * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils * @author Michael Slusarz * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 */ diff --git a/lib/Horde/Core/Factory/MimeViewer.php b/lib/Horde/Core/Factory/MimeViewer.php index 0a901aba..cc16b2e6 100644 --- a/lib/Horde/Core/Factory/MimeViewer.php +++ b/lib/Horde/Core/Factory/MimeViewer.php @@ -10,6 +10,7 @@ * @copyright 2010-2017 Horde LLC * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ /** diff --git a/lib/Horde/Core/HordeMap.php b/lib/Horde/Core/HordeMap.php index 80bc7524..5032f97b 100644 --- a/lib/Horde/Core/HordeMap.php +++ b/lib/Horde/Core/HordeMap.php @@ -10,6 +10,7 @@ * @copyright 2009-2017 Horde LLC (http://www.horde.org/) * @license http://www.horde.org/licenses/lgpl21 LGPL * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ /** diff --git a/lib/Horde/Core/Mime/Headers/Received.php b/lib/Horde/Core/Mime/Headers/Received.php index 5218ad68..6a11ff0a 100644 --- a/lib/Horde/Core/Mime/Headers/Received.php +++ b/lib/Horde/Core/Mime/Headers/Received.php @@ -10,6 +10,7 @@ * @copyright 2014-2017 Horde LLC * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ /** diff --git a/lib/Horde/Core/Mime/Viewer/Vcard.php b/lib/Horde/Core/Mime/Viewer/Vcard.php index 8b122c17..176a1366 100644 --- a/lib/Horde/Core/Mime/Viewer/Vcard.php +++ b/lib/Horde/Core/Mime/Viewer/Vcard.php @@ -15,6 +15,7 @@ * @category Horde * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ class Horde_Core_Mime_Viewer_Vcard extends Horde_Mime_Viewer_Base { diff --git a/lib/Horde/Core/Notification/Event/Status.php b/lib/Horde/Core/Notification/Event/Status.php index d1f8aa5c..96fcede1 100644 --- a/lib/Horde/Core/Notification/Event/Status.php +++ b/lib/Horde/Core/Notification/Event/Status.php @@ -12,6 +12,7 @@ * @category Horde * @license http://www.horde.org/licenses/lgpl21 LGPL * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ class Horde_Core_Notification_Event_Status extends Horde_Notification_Event_Status { diff --git a/lib/Horde/Core/Tree/Renderer/Javascript.php b/lib/Horde/Core/Tree/Renderer/Javascript.php index f18ab6c1..364b9a66 100644 --- a/lib/Horde/Core/Tree/Renderer/Javascript.php +++ b/lib/Horde/Core/Tree/Renderer/Javascript.php @@ -15,6 +15,7 @@ * @category Horde * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ class Horde_Core_Tree_Renderer_Javascript extends Horde_Core_Tree_Renderer_Html { diff --git a/lib/Horde/Core/Ui/VarRenderer/Html.php b/lib/Horde/Core/Ui/VarRenderer/Html.php index a253845e..23504af4 100644 --- a/lib/Horde/Core/Ui/VarRenderer/Html.php +++ b/lib/Horde/Core/Ui/VarRenderer/Html.php @@ -15,6 +15,7 @@ * @category Horde * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ class Horde_Core_Ui_VarRenderer_Html extends Horde_Core_Ui_VarRenderer { diff --git a/lib/Horde/Deprecated.php b/lib/Horde/Deprecated.php index b02e59c7..7295baa7 100644 --- a/lib/Horde/Deprecated.php +++ b/lib/Horde/Deprecated.php @@ -10,6 +10,7 @@ * @copyright 2013-2017 Horde LLC * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ /** diff --git a/lib/Horde/PageOutput.php b/lib/Horde/PageOutput.php index dcdeefe0..fa94f893 100644 --- a/lib/Horde/PageOutput.php +++ b/lib/Horde/PageOutput.php @@ -10,6 +10,7 @@ * @copyright 2012-2017 Horde LLC * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ /** diff --git a/lib/Horde/Registry.php b/lib/Horde/Registry.php index aed061b7..b2920822 100644 --- a/lib/Horde/Registry.php +++ b/lib/Horde/Registry.php @@ -10,8 +10,11 @@ * @copyright 1999-2017 Horde LLC * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ +use Horde\Browser\Browser; +use Horde\Browser\BrowserFamily; use Horde\Core\Auth\AuthCredentialStore; use Horde\Core\Config\ConfigMetadataProvider; use Horde\Core\Config\Driver\DriverRepository; @@ -392,10 +395,8 @@ public static function appInit($app, array $args = []) switch ($args['session_control']) { case 'netscape': // Chicken/egg: Browser object doesn't exist yet. - // Can't use Horde_Core_Browser since it depends on registry to be - // configured. - $browser = new Horde_Browser(); - if ($browser->isBrowser('mozilla')) { + $browser = new Browser(); + if ($browser->isBrowser(BrowserFamily::Firefox)) { $args['session_cache_limiter'] = 'private, must-revalidate'; } break; @@ -2817,7 +2818,7 @@ public function remoteHost() */ public function setAuth($authId, $credentials, array $options = []) { - global $browser, $injector, $session; + global $injector, $session; $app = empty($options['app']) ? 'horde' @@ -2831,7 +2832,7 @@ public function setAuth($authId, $credentials, array $options = []) /* Initial authentication to Horde. */ $session->set('horde', 'auth/authId', $authId); - $session->set('horde', 'auth/browser', $browser->getAgentString()); + $session->set('horde', 'auth/browser', $injector->get(Browser::class)->getUserAgent()); if (!empty($options['change'])) { $session->set('horde', 'auth/change', 1); } @@ -2871,7 +2872,7 @@ public function setAuth($authId, $credentials, array $options = []) */ public function checkExistingAuth($app = 'horde') { - global $browser, $conf, $injector, $session; + global $conf, $injector, $session; if (!empty($this->_cache['existing'][$app])) { return true; @@ -2888,11 +2889,17 @@ public function checkExistingAuth($app = 'horde') return false; } - if (!empty($conf['auth']['checkbrowser']) - && ($session->get('horde', 'auth/browser') != $browser->getAgentString())) { - $injector->get(Horde_Core_Factory_Auth::class)->create() - ->setError(Horde_Core_Auth_Application::REASON_BROWSER); - return false; + if (!empty($conf['auth']['checkbrowser'])) { + $browser = $injector->get(Browser::class); + $originalAgent = $session->get('horde', 'auth/browser'); + if ($originalAgent !== $browser->getUserAgent() + && !(in_array($_SERVER['REQUEST_METHOD'] ?? '', ['GET', 'HEAD'], true) + && is_string($originalAgent) + && $browser->isCompanionOf($originalAgent))) { + $injector->get(Horde_Core_Factory_Auth::class)->create() + ->setError(Horde_Core_Auth_Application::REASON_BROWSER); + return false; + } } if (!empty($conf['session']['max_time']) diff --git a/lib/Horde/Themes/Css.php b/lib/Horde/Themes/Css.php index 5d04c089..13a9bdaa 100644 --- a/lib/Horde/Themes/Css.php +++ b/lib/Horde/Themes/Css.php @@ -13,6 +13,7 @@ * @category Horde * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ use Horde\Log\Logger; diff --git a/lib/Horde/Themes/Css/Compress.php b/lib/Horde/Themes/Css/Compress.php index 6b81a94a..140d0b70 100644 --- a/lib/Horde/Themes/Css/Compress.php +++ b/lib/Horde/Themes/Css/Compress.php @@ -10,6 +10,7 @@ * @copyright 2014-2026 Horde LLC * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ use Horde\CssMinify\CssParserMinifier; diff --git a/lib/Horde/Themes/Image.php b/lib/Horde/Themes/Image.php index 75290e97..b99c21fd 100644 --- a/lib/Horde/Themes/Image.php +++ b/lib/Horde/Themes/Image.php @@ -10,6 +10,7 @@ * @copyright 2010-2017 Horde LLC * @license http://www.horde.org/licenses/lgpl21 LGPL * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ /** diff --git a/src/DefaultInjectorBindings.php b/src/DefaultInjectorBindings.php index 932abcb6..77a9382c 100644 --- a/src/DefaultInjectorBindings.php +++ b/src/DefaultInjectorBindings.php @@ -12,6 +12,7 @@ * @copyright 2026 The Horde Project * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ namespace Horde\Core; diff --git a/src/Horde.php b/src/Horde.php index 42d3c759..0de4b29c 100644 --- a/src/Horde.php +++ b/src/Horde.php @@ -7,6 +7,8 @@ * * See the enclosed file LICENSE for license information (LGPL). If you * did not receive this file, see http://www.horde.org/licenses/lgpl21. + * + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ namespace Horde\Core; diff --git a/test/Unit/Registry/BrowserAuthenticationTest.php b/test/Unit/Registry/BrowserAuthenticationTest.php new file mode 100644 index 00000000..0c273614 --- /dev/null +++ b/test/Unit/Registry/BrowserAuthenticationTest.php @@ -0,0 +1,189 @@ + */ + private array $savedGlobals = []; + + protected function setUp(): void + { + foreach (['browser', 'conf', 'injector', 'session', 'prefs', '_SESSION', '_SERVER'] as $name) { + if (array_key_exists($name, $GLOBALS)) { + $this->savedGlobals[$name] = $GLOBALS[$name]; + } + } + + $this->registry = (new ReflectionClass(Horde_Registry::class))->newInstanceWithoutConstructor(); + $this->auth = $this->createMock(Horde_Core_Auth_Application::class); + $this->session = new HordeSession(new SessionId('browser-auth-test'), []); + $this->session->setScoped('horde', 'auth/browser', self::SAFARI); + $this->session->setScoped('horde', 'auth/remoteAddr', '192.0.2.1'); + $this->session->setSessionBegin(time()); + $this->browser = new Browser(self::HELPER); + + $factory = $this->createStub(Horde_Core_Factory_Auth::class); + $factory->method('create')->willReturn($this->auth); + $injector = $this->createStub(Horde_Injector::class); + $injector->method('get')->willReturnCallback(fn (string $class) => match ($class) { + Browser::class => $this->browser, + Horde_Core_Factory_Auth::class => $factory, + default => self::fail('Unexpected injector lookup: ' . $class), + }); + + $GLOBALS['injector'] = $injector; + $GLOBALS['session'] = new Horde_Session($this->session); + $GLOBALS['browser'] = new \stdClass(); + $GLOBALS['conf'] = ['auth' => ['checkbrowser' => true]]; + $_SERVER['REQUEST_METHOD'] = 'GET'; + } + + protected function tearDown(): void + { + foreach (['browser', 'conf', 'injector', 'session', 'prefs', '_SESSION', '_SERVER'] as $name) { + if (array_key_exists($name, $this->savedGlobals)) { + $GLOBALS[$name] = $this->savedGlobals[$name]; + } else { + unset($GLOBALS[$name]); + } + } + } + + #[DataProvider('browserRequestProvider')] + public function testBrowserRequestPolicy( + string $method, + string $current, + ?string $original, + bool $checkBrowser, + bool $expected + ): void { + $_SERVER['REQUEST_METHOD'] = $method; + $this->browser = new Browser($current); + $this->session->setScoped('horde', 'auth/browser', $original); + $GLOBALS['conf']['auth']['checkbrowser'] = $checkBrowser; + + if ($expected) { + $this->auth->expects(self::once())->method('validateAuth')->willReturn(true); + $this->auth->expects(self::never())->method('setError'); + } else { + $this->auth->expects(self::never())->method('validateAuth'); + $this->auth->expects(self::once())->method('setError') + ->with(Horde_Core_Auth_Application::REASON_BROWSER); + } + + self::assertSame($expected, $this->registry->checkExistingAuth()); + self::assertSame($original, $this->session->getScoped('horde', 'auth/browser')); + } + + public static function browserRequestProvider(): array + { + return [ + 'helper GET' => ['GET', self::HELPER, self::SAFARI, true, true], + 'helper HEAD' => ['HEAD', self::HELPER, self::SAFARI, true, true], + 'helper POST rejected' => ['POST', self::HELPER, self::SAFARI, true, false], + 'helper PUT rejected' => ['PUT', self::HELPER, self::SAFARI, true, false], + 'helper without request method rejected' => ['', self::HELPER, self::SAFARI, true, false], + 'ordinary POST unchanged' => ['POST', self::SAFARI, self::SAFARI, true, true], + 'ordinary browser change rejected' => ['GET', 'Different Browser/1.0', self::SAFARI, true, false], + 'helper for unknown browser rejected' => ['GET', self::HELPER, 'Different Browser/1.0', true, false], + 'missing stored browser rejected' => ['GET', self::HELPER, null, true, false], + 'disabled browser check unchanged' => ['POST', 'Different Browser/1.0', self::SAFARI, false, true], + ]; + } + + public function testHelperDoesNotBypassSessionExpiry(): void + { + $GLOBALS['conf']['session']['max_time'] = 60; + $this->session->setSessionBegin(time() - 120); + $this->auth->expects(self::never())->method('validateAuth'); + $this->auth->expects(self::once())->method('setError') + ->with(Horde_Core_Auth_Application::REASON_SESSIONMAXTIME); + + self::assertFalse($this->registry->checkExistingAuth()); + } + + public function testHelperDoesNotBypassIpCheck(): void + { + $GLOBALS['conf']['auth']['checkip'] = true; + $_SERVER['REMOTE_ADDR'] = '192.0.2.2'; + unset($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['REMOTE_HOST']); + $this->auth->expects(self::never())->method('validateAuth'); + $this->auth->expects(self::once())->method('setError') + ->with(Horde_Core_Auth_Application::REASON_SESSIONIP); + + self::assertFalse($this->registry->checkExistingAuth()); + } + + public function testHelperDoesNotBypassBackendValidation(): void + { + $this->auth->expects(self::once())->method('validateAuth')->willReturn(false); + $this->auth->method('getError')->willReturn(false); + $this->auth->expects(self::once())->method('setError')->with(Horde_Auth::REASON_SESSION); + + self::assertFalse($this->registry->checkExistingAuth()); + } + + public function testHelperStillValidatesApplicationAuth(): void + { + $this->auth->expects(self::exactly(2))->method('validateAuth')->willReturn(true); + $this->auth->expects(self::never())->method('setError'); + + self::assertTrue($this->registry->checkExistingAuth('imp')); + self::assertSame(self::SAFARI, $this->session->getScoped('horde', 'auth/browser')); + } + + public function testLoginStoresModernBrowserUserAgent(): void + { + $this->browser = new Browser(self::SAFARI); + $this->auth->expects(self::never())->method('validateAuth'); + $registry = $this->getMockBuilder(Horde_Registry::class) + ->disableOriginalConstructor() + ->onlyMethods(['getAuth', 'getApp', 'remoteHost', 'setAuthCredential', 'loadPrefs', 'setLanguageEnvironment']) + ->getMock(); + $registry->method('getAuth')->willReturn(false); + $registry->method('getApp')->willReturn('horde'); + $registry->method('remoteHost')->willReturn((object) ['addr' => '192.0.2.1']); + $registry->expects(self::once())->method('setAuthCredential') + ->with(['password' => 'test'], null, 'horde'); + $registry->expects(self::once())->method('loadPrefs')->with('horde'); + $registry->expects(self::once())->method('setLanguageEnvironment')->with(null, 'horde'); + + $registry->setAuth('test', ['password' => 'test'], ['no_convert' => true]); + + self::assertSame(self::SAFARI, $this->session->getScoped('horde', 'auth/browser')); + } +} diff --git a/test/Unit/Themes/Css/CompressTest.php b/test/Unit/Themes/Css/CompressTest.php index 4b8a4b82..3fcf712b 100644 --- a/test/Unit/Themes/Css/CompressTest.php +++ b/test/Unit/Themes/Css/CompressTest.php @@ -7,6 +7,7 @@ * * @category Horde * @package Core + * @todo Framework 6.1 migrate to Horde\Browser\Browser or Horde\Browser\HttpUtils */ namespace Horde\Core\Test\Unit\Themes\Css;