From 33e1c8b2361094be5466f5717bfa88ede7463784 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 4 Dec 2023 10:38:46 +0100 Subject: [PATCH] fix(security): Handle idn_to_utf8 returning false Signed-off-by: Joas Schilling --- lib/private/Security/RemoteHostValidator.php | 4 ++++ tests/lib/Http/Client/ClientTest.php | 1 + tests/lib/Security/RemoteHostValidatorTest.php | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/private/Security/RemoteHostValidator.php b/lib/private/Security/RemoteHostValidator.php index 385b38cff983d..9cc69594c3273 100644 --- a/lib/private/Security/RemoteHostValidator.php +++ b/lib/private/Security/RemoteHostValidator.php @@ -52,6 +52,10 @@ public function isValid(string $host): bool { } $host = idn_to_utf8(strtolower(urldecode($host))); + if ($host === false) { + return false; + } + // Remove brackets from IPv6 addresses if (str_starts_with($host, '[') && str_ends_with($host, ']')) { $host = substr($host, 1, -1); diff --git a/tests/lib/Http/Client/ClientTest.php b/tests/lib/Http/Client/ClientTest.php index 3cef9d7598656..0e6e265584ec1 100644 --- a/tests/lib/Http/Client/ClientTest.php +++ b/tests/lib/Http/Client/ClientTest.php @@ -149,6 +149,7 @@ public function dataPreventLocalAddress():array { ['https://service.localhost'], ['!@#$', true], // test invalid url ['https://normal.host.com'], + ['https://com.one-.nextcloud-one.com'], ]; } diff --git a/tests/lib/Security/RemoteHostValidatorTest.php b/tests/lib/Security/RemoteHostValidatorTest.php index 030a75b1e79ec..b1371d9343c39 100644 --- a/tests/lib/Security/RemoteHostValidatorTest.php +++ b/tests/lib/Security/RemoteHostValidatorTest.php @@ -60,8 +60,17 @@ protected function setUp(): void { ); } - public function testValid(): void { - $host = 'nextcloud.com'; + public function dataValid(): array { + return [ + ['nextcloud.com', true], + ['com.one-.nextcloud-one.com', false], + ]; + } + + /** + * @dataProvider dataValid + */ + public function testValid(string $host, bool $expected): void { $this->hostnameClassifier ->method('isLocalHostname') ->with($host) @@ -73,7 +82,7 @@ public function testValid(): void { $valid = $this->validator->isValid($host); - self::assertTrue($valid); + self::assertSame($expected, $valid); } public function testLocalHostname(): void {