1
0

RemoteHostValidatorIntegrationTest.php 3.6 KB

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