123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- <?php
- declare(strict_types=1);
- /**
- * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace lib\Http\Client;
- use GuzzleHttp\Handler\MockHandler;
- use GuzzleHttp\HandlerStack;
- use GuzzleHttp\Psr7\Request;
- use GuzzleHttp\Psr7\Response;
- use OC\Http\Client\DnsPinMiddleware;
- use OC\Http\Client\NegativeDnsCache;
- use OC\Memcache\NullCache;
- use OC\Net\IpAddressClassifier;
- use OCP\Http\Client\LocalServerException;
- use OCP\ICacheFactory;
- use Psr\Http\Message\RequestInterface;
- use Test\TestCase;
- class DnsPinMiddlewareTest extends TestCase {
- private DnsPinMiddleware $dnsPinMiddleware;
- protected function setUp(): void {
- parent::setUp();
- $cacheFactory = $this->createMock(ICacheFactory::class);
- $cacheFactory
- ->method('createLocal')
- ->willReturn(new NullCache());
- $ipAddressClassifier = new IpAddressClassifier();
- $negativeDnsCache = new NegativeDnsCache($cacheFactory);
- $this->dnsPinMiddleware = $this->getMockBuilder(DnsPinMiddleware::class)
- ->setConstructorArgs([$negativeDnsCache, $ipAddressClassifier])
- ->onlyMethods(['dnsGetRecord'])
- ->getMock();
- }
- public function testPopulateDnsCacheIPv4(): void {
- $mockHandler = new MockHandler([
- static function (RequestInterface $request, array $options) {
- self::arrayHasKey('curl', $options);
- self::arrayHasKey(CURLOPT_RESOLVE, $options['curl']);
- self::assertEquals([
- 'www.example.com:80:1.1.1.1',
- 'www.example.com:443:1.1.1.1'
- ], $options['curl'][CURLOPT_RESOLVE]);
- return new Response(200);
- },
- ]);
- $this->dnsPinMiddleware
- ->method('dnsGetRecord')
- ->willReturnCallback(function (string $hostname, int $type) {
- // example.com SOA
- if ($hostname === 'example.com') {
- return match ($type) {
- DNS_SOA => [
- [
- 'host' => 'example.com',
- 'class' => 'IN',
- 'ttl' => 7079,
- 'type' => 'SOA',
- 'minimum-ttl' => 3600,
- ]
- ],
- };
- }
- // example.com A, AAAA, CNAME
- if ($hostname === 'www.example.com') {
- return match ($type) {
- DNS_A => [],
- DNS_AAAA => [],
- DNS_CNAME => [
- [
- 'host' => 'www.example.com',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'A',
- 'target' => 'www.example.net'
- ]
- ],
- };
- }
- // example.net SOA
- if ($hostname === 'example.net') {
- return match ($type) {
- DNS_SOA => [
- [
- 'host' => 'example.net',
- 'class' => 'IN',
- 'ttl' => 7079,
- 'type' => 'SOA',
- 'minimum-ttl' => 3600,
- ]
- ],
- };
- }
- // example.net A, AAAA, CNAME
- if ($hostname === 'www.example.net') {
- return match ($type) {
- DNS_A => [
- [
- 'host' => 'www.example.net',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'A',
- 'ip' => '1.1.1.1'
- ]
- ],
- DNS_AAAA => [],
- DNS_CNAME => [],
- };
- }
- return false;
- });
- $stack = new HandlerStack($mockHandler);
- $stack->push($this->dnsPinMiddleware->addDnsPinning());
- $handler = $stack->resolve();
- $handler(
- new Request('GET', 'https://www.example.com'),
- ['nextcloud' => ['allow_local_address' => false]]
- );
- }
- public function testPopulateDnsCacheIPv6(): void {
- $mockHandler = new MockHandler([
- static function (RequestInterface $request, array $options) {
- self::arrayHasKey('curl', $options);
- self::arrayHasKey(CURLOPT_RESOLVE, $options['curl']);
- self::assertEquals([
- 'www.example.com:80:1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001',
- 'www.example.com:443:1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001'
- ], $options['curl'][CURLOPT_RESOLVE]);
- return new Response(200);
- },
- ]);
- $this->dnsPinMiddleware
- ->method('dnsGetRecord')
- ->willReturnCallback(function (string $hostname, int $type) {
- // example.com SOA
- if ($hostname === 'example.com') {
- return match ($type) {
- DNS_SOA => [
- [
- 'host' => 'example.com',
- 'class' => 'IN',
- 'ttl' => 7079,
- 'type' => 'SOA',
- 'minimum-ttl' => 3600,
- ]
- ],
- };
- }
- // example.com A, AAAA, CNAME
- if ($hostname === 'www.example.com') {
- return match ($type) {
- DNS_A => [],
- DNS_AAAA => [],
- DNS_CNAME => [
- [
- 'host' => 'www.example.com',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'A',
- 'target' => 'www.example.net'
- ]
- ],
- };
- }
- // example.net SOA
- if ($hostname === 'example.net') {
- return match ($type) {
- DNS_SOA => [
- [
- 'host' => 'example.net',
- 'class' => 'IN',
- 'ttl' => 7079,
- 'type' => 'SOA',
- 'minimum-ttl' => 3600,
- ]
- ],
- };
- }
- // example.net A, AAAA, CNAME
- if ($hostname === 'www.example.net') {
- return match ($type) {
- DNS_A => [
- [
- 'host' => 'www.example.net',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'A',
- 'ip' => '1.1.1.1'
- ],
- [
- 'host' => 'www.example.net',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'A',
- 'ip' => '1.0.0.1'
- ],
- ],
- DNS_AAAA => [
- [
- 'host' => 'www.example.net',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'AAAA',
- 'ip' => '2606:4700:4700::1111'
- ],
- [
- 'host' => 'www.example.net',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'AAAA',
- 'ip' => '2606:4700:4700::1001'
- ],
- ],
- DNS_CNAME => [],
- };
- }
- return false;
- });
- $stack = new HandlerStack($mockHandler);
- $stack->push($this->dnsPinMiddleware->addDnsPinning());
- $handler = $stack->resolve();
- $handler(
- new Request('GET', 'https://www.example.com'),
- ['nextcloud' => ['allow_local_address' => false]]
- );
- }
- public function testAllowLocalAddress(): void {
- $mockHandler = new MockHandler([
- static function (RequestInterface $request, array $options) {
- self::assertArrayNotHasKey('curl', $options);
- return new Response(200);
- },
- ]);
- $stack = new HandlerStack($mockHandler);
- $stack->push($this->dnsPinMiddleware->addDnsPinning());
- $handler = $stack->resolve();
- $handler(
- new Request('GET', 'https://www.example.com'),
- ['nextcloud' => ['allow_local_address' => true]]
- );
- }
- public function testRejectIPv4(): void {
- $this->expectException(LocalServerException::class);
- $this->expectExceptionMessage('violates local access rules');
- $mockHandler = new MockHandler([
- static function (RequestInterface $request, array $options) {
- // The handler should not be called
- },
- ]);
- $this->dnsPinMiddleware
- ->method('dnsGetRecord')
- ->willReturnCallback(function (string $hostname, int $type) {
- return match ($type) {
- DNS_SOA => [
- [
- 'host' => 'example.com',
- 'class' => 'IN',
- 'ttl' => 7079,
- 'type' => 'SOA',
- 'minimum-ttl' => 3600,
- ]
- ],
- DNS_A => [
- [
- 'host' => 'example.com',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'A',
- 'ip' => '192.168.0.1'
- ]
- ],
- DNS_AAAA => [],
- DNS_CNAME => [],
- };
- });
- $stack = new HandlerStack($mockHandler);
- $stack->push($this->dnsPinMiddleware->addDnsPinning());
- $handler = $stack->resolve();
- $handler(
- new Request('GET', 'https://www.example.com'),
- ['nextcloud' => ['allow_local_address' => false]]
- );
- }
- public function testRejectIPv6(): void {
- $this->expectException(LocalServerException::class);
- $this->expectExceptionMessage('violates local access rules');
- $mockHandler = new MockHandler([
- static function (RequestInterface $request, array $options) {
- // The handler should not be called
- },
- ]);
- $this->dnsPinMiddleware
- ->method('dnsGetRecord')
- ->willReturnCallback(function (string $hostname, int $type) {
- return match ($type) {
- DNS_SOA => [
- [
- 'host' => 'example.com',
- 'class' => 'IN',
- 'ttl' => 7079,
- 'type' => 'SOA',
- 'minimum-ttl' => 3600,
- ]
- ],
- DNS_A => [],
- DNS_AAAA => [
- [
- 'host' => 'ipv6.example.com',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'AAAA',
- 'ipv6' => 'fd12:3456:789a:1::1'
- ]
- ],
- DNS_CNAME => [],
- };
- });
- $stack = new HandlerStack($mockHandler);
- $stack->push($this->dnsPinMiddleware->addDnsPinning());
- $handler = $stack->resolve();
- $handler(
- new Request('GET', 'https://ipv6.example.com'),
- ['nextcloud' => ['allow_local_address' => false]]
- );
- }
- public function testRejectCanonicalName(): void {
- $this->expectException(LocalServerException::class);
- $this->expectExceptionMessage('violates local access rules');
- $mockHandler = new MockHandler([
- static function (RequestInterface $request, array $options) {
- // The handler should not be called
- },
- ]);
- $this->dnsPinMiddleware
- ->method('dnsGetRecord')
- ->willReturnCallback(function (string $hostname, int $type) {
- // example.com SOA
- if ($hostname === 'example.com') {
- return match ($type) {
- DNS_SOA => [
- [
- 'host' => 'example.com',
- 'class' => 'IN',
- 'ttl' => 7079,
- 'type' => 'SOA',
- 'minimum-ttl' => 3600,
- ]
- ],
- };
- }
- // example.com A, AAAA, CNAME
- if ($hostname === 'www.example.com') {
- return match ($type) {
- DNS_A => [],
- DNS_AAAA => [],
- DNS_CNAME => [
- [
- 'host' => 'www.example.com',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'A',
- 'target' => 'www.example.net'
- ]
- ],
- };
- }
- // example.net SOA
- if ($hostname === 'example.net') {
- return match ($type) {
- DNS_SOA => [
- [
- 'host' => 'example.net',
- 'class' => 'IN',
- 'ttl' => 7079,
- 'type' => 'SOA',
- 'minimum-ttl' => 3600,
- ]
- ],
- };
- }
- // example.net A, AAAA, CNAME
- if ($hostname === 'www.example.net') {
- return match ($type) {
- DNS_A => [
- [
- 'host' => 'www.example.net',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'A',
- 'ip' => '192.168.0.2'
- ]
- ],
- DNS_AAAA => [],
- DNS_CNAME => [],
- };
- }
- return false;
- });
- $stack = new HandlerStack($mockHandler);
- $stack->push($this->dnsPinMiddleware->addDnsPinning());
- $handler = $stack->resolve();
- $handler(
- new Request('GET', 'https://www.example.com'),
- ['nextcloud' => ['allow_local_address' => false]]
- );
- }
- public function testRejectFaultyResponse(): void {
- $this->expectException(LocalServerException::class);
- $this->expectExceptionMessage('No DNS record found for www.example.com');
- $mockHandler = new MockHandler([
- static function (RequestInterface $request, array $options) {
- // The handler should not be called
- },
- ]);
- $this->dnsPinMiddleware
- ->method('dnsGetRecord')
- ->willReturnCallback(function (string $hostname, int $type) {
- return false;
- });
- $stack = new HandlerStack($mockHandler);
- $stack->push($this->dnsPinMiddleware->addDnsPinning());
- $handler = $stack->resolve();
- $handler(
- new Request('GET', 'https://www.example.com'),
- ['nextcloud' => ['allow_local_address' => false]]
- );
- }
- public function testIgnoreSubdomainForSoaQuery(): void {
- $mockHandler = new MockHandler([
- static function (RequestInterface $request, array $options) {
- // The handler should not be called
- },
- ]);
- $dnsQueries = [];
- $this->dnsPinMiddleware
- ->method('dnsGetRecord')
- ->willReturnCallback(function (string $hostname, int $type) use (&$dnsQueries) {
- // log query
- $dnsQueries[] = $hostname . $type;
- // example.com SOA
- if ($hostname === 'example.com') {
- return match ($type) {
- DNS_SOA => [
- [
- 'host' => 'example.com',
- 'class' => 'IN',
- 'ttl' => 7079,
- 'type' => 'SOA',
- 'minimum-ttl' => 3600,
- ]
- ],
- };
- }
- // example.net A, AAAA, CNAME
- if ($hostname === 'subsubdomain.subdomain.example.com') {
- return match ($type) {
- DNS_A => [
- [
- 'host' => 'subsubdomain.subdomain.example.com',
- 'class' => 'IN',
- 'ttl' => 1800,
- 'type' => 'A',
- 'ip' => '1.1.1.1'
- ]
- ],
- DNS_AAAA => [],
- DNS_CNAME => [],
- };
- }
- return false;
- });
- $stack = new HandlerStack($mockHandler);
- $stack->push($this->dnsPinMiddleware->addDnsPinning());
- $handler = $stack->resolve();
- $handler(
- new Request('GET', 'https://subsubdomain.subdomain.example.com'),
- ['nextcloud' => ['allow_local_address' => false]]
- );
- $this->assertCount(3, $dnsQueries);
- $this->assertContains('example.com' . DNS_SOA, $dnsQueries);
- $this->assertContains('subsubdomain.subdomain.example.com' . DNS_A, $dnsQueries);
- $this->assertContains('subsubdomain.subdomain.example.com' . DNS_AAAA, $dnsQueries);
- // CNAME should not be queried if A or AAAA succeeded already
- $this->assertNotContains('subsubdomain.subdomain.example.com' . DNS_CNAME, $dnsQueries);
- }
- }
|