Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8653b9

Browse files
committed
bug #21968 Fixed pathinfo calculation for requests starting with a question mark. (syzygymsu)
This PR was squashed before being merged into the 2.7 branch (closes #21968). Discussion ---------- Fixed pathinfo calculation for requests starting with a question mark. | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21967 | License | MIT | Doc PR | With improper `strpos` result check calculated pathinfo for requests starting with '?' equals to request itself. Correct pathinfo for those requests should be '/'. Commits ------- 43297b4 Fixed pathinfo calculation for requests starting with a question mark.
2 parents d562cac + 43297b4 commit e8653b9

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,9 @@ protected function prepareBaseUrl()
17741774

17751775
// Does the baseUrl have anything in common with the request_uri?
17761776
$requestUri = $this->getRequestUri();
1777+
if ($requestUri !== '' && $requestUri[0] !== '/') {
1778+
$requestUri = '/'.$requestUri;
1779+
}
17771780

17781781
if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, $baseUrl)) {
17791782
// full $baseUrl matches
@@ -1846,9 +1849,12 @@ protected function preparePathInfo()
18461849
}
18471850

18481851
// Remove the query string from REQUEST_URI
1849-
if ($pos = strpos($requestUri, '?')) {
1852+
if (false !== $pos = strpos($requestUri, '?')) {
18501853
$requestUri = substr($requestUri, 0, $pos);
18511854
}
1855+
if ($requestUri !== '' && $requestUri[0] !== '/') {
1856+
$requestUri = '/'.$requestUri;
1857+
}
18521858

18531859
$pathInfo = substr($requestUri, strlen($baseUrl));
18541860
if (null !== $baseUrl && (false === $pathInfo || '' === $pathInfo)) {

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,12 @@ public function testGetPathInfo()
12641264
$request->initialize(array(), array(), array(), array(), array(), $server);
12651265

12661266
$this->assertEquals('/path%20test/info', $request->getPathInfo());
1267+
1268+
$server = array();
1269+
$server['REQUEST_URI'] = '?a=b';
1270+
$request->initialize(array(), array(), array(), array(), array(), $server);
1271+
1272+
$this->assertEquals('/', $request->getPathInfo());
12671273
}
12681274

12691275
public function testGetPreferredLanguage()
@@ -1992,6 +1998,61 @@ public function methodCacheableProvider()
19921998
array('CONNECT', false),
19931999
);
19942000
}
2001+
2002+
public function nonstandardRequestsData()
2003+
{
2004+
return array(
2005+
array('', '', '/', 'http://host:8080/', ''),
2006+
array('/', '', '/', 'http://host:8080/', ''),
2007+
2008+
array('hello/app.php/x', '', '/x', 'http://host:8080/hello/app.php/x', '/hello', '/hello/app.php'),
2009+
array('/hello/app.php/x', '', '/x', 'http://host:8080/hello/app.php/x', '/hello', '/hello/app.php'),
2010+
2011+
array('', 'a=b', '/', 'http://host:8080/?a=b'),
2012+
array('?a=b', 'a=b', '/', 'http://host:8080/?a=b'),
2013+
array('/?a=b', 'a=b', '/', 'http://host:8080/?a=b'),
2014+
2015+
array('x', 'a=b', '/x', 'http://host:8080/x?a=b'),
2016+
array('x?a=b', 'a=b', '/x', 'http://host:8080/x?a=b'),
2017+
array('/x?a=b', 'a=b', '/x', 'http://host:8080/x?a=b'),
2018+
2019+
array('hello/x', '', '/x', 'http://host:8080/hello/x', '/hello'),
2020+
array('/hello/x', '', '/x', 'http://host:8080/hello/x', '/hello'),
2021+
2022+
array('hello/app.php/x', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
2023+
array('hello/app.php/x?a=b', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
2024+
array('/hello/app.php/x?a=b', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
2025+
);
2026+
}
2027+
2028+
/**
2029+
* @dataProvider nonstandardRequestsData
2030+
*/
2031+
public function testNonstandardRequests($requestUri, $queryString, $expectedPathInfo, $expectedUri, $expectedBasePath = '', $expectedBaseUrl = null)
2032+
{
2033+
if (null === $expectedBaseUrl) {
2034+
$expectedBaseUrl = $expectedBasePath;
2035+
}
2036+
2037+
$server = array(
2038+
'HTTP_HOST' => 'host:8080',
2039+
'SERVER_PORT' => '8080',
2040+
'QUERY_STRING' => $queryString,
2041+
'PHP_SELF' => '/hello/app.php',
2042+
'SCRIPT_FILENAME' => '/some/path/app.php',
2043+
'REQUEST_URI' => $requestUri,
2044+
);
2045+
2046+
$request = new Request(array(), array(), array(), array(), array(), $server);
2047+
2048+
$this->assertEquals($expectedPathInfo, $request->getPathInfo());
2049+
$this->assertEquals($expectedUri, $request->getUri());
2050+
$this->assertEquals($queryString, $request->getQueryString());
2051+
$this->assertEquals(8080, $request->getPort());
2052+
$this->assertEquals('host:8080', $request->getHttpHost());
2053+
$this->assertEquals($expectedBaseUrl, $request->getBaseUrl());
2054+
$this->assertEquals($expectedBasePath, $request->getBasePath());
2055+
}
19952056
}
19962057

19972058
class RequestContentProxy extends Request

src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function testCheckRequestPath()
144144
// Plus must not decoded to space
145145
$this->assertTrue($utils->checkRequestPath($this->getRequest('/foo+bar'), '/foo+bar'));
146146
// Checking unicode
147-
$this->assertTrue($utils->checkRequestPath($this->getRequest(urlencode('/вход')), '/вход'));
147+
$this->assertTrue($utils->checkRequestPath($this->getRequest('/'.urlencode('вход')), '/вход'));
148148
}
149149

150150
public function testCheckRequestPathWithUrlMatcherAndResourceNotFound()

0 commit comments

Comments
 (0)