Skip to content

Commit 9077baf

Browse files
committed
Merge branch '2.8' into 3.2
* 2.8: Fixed pathinfo calculation for requests starting with a question mark. [Security] simplify the SwitchUserListenerTest
2 parents 6fb9ca6 + 4665cb9 commit 9077baf

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

Request.php

+7-1
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)) {

Tests/RequestTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,12 @@ public function testGetPathInfo()
12811281
$request->initialize(array(), array(), array(), array(), array(), $server);
12821282

12831283
$this->assertEquals('/path%20test/info', $request->getPathInfo());
1284+
1285+
$server = array();
1286+
$server['REQUEST_URI'] = '?a=b';
1287+
$request->initialize(array(), array(), array(), array(), array(), $server);
1288+
1289+
$this->assertEquals('/', $request->getPathInfo());
12841290
}
12851291

12861292
public function testGetParameterPrecedence()
@@ -2110,6 +2116,61 @@ public function methodCacheableProvider()
21102116
array('CONNECT', false),
21112117
);
21122118
}
2119+
2120+
public function nonstandardRequestsData()
2121+
{
2122+
return array(
2123+
array('', '', '/', 'http://host:8080/', ''),
2124+
array('/', '', '/', 'http://host:8080/', ''),
2125+
2126+
array('hello/app.php/x', '', '/x', 'http://host:8080/hello/app.php/x', '/hello', '/hello/app.php'),
2127+
array('/hello/app.php/x', '', '/x', 'http://host:8080/hello/app.php/x', '/hello', '/hello/app.php'),
2128+
2129+
array('', 'a=b', '/', 'http://host:8080/?a=b'),
2130+
array('?a=b', 'a=b', '/', 'http://host:8080/?a=b'),
2131+
array('/?a=b', 'a=b', '/', 'http://host:8080/?a=b'),
2132+
2133+
array('x', 'a=b', '/x', 'http://host:8080/x?a=b'),
2134+
array('x?a=b', 'a=b', '/x', 'http://host:8080/x?a=b'),
2135+
array('/x?a=b', 'a=b', '/x', 'http://host:8080/x?a=b'),
2136+
2137+
array('hello/x', '', '/x', 'http://host:8080/hello/x', '/hello'),
2138+
array('/hello/x', '', '/x', 'http://host:8080/hello/x', '/hello'),
2139+
2140+
array('hello/app.php/x', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
2141+
array('hello/app.php/x?a=b', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
2142+
array('/hello/app.php/x?a=b', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
2143+
);
2144+
}
2145+
2146+
/**
2147+
* @dataProvider nonstandardRequestsData
2148+
*/
2149+
public function testNonstandardRequests($requestUri, $queryString, $expectedPathInfo, $expectedUri, $expectedBasePath = '', $expectedBaseUrl = null)
2150+
{
2151+
if (null === $expectedBaseUrl) {
2152+
$expectedBaseUrl = $expectedBasePath;
2153+
}
2154+
2155+
$server = array(
2156+
'HTTP_HOST' => 'host:8080',
2157+
'SERVER_PORT' => '8080',
2158+
'QUERY_STRING' => $queryString,
2159+
'PHP_SELF' => '/hello/app.php',
2160+
'SCRIPT_FILENAME' => '/some/path/app.php',
2161+
'REQUEST_URI' => $requestUri,
2162+
);
2163+
2164+
$request = new Request(array(), array(), array(), array(), array(), $server);
2165+
2166+
$this->assertEquals($expectedPathInfo, $request->getPathInfo());
2167+
$this->assertEquals($expectedUri, $request->getUri());
2168+
$this->assertEquals($queryString, $request->getQueryString());
2169+
$this->assertEquals(8080, $request->getPort());
2170+
$this->assertEquals('host:8080', $request->getHttpHost());
2171+
$this->assertEquals($expectedBaseUrl, $request->getBaseUrl());
2172+
$this->assertEquals($expectedBasePath, $request->getBasePath());
2173+
}
21132174
}
21142175

21152176
class RequestContentProxy extends Request

0 commit comments

Comments
 (0)