1
0

RemoteAddressTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Security\Ip;
  8. use OC\Security\Ip\RemoteAddress;
  9. use OCP\IConfig;
  10. use OCP\IRequest;
  11. class RemoteAddressTest extends \Test\TestCase {
  12. private IConfig $config;
  13. private IRequest $request;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->config = $this->createMock(IConfig::class);
  17. $this->request = $this->createMock(IRequest::class);
  18. }
  19. /**
  20. * @param mixed $allowedRanges
  21. * @dataProvider dataProvider
  22. */
  23. public function testAllowedIps(string $remoteIp, $allowedRanges, bool $expected): void {
  24. $this->request
  25. ->method('getRemoteAddress')
  26. ->willReturn($remoteIp);
  27. $this->config
  28. ->method('getSystemValue')
  29. ->with('allowed_admin_ranges', false)
  30. ->willReturn($allowedRanges);
  31. $remoteAddress = new RemoteAddress($this->config, $this->request);
  32. $this->assertEquals($expected, $remoteAddress->allowsAdminActions());
  33. }
  34. /**
  35. * @return array<string, mixed, bool>
  36. */
  37. public function dataProvider(): array {
  38. return [
  39. // No IP (ie. CLI)
  40. ['', ['192.168.1.2/24'], true],
  41. ['', ['fe80/8'], true],
  42. // No configuration
  43. ['1.2.3.4', false, true],
  44. ['1234:4567:8910::', false, true],
  45. // v6 Zone ID
  46. ['fe80::1fc4:15d8:78db:2319%enp4s0', false, true],
  47. // Empty configuration
  48. ['1.2.3.4', [], true],
  49. ['1234:4567:8910::', [], true],
  50. // Invalid configuration
  51. ['1.2.3.4', 'hello', true],
  52. ['1234:4567:8910::', 'world', true],
  53. // Mixed configuration
  54. ['192.168.1.5', ['1.2.3.*', '1234::/8'], false],
  55. ['::1', ['127.0.0.1', '1234::/8'], false],
  56. ['192.168.1.5', ['192.168.1.0/24', '1234::/8'], true],
  57. // Allowed IP
  58. ['1.2.3.4', ['1.2.3.*'], true],
  59. ['fc00:1:2:3::1', ['fc00::/7'], true],
  60. ['1.2.3.4', ['192.168.1.2/24', '1.2.3.0/24'], true],
  61. ['1234:4567:8910::1', ['fe80::/8','1234:4567::/16'], true],
  62. // Blocked IP
  63. ['192.168.1.5', ['1.2.3.*'], false],
  64. ['9234:4567:8910::', ['1234:4567::1'], false],
  65. ['192.168.2.1', ['192.168.1.2/24', '1.2.3.0/24'], false],
  66. ['9234:4567:8910::', ['fe80::/8','1234:4567::/16'], false],
  67. ];
  68. }
  69. }