Skip to content

[HttpClient] reject malformed URLs with a meaningful exception #57981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ private static function jsonEncode($value, ?int $flags = null, int $maxDepth = 5
*/
private static function resolveUrl(array $url, ?array $base, array $queryDefaults = []): array
{
$givenUrl = $url;

if (null !== $base && '' === ($base['scheme'] ?? '').($base['authority'] ?? '')) {
throw new InvalidArgumentException(sprintf('Invalid "base_uri" option: host or scheme is missing in "%s".', implode('', $base)));
}
Expand Down Expand Up @@ -498,6 +500,10 @@ private static function resolveUrl(array $url, ?array $base, array $queryDefault
$url['query'] = null;
}

if (null !== $url['scheme'] && null === $url['authority']) {
throw new InvalidArgumentException(\sprintf('Invalid URL: host is missing in "%s".', implode('', $givenUrl)));
}

return $url;
}

Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\SkippedTestSuiteError;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Internal\ClientState;
use Symfony\Component\HttpClient\Response\StreamWrapper;
Expand Down Expand Up @@ -455,4 +456,14 @@ public function testNullBody()

$this->expectNotToPerformAssertions();
}

public function testMisspelledScheme()
{
$httpClient = $this->getHttpClient(__FUNCTION__);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid URL: host is missing in "http:/localhost:8057/".');

$httpClient->request('GET', 'http:/localhost:8057/');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public function testResolveUrl(string $base, string $url, string $expected)
public static function provideResolveUrl(): array
{
return [
[self::RFC3986_BASE, 'http:h', 'http:h'],
[self::RFC3986_BASE, 'g', 'http://a/b/c/g'],
[self::RFC3986_BASE, './g', 'http://a/b/c/g'],
[self::RFC3986_BASE, 'g/', 'http://a/b/c/g/'],
Expand Down Expand Up @@ -117,7 +116,6 @@ public static function provideResolveUrl(): array
['http://u:p@a/b/c/d;p?q', '.', 'http://u:p@a/b/c/'],
// path ending with slash or no slash at all
['http://a/b/c/d/', 'e', 'http://a/b/c/d/e'],
['http:no-slash', 'e', 'http:e'],
// falsey relative parts
[self::RFC3986_BASE, '//0', 'http://0/'],
[self::RFC3986_BASE, '0', 'http://a/b/c/0'],
Expand Down
Loading