Skip to content

Commit

Permalink
chore: always execute parse_url in preventLocalAddress
Browse files Browse the repository at this point in the history
This change should make it easier to spot wrong uses of the HTTP client on development setups where allow_local_remote_servers is usually true.

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Oct 1, 2024
1 parent 8708164 commit 6be0043
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/private/Http/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,15 @@ private function isLocalAddressAllowed(array $options) : bool {
}

protected function preventLocalAddress(string $uri, array $options): void {
if ($this->isLocalAddressAllowed($options)) {
return;
}

$host = parse_url($uri, PHP_URL_HOST);
if ($host === false || $host === null) {
throw new LocalServerException('Could not detect any host');
}

if ($this->isLocalAddressAllowed($options)) {
return;
}

if (!$this->remoteHostValidator->isValid($host)) {
throw new LocalServerException('Host "' . $host . '" violates local access rules');
}
Expand Down
8 changes: 7 additions & 1 deletion tests/lib/Http/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ public function testGetProxyUriProxyHostWithPasswordAndExclude(): void {
], self::invokePrivate($this->client, 'getProxyUri'));
}

public function testPreventLocalAddressThrowOnInvalidUri(): void {
$this->expectException(LocalServerException::class);
$this->expectExceptionMessage('Could not detect any host');

self::invokePrivate($this->client, 'preventLocalAddress', ['!@#$', []]);
}

public function dataPreventLocalAddress():array {
return [
['https://localhost/foo.bar'],
Expand All @@ -146,7 +153,6 @@ public function dataPreventLocalAddress():array {
['https://10.0.0.1'],
['https://another-host.local'],
['https://service.localhost'],
['!@#$', true], // test invalid url
['https://normal.host.com'],
['https://com.one-.nextcloud-one.com'],
];
Expand Down

0 comments on commit 6be0043

Please sign in to comment.