1
0

ClientTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright (c) 2015 Lukas Reschke <lukas@owncloud.com>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. namespace Test\Http\Client;
  10. use GuzzleHttp\Psr7\Response;
  11. use OC\Http\Client\Client;
  12. use OC\Security\CertificateManager;
  13. use OCP\Http\Client\LocalServerException;
  14. use OCP\ICertificateManager;
  15. use OCP\IConfig;
  16. use OCP\Security\IRemoteHostValidator;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Psr\Log\LoggerInterface;
  19. use function parse_url;
  20. /**
  21. * Class ClientTest
  22. */
  23. class ClientTest extends \Test\TestCase {
  24. /** @var \GuzzleHttp\Client|MockObject */
  25. private $guzzleClient;
  26. /** @var CertificateManager|MockObject */
  27. private $certificateManager;
  28. /** @var Client */
  29. private $client;
  30. /** @var IConfig|MockObject */
  31. private $config;
  32. /** @var IRemoteHostValidator|MockObject */
  33. private IRemoteHostValidator $remoteHostValidator;
  34. private LoggerInterface $logger;
  35. /** @var array */
  36. private $defaultRequestOptions;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->config = $this->createMock(IConfig::class);
  40. $this->guzzleClient = $this->createMock(\GuzzleHttp\Client::class);
  41. $this->certificateManager = $this->createMock(ICertificateManager::class);
  42. $this->remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
  43. $this->logger = $this->createMock(LoggerInterface::class);
  44. $this->client = new Client(
  45. $this->config,
  46. $this->certificateManager,
  47. $this->guzzleClient,
  48. $this->remoteHostValidator,
  49. $this->logger,
  50. );
  51. }
  52. public function testGetProxyUri(): void {
  53. $this->config
  54. ->method('getSystemValueString')
  55. ->with('proxy', '')
  56. ->willReturn('');
  57. $this->assertNull(self::invokePrivate($this->client, 'getProxyUri'));
  58. }
  59. public function testGetProxyUriProxyHostEmptyPassword(): void {
  60. $this->config
  61. ->method('getSystemValue')
  62. ->will($this->returnValueMap([
  63. ['proxyexclude', [], []],
  64. ]));
  65. $this->config
  66. ->method('getSystemValueString')
  67. ->will($this->returnValueMap([
  68. ['proxy', '', 'foo'],
  69. ['proxyuserpwd', '', ''],
  70. ]));
  71. $this->assertEquals([
  72. 'http' => 'foo',
  73. 'https' => 'foo'
  74. ], self::invokePrivate($this->client, 'getProxyUri'));
  75. }
  76. public function testGetProxyUriProxyHostWithPassword(): void {
  77. $this->config
  78. ->expects($this->once())
  79. ->method('getSystemValue')
  80. ->with('proxyexclude', [])
  81. ->willReturn([]);
  82. $this->config
  83. ->expects($this->exactly(2))
  84. ->method('getSystemValueString')
  85. ->withConsecutive(
  86. ['proxy', ''],
  87. ['proxyuserpwd', ''],
  88. )
  89. ->willReturnOnConsecutiveCalls(
  90. 'foo',
  91. 'username:password',
  92. );
  93. $this->assertEquals([
  94. 'http' => 'username:password@foo',
  95. 'https' => 'username:password@foo'
  96. ], self::invokePrivate($this->client, 'getProxyUri'));
  97. }
  98. public function testGetProxyUriProxyHostWithPasswordAndExclude(): void {
  99. $this->config
  100. ->expects($this->once())
  101. ->method('getSystemValue')
  102. ->with('proxyexclude', [])
  103. ->willReturn(['bar']);
  104. $this->config
  105. ->expects($this->exactly(2))
  106. ->method('getSystemValueString')
  107. ->withConsecutive(
  108. ['proxy', ''],
  109. ['proxyuserpwd', ''],
  110. )
  111. ->willReturnOnConsecutiveCalls(
  112. 'foo',
  113. 'username:password',
  114. );
  115. $this->assertEquals([
  116. 'http' => 'username:password@foo',
  117. 'https' => 'username:password@foo',
  118. 'no' => ['bar']
  119. ], self::invokePrivate($this->client, 'getProxyUri'));
  120. }
  121. public function dataPreventLocalAddress():array {
  122. return [
  123. ['https://localhost/foo.bar'],
  124. ['https://localHost/foo.bar'],
  125. ['https://random-host/foo.bar'],
  126. ['https://[::1]/bla.blub'],
  127. ['https://[::]/bla.blub'],
  128. ['https://192.168.0.1'],
  129. ['https://172.16.42.1'],
  130. ['https://[fdf8:f53b:82e4::53]/secret.ics'],
  131. ['https://[fe80::200:5aee:feaa:20a2]/secret.ics'],
  132. ['https://[0:0:0:0:0:0:10.0.0.1]/secret.ics'],
  133. ['https://[0:0:0:0:0:ffff:127.0.0.0]/secret.ics'],
  134. ['https://10.0.0.1'],
  135. ['https://another-host.local'],
  136. ['https://service.localhost'],
  137. ['!@#$', true], // test invalid url
  138. ['https://normal.host.com'],
  139. ['https://com.one-.nextcloud-one.com'],
  140. ];
  141. }
  142. /**
  143. * @dataProvider dataPreventLocalAddress
  144. * @param string $uri
  145. */
  146. public function testPreventLocalAddressDisabledByGlobalConfig(string $uri): void {
  147. $this->config->expects($this->once())
  148. ->method('getSystemValueBool')
  149. ->with('allow_local_remote_servers', false)
  150. ->willReturn(true);
  151. self::invokePrivate($this->client, 'preventLocalAddress', [$uri, []]);
  152. }
  153. /**
  154. * @dataProvider dataPreventLocalAddress
  155. * @param string $uri
  156. */
  157. public function testPreventLocalAddressDisabledByOption(string $uri): void {
  158. $this->config->expects($this->never())
  159. ->method('getSystemValueBool');
  160. self::invokePrivate($this->client, 'preventLocalAddress', [$uri, [
  161. 'nextcloud' => ['allow_local_address' => true],
  162. ]]);
  163. }
  164. /**
  165. * @dataProvider dataPreventLocalAddress
  166. * @param string $uri
  167. */
  168. public function testPreventLocalAddressOnGet(string $uri): void {
  169. $host = parse_url($uri, PHP_URL_HOST);
  170. $this->expectException(LocalServerException::class);
  171. $this->remoteHostValidator
  172. ->method('isValid')
  173. ->with($host)
  174. ->willReturn(false);
  175. $this->client->get($uri);
  176. }
  177. /**
  178. * @dataProvider dataPreventLocalAddress
  179. * @param string $uri
  180. */
  181. public function testPreventLocalAddressOnHead(string $uri): void {
  182. $host = parse_url($uri, PHP_URL_HOST);
  183. $this->expectException(LocalServerException::class);
  184. $this->remoteHostValidator
  185. ->method('isValid')
  186. ->with($host)
  187. ->willReturn(false);
  188. $this->client->head($uri);
  189. }
  190. /**
  191. * @dataProvider dataPreventLocalAddress
  192. * @param string $uri
  193. */
  194. public function testPreventLocalAddressOnPost(string $uri): void {
  195. $host = parse_url($uri, PHP_URL_HOST);
  196. $this->expectException(LocalServerException::class);
  197. $this->remoteHostValidator
  198. ->method('isValid')
  199. ->with($host)
  200. ->willReturn(false);
  201. $this->client->post($uri);
  202. }
  203. /**
  204. * @dataProvider dataPreventLocalAddress
  205. * @param string $uri
  206. */
  207. public function testPreventLocalAddressOnPut(string $uri): void {
  208. $host = parse_url($uri, PHP_URL_HOST);
  209. $this->expectException(LocalServerException::class);
  210. $this->remoteHostValidator
  211. ->method('isValid')
  212. ->with($host)
  213. ->willReturn(false);
  214. $this->client->put($uri);
  215. }
  216. /**
  217. * @dataProvider dataPreventLocalAddress
  218. * @param string $uri
  219. */
  220. public function testPreventLocalAddressOnDelete(string $uri): void {
  221. $host = parse_url($uri, PHP_URL_HOST);
  222. $this->expectException(LocalServerException::class);
  223. $this->remoteHostValidator
  224. ->method('isValid')
  225. ->with($host)
  226. ->willReturn(false);
  227. $this->client->delete($uri);
  228. }
  229. private function setUpDefaultRequestOptions(): void {
  230. $this->config
  231. ->method('getSystemValue')
  232. ->will($this->returnValueMap([
  233. ['proxyexclude', [], []],
  234. ]));
  235. $this->config
  236. ->method('getSystemValueString')
  237. ->will($this->returnValueMap([
  238. ['proxy', '', 'foo'],
  239. ['proxyuserpwd', '', ''],
  240. ]));
  241. $this->config
  242. ->method('getSystemValueBool')
  243. ->will($this->returnValueMap([
  244. ['installed', false, true],
  245. ['allow_local_remote_servers', false, true],
  246. ]));
  247. $this->certificateManager
  248. ->expects($this->once())
  249. ->method('getAbsoluteBundlePath')
  250. ->with()
  251. ->willReturn('/my/path.crt');
  252. $this->defaultRequestOptions = [
  253. 'verify' => '/my/path.crt',
  254. 'proxy' => [
  255. 'http' => 'foo',
  256. 'https' => 'foo'
  257. ],
  258. 'headers' => [
  259. 'User-Agent' => 'Nextcloud Server Crawler',
  260. 'Accept-Encoding' => 'gzip',
  261. ],
  262. 'timeout' => 30,
  263. 'nextcloud' => [
  264. 'allow_local_address' => true,
  265. ],
  266. ];
  267. }
  268. public function testGet(): void {
  269. $this->setUpDefaultRequestOptions();
  270. $this->guzzleClient->method('request')
  271. ->with('get', 'http://localhost/', $this->defaultRequestOptions)
  272. ->willReturn(new Response(418));
  273. $this->assertEquals(418, $this->client->get('http://localhost/', [])->getStatusCode());
  274. }
  275. public function testGetWithOptions(): void {
  276. $this->setUpDefaultRequestOptions();
  277. $options = array_merge($this->defaultRequestOptions, [
  278. 'verify' => false,
  279. 'proxy' => [
  280. 'http' => 'bar',
  281. 'https' => 'bar'
  282. ],
  283. ]);
  284. $this->guzzleClient->method('request')
  285. ->with('get', 'http://localhost/', $options)
  286. ->willReturn(new Response(418));
  287. $this->assertEquals(418, $this->client->get('http://localhost/', $options)->getStatusCode());
  288. }
  289. public function testPost(): void {
  290. $this->setUpDefaultRequestOptions();
  291. $this->guzzleClient->method('request')
  292. ->with('post', 'http://localhost/', $this->defaultRequestOptions)
  293. ->willReturn(new Response(418));
  294. $this->assertEquals(418, $this->client->post('http://localhost/', [])->getStatusCode());
  295. }
  296. public function testPostWithOptions(): void {
  297. $this->setUpDefaultRequestOptions();
  298. $options = array_merge($this->defaultRequestOptions, [
  299. 'verify' => false,
  300. 'proxy' => [
  301. 'http' => 'bar',
  302. 'https' => 'bar'
  303. ],
  304. ]);
  305. $this->guzzleClient->method('request')
  306. ->with('post', 'http://localhost/', $options)
  307. ->willReturn(new Response(418));
  308. $this->assertEquals(418, $this->client->post('http://localhost/', $options)->getStatusCode());
  309. }
  310. public function testPut(): void {
  311. $this->setUpDefaultRequestOptions();
  312. $this->guzzleClient->method('request')
  313. ->with('put', 'http://localhost/', $this->defaultRequestOptions)
  314. ->willReturn(new Response(418));
  315. $this->assertEquals(418, $this->client->put('http://localhost/', [])->getStatusCode());
  316. }
  317. public function testPutWithOptions(): void {
  318. $this->setUpDefaultRequestOptions();
  319. $options = array_merge($this->defaultRequestOptions, [
  320. 'verify' => false,
  321. 'proxy' => [
  322. 'http' => 'bar',
  323. 'https' => 'bar'
  324. ],
  325. ]);
  326. $this->guzzleClient->method('request')
  327. ->with('put', 'http://localhost/', $options)
  328. ->willReturn(new Response(418));
  329. $this->assertEquals(418, $this->client->put('http://localhost/', $options)->getStatusCode());
  330. }
  331. public function testDelete(): void {
  332. $this->setUpDefaultRequestOptions();
  333. $this->guzzleClient->method('request')
  334. ->with('delete', 'http://localhost/', $this->defaultRequestOptions)
  335. ->willReturn(new Response(418));
  336. $this->assertEquals(418, $this->client->delete('http://localhost/', [])->getStatusCode());
  337. }
  338. public function testDeleteWithOptions(): void {
  339. $this->setUpDefaultRequestOptions();
  340. $options = array_merge($this->defaultRequestOptions, [
  341. 'verify' => false,
  342. 'proxy' => [
  343. 'http' => 'bar',
  344. 'https' => 'bar'
  345. ],
  346. ]);
  347. $this->guzzleClient->method('request')
  348. ->with('delete', 'http://localhost/', $options)
  349. ->willReturn(new Response(418));
  350. $this->assertEquals(418, $this->client->delete('http://localhost/', $options)->getStatusCode());
  351. }
  352. public function testOptions(): void {
  353. $this->setUpDefaultRequestOptions();
  354. $this->guzzleClient->method('request')
  355. ->with('options', 'http://localhost/', $this->defaultRequestOptions)
  356. ->willReturn(new Response(418));
  357. $this->assertEquals(418, $this->client->options('http://localhost/', [])->getStatusCode());
  358. }
  359. public function testOptionsWithOptions(): void {
  360. $this->setUpDefaultRequestOptions();
  361. $options = array_merge($this->defaultRequestOptions, [
  362. 'verify' => false,
  363. 'proxy' => [
  364. 'http' => 'bar',
  365. 'https' => 'bar'
  366. ],
  367. ]);
  368. $this->guzzleClient->method('request')
  369. ->with('options', 'http://localhost/', $options)
  370. ->willReturn(new Response(418));
  371. $this->assertEquals(418, $this->client->options('http://localhost/', $options)->getStatusCode());
  372. }
  373. public function testHead(): void {
  374. $this->setUpDefaultRequestOptions();
  375. $this->guzzleClient->method('request')
  376. ->with('head', 'http://localhost/', $this->defaultRequestOptions)
  377. ->willReturn(new Response(418));
  378. $this->assertEquals(418, $this->client->head('http://localhost/', [])->getStatusCode());
  379. }
  380. public function testHeadWithOptions(): void {
  381. $this->setUpDefaultRequestOptions();
  382. $options = array_merge($this->defaultRequestOptions, [
  383. 'verify' => false,
  384. 'proxy' => [
  385. 'http' => 'bar',
  386. 'https' => 'bar'
  387. ],
  388. ]);
  389. $this->guzzleClient->method('request')
  390. ->with('head', 'http://localhost/', $options)
  391. ->willReturn(new Response(418));
  392. $this->assertEquals(418, $this->client->head('http://localhost/', $options)->getStatusCode());
  393. }
  394. public function testSetDefaultOptionsWithNotInstalled(): void {
  395. $this->config
  396. ->expects($this->exactly(2))
  397. ->method('getSystemValueBool')
  398. ->withConsecutive(
  399. ['installed', false],
  400. ['allow_local_remote_servers', false],
  401. )
  402. ->willReturnOnConsecutiveCalls(
  403. false,
  404. false,
  405. );
  406. $this->config
  407. ->expects($this->once())
  408. ->method('getSystemValueString')
  409. ->with('proxy', '')
  410. ->willReturn('');
  411. $this->certificateManager
  412. ->expects($this->never())
  413. ->method('listCertificates');
  414. $this->assertEquals([
  415. 'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt',
  416. 'headers' => [
  417. 'User-Agent' => 'Nextcloud Server Crawler',
  418. 'Accept-Encoding' => 'gzip',
  419. ],
  420. 'timeout' => 30,
  421. 'nextcloud' => [
  422. 'allow_local_address' => false,
  423. ],
  424. 'allow_redirects' => [
  425. 'on_redirect' => function (
  426. \Psr\Http\Message\RequestInterface $request,
  427. \Psr\Http\Message\ResponseInterface $response,
  428. \Psr\Http\Message\UriInterface $uri
  429. ) {
  430. },
  431. ],
  432. ], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
  433. }
  434. public function testSetDefaultOptionsWithProxy(): void {
  435. $this->config
  436. ->expects($this->exactly(2))
  437. ->method('getSystemValueBool')
  438. ->withConsecutive(
  439. ['installed', false],
  440. ['allow_local_remote_servers', false],
  441. )
  442. ->willReturnOnConsecutiveCalls(
  443. true,
  444. false,
  445. );
  446. $this->config
  447. ->expects($this->once())
  448. ->method('getSystemValue')
  449. ->with('proxyexclude', [])
  450. ->willReturn([]);
  451. $this->config
  452. ->expects($this->exactly(2))
  453. ->method('getSystemValueString')
  454. ->withConsecutive(
  455. ['proxy', ''],
  456. ['proxyuserpwd', ''],
  457. )
  458. ->willReturnOnConsecutiveCalls(
  459. 'foo',
  460. '',
  461. );
  462. $this->certificateManager
  463. ->expects($this->once())
  464. ->method('getAbsoluteBundlePath')
  465. ->with()
  466. ->willReturn('/my/path.crt');
  467. $this->assertEquals([
  468. 'verify' => '/my/path.crt',
  469. 'proxy' => [
  470. 'http' => 'foo',
  471. 'https' => 'foo'
  472. ],
  473. 'headers' => [
  474. 'User-Agent' => 'Nextcloud Server Crawler',
  475. 'Accept-Encoding' => 'gzip',
  476. ],
  477. 'timeout' => 30,
  478. 'nextcloud' => [
  479. 'allow_local_address' => false,
  480. ],
  481. 'allow_redirects' => [
  482. 'on_redirect' => function (
  483. \Psr\Http\Message\RequestInterface $request,
  484. \Psr\Http\Message\ResponseInterface $response,
  485. \Psr\Http\Message\UriInterface $uri
  486. ) {
  487. },
  488. ],
  489. ], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
  490. }
  491. public function testSetDefaultOptionsWithProxyAndExclude(): void {
  492. $this->config
  493. ->expects($this->exactly(2))
  494. ->method('getSystemValueBool')
  495. ->withConsecutive(
  496. ['installed', false],
  497. ['allow_local_remote_servers', false],
  498. )
  499. ->willReturnOnConsecutiveCalls(
  500. true,
  501. false,
  502. );
  503. $this->config
  504. ->expects($this->once())
  505. ->method('getSystemValue')
  506. ->with('proxyexclude', [])
  507. ->willReturn(['bar']);
  508. $this->config
  509. ->expects($this->exactly(2))
  510. ->method('getSystemValueString')
  511. ->withConsecutive(
  512. ['proxy', ''],
  513. ['proxyuserpwd', ''],
  514. )
  515. ->willReturnOnConsecutiveCalls(
  516. 'foo',
  517. '',
  518. );
  519. $this->certificateManager
  520. ->expects($this->once())
  521. ->method('getAbsoluteBundlePath')
  522. ->with()
  523. ->willReturn('/my/path.crt');
  524. $this->assertEquals([
  525. 'verify' => '/my/path.crt',
  526. 'proxy' => [
  527. 'http' => 'foo',
  528. 'https' => 'foo',
  529. 'no' => ['bar']
  530. ],
  531. 'headers' => [
  532. 'User-Agent' => 'Nextcloud Server Crawler',
  533. 'Accept-Encoding' => 'gzip',
  534. ],
  535. 'timeout' => 30,
  536. 'nextcloud' => [
  537. 'allow_local_address' => false,
  538. ],
  539. 'allow_redirects' => [
  540. 'on_redirect' => function (
  541. \Psr\Http\Message\RequestInterface $request,
  542. \Psr\Http\Message\ResponseInterface $response,
  543. \Psr\Http\Message\UriInterface $uri
  544. ) {
  545. },
  546. ],
  547. ], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
  548. }
  549. }