Skip to content

Commit

Permalink
[DependencyInjection][Routing][HttpClient] Reject URIs that contain i…
Browse files Browse the repository at this point in the history
…nvalid characters
  • Loading branch information
nicolas-grekas committed Nov 6, 2024
1 parent d304eeb commit 0782e32
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public function __construct(string $baseUrl = '', string $method = 'GET', string

public static function fromUri(string $uri, string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443): self
{
if (false !== ($i = strpos($uri, '\\')) && $i < strcspn($uri, '?#')) {
$uri = '';
}
if ('' !== $uri && (\ord($uri[0]) <= 32 || \ord($uri[-1]) <= 32 || \strlen($uri) !== strcspn($uri, "\r\n\t"))) {
$uri = '';
}

$uri = parse_url($uri);
$scheme = $uri['scheme'] ?? $scheme;
$host = $uri['host'] ?? $host;
Expand Down
22 changes: 22 additions & 0 deletions Tests/RequestContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ public function testFromUriBeingEmpty()
$this->assertSame('/', $requestContext->getPathInfo());
}

/**
* @testWith ["http://foo.com\\bar"]
* ["\\\\foo.com/bar"]
* ["a\rb"]
* ["a\nb"]
* ["a\tb"]
* ["\u0000foo"]
* ["foo\u0000"]
* [" foo"]
* ["foo "]
* [":"]
*/
public function testFromBadUri(string $uri)
{
$context = RequestContext::fromUri($uri);

$this->assertSame('http', $context->getScheme());
$this->assertSame('localhost', $context->getHost());
$this->assertSame('', $context->getBaseUrl());
$this->assertSame('/', $context->getPathInfo());
}

public function testFromRequest()
{
$request = Request::create('https://test.com:444/foo?bar=baz');
Expand Down

0 comments on commit 0782e32

Please sign in to comment.