Skip to content

Commit ba020a3

Browse files
Merge branch '5.4' into 6.4
* 5.4: Do not read from argv on non-CLI SAPIs [Process] Use %PATH% before %CD% to load the shell on Windows [HttpFoundation] Reject URIs that contain invalid characters [HttpClient] Filter private IPs before connecting when Host == IP
2 parents 4875486 + 168b77c commit ba020a3

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

Request.php

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpFoundation;
1313

14+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
1415
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
1516
use Symfony\Component\HttpFoundation\Exception\JsonException;
1617
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
@@ -326,6 +327,8 @@ public static function createFromGlobals(): static
326327
* @param array $files The request files ($_FILES)
327328
* @param array $server The server parameters ($_SERVER)
328329
* @param string|resource|null $content The raw body data
330+
*
331+
* @throws BadRequestException When the URI is invalid
329332
*/
330333
public static function create(string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], $content = null): static
331334
{
@@ -354,6 +357,20 @@ public static function create(string $uri, string $method = 'GET', array $parame
354357
unset($components['fragment']);
355358
}
356359

360+
if (false === $components) {
361+
throw new BadRequestException('Invalid URI.');
362+
}
363+
364+
if (false !== ($i = strpos($uri, '\\')) && $i < strcspn($uri, '?#')) {
365+
throw new BadRequestException('Invalid URI: A URI cannot contain a backslash.');
366+
}
367+
if (\strlen($uri) !== strcspn($uri, "\r\n\t")) {
368+
throw new BadRequestException('Invalid URI: A URI cannot contain CR/LF/TAB characters.');
369+
}
370+
if ('' !== $uri && (\ord($uri[0]) <= 32 || \ord($uri[-1]) <= 32)) {
371+
throw new BadRequestException('Invalid URI: A URI must not start nor end with ASCII control characters or spaces.');
372+
}
373+
357374
if (isset($components['host'])) {
358375
$server['SERVER_NAME'] = $components['host'];
359376
$server['HTTP_HOST'] = $components['host'];

Tests/RequestTest.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
16+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
1617
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
1718
use Symfony\Component\HttpFoundation\Exception\JsonException;
1819
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
@@ -305,9 +306,34 @@ public function testCreateWithRequestUri()
305306
$this->assertTrue($request->isSecure());
306307

307308
// Fragment should not be included in the URI
308-
$request = Request::create('http://test.com/foo#bar');
309-
$request->server->set('REQUEST_URI', 'http://test.com/foo#bar');
309+
$request = Request::create('http://test.com/foo#bar\\baz');
310+
$request->server->set('REQUEST_URI', 'http://test.com/foo#bar\\baz');
310311
$this->assertEquals('http://test.com/foo', $request->getUri());
312+
313+
$request = Request::create('http://test.com/foo?bar=f\\o');
314+
$this->assertEquals('http://test.com/foo?bar=f%5Co', $request->getUri());
315+
$this->assertEquals('/foo', $request->getPathInfo());
316+
$this->assertEquals('bar=f%5Co', $request->getQueryString());
317+
}
318+
319+
/**
320+
* @testWith ["http://foo.com\\bar"]
321+
* ["\\\\foo.com/bar"]
322+
* ["a\rb"]
323+
* ["a\nb"]
324+
* ["a\tb"]
325+
* ["\u0000foo"]
326+
* ["foo\u0000"]
327+
* [" foo"]
328+
* ["foo "]
329+
* [":"]
330+
*/
331+
public function testCreateWithBadRequestUri(string $uri)
332+
{
333+
$this->expectException(BadRequestException::class);
334+
$this->expectExceptionMessage('Invalid URI');
335+
336+
Request::create($uri);
311337
}
312338

313339
/**

0 commit comments

Comments
 (0)