Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 546dd7b

Browse files
bug #57981 [HttpClient] reject malformed URLs with a meaningful exception (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpClient] reject malformed URLs with a meaningful exception | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #57976 | License | MIT Commits ------- 2a86cbf reject malformed URLs with a meaningful exception
2 parents 642d823 + 2a86cbf commit 546dd7b

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ private static function jsonEncode($value, ?int $flags = null, int $maxDepth = 5
445445
*/
446446
private static function resolveUrl(array $url, ?array $base, array $queryDefaults = []): array
447447
{
448+
$givenUrl = $url;
449+
448450
if (null !== $base && '' === ($base['scheme'] ?? '').($base['authority'] ?? '')) {
449451
throw new InvalidArgumentException(sprintf('Invalid "base_uri" option: host or scheme is missing in "%s".', implode('', $base)));
450452
}
@@ -498,6 +500,10 @@ private static function resolveUrl(array $url, ?array $base, array $queryDefault
498500
$url['query'] = null;
499501
}
500502

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

src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\SkippedTestSuiteError;
1515
use Symfony\Component\HttpClient\Exception\ClientException;
16+
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
1617
use Symfony\Component\HttpClient\Exception\TransportException;
1718
use Symfony\Component\HttpClient\Internal\ClientState;
1819
use Symfony\Component\HttpClient\Response\StreamWrapper;
@@ -455,4 +456,14 @@ public function testNullBody()
455456

456457
$this->expectNotToPerformAssertions();
457458
}
459+
460+
public function testMisspelledScheme()
461+
{
462+
$httpClient = $this->getHttpClient(__FUNCTION__);
463+
464+
$this->expectException(InvalidArgumentException::class);
465+
$this->expectExceptionMessage('Invalid URL: host is missing in "http:/localhost:8057/".');
466+
467+
$httpClient->request('GET', 'http:/localhost:8057/');
468+
}
458469
}

src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public function testResolveUrl(string $base, string $url, string $expected)
6363
public static function provideResolveUrl(): array
6464
{
6565
return [
66-
[self::RFC3986_BASE, 'http:h', 'http:h'],
6766
[self::RFC3986_BASE, 'g', 'http://a/b/c/g'],
6867
[self::RFC3986_BASE, './g', 'http://a/b/c/g'],
6968
[self::RFC3986_BASE, 'g/', 'http://a/b/c/g/'],
@@ -117,7 +116,6 @@ public static function provideResolveUrl(): array
117116
['http://u:p@a/b/c/d;p?q', '.', 'http://u:p@a/b/c/'],
118117
// path ending with slash or no slash at all
119118
['http://a/b/c/d/', 'e', 'http://a/b/c/d/e'],
120-
['http:no-slash', 'e', 'http:e'],
121119
// falsey relative parts
122120
[self::RFC3986_BASE, '//0', 'http://0/'],
123121
[self::RFC3986_BASE, '0', 'http://a/b/c/0'],

0 commit comments

Comments
 (0)