DnsPinMiddlewareTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de>
  5. *
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace lib\Http\Client;
  25. use GuzzleHttp\Handler\MockHandler;
  26. use GuzzleHttp\HandlerStack;
  27. use GuzzleHttp\Psr7\Request;
  28. use GuzzleHttp\Psr7\Response;
  29. use OC\Http\Client\DnsPinMiddleware;
  30. use OC\Http\Client\NegativeDnsCache;
  31. use OC\Memcache\NullCache;
  32. use OC\Net\IpAddressClassifier;
  33. use OCP\Http\Client\LocalServerException;
  34. use OCP\ICacheFactory;
  35. use Psr\Http\Message\RequestInterface;
  36. use Test\TestCase;
  37. class DnsPinMiddlewareTest extends TestCase {
  38. private DnsPinMiddleware $dnsPinMiddleware;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $cacheFactory = $this->createMock(ICacheFactory::class);
  42. $cacheFactory
  43. ->method('createLocal')
  44. ->willReturn(new NullCache());
  45. $ipAddressClassifier = new IpAddressClassifier();
  46. $negativeDnsCache = new NegativeDnsCache($cacheFactory);
  47. $this->dnsPinMiddleware = $this->getMockBuilder(DnsPinMiddleware::class)
  48. ->setConstructorArgs([$negativeDnsCache, $ipAddressClassifier])
  49. ->onlyMethods(['dnsGetRecord'])
  50. ->getMock();
  51. }
  52. public function testPopulateDnsCacheIPv4() {
  53. $mockHandler = new MockHandler([
  54. static function (RequestInterface $request, array $options) {
  55. self::arrayHasKey('curl', $options);
  56. self::arrayHasKey(CURLOPT_RESOLVE, $options['curl']);
  57. self::assertEquals([
  58. 'www.example.com:80:1.1.1.1',
  59. 'www.example.com:443:1.1.1.1'
  60. ], $options['curl'][CURLOPT_RESOLVE]);
  61. return new Response(200);
  62. },
  63. ]);
  64. $this->dnsPinMiddleware
  65. ->method('dnsGetRecord')
  66. ->willReturnCallback(function (string $hostname, int $type) {
  67. // example.com SOA
  68. if ($hostname === 'example.com') {
  69. return match ($type) {
  70. DNS_SOA => [
  71. [
  72. 'host' => 'example.com',
  73. 'class' => 'IN',
  74. 'ttl' => 7079,
  75. 'type' => 'SOA',
  76. 'minimum-ttl' => 3600,
  77. ]
  78. ],
  79. };
  80. }
  81. // example.com A, AAAA, CNAME
  82. if ($hostname === 'www.example.com') {
  83. return match ($type) {
  84. DNS_A => [],
  85. DNS_AAAA => [],
  86. DNS_CNAME => [
  87. [
  88. 'host' => 'www.example.com',
  89. 'class' => 'IN',
  90. 'ttl' => 1800,
  91. 'type' => 'A',
  92. 'target' => 'www.example.net'
  93. ]
  94. ],
  95. };
  96. }
  97. // example.net SOA
  98. if ($hostname === 'example.net') {
  99. return match ($type) {
  100. DNS_SOA => [
  101. [
  102. 'host' => 'example.net',
  103. 'class' => 'IN',
  104. 'ttl' => 7079,
  105. 'type' => 'SOA',
  106. 'minimum-ttl' => 3600,
  107. ]
  108. ],
  109. };
  110. }
  111. // example.net A, AAAA, CNAME
  112. if ($hostname === 'www.example.net') {
  113. return match ($type) {
  114. DNS_A => [
  115. [
  116. 'host' => 'www.example.net',
  117. 'class' => 'IN',
  118. 'ttl' => 1800,
  119. 'type' => 'A',
  120. 'ip' => '1.1.1.1'
  121. ]
  122. ],
  123. DNS_AAAA => [],
  124. DNS_CNAME => [],
  125. };
  126. }
  127. return false;
  128. });
  129. $stack = new HandlerStack($mockHandler);
  130. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  131. $handler = $stack->resolve();
  132. $handler(
  133. new Request('GET', 'https://www.example.com'),
  134. ['nextcloud' => ['allow_local_address' => false]]
  135. );
  136. }
  137. public function testPopulateDnsCacheIPv6() {
  138. $mockHandler = new MockHandler([
  139. static function (RequestInterface $request, array $options) {
  140. self::arrayHasKey('curl', $options);
  141. self::arrayHasKey(CURLOPT_RESOLVE, $options['curl']);
  142. self::assertEquals([
  143. 'www.example.com:80:1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001',
  144. 'www.example.com:443:1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001'
  145. ], $options['curl'][CURLOPT_RESOLVE]);
  146. return new Response(200);
  147. },
  148. ]);
  149. $this->dnsPinMiddleware
  150. ->method('dnsGetRecord')
  151. ->willReturnCallback(function (string $hostname, int $type) {
  152. // example.com SOA
  153. if ($hostname === 'example.com') {
  154. return match ($type) {
  155. DNS_SOA => [
  156. [
  157. 'host' => 'example.com',
  158. 'class' => 'IN',
  159. 'ttl' => 7079,
  160. 'type' => 'SOA',
  161. 'minimum-ttl' => 3600,
  162. ]
  163. ],
  164. };
  165. }
  166. // example.com A, AAAA, CNAME
  167. if ($hostname === 'www.example.com') {
  168. return match ($type) {
  169. DNS_A => [],
  170. DNS_AAAA => [],
  171. DNS_CNAME => [
  172. [
  173. 'host' => 'www.example.com',
  174. 'class' => 'IN',
  175. 'ttl' => 1800,
  176. 'type' => 'A',
  177. 'target' => 'www.example.net'
  178. ]
  179. ],
  180. };
  181. }
  182. // example.net SOA
  183. if ($hostname === 'example.net') {
  184. return match ($type) {
  185. DNS_SOA => [
  186. [
  187. 'host' => 'example.net',
  188. 'class' => 'IN',
  189. 'ttl' => 7079,
  190. 'type' => 'SOA',
  191. 'minimum-ttl' => 3600,
  192. ]
  193. ],
  194. };
  195. }
  196. // example.net A, AAAA, CNAME
  197. if ($hostname === 'www.example.net') {
  198. return match ($type) {
  199. DNS_A => [
  200. [
  201. 'host' => 'www.example.net',
  202. 'class' => 'IN',
  203. 'ttl' => 1800,
  204. 'type' => 'A',
  205. 'ip' => '1.1.1.1'
  206. ],
  207. [
  208. 'host' => 'www.example.net',
  209. 'class' => 'IN',
  210. 'ttl' => 1800,
  211. 'type' => 'A',
  212. 'ip' => '1.0.0.1'
  213. ],
  214. ],
  215. DNS_AAAA => [
  216. [
  217. 'host' => 'www.example.net',
  218. 'class' => 'IN',
  219. 'ttl' => 1800,
  220. 'type' => 'AAAA',
  221. 'ip' => '2606:4700:4700::1111'
  222. ],
  223. [
  224. 'host' => 'www.example.net',
  225. 'class' => 'IN',
  226. 'ttl' => 1800,
  227. 'type' => 'AAAA',
  228. 'ip' => '2606:4700:4700::1001'
  229. ],
  230. ],
  231. DNS_CNAME => [],
  232. };
  233. }
  234. return false;
  235. });
  236. $stack = new HandlerStack($mockHandler);
  237. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  238. $handler = $stack->resolve();
  239. $handler(
  240. new Request('GET', 'https://www.example.com'),
  241. ['nextcloud' => ['allow_local_address' => false]]
  242. );
  243. }
  244. public function testAllowLocalAddress() {
  245. $mockHandler = new MockHandler([
  246. static function (RequestInterface $request, array $options) {
  247. self::assertArrayNotHasKey('curl', $options);
  248. return new Response(200);
  249. },
  250. ]);
  251. $stack = new HandlerStack($mockHandler);
  252. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  253. $handler = $stack->resolve();
  254. $handler(
  255. new Request('GET', 'https://www.example.com'),
  256. ['nextcloud' => ['allow_local_address' => true]]
  257. );
  258. }
  259. public function testRejectIPv4() {
  260. $this->expectException(LocalServerException::class);
  261. $this->expectExceptionMessage('violates local access rules');
  262. $mockHandler = new MockHandler([
  263. static function (RequestInterface $request, array $options) {
  264. // The handler should not be called
  265. },
  266. ]);
  267. $this->dnsPinMiddleware
  268. ->method('dnsGetRecord')
  269. ->willReturnCallback(function (string $hostname, int $type) {
  270. return match ($type) {
  271. DNS_SOA => [
  272. [
  273. 'host' => 'example.com',
  274. 'class' => 'IN',
  275. 'ttl' => 7079,
  276. 'type' => 'SOA',
  277. 'minimum-ttl' => 3600,
  278. ]
  279. ],
  280. DNS_A => [
  281. [
  282. 'host' => 'example.com',
  283. 'class' => 'IN',
  284. 'ttl' => 1800,
  285. 'type' => 'A',
  286. 'ip' => '192.168.0.1'
  287. ]
  288. ],
  289. DNS_AAAA => [],
  290. DNS_CNAME => [],
  291. };
  292. });
  293. $stack = new HandlerStack($mockHandler);
  294. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  295. $handler = $stack->resolve();
  296. $handler(
  297. new Request('GET', 'https://www.example.com'),
  298. ['nextcloud' => ['allow_local_address' => false]]
  299. );
  300. }
  301. public function testRejectIPv6() {
  302. $this->expectException(LocalServerException::class);
  303. $this->expectExceptionMessage('violates local access rules');
  304. $mockHandler = new MockHandler([
  305. static function (RequestInterface $request, array $options) {
  306. // The handler should not be called
  307. },
  308. ]);
  309. $this->dnsPinMiddleware
  310. ->method('dnsGetRecord')
  311. ->willReturnCallback(function (string $hostname, int $type) {
  312. return match ($type) {
  313. DNS_SOA => [
  314. [
  315. 'host' => 'example.com',
  316. 'class' => 'IN',
  317. 'ttl' => 7079,
  318. 'type' => 'SOA',
  319. 'minimum-ttl' => 3600,
  320. ]
  321. ],
  322. DNS_A => [],
  323. DNS_AAAA => [
  324. [
  325. 'host' => 'ipv6.example.com',
  326. 'class' => 'IN',
  327. 'ttl' => 1800,
  328. 'type' => 'AAAA',
  329. 'ipv6' => 'fd12:3456:789a:1::1'
  330. ]
  331. ],
  332. DNS_CNAME => [],
  333. };
  334. });
  335. $stack = new HandlerStack($mockHandler);
  336. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  337. $handler = $stack->resolve();
  338. $handler(
  339. new Request('GET', 'https://ipv6.example.com'),
  340. ['nextcloud' => ['allow_local_address' => false]]
  341. );
  342. }
  343. public function testRejectCanonicalName() {
  344. $this->expectException(LocalServerException::class);
  345. $this->expectExceptionMessage('violates local access rules');
  346. $mockHandler = new MockHandler([
  347. static function (RequestInterface $request, array $options) {
  348. // The handler should not be called
  349. },
  350. ]);
  351. $this->dnsPinMiddleware
  352. ->method('dnsGetRecord')
  353. ->willReturnCallback(function (string $hostname, int $type) {
  354. // example.com SOA
  355. if ($hostname === 'example.com') {
  356. return match ($type) {
  357. DNS_SOA => [
  358. [
  359. 'host' => 'example.com',
  360. 'class' => 'IN',
  361. 'ttl' => 7079,
  362. 'type' => 'SOA',
  363. 'minimum-ttl' => 3600,
  364. ]
  365. ],
  366. };
  367. }
  368. // example.com A, AAAA, CNAME
  369. if ($hostname === 'www.example.com') {
  370. return match ($type) {
  371. DNS_A => [],
  372. DNS_AAAA => [],
  373. DNS_CNAME => [
  374. [
  375. 'host' => 'www.example.com',
  376. 'class' => 'IN',
  377. 'ttl' => 1800,
  378. 'type' => 'A',
  379. 'target' => 'www.example.net'
  380. ]
  381. ],
  382. };
  383. }
  384. // example.net SOA
  385. if ($hostname === 'example.net') {
  386. return match ($type) {
  387. DNS_SOA => [
  388. [
  389. 'host' => 'example.net',
  390. 'class' => 'IN',
  391. 'ttl' => 7079,
  392. 'type' => 'SOA',
  393. 'minimum-ttl' => 3600,
  394. ]
  395. ],
  396. };
  397. }
  398. // example.net A, AAAA, CNAME
  399. if ($hostname === 'www.example.net') {
  400. return match ($type) {
  401. DNS_A => [
  402. [
  403. 'host' => 'www.example.net',
  404. 'class' => 'IN',
  405. 'ttl' => 1800,
  406. 'type' => 'A',
  407. 'ip' => '192.168.0.2'
  408. ]
  409. ],
  410. DNS_AAAA => [],
  411. DNS_CNAME => [],
  412. };
  413. }
  414. return false;
  415. });
  416. $stack = new HandlerStack($mockHandler);
  417. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  418. $handler = $stack->resolve();
  419. $handler(
  420. new Request('GET', 'https://www.example.com'),
  421. ['nextcloud' => ['allow_local_address' => false]]
  422. );
  423. }
  424. public function testRejectFaultyResponse() {
  425. $this->expectException(LocalServerException::class);
  426. $this->expectExceptionMessage('No DNS record found for www.example.com');
  427. $mockHandler = new MockHandler([
  428. static function (RequestInterface $request, array $options) {
  429. // The handler should not be called
  430. },
  431. ]);
  432. $this->dnsPinMiddleware
  433. ->method('dnsGetRecord')
  434. ->willReturnCallback(function (string $hostname, int $type) {
  435. return false;
  436. });
  437. $stack = new HandlerStack($mockHandler);
  438. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  439. $handler = $stack->resolve();
  440. $handler(
  441. new Request('GET', 'https://www.example.com'),
  442. ['nextcloud' => ['allow_local_address' => false]]
  443. );
  444. }
  445. public function testIgnoreSubdomainForSoaQuery() {
  446. $mockHandler = new MockHandler([
  447. static function (RequestInterface $request, array $options) {
  448. // The handler should not be called
  449. },
  450. ]);
  451. $dnsQueries = [];
  452. $this->dnsPinMiddleware
  453. ->method('dnsGetRecord')
  454. ->willReturnCallback(function (string $hostname, int $type) use (&$dnsQueries) {
  455. // log query
  456. $dnsQueries[] = $hostname . $type;
  457. // example.com SOA
  458. if ($hostname === 'example.com') {
  459. return match ($type) {
  460. DNS_SOA => [
  461. [
  462. 'host' => 'example.com',
  463. 'class' => 'IN',
  464. 'ttl' => 7079,
  465. 'type' => 'SOA',
  466. 'minimum-ttl' => 3600,
  467. ]
  468. ],
  469. };
  470. }
  471. // example.net A, AAAA, CNAME
  472. if ($hostname === 'subsubdomain.subdomain.example.com') {
  473. return match ($type) {
  474. DNS_A => [
  475. [
  476. 'host' => 'subsubdomain.subdomain.example.com',
  477. 'class' => 'IN',
  478. 'ttl' => 1800,
  479. 'type' => 'A',
  480. 'ip' => '1.1.1.1'
  481. ]
  482. ],
  483. DNS_AAAA => [],
  484. DNS_CNAME => [],
  485. };
  486. }
  487. return false;
  488. });
  489. $stack = new HandlerStack($mockHandler);
  490. $stack->push($this->dnsPinMiddleware->addDnsPinning());
  491. $handler = $stack->resolve();
  492. $handler(
  493. new Request('GET', 'https://subsubdomain.subdomain.example.com'),
  494. ['nextcloud' => ['allow_local_address' => false]]
  495. );
  496. $this->assertCount(3, $dnsQueries);
  497. $this->assertContains('example.com' . DNS_SOA, $dnsQueries);
  498. $this->assertContains('subsubdomain.subdomain.example.com' . DNS_A, $dnsQueries);
  499. $this->assertContains('subsubdomain.subdomain.example.com' . DNS_AAAA, $dnsQueries);
  500. // CNAME should not be queried if A or AAAA succeeded already
  501. $this->assertNotContains('subsubdomain.subdomain.example.com' . DNS_CNAME, $dnsQueries);
  502. }
  503. }