diff --git a/lib/private/Http/Client/Client.php b/lib/private/Http/Client/Client.php index 553a8921a8012..b2c6aa265b0d7 100644 --- a/lib/private/Http/Client/Client.php +++ b/lib/private/Http/Client/Client.php @@ -18,6 +18,7 @@ use OCP\ICertificateManager; use OCP\IConfig; use OCP\Security\IRemoteHostValidator; +use OCP\ServerVersion; use Psr\Log\LoggerInterface; use function parse_url; @@ -41,6 +42,7 @@ public function __construct( GuzzleClient $client, IRemoteHostValidator $remoteHostValidator, protected LoggerInterface $logger, + protected ServerVersion $serverVersion, ) { $this->config = $config; $this->client = $client; @@ -81,7 +83,8 @@ private function buildRequestOptions(array $options): array { $options = array_merge($defaults, $options); if (!isset($options[RequestOptions::HEADERS]['User-Agent'])) { - $options[RequestOptions::HEADERS]['User-Agent'] = 'Nextcloud Server Crawler'; + $userAgent = 'Nextcloud-Server-Crawler/' . $this->serverVersion->getVersionString(); + $options[RequestOptions::HEADERS]['User-Agent'] = $userAgent; } if (!isset($options[RequestOptions::HEADERS]['Accept-Encoding'])) { diff --git a/lib/private/Http/Client/ClientService.php b/lib/private/Http/Client/ClientService.php index b719f3d369d14..a505dff9aa20c 100644 --- a/lib/private/Http/Client/ClientService.php +++ b/lib/private/Http/Client/ClientService.php @@ -18,6 +18,7 @@ use OCP\ICertificateManager; use OCP\IConfig; use OCP\Security\IRemoteHostValidator; +use OCP\ServerVersion; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; @@ -43,6 +44,7 @@ public function __construct( IRemoteHostValidator $remoteHostValidator, IEventLogger $eventLogger, protected LoggerInterface $logger, + protected ServerVersion $serverVersion, ) { $this->config = $config; $this->certificateManager = $certificateManager; @@ -74,6 +76,7 @@ public function newClient(): IClient { $client, $this->remoteHostValidator, $this->logger, + $this->serverVersion, ); } } diff --git a/tests/lib/Http/Client/ClientServiceTest.php b/tests/lib/Http/Client/ClientServiceTest.php index fd5b155ca69ce..e7f116b9b6910 100644 --- a/tests/lib/Http/Client/ClientServiceTest.php +++ b/tests/lib/Http/Client/ClientServiceTest.php @@ -21,6 +21,7 @@ use OCP\ICertificateManager; use OCP\IConfig; use OCP\Security\IRemoteHostValidator; +use OCP\ServerVersion; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; @@ -45,6 +46,7 @@ public function testNewClient(): void { $remoteHostValidator = $this->createMock(IRemoteHostValidator::class); $eventLogger = $this->createMock(IEventLogger::class); $logger = $this->createMock(LoggerInterface::class); + $serverVersion = $this->createMock(ServerVersion::class); $clientService = new ClientService( $config, @@ -53,6 +55,7 @@ public function testNewClient(): void { $remoteHostValidator, $eventLogger, $logger, + $serverVersion, ); $handler = new CurlHandler(); @@ -72,6 +75,7 @@ public function testNewClient(): void { $guzzleClient, $remoteHostValidator, $logger, + $serverVersion, ), $clientService->newClient() ); @@ -94,6 +98,7 @@ public function testDisableDnsPinning(): void { $remoteHostValidator = $this->createMock(IRemoteHostValidator::class); $eventLogger = $this->createMock(IEventLogger::class); $logger = $this->createMock(LoggerInterface::class); + $serverVersion = $this->createMock(ServerVersion::class); $clientService = new ClientService( $config, @@ -102,6 +107,7 @@ public function testDisableDnsPinning(): void { $remoteHostValidator, $eventLogger, $logger, + $serverVersion, ); $handler = new CurlHandler(); @@ -120,6 +126,7 @@ public function testDisableDnsPinning(): void { $guzzleClient, $remoteHostValidator, $logger, + $serverVersion, ), $clientService->newClient() ); diff --git a/tests/lib/Http/Client/ClientTest.php b/tests/lib/Http/Client/ClientTest.php index e76b66b52d78d..5915e060b8726 100644 --- a/tests/lib/Http/Client/ClientTest.php +++ b/tests/lib/Http/Client/ClientTest.php @@ -17,6 +17,7 @@ use OCP\ICertificateManager; use OCP\IConfig; use OCP\Security\IRemoteHostValidator; +use OCP\ServerVersion; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use function parse_url; @@ -36,6 +37,7 @@ class ClientTest extends \Test\TestCase { /** @var IRemoteHostValidator|MockObject */ private IRemoteHostValidator $remoteHostValidator; private LoggerInterface $logger; + private ServerVersion $serverVersion; /** @var array */ private $defaultRequestOptions; @@ -46,12 +48,15 @@ protected function setUp(): void { $this->certificateManager = $this->createMock(ICertificateManager::class); $this->remoteHostValidator = $this->createMock(IRemoteHostValidator::class); $this->logger = $this->createMock(LoggerInterface::class); + $this->serverVersion = $this->createMock(ServerVersion::class); + $this->client = new Client( $this->config, $this->certificateManager, $this->guzzleClient, $this->remoteHostValidator, $this->logger, + $this->serverVersion, ); } @@ -276,6 +281,9 @@ private function setUpDefaultRequestOptions(): void { ->with() ->willReturn('/my/path.crt'); + $this->serverVersion->method('getVersionString') + ->willReturn('123.45.6'); + $this->defaultRequestOptions = [ 'verify' => '/my/path.crt', 'proxy' => [ @@ -283,7 +291,7 @@ private function setUpDefaultRequestOptions(): void { 'https' => 'foo' ], 'headers' => [ - 'User-Agent' => 'Nextcloud Server Crawler', + 'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6', 'Accept-Encoding' => 'gzip', ], 'timeout' => 30, @@ -466,10 +474,13 @@ public function testSetDefaultOptionsWithNotInstalled(): void { ->expects($this->never()) ->method('listCertificates'); + $this->serverVersion->method('getVersionString') + ->willReturn('123.45.6'); + $this->assertEquals([ 'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt', 'headers' => [ - 'User-Agent' => 'Nextcloud Server Crawler', + 'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6', 'Accept-Encoding' => 'gzip', ], 'timeout' => 30, @@ -513,6 +524,9 @@ public function testSetDefaultOptionsWithProxy(): void { ->with() ->willReturn('/my/path.crt'); + $this->serverVersion->method('getVersionString') + ->willReturn('123.45.6'); + $this->assertEquals([ 'verify' => '/my/path.crt', 'proxy' => [ @@ -520,7 +534,7 @@ public function testSetDefaultOptionsWithProxy(): void { 'https' => 'foo' ], 'headers' => [ - 'User-Agent' => 'Nextcloud Server Crawler', + 'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6', 'Accept-Encoding' => 'gzip', ], 'timeout' => 30, @@ -564,6 +578,9 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void { ->with() ->willReturn('/my/path.crt'); + $this->serverVersion->method('getVersionString') + ->willReturn('123.45.6'); + $this->assertEquals([ 'verify' => '/my/path.crt', 'proxy' => [ @@ -572,7 +589,7 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void { 'no' => ['bar'] ], 'headers' => [ - 'User-Agent' => 'Nextcloud Server Crawler', + 'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6', 'Accept-Encoding' => 'gzip', ], 'timeout' => 30,