Skip to content

Commit

Permalink
Merge pull request #40108 from nextcloud/bug/noid/config-dns-pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
kesselb authored Aug 30, 2023
2 parents 9d1547f + eab46bd commit ec089cf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/private/Http/Client/ClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
namespace OC\Http\Client;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use OCP\Diagnostics\IEventLogger;
use OCP\Http\Client\IClient;
Expand Down Expand Up @@ -75,7 +75,9 @@ public function __construct(
public function newClient(): IClient {
$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
$stack->push($this->dnsPinMiddleware->addDnsPinning());
if ($this->config->getSystemValueBool('dns_pinning', true)) {
$stack->push($this->dnsPinMiddleware->addDnsPinning());
}
$stack->push(Middleware::tap(function (RequestInterface $request) {
$this->eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
}, function () {
Expand Down
53 changes: 52 additions & 1 deletion tests/lib/Http/Client/ClientServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Test\Http\Client;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use OC\Http\Client\Client;
use OC\Http\Client\ClientService;
Expand All @@ -32,6 +32,9 @@ class ClientServiceTest extends \Test\TestCase {
public function testNewClient(): void {
/** @var IConfig $config */
$config = $this->createMock(IConfig::class);
$config->method('getSystemValueBool')
->with('dns_pinning', true)
->willReturn(true);
/** @var ICertificateManager $certificateManager */
$certificateManager = $this->createMock(ICertificateManager::class);
$dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
Expand Down Expand Up @@ -74,4 +77,52 @@ public function testNewClient(): void {
$clientService->newClient()
);
}

public function testDisableDnsPinning(): void {
/** @var IConfig $config */
$config = $this->createMock(IConfig::class);
$config->method('getSystemValueBool')
->with('dns_pinning', true)
->willReturn(false);
/** @var ICertificateManager $certificateManager */
$certificateManager = $this->createMock(ICertificateManager::class);
$dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
$dnsPinMiddleware
->expects($this->never())
->method('addDnsPinning')
->willReturn(function () {
});
$remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
$eventLogger = $this->createMock(IEventLogger::class);
$logger = $this->createMock(LoggerInterface::class);

$clientService = new ClientService(
$config,
$certificateManager,
$dnsPinMiddleware,
$remoteHostValidator,
$eventLogger,
$logger,
);

$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
$stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger) {
$eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
}, function () use ($eventLogger) {
$eventLogger->end('http:request');
}), 'event logger');
$guzzleClient = new GuzzleClient(['handler' => $stack]);

$this->assertEquals(
new Client(
$config,
$certificateManager,
$guzzleClient,
$remoteHostValidator,
$logger,
),
$clientService->newClient()
);
}
}

0 comments on commit ec089cf

Please sign in to comment.