Skip to content

Commit 0782e32

Browse files
[DependencyInjection][Routing][HttpClient] Reject URIs that contain invalid characters
1 parent d304eeb commit 0782e32

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

RequestContext.php

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public function __construct(string $baseUrl = '', string $method = 'GET', string
4747

4848
public static function fromUri(string $uri, string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443): self
4949
{
50+
if (false !== ($i = strpos($uri, '\\')) && $i < strcspn($uri, '?#')) {
51+
$uri = '';
52+
}
53+
if ('' !== $uri && (\ord($uri[0]) <= 32 || \ord($uri[-1]) <= 32 || \strlen($uri) !== strcspn($uri, "\r\n\t"))) {
54+
$uri = '';
55+
}
56+
5057
$uri = parse_url($uri);
5158
$scheme = $uri['scheme'] ?? $scheme;
5259
$host = $uri['host'] ?? $host;

Tests/RequestContextTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ public function testFromUriBeingEmpty()
8585
$this->assertSame('/', $requestContext->getPathInfo());
8686
}
8787

88+
/**
89+
* @testWith ["http://foo.com\\bar"]
90+
* ["\\\\foo.com/bar"]
91+
* ["a\rb"]
92+
* ["a\nb"]
93+
* ["a\tb"]
94+
* ["\u0000foo"]
95+
* ["foo\u0000"]
96+
* [" foo"]
97+
* ["foo "]
98+
* [":"]
99+
*/
100+
public function testFromBadUri(string $uri)
101+
{
102+
$context = RequestContext::fromUri($uri);
103+
104+
$this->assertSame('http', $context->getScheme());
105+
$this->assertSame('localhost', $context->getHost());
106+
$this->assertSame('', $context->getBaseUrl());
107+
$this->assertSame('/', $context->getPathInfo());
108+
}
109+
88110
public function testFromRequest()
89111
{
90112
$request = Request::create('https://test.com:444/foo?bar=baz');

0 commit comments

Comments
 (0)