Skip to content

Commit

Permalink
fix(security): Handle idn_to_utf8 returning false
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Dec 4, 2023
1 parent df7bc46 commit 33e1c8b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/private/Security/RemoteHostValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/lib/Http/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}

Expand Down
15 changes: 12 additions & 3 deletions tests/lib/Security/RemoteHostValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -73,7 +82,7 @@ public function testValid(): void {

$valid = $this->validator->isValid($host);

self::assertTrue($valid);
self::assertSame($expected, $valid);
}

public function testLocalHostname(): void {
Expand Down

0 comments on commit 33e1c8b

Please sign in to comment.