[HttpFoundation] Removes use of parameter in Request::getClientIp function.

This commit is contained in:
marc.weistroff 2012-03-07 16:11:42 +01:00
parent 35d63b6691
commit f7188598a3
2 changed files with 48 additions and 9 deletions

View File

@ -436,6 +436,17 @@ class Request
self::$trustProxy = true; self::$trustProxy = true;
} }
/**
* Returns true if $_SERVER entries coming from proxies are trusted,
* false otherwise.
*
* @return boolean
*/
static public function isProxyTrusted()
{
return self::$trustProxy;
}
/** /**
* Gets a "parameter" value. * Gets a "parameter" value.
* *
@ -521,12 +532,12 @@ class Request
* *
* @api * @api
*/ */
public function getClientIp($proxy = false) public function getClientIp()
{ {
if ($proxy) { if (self::$trustProxy) {
if ($this->server->has('HTTP_CLIENT_IP')) { if ($this->server->has('HTTP_CLIENT_IP')) {
return $this->server->get('HTTP_CLIENT_IP'); return $this->server->get('HTTP_CLIENT_IP');
} elseif (self::$trustProxy && $this->server->has('HTTP_X_FORWARDED_FOR')) { } elseif ($this->server->has('HTTP_X_FORWARDED_FOR')) {
$clientIp = explode(',', $this->server->get('HTTP_X_FORWARDED_FOR'), 2); $clientIp = explode(',', $this->server->get('HTTP_X_FORWARDED_FOR'), 2);
return isset($clientIp[0]) ? trim($clientIp[0]) : ''; return isset($clientIp[0]) ? trim($clientIp[0]) : '';

View File

@ -18,11 +18,6 @@ use Symfony\Component\HttpFoundation\Request;
class RequestTest extends \PHPUnit_Framework_TestCase class RequestTest extends \PHPUnit_Framework_TestCase
{ {
public function setUp()
{
Request::trustProxyData();
}
/** /**
* @covers Symfony\Component\HttpFoundation\Request::__construct * @covers Symfony\Component\HttpFoundation\Request::__construct
*/ */
@ -472,6 +467,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com')); $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com'));
$this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from server name'); $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from server name');
$this->startTrustingProxyData();
// X_FORWARDED_HOST. // X_FORWARDED_HOST.
$request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com')); $request->initialize(array(), array(), array(), array(), array(), array('HTTP_X_FORWARDED_HOST' => 'www.exemple.com'));
$this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from X_FORWARDED_HOST'); $this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from X_FORWARDED_HOST');
@ -492,6 +488,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_HOST' => 'www.host.com')); $request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com', 'HTTP_HOST' => 'www.host.com'));
$this->assertEquals('www.host.com', $request->getHost(), '->getHost() value from Host header has priority over SERVER_NAME '); $this->assertEquals('www.host.com', $request->getHost(), '->getHost() value from Host header has priority over SERVER_NAME ');
$this->stopTrustingProxyData();
} }
/** /**
@ -532,7 +529,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetClientIp($expected, $proxy, $remoteAddr, $httpClientIp, $httpForwardedFor) public function testGetClientIp($expected, $proxy, $remoteAddr, $httpClientIp, $httpForwardedFor)
{ {
$request = new Request; $request = new Request();
$this->assertEquals('', $request->getClientIp()); $this->assertEquals('', $request->getClientIp());
$this->assertEquals('', $request->getClientIp(true)); $this->assertEquals('', $request->getClientIp(true));
@ -545,7 +542,13 @@ class RequestTest extends \PHPUnit_Framework_TestCase
} }
$request->initialize(array(), array(), array(), array(), array(), $server); $request->initialize(array(), array(), array(), array(), array(), $server);
if ($proxy) {
$this->startTrustingProxyData();
}
$this->assertEquals($expected, $request->getClientIp($proxy)); $this->assertEquals($expected, $request->getClientIp($proxy));
if ($proxy) {
$this->stopTrustingProxyData();
}
} }
public function testGetClientIpProvider() public function testGetClientIpProvider()
@ -665,9 +668,11 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertArrayNotHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER); $this->assertArrayNotHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);
$this->startTrustingProxyData();
$request->headers->set('X_FORWARDED_PROTO', 'https'); $request->headers->set('X_FORWARDED_PROTO', 'https');
$this->assertTrue($request->isSecure()); $this->assertTrue($request->isSecure());
$this->stopTrustingProxyData();
$request->overrideGlobals(); $request->overrideGlobals();
@ -853,8 +858,10 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$request->headers->set('X-Forwarded-Proto', 'https'); $request->headers->set('X-Forwarded-Proto', 'https');
$request->headers->set('X-Forwarded-Port', 443); $request->headers->set('X-Forwarded-Port', 443);
$this->startTrustingProxyData();
$this->assertTrue($request->isSecure()); $this->assertTrue($request->isSecure());
$this->assertEquals(443, $request->getPort()); $this->assertEquals(443, $request->getPort());
$this->stopTrustingProxyData();
} }
public function testHasSession() public function testHasSession()
@ -909,6 +916,27 @@ class RequestTest extends \PHPUnit_Framework_TestCase
array('text/html,application/xhtml+xml', array('application/xhtml+xml' => 1, 'text/html' => 1)), array('text/html,application/xhtml+xml', array('application/xhtml+xml' => 1, 'text/html' => 1)),
); );
} }
public function testIsProxyTrusted()
{
$this->startTrustingProxyData();
$this->assertTrue(Request::isProxyTrusted());
$this->stopTrustingProxyData();
$this->assertFalse(Request::isProxyTrusted());
}
private function startTrustingProxyData()
{
Request::trustProxyData();
}
private function stopTrustingProxyData()
{
$class = new \ReflectionClass('Symfony\\Component\\HttpFoundation\\Request');
$property = $class->getProperty('trustProxy');
$property->setAccessible(true);
$property->setValue(false);
}
} }
class RequestContentProxy extends Request class RequestContentProxy extends Request