Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/private/Http/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,6 +42,7 @@ public function __construct(
GuzzleClient $client,
IRemoteHostValidator $remoteHostValidator,
protected LoggerInterface $logger,
protected ServerVersion $serverVersion,
) {
$this->config = $config;
$this->client = $client;
Expand Down Expand Up @@ -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'])) {
Expand Down
3 changes: 3 additions & 0 deletions lib/private/Http/Client/ClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -43,6 +44,7 @@ public function __construct(
IRemoteHostValidator $remoteHostValidator,
IEventLogger $eventLogger,
protected LoggerInterface $logger,
protected ServerVersion $serverVersion,
) {
$this->config = $config;
$this->certificateManager = $certificateManager;
Expand Down Expand Up @@ -74,6 +76,7 @@ public function newClient(): IClient {
$client,
$this->remoteHostValidator,
$this->logger,
$this->serverVersion,
);
}
}
7 changes: 7 additions & 0 deletions tests/lib/Http/Client/ClientServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand All @@ -53,6 +55,7 @@ public function testNewClient(): void {
$remoteHostValidator,
$eventLogger,
$logger,
$serverVersion,
);

$handler = new CurlHandler();
Expand All @@ -72,6 +75,7 @@ public function testNewClient(): void {
$guzzleClient,
$remoteHostValidator,
$logger,
$serverVersion,
),
$clientService->newClient()
);
Expand All @@ -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,
Expand All @@ -102,6 +107,7 @@ public function testDisableDnsPinning(): void {
$remoteHostValidator,
$eventLogger,
$logger,
$serverVersion,
);

$handler = new CurlHandler();
Expand All @@ -120,6 +126,7 @@ public function testDisableDnsPinning(): void {
$guzzleClient,
$remoteHostValidator,
$logger,
$serverVersion,
),
$clientService->newClient()
);
Expand Down
25 changes: 21 additions & 4 deletions tests/lib/Http/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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,
);
}

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

$this->serverVersion->method('getVersionString')
->willReturn('123.45.6');

$this->defaultRequestOptions = [
'verify' => '/my/path.crt',
'proxy' => [
'http' => 'foo',
'https' => 'foo'
],
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler',
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
'Accept-Encoding' => 'gzip',
],
'timeout' => 30,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -513,14 +524,17 @@ public function testSetDefaultOptionsWithProxy(): void {
->with()
->willReturn('/my/path.crt');

$this->serverVersion->method('getVersionString')
->willReturn('123.45.6');

$this->assertEquals([
'verify' => '/my/path.crt',
'proxy' => [
'http' => 'foo',
'https' => 'foo'
],
'headers' => [
'User-Agent' => 'Nextcloud Server Crawler',
'User-Agent' => 'Nextcloud-Server-Crawler/123.45.6',
'Accept-Encoding' => 'gzip',
],
'timeout' => 30,
Expand Down Expand Up @@ -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' => [
Expand All @@ -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,
Expand Down
Loading