Skip to content

Commit

Permalink
[HttpFoundation] Removes use of parameter in Request::getClientIp fun…
Browse files Browse the repository at this point in the history
…ction.
  • Loading branch information
marc.weistroff committed Mar 7, 2012
1 parent 35d63b6 commit f718859
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -436,6 +436,17 @@ static public function trustProxyData()
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.
*
Expand Down Expand Up @@ -521,12 +532,12 @@ public function setSession(SessionInterface $session)
*
* @api
*/
public function getClientIp($proxy = false)
public function getClientIp()
{
if ($proxy) {
if (self::$trustProxy) {
if ($this->server->has('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);

return isset($clientIp[0]) ? trim($clientIp[0]) : '';
Expand Down
40 changes: 34 additions & 6 deletions tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php
Expand Up @@ -18,11 +18,6 @@

class RequestTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
Request::trustProxyData();
}

/**
* @covers Symfony\Component\HttpFoundation\Request::__construct
*/
Expand Down Expand Up @@ -472,6 +467,7 @@ public function testGetHost()
$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->startTrustingProxyData();
// X_FORWARDED_HOST.
$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');
Expand All @@ -492,6 +488,7 @@ public function testGetHost()

$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->stopTrustingProxyData();
}

/**
Expand Down Expand Up @@ -532,7 +529,7 @@ public function testGetSetMethod()
*/
public function testGetClientIp($expected, $proxy, $remoteAddr, $httpClientIp, $httpForwardedFor)
{
$request = new Request;
$request = new Request();
$this->assertEquals('', $request->getClientIp());
$this->assertEquals('', $request->getClientIp(true));

Expand All @@ -545,7 +542,13 @@ public function testGetClientIp($expected, $proxy, $remoteAddr, $httpClientIp, $
}

$request->initialize(array(), array(), array(), array(), array(), $server);
if ($proxy) {
$this->startTrustingProxyData();
}
$this->assertEquals($expected, $request->getClientIp($proxy));
if ($proxy) {
$this->stopTrustingProxyData();
}
}

public function testGetClientIpProvider()
Expand Down Expand Up @@ -665,9 +668,11 @@ public function testOverrideGlobals()

$this->assertArrayNotHasKey('HTTP_X_FORWARDED_PROTO', $_SERVER);

$this->startTrustingProxyData();
$request->headers->set('X_FORWARDED_PROTO', 'https');

$this->assertTrue($request->isSecure());
$this->stopTrustingProxyData();

$request->overrideGlobals();

Expand Down Expand Up @@ -853,8 +858,10 @@ public function testForwardedSecure()
$request->headers->set('X-Forwarded-Proto', 'https');
$request->headers->set('X-Forwarded-Port', 443);

$this->startTrustingProxyData();
$this->assertTrue($request->isSecure());
$this->assertEquals(443, $request->getPort());
$this->stopTrustingProxyData();
}

public function testHasSession()
Expand Down Expand Up @@ -909,6 +916,27 @@ public function splitHttpAcceptHeaderData()
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
Expand Down

0 comments on commit f718859

Please sign in to comment.