From 0ddb3107c35dba5d9bcca2bc5bc96418f45e47fa Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Fri, 7 Dec 2018 17:21:59 +0100 Subject: [PATCH] [HttpFoundation] Fix prepare request uri when it starts with double slashes - KISS --- .../Component/HttpFoundation/Request.php | 18 +++++++++--------- .../HttpFoundation/Tests/RequestTest.php | 10 ++++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 8224c0d9b8ee1..5eb4617120b92 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1836,17 +1836,17 @@ protected function prepareRequestUri() $this->server->remove('IIS_WasUrlRewritten'); } elseif ($this->server->has('REQUEST_URI')) { $requestUri = $this->server->get('REQUEST_URI'); - - // The REQUEST_URI starts with double slashes. - if (0 === strpos($requestUri, '//')) { - // We assume that the scheme and host are not provided (e.g. //path/too). - // Then patch the call to `parse_url()`. - $requestUri = "http://example.com$requestUri"; + $uriComponents = array(); + + // HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path, + // only use URL path. + if ('' !== $requestUri && '/' !== $requestUri[0]) { + $uriComponents = parse_url($requestUri); + } elseif (false !== $pos = strpos($requestUri, '#')) { + // Remove the fragment. + $requestUri = substr($requestUri, 0, $pos); } - // HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path, only use URL path - $uriComponents = parse_url($requestUri); - if (isset($uriComponents['path'])) { $requestUri = $uriComponents['path']; } diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php index 0fb606ad4fb55..816cb82de7ae2 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php @@ -282,9 +282,19 @@ public function testCreateWithRequestUri() $request->server->set('REQUEST_URI', 'http://test.com/foo#bar'); $this->assertEquals('http://test.com/foo', $request->getUri()); + $request = Request::create('http://test.com/foo#bar'); + $request->server->set('REQUEST_URI', '/foo#bar'); + $this->assertEquals('http://test.com/foo', $request->getUri()); + + // When the path starts with 2 slashes $request = Request::create('http://foo//bar/foo'); $request->server->set('REQUEST_URI', '//bar/foo'); $this->assertEquals('//bar/foo', $request->getPathInfo(), 'When the REQUEST_URI starts with double slashes.'); + + // When the path starts with more than 2 slashes + $request = Request::create('http://foo///bar/foo'); + $request->server->set('REQUEST_URI', '///bar/foo'); + $this->assertEquals('///bar/foo', $request->getPathInfo(), 'When the REQUEST_URI starts with more than 2 slashes.'); } public function testCreateCheckPrecedence()