chore: Add server version to default crawler user agent

Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
pull/56280/head
Marcel Müller 2025-11-06 23:00:47 +07:00
parent 52f99e6f6c
commit b91034b3cf
4 changed files with 35 additions and 5 deletions

@ -18,6 +18,7 @@ use OCP\Http\Client\LocalServerException;
use OCP\ICertificateManager; use OCP\ICertificateManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\Security\IRemoteHostValidator; use OCP\Security\IRemoteHostValidator;
use OCP\ServerVersion;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use function parse_url; use function parse_url;
@ -41,6 +42,7 @@ class Client implements IClient {
GuzzleClient $client, GuzzleClient $client,
IRemoteHostValidator $remoteHostValidator, IRemoteHostValidator $remoteHostValidator,
protected LoggerInterface $logger, protected LoggerInterface $logger,
protected ServerVersion $serverVersion,
) { ) {
$this->config = $config; $this->config = $config;
$this->client = $client; $this->client = $client;
@ -81,7 +83,8 @@ class Client implements IClient {
$options = array_merge($defaults, $options); $options = array_merge($defaults, $options);
if (!isset($options[RequestOptions::HEADERS]['User-Agent'])) { 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'])) { if (!isset($options[RequestOptions::HEADERS]['Accept-Encoding'])) {

@ -18,6 +18,7 @@ use OCP\Http\Client\IClientService;
use OCP\ICertificateManager; use OCP\ICertificateManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\Security\IRemoteHostValidator; use OCP\Security\IRemoteHostValidator;
use OCP\ServerVersion;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -43,6 +44,7 @@ class ClientService implements IClientService {
IRemoteHostValidator $remoteHostValidator, IRemoteHostValidator $remoteHostValidator,
IEventLogger $eventLogger, IEventLogger $eventLogger,
protected LoggerInterface $logger, protected LoggerInterface $logger,
protected ServerVersion $serverVersion,
) { ) {
$this->config = $config; $this->config = $config;
$this->certificateManager = $certificateManager; $this->certificateManager = $certificateManager;
@ -74,6 +76,7 @@ class ClientService implements IClientService {
$client, $client,
$this->remoteHostValidator, $this->remoteHostValidator,
$this->logger, $this->logger,
$this->serverVersion,
); );
} }
} }

@ -21,6 +21,7 @@ use OCP\Diagnostics\IEventLogger;
use OCP\ICertificateManager; use OCP\ICertificateManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\Security\IRemoteHostValidator; use OCP\Security\IRemoteHostValidator;
use OCP\ServerVersion;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -45,6 +46,7 @@ class ClientServiceTest extends \Test\TestCase {
$remoteHostValidator = $this->createMock(IRemoteHostValidator::class); $remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
$eventLogger = $this->createMock(IEventLogger::class); $eventLogger = $this->createMock(IEventLogger::class);
$logger = $this->createMock(LoggerInterface::class); $logger = $this->createMock(LoggerInterface::class);
$serverVersion = $this->createMock(ServerVersion::class);
$clientService = new ClientService( $clientService = new ClientService(
$config, $config,
@ -53,6 +55,7 @@ class ClientServiceTest extends \Test\TestCase {
$remoteHostValidator, $remoteHostValidator,
$eventLogger, $eventLogger,
$logger, $logger,
$serverVersion,
); );
$handler = new CurlHandler(); $handler = new CurlHandler();
@ -72,6 +75,7 @@ class ClientServiceTest extends \Test\TestCase {
$guzzleClient, $guzzleClient,
$remoteHostValidator, $remoteHostValidator,
$logger, $logger,
$serverVersion,
), ),
$clientService->newClient() $clientService->newClient()
); );
@ -94,6 +98,7 @@ class ClientServiceTest extends \Test\TestCase {
$remoteHostValidator = $this->createMock(IRemoteHostValidator::class); $remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
$eventLogger = $this->createMock(IEventLogger::class); $eventLogger = $this->createMock(IEventLogger::class);
$logger = $this->createMock(LoggerInterface::class); $logger = $this->createMock(LoggerInterface::class);
$serverVersion = $this->createMock(ServerVersion::class);
$clientService = new ClientService( $clientService = new ClientService(
$config, $config,
@ -102,6 +107,7 @@ class ClientServiceTest extends \Test\TestCase {
$remoteHostValidator, $remoteHostValidator,
$eventLogger, $eventLogger,
$logger, $logger,
$serverVersion,
); );
$handler = new CurlHandler(); $handler = new CurlHandler();
@ -120,6 +126,7 @@ class ClientServiceTest extends \Test\TestCase {
$guzzleClient, $guzzleClient,
$remoteHostValidator, $remoteHostValidator,
$logger, $logger,
$serverVersion,
), ),
$clientService->newClient() $clientService->newClient()
); );

@ -17,6 +17,7 @@ use OCP\Http\Client\LocalServerException;
use OCP\ICertificateManager; use OCP\ICertificateManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\Security\IRemoteHostValidator; use OCP\Security\IRemoteHostValidator;
use OCP\ServerVersion;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use function parse_url; use function parse_url;
@ -36,6 +37,7 @@ class ClientTest extends \Test\TestCase {
/** @var IRemoteHostValidator|MockObject */ /** @var IRemoteHostValidator|MockObject */
private IRemoteHostValidator $remoteHostValidator; private IRemoteHostValidator $remoteHostValidator;
private LoggerInterface $logger; private LoggerInterface $logger;
private ServerVersion $serverVersion;
/** @var array */ /** @var array */
private $defaultRequestOptions; private $defaultRequestOptions;
@ -46,12 +48,15 @@ class ClientTest extends \Test\TestCase {
$this->certificateManager = $this->createMock(ICertificateManager::class); $this->certificateManager = $this->createMock(ICertificateManager::class);
$this->remoteHostValidator = $this->createMock(IRemoteHostValidator::class); $this->remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
$this->logger = $this->createMock(LoggerInterface::class); $this->logger = $this->createMock(LoggerInterface::class);
$this->serverVersion = $this->createMock(ServerVersion::class);
$this->client = new Client( $this->client = new Client(
$this->config, $this->config,
$this->certificateManager, $this->certificateManager,
$this->guzzleClient, $this->guzzleClient,
$this->remoteHostValidator, $this->remoteHostValidator,
$this->logger, $this->logger,
$this->serverVersion,
); );
} }
@ -276,6 +281,9 @@ class ClientTest extends \Test\TestCase {
->with() ->with()
->willReturn('/my/path.crt'); ->willReturn('/my/path.crt');
$this->serverVersion->method('getVersionString')
->willReturn('123.45.6');
$this->defaultRequestOptions = [ $this->defaultRequestOptions = [
'verify' => '/my/path.crt', 'verify' => '/my/path.crt',
'proxy' => [ 'proxy' => [
@ -283,7 +291,7 @@ class ClientTest extends \Test\TestCase {
'https' => 'foo' 'https' => 'foo'
], ],
'headers' => [ 'headers' => [
'User-Agent' => 'Nextcloud Server Crawler', 'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
'Accept-Encoding' => 'gzip', 'Accept-Encoding' => 'gzip',
], ],
'timeout' => 30, 'timeout' => 30,
@ -466,10 +474,13 @@ class ClientTest extends \Test\TestCase {
->expects($this->never()) ->expects($this->never())
->method('listCertificates'); ->method('listCertificates');
$this->serverVersion->method('getVersionString')
->willReturn('123.45.6');
$this->assertEquals([ $this->assertEquals([
'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt', 'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt',
'headers' => [ 'headers' => [
'User-Agent' => 'Nextcloud Server Crawler', 'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
'Accept-Encoding' => 'gzip', 'Accept-Encoding' => 'gzip',
], ],
'timeout' => 30, 'timeout' => 30,
@ -513,6 +524,9 @@ class ClientTest extends \Test\TestCase {
->with() ->with()
->willReturn('/my/path.crt'); ->willReturn('/my/path.crt');
$this->serverVersion->method('getVersionString')
->willReturn('123.45.6');
$this->assertEquals([ $this->assertEquals([
'verify' => '/my/path.crt', 'verify' => '/my/path.crt',
'proxy' => [ 'proxy' => [
@ -520,7 +534,7 @@ class ClientTest extends \Test\TestCase {
'https' => 'foo' 'https' => 'foo'
], ],
'headers' => [ 'headers' => [
'User-Agent' => 'Nextcloud Server Crawler', 'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
'Accept-Encoding' => 'gzip', 'Accept-Encoding' => 'gzip',
], ],
'timeout' => 30, 'timeout' => 30,
@ -564,6 +578,9 @@ class ClientTest extends \Test\TestCase {
->with() ->with()
->willReturn('/my/path.crt'); ->willReturn('/my/path.crt');
$this->serverVersion->method('getVersionString')
->willReturn('123.45.6');
$this->assertEquals([ $this->assertEquals([
'verify' => '/my/path.crt', 'verify' => '/my/path.crt',
'proxy' => [ 'proxy' => [
@ -572,7 +589,7 @@ class ClientTest extends \Test\TestCase {
'no' => ['bar'] 'no' => ['bar']
], ],
'headers' => [ 'headers' => [
'User-Agent' => 'Nextcloud Server Crawler', 'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
'Accept-Encoding' => 'gzip', 'Accept-Encoding' => 'gzip',
], ],
'timeout' => 30, 'timeout' => 30,