| 11 | use PHPUnit\Framework\TestCase; |
| 12 | |
| 13 | class ExternalUrlValidatorTest extends TestCase { |
| 14 | private IRemoteHostValidator $remoteHostValidator; |
| 15 | |
| 16 | protected function setUp(): void { |
| 17 | parent::setUp(); |
| 18 | $this->remoteHostValidator = $this->createMock(IRemoteHostValidator::class); |
| 19 | $this->remoteHostValidator->method('isValid')->willReturn(false); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @dataProvider blockedUrls |
| 24 | */ |
| 25 | public function testValidateRejectsPrivateAndReservedTargets(string $url): void { |
| 26 | $this->assertNotNull((new ExternalUrlValidator($this->remoteHostValidator))->validate($url)); |
| 27 | } |
| 28 | |
| 29 | public function blockedUrls(): array { |
| 30 | return [ |
| 31 | 'localhost' => ['http://localhost/status'], |
| 32 | 'loopback' => ['http://127.0.0.1/status'], |
| 33 | 'private ipv4' => ['http://192.168.1.10/status'], |
| 34 | 'link local' => ['http://169.254.169.254/latest/meta-data'], |
| 35 | 'ipv6 loopback' => ['http://[::1]/status'], |
| 36 | 'ipv4-mapped ipv6 loopback' => ['http://[::ffff:127.0.0.1]/status'], |
| 37 | 'ipv4-mapped ipv6 metadata' => ['http://[::ffff:169.254.169.254]/latest/meta-data'], |
| 38 | 'userinfo' => ['https://user:password@example.com/data'], |
| 39 | 'file scheme' => ['file:///etc/passwd'], |
| 40 | ]; |
| 41 | } |
| 42 | |
| 43 | public function testValidateUsesNextcloudRemoteHostPolicy(): void { |
| 44 | $remoteHostValidator = $this->createMock(IRemoteHostValidator::class); |
| 45 | $remoteHostValidator |
| 46 | ->expects($this->once()) |
| 47 | ->method('isValid') |
| 48 | ->with('10.0.0.150') |
| 49 | ->willReturn(true); |
| 50 | |
| 51 | $this->assertNull((new ExternalUrlValidator($remoteHostValidator))->validate('http://10.0.0.150/em1data/0/data.csv')); |
| 52 | } |
| 53 | } |
nothing calls this directly
no outgoing calls
no test coverage detected