Skip to content

Commit cd65d42

Browse files
Merge branch '6.4' into 7.1
* 6.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 4f4d5a2 + ba020a3 commit cd65d42

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
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;
@@ -276,6 +277,8 @@ public static function createFromGlobals(): static
276277
* @param array $files The request files ($_FILES)
277278
* @param array $server The server parameters ($_SERVER)
278279
* @param string|resource|null $content The raw body data
280+
*
281+
* @throws BadRequestException When the URI is invalid
279282
*/
280283
public static function create(string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], $content = null): static
281284
{
@@ -303,6 +306,20 @@ public static function create(string $uri, string $method = 'GET', array $parame
303306
throw new \InvalidArgumentException(sprintf('Malformed URI "%s".', $uri));
304307
}
305308

309+
if (false === $components) {
310+
throw new BadRequestException('Invalid URI.');
311+
}
312+
313+
if (false !== ($i = strpos($uri, '\\')) && $i < strcspn($uri, '?#')) {
314+
throw new BadRequestException('Invalid URI: A URI cannot contain a backslash.');
315+
}
316+
if (\strlen($uri) !== strcspn($uri, "\r\n\t")) {
317+
throw new BadRequestException('Invalid URI: A URI cannot contain CR/LF/TAB characters.');
318+
}
319+
if ('' !== $uri && (\ord($uri[0]) <= 32 || \ord($uri[-1]) <= 32)) {
320+
throw new BadRequestException('Invalid URI: A URI must not start nor end with ASCII control characters or spaces.');
321+
}
322+
306323
if (isset($components['host'])) {
307324
$server['SERVER_NAME'] = $components['host'];
308325
$server['HTTP_HOST'] = $components['host'];

Tests/RequestTest.php

+28-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
1516
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
1617
use Symfony\Component\HttpFoundation\Exception\JsonException;
1718
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
@@ -289,9 +290,34 @@ public function testCreateWithRequestUri()
289290
$this->assertTrue($request->isSecure());
290291

291292
// Fragment should not be included in the URI
292-
$request = Request::create('http://test.com/foo#bar');
293-
$request->server->set('REQUEST_URI', 'http://test.com/foo#bar');
293+
$request = Request::create('http://test.com/foo#bar\\baz');
294+
$request->server->set('REQUEST_URI', 'http://test.com/foo#bar\\baz');
294295
$this->assertEquals('http://test.com/foo', $request->getUri());
296+
297+
$request = Request::create('http://test.com/foo?bar=f\\o');
298+
$this->assertEquals('http://test.com/foo?bar=f%5Co', $request->getUri());
299+
$this->assertEquals('/foo', $request->getPathInfo());
300+
$this->assertEquals('bar=f%5Co', $request->getQueryString());
301+
}
302+
303+
/**
304+
* @testWith ["http://foo.com\\bar"]
305+
* ["\\\\foo.com/bar"]
306+
* ["a\rb"]
307+
* ["a\nb"]
308+
* ["a\tb"]
309+
* ["\u0000foo"]
310+
* ["foo\u0000"]
311+
* [" foo"]
312+
* ["foo "]
313+
* [":"]
314+
*/
315+
public function testCreateWithBadRequestUri(string $uri)
316+
{
317+
$this->expectException(BadRequestException::class);
318+
$this->expectExceptionMessage('Invalid URI');
319+
320+
Request::create($uri);
295321
}
296322

297323
/**
@@ -2655,13 +2681,6 @@ public function testReservedFlags()
26552681
$this->assertNotSame(0b10000000, $value, sprintf('The constant "%s" should not use the reserved value "0b10000000".', $constant));
26562682
}
26572683
}
2658-
2659-
public function testMalformedUriCreationException()
2660-
{
2661-
$this->expectException(\InvalidArgumentException::class);
2662-
$this->expectExceptionMessage('Malformed URI "/invalid-path:123".');
2663-
Request::create('/invalid-path:123');
2664-
}
26652684
}
26662685

26672686
class RequestContentProxy extends Request

0 commit comments

Comments
 (0)