RemoteHostValidatorIntegrationTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace lib\Security;
  8. use OC\Net\HostnameClassifier;
  9. use OC\Net\IpAddressClassifier;
  10. use OC\Security\RemoteHostValidator;
  11. use OCP\IConfig;
  12. use OCP\Server;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Psr\Log\NullLogger;
  15. use Test\TestCase;
  16. class RemoteHostValidatorIntegrationTest extends TestCase {
  17. /** @var IConfig|IConfig&MockObject|MockObject */
  18. private IConfig $config;
  19. private RemoteHostValidator $validator;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. // Mock config to avoid any side effects
  23. $this->config = $this->createMock(IConfig::class);
  24. $this->validator = new RemoteHostValidator(
  25. $this->config,
  26. Server::get(HostnameClassifier::class),
  27. Server::get(IpAddressClassifier::class),
  28. new NullLogger(),
  29. );
  30. }
  31. public function localHostsData(): array {
  32. return [
  33. ['[::1]'],
  34. ['[::]'],
  35. ['192.168.0.1'],
  36. ['172.16.42.1'],
  37. ['[fdf8:f53b:82e4::53]'],
  38. ['[fe80::200:5aee:feaa:20a2]'],
  39. ['[0:0:0:0:0:ffff:10.0.0.1]'],
  40. ['[0:0:0:0:0:ffff:127.0.0.0]'],
  41. ['10.0.0.1'],
  42. ['!@#$'], // test invalid url
  43. ['100.100.100.200'],
  44. ['192.0.0.1'],
  45. ['0177.0.0.9'],
  46. ['⑯⑨。②⑤④。⑯⑨。②⑤④'],
  47. ['127。②⑤④。⑯⑨.②⑤④'],
  48. ['127.0.00000000000000000000000000000000001'],
  49. ['127.1'],
  50. ['127.000.001'],
  51. ['0177.0.0.01'],
  52. ['0x7f.0x0.0x0.0x1'],
  53. ['0x7f000001'],
  54. ['2130706433'],
  55. ['00000000000000000000000000000000000000000000000000177.1'],
  56. ['0x7f.1'],
  57. ['127.0x1'],
  58. ['[0000:0000:0000:0000:0000:0000:0000:0001]'],
  59. ['[0:0:0:0:0:0:0:1]'],
  60. ['[0:0:0:0::0:0:1]'],
  61. ['%31%32%37%2E%30%2E%30%2E%31'],
  62. ['%31%32%37%2E%30%2E%30.%31'],
  63. ['[%3A%3A%31]'],
  64. ];
  65. }
  66. /**
  67. * @dataProvider localHostsData
  68. */
  69. public function testLocalHostsWhenNotAllowed(string $host): void {
  70. $this->config
  71. ->method('getSystemValueBool')
  72. ->with('allow_local_remote_servers', false)
  73. ->willReturn(false);
  74. $isValid = $this->validator->isValid($host);
  75. self::assertFalse($isValid);
  76. }
  77. /**
  78. * @dataProvider localHostsData
  79. */
  80. public function testLocalHostsWhenAllowed(string $host): void {
  81. $this->config
  82. ->method('getSystemValueBool')
  83. ->with('allow_local_remote_servers', false)
  84. ->willReturn(true);
  85. $isValid = $this->validator->isValid($host);
  86. self::assertTrue($isValid);
  87. }
  88. public function externalAddressesData():array {
  89. return [
  90. ['8.8.8.8'],
  91. ['8.8.4.4'],
  92. ['8.8.8.8'],
  93. ['8.8.4.4'],
  94. ['[2001:4860:4860::8888]'],
  95. ];
  96. }
  97. /**
  98. * @dataProvider externalAddressesData
  99. */
  100. public function testExternalHost(string $host): void {
  101. $this->config
  102. ->method('getSystemValueBool')
  103. ->with('allow_local_remote_servers', false)
  104. ->willReturn(false);
  105. $isValid = $this->validator->isValid($host);
  106. self::assertTrue($isValid);
  107. }
  108. }