From ef795ffcdb2bbeff4a8f35091d6f211c4a27d896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C4=9F=C4=B1zhan=20Burak=20Yakar?= Date: Wed, 8 Apr 2026 13:06:27 +0300 Subject: [PATCH] Honor "hide_version_public" setting in JSON response headers (#3394) * Honor "hide_version_public" setting while setting JSON headers * Fix PHPUnit tests to mock shouldExposeVersion() after refactoring Co-authored-by: admdly <2807620+admdly@users.noreply.github.com> --- src/modules/Api/Controller/Client.php | 4 +- src/modules/System/Api/Guest.php | 12 ++---- src/modules/System/Service.php | 7 ++++ tests-legacy/modules/System/Api/GuestTest.php | 42 +++++-------------- 4 files changed, 24 insertions(+), 41 deletions(-) diff --git a/src/modules/Api/Controller/Client.php b/src/modules/Api/Controller/Client.php index d204af220..4f89f6e18 100644 --- a/src/modules/Api/Controller/Client.php +++ b/src/modules/Api/Controller/Client.php @@ -331,7 +331,9 @@ class Client implements InjectionAwareInterface header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json; charset=utf-8'); - header('X-FOSSBilling-Version: ' . \FOSSBilling\Version::VERSION); + if ($this->di['mod_service']('system')->shouldExposeVersion()) { + header('X-FOSSBilling-Version: ' . \FOSSBilling\Version::VERSION); + } header('X-RateLimit-Span: ' . $this->_api_config['rate_span']); header('X-RateLimit-Limit: ' . $this->_api_config['rate_limit']); header('X-RateLimit-Remaining: ' . $this->_requests_left); diff --git a/src/modules/System/Api/Guest.php b/src/modules/System/Api/Guest.php index c10b9347b..637cf5d3e 100644 --- a/src/modules/System/Api/Guest.php +++ b/src/modules/System/Api/Guest.php @@ -28,15 +28,9 @@ class Guest extends \Api_Abstract */ public function version() { - $hideVersionGuest = $this->getService()->getParamValue('hide_version_public'); - - // Only provide the FOSSBilling version if configured to do so or if the request is being made by an administrator. - if ($this->di['auth']->isAdminLoggedIn() || !$hideVersionGuest) { - return $this->getService()->getVersion(); - } - - // return an empty string - return ''; + $service = $this->getService(); + + return $service->shouldExposeVersion() ? $service->getVersion() : ''; } /** diff --git a/src/modules/System/Service.php b/src/modules/System/Service.php index 1db630c53..9bb5e9ad1 100644 --- a/src/modules/System/Service.php +++ b/src/modules/System/Service.php @@ -1000,6 +1000,13 @@ class Service return Version::VERSION; } + public function shouldExposeVersion(): bool + { + $hideVersionPublic = $this->getParamValue('hide_version_public'); + + return $this->di['auth']->isAdminLoggedIn() || !$hideVersionPublic; + } + public function getPendingMessages() { $messages = $this->di['session']->get('pending_messages'); diff --git a/tests-legacy/modules/System/Api/GuestTest.php b/tests-legacy/modules/System/Api/GuestTest.php index d01bc62e6..1b11ad64a 100644 --- a/tests-legacy/modules/System/Api/GuestTest.php +++ b/tests-legacy/modules/System/Api/GuestTest.php @@ -26,19 +26,15 @@ final class GuestTest extends \BBTestCase public function testVersionAdmin(): void { - $authorizationMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock(); - $authorizationMock->expects($this->atLeastOnce()) - ->method('isAdminLoggedIn') - ->willReturn(true); - - $di = $this->getDi(); - $di['auth'] = $authorizationMock; - $serviceMock = $this->createMock(\Box\Mod\System\Service::class); + $serviceMock->expects($this->atLeastOnce()) + ->method('shouldExposeVersion') + ->willReturn(true); $serviceMock->expects($this->atLeastOnce()) ->method('getVersion') ->willReturn(\FOSSBilling\Version::VERSION); + $di = $this->getDi(); $this->api->setDi($di); $this->api->setService($serviceMock); $result = $this->api->version(); @@ -49,23 +45,15 @@ final class GuestTest extends \BBTestCase public function testVersionShowPublicOn(): void { - $authorizationMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock(); - $authorizationMock->expects($this->atLeastOnce()) - ->method('isAdminLoggedIn') - ->willReturn(false); - - $di = $this->getDi(); - $di['auth'] = $authorizationMock; $serviceMock = $this->createMock(\Box\Mod\System\Service::class); + $serviceMock->expects($this->atLeastOnce()) + ->method('shouldExposeVersion') + ->willReturn(true); $serviceMock->expects($this->atLeastOnce()) ->method('getVersion') ->willReturn(\FOSSBilling\Version::VERSION); - $serviceMock->expects($this->atLeastOnce()) - ->method('getParamValue') - ->with('hide_version_public') - ->willReturn(0); - + $di = $this->getDi(); $this->api->setDi($di); $this->api->setService($serviceMock); $result = $this->api->version(); @@ -76,20 +64,12 @@ final class GuestTest extends \BBTestCase public function testVersionShowPublicOff(): void { - $authorizationMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock(); - $authorizationMock->expects($this->atLeastOnce()) - ->method('isAdminLoggedIn') + $serviceMock = $this->createMock(\Box\Mod\System\Service::class); + $serviceMock->expects($this->atLeastOnce()) + ->method('shouldExposeVersion') ->willReturn(false); $di = $this->getDi(); - $di['auth'] = $authorizationMock; - - $serviceMock = $this->createMock(\Box\Mod\System\Service::class); - $serviceMock->expects($this->atLeastOnce()) - ->method('getParamValue') - ->with('hide_version_public') - ->willReturn(1); - $this->api->setDi($di); $this->api->setService($serviceMock); $result = $this->api->version();