Skip to content

Commit d14cf6a

Browse files
Merge pull request #56280 from nextcloud/chore/noid/version-in-crawler-agent
Add server version to default crawler user agent
2 parents cd1c45d + b91034b commit d14cf6a

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

lib/private/Http/Client/Client.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\ICertificateManager;
1919
use OCP\IConfig;
2020
use OCP\Security\IRemoteHostValidator;
21+
use OCP\ServerVersion;
2122
use Psr\Log\LoggerInterface;
2223
use function parse_url;
2324

@@ -41,6 +42,7 @@ public function __construct(
4142
GuzzleClient $client,
4243
IRemoteHostValidator $remoteHostValidator,
4344
protected LoggerInterface $logger,
45+
protected ServerVersion $serverVersion,
4446
) {
4547
$this->config = $config;
4648
$this->client = $client;
@@ -81,7 +83,8 @@ private function buildRequestOptions(array $options): array {
8183
$options = array_merge($defaults, $options);
8284

8385
if (!isset($options[RequestOptions::HEADERS]['User-Agent'])) {
84-
$options[RequestOptions::HEADERS]['User-Agent'] = 'Nextcloud Server Crawler';
86+
$userAgent = 'Nextcloud-Server-Crawler/' . $this->serverVersion->getVersionString();
87+
$options[RequestOptions::HEADERS]['User-Agent'] = $userAgent;
8588
}
8689

8790
if (!isset($options[RequestOptions::HEADERS]['Accept-Encoding'])) {

lib/private/Http/Client/ClientService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\ICertificateManager;
1919
use OCP\IConfig;
2020
use OCP\Security\IRemoteHostValidator;
21+
use OCP\ServerVersion;
2122
use Psr\Http\Message\RequestInterface;
2223
use Psr\Log\LoggerInterface;
2324

@@ -43,6 +44,7 @@ public function __construct(
4344
IRemoteHostValidator $remoteHostValidator,
4445
IEventLogger $eventLogger,
4546
protected LoggerInterface $logger,
47+
protected ServerVersion $serverVersion,
4648
) {
4749
$this->config = $config;
4850
$this->certificateManager = $certificateManager;
@@ -74,6 +76,7 @@ public function newClient(): IClient {
7476
$client,
7577
$this->remoteHostValidator,
7678
$this->logger,
79+
$this->serverVersion,
7780
);
7881
}
7982
}

tests/lib/Http/Client/ClientServiceTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OCP\ICertificateManager;
2222
use OCP\IConfig;
2323
use OCP\Security\IRemoteHostValidator;
24+
use OCP\ServerVersion;
2425
use Psr\Http\Message\RequestInterface;
2526
use Psr\Log\LoggerInterface;
2627

@@ -45,6 +46,7 @@ public function testNewClient(): void {
4546
$remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
4647
$eventLogger = $this->createMock(IEventLogger::class);
4748
$logger = $this->createMock(LoggerInterface::class);
49+
$serverVersion = $this->createMock(ServerVersion::class);
4850

4951
$clientService = new ClientService(
5052
$config,
@@ -53,6 +55,7 @@ public function testNewClient(): void {
5355
$remoteHostValidator,
5456
$eventLogger,
5557
$logger,
58+
$serverVersion,
5659
);
5760

5861
$handler = new CurlHandler();
@@ -72,6 +75,7 @@ public function testNewClient(): void {
7275
$guzzleClient,
7376
$remoteHostValidator,
7477
$logger,
78+
$serverVersion,
7579
),
7680
$clientService->newClient()
7781
);
@@ -94,6 +98,7 @@ public function testDisableDnsPinning(): void {
9498
$remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
9599
$eventLogger = $this->createMock(IEventLogger::class);
96100
$logger = $this->createMock(LoggerInterface::class);
101+
$serverVersion = $this->createMock(ServerVersion::class);
97102

98103
$clientService = new ClientService(
99104
$config,
@@ -102,6 +107,7 @@ public function testDisableDnsPinning(): void {
102107
$remoteHostValidator,
103108
$eventLogger,
104109
$logger,
110+
$serverVersion,
105111
);
106112

107113
$handler = new CurlHandler();
@@ -120,6 +126,7 @@ public function testDisableDnsPinning(): void {
120126
$guzzleClient,
121127
$remoteHostValidator,
122128
$logger,
129+
$serverVersion,
123130
),
124131
$clientService->newClient()
125132
);

tests/lib/Http/Client/ClientTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\ICertificateManager;
1818
use OCP\IConfig;
1919
use OCP\Security\IRemoteHostValidator;
20+
use OCP\ServerVersion;
2021
use PHPUnit\Framework\MockObject\MockObject;
2122
use Psr\Log\LoggerInterface;
2223
use function parse_url;
@@ -36,6 +37,7 @@ class ClientTest extends \Test\TestCase {
3637
/** @var IRemoteHostValidator|MockObject */
3738
private IRemoteHostValidator $remoteHostValidator;
3839
private LoggerInterface $logger;
40+
private ServerVersion $serverVersion;
3941
/** @var array */
4042
private $defaultRequestOptions;
4143

@@ -46,12 +48,15 @@ protected function setUp(): void {
4648
$this->certificateManager = $this->createMock(ICertificateManager::class);
4749
$this->remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
4850
$this->logger = $this->createMock(LoggerInterface::class);
51+
$this->serverVersion = $this->createMock(ServerVersion::class);
52+
4953
$this->client = new Client(
5054
$this->config,
5155
$this->certificateManager,
5256
$this->guzzleClient,
5357
$this->remoteHostValidator,
5458
$this->logger,
59+
$this->serverVersion,
5560
);
5661
}
5762

@@ -276,14 +281,17 @@ private function setUpDefaultRequestOptions(): void {
276281
->with()
277282
->willReturn('/my/path.crt');
278283

284+
$this->serverVersion->method('getVersionString')
285+
->willReturn('123.45.6');
286+
279287
$this->defaultRequestOptions = [
280288
'verify' => '/my/path.crt',
281289
'proxy' => [
282290
'http' => 'foo',
283291
'https' => 'foo'
284292
],
285293
'headers' => [
286-
'User-Agent' => 'Nextcloud Server Crawler',
294+
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
287295
'Accept-Encoding' => 'gzip',
288296
],
289297
'timeout' => 30,
@@ -466,10 +474,13 @@ public function testSetDefaultOptionsWithNotInstalled(): void {
466474
->expects($this->never())
467475
->method('listCertificates');
468476

477+
$this->serverVersion->method('getVersionString')
478+
->willReturn('123.45.6');
479+
469480
$this->assertEquals([
470481
'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt',
471482
'headers' => [
472-
'User-Agent' => 'Nextcloud Server Crawler',
483+
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
473484
'Accept-Encoding' => 'gzip',
474485
],
475486
'timeout' => 30,
@@ -513,14 +524,17 @@ public function testSetDefaultOptionsWithProxy(): void {
513524
->with()
514525
->willReturn('/my/path.crt');
515526

527+
$this->serverVersion->method('getVersionString')
528+
->willReturn('123.45.6');
529+
516530
$this->assertEquals([
517531
'verify' => '/my/path.crt',
518532
'proxy' => [
519533
'http' => 'foo',
520534
'https' => 'foo'
521535
],
522536
'headers' => [
523-
'User-Agent' => 'Nextcloud Server Crawler',
537+
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
524538
'Accept-Encoding' => 'gzip',
525539
],
526540
'timeout' => 30,
@@ -564,6 +578,9 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void {
564578
->with()
565579
->willReturn('/my/path.crt');
566580

581+
$this->serverVersion->method('getVersionString')
582+
->willReturn('123.45.6');
583+
567584
$this->assertEquals([
568585
'verify' => '/my/path.crt',
569586
'proxy' => [
@@ -572,7 +589,7 @@ public function testSetDefaultOptionsWithProxyAndExclude(): void {
572589
'no' => ['bar']
573590
],
574591
'headers' => [
575-
'User-Agent' => 'Nextcloud Server Crawler',
592+
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
576593
'Accept-Encoding' => 'gzip',
577594
],
578595
'timeout' => 30,

0 commit comments

Comments
 (0)