Skip to content

Commit

Permalink
fixed algorithm used to determine the trusted client IP
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Nov 29, 2012
1 parent 254b110 commit b45873a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 35 deletions.
64 changes: 48 additions & 16 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -20,7 +20,9 @@
*/
class Request
{
protected static $trustProxy = false;
protected static $trustProxyData = false;

protected static $trustedProxies = array();

/**
* @var \Symfony\Component\HttpFoundation\ParameterBag
Expand Down Expand Up @@ -357,14 +359,26 @@ public function overrideGlobals()
/**
* Trusts $_SERVER entries coming from proxies.
*
* You should only call this method if your application
* is hosted behind a reverse proxy that you manage.
* @deprecated Deprecated since version 2.0, to be removed in 2.3. Use setTrustedProxies instead.

This comment has been minimized.

Copy link
@colinfrei

colinfrei Dec 3, 2012

Contributor

"Deprecated since version 2.0" seems wrong, shouldn't that be "since version 2.2"?

This comment has been minimized.

Copy link
@stof

stof Dec 3, 2012

Member

actually, it should be 2.0.19

*/
public static function trustProxyData()
{
self::$trustProxyData = true;
}

/**
* Sets a list of trusted proxies.
*
* You should only list the reverse proxies that you manage directly.
*
* @param array $proxies A list of trusted proxies
*
* @api
*/
public static function trustProxyData()
public static function setTrustedProxies(array $proxies)
{
self::$trustProxy = true;
self::$trustedProxies = $proxies;
self::$trustProxyData = $proxies ? true : false;
}

/**
Expand Down Expand Up @@ -441,23 +455,41 @@ public function setSession(Session $session)
/**
* Returns the client IP address.
*
* @param Boolean $proxy Whether the current request has been made behind a proxy or not
* This method can read the client IP address from the "X-Forwarded-For" header
* when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-For"
* header value is a comma+space separated list of IP addresses, the left-most
* being the original client, and each successive proxy that passed the request
* adding the IP address where it received the request from.
*
* @param Boolean $proxy Whether the current request has been made behind a proxy or not (deprecated)
*
* @return string The client IP address
*
* @see http://en.wikipedia.org/wiki/X-Forwarded-For
*
* @deprecated The proxy argument is deprecated since version 2.0 and will be removed in 2.3. Use setTrustedProxies instead.
*
* @api
*/
public function getClientIp($proxy = false)
{
if ($proxy) {
if (self::$trustProxy && $this->server->has('HTTP_X_FORWARDED_FOR')) {
$clientIp = explode(',', $this->server->get('HTTP_X_FORWARDED_FOR'), 2);
$ip = $this->server->get('REMOTE_ADDR');

return isset($clientIp[0]) ? trim($clientIp[0]) : '';
}
if (!$proxy && !self::$trustProxyData) {
return $ip;
}

return $this->server->get('REMOTE_ADDR');
if (!$this->server->has('HTTP_X_FORWARDED_FOR')) {
return $ip;
}

$clientIps = array_map('trim', explode(',', $this->server->get('HTTP_X_FORWARDED_FOR')));
$clientIps[] = $ip;

$trustedProxies = ($proxy || self::$trustProxyData) && !self::$trustedProxies ? array($ip) : self::$trustedProxies;
$clientIps = array_diff($clientIps, $trustedProxies);

return array_pop($clientIps);
}

/**
Expand Down Expand Up @@ -560,7 +592,7 @@ public function getScheme()
*/
public function getPort()
{
if (self::$trustProxy && $this->headers->has('X-Forwarded-Port')) {
if (self::$trustProxyData && $this->headers->has('X-Forwarded-Port')) {
return $this->headers->get('X-Forwarded-Port');
}

Expand Down Expand Up @@ -683,9 +715,9 @@ public function isSecure()
return (
(strtolower($this->server->get('HTTPS')) == 'on' || $this->server->get('HTTPS') == 1)
||
(self::$trustProxy && strtolower($this->headers->get('SSL_HTTPS')) == 'on' || $this->headers->get('SSL_HTTPS') == 1)
(self::$trustProxyData && strtolower($this->headers->get('SSL_HTTPS')) == 'on' || $this->headers->get('SSL_HTTPS') == 1)
||
(self::$trustProxy && strtolower($this->headers->get('X_FORWARDED_PROTO')) == 'https')
(self::$trustProxyData && strtolower($this->headers->get('X_FORWARDED_PROTO')) == 'https')
);
}

Expand All @@ -698,7 +730,7 @@ public function isSecure()
*/
public function getHost()
{
if (self::$trustProxy && $host = $this->headers->get('X_FORWARDED_HOST')) {
if (self::$trustProxyData && $host = $this->headers->get('X_FORWARDED_HOST')) {
$elements = explode(',', $host);

$host = trim($elements[count($elements) - 1]);
Expand Down
41 changes: 22 additions & 19 deletions tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php
Expand Up @@ -19,11 +19,6 @@

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

/**
* @covers Symfony\Component\HttpFoundation\Request::__construct
*/
Expand Down Expand Up @@ -430,15 +425,17 @@ public function testGetHost()
$request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com'));
$this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header');

// Host header with port number.
// Host header with port number
$request->initialize(array(), array(), array(), array(), array(), array('HTTP_HOST' => 'www.exemple.com:8080'));
$this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from Host Header with port number');

// Server values.
// Server values
$request->initialize(array(), array(), array(), array(), array(), array('SERVER_NAME' => 'www.exemple.com'));
$this->assertEquals('www.exemple.com', $request->getHost(), '->getHost() from server name');

// X_FORWARDED_HOST.
Request::setTrustedProxies(array('1.1.1.1'));

// 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 @@ -458,6 +455,8 @@ 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 ');

Request::setTrustedProxies(array());
}

/**
Expand Down Expand Up @@ -496,31 +495,35 @@ public function testGetSetMethod()
/**
* @dataProvider testGetClientIpProvider
*/
public function testGetClientIp($expected, $proxy, $remoteAddr, $httpForwardedFor)
public function testGetClientIp($expected, $proxy, $remoteAddr, $httpForwardedFor, $trustedProxies)
{
$request = new Request;
$this->assertEquals('', $request->getClientIp());
$this->assertEquals('', $request->getClientIp(true));
$request = new Request();

$server = array('REMOTE_ADDR' => $remoteAddr);
if (null !== $httpForwardedFor) {
$server['HTTP_X_FORWARDED_FOR'] = $httpForwardedFor;
}

if ($proxy || $trustedProxies) {
Request::setTrustedProxies(null === $trustedProxies ? array($remoteAddr) : $trustedProxies);
}

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

public function testGetClientIpProvider()
{
return array(
array('88.88.88.88', false, '88.88.88.88', null),
array('127.0.0.1', false, '127.0.0.1', null),
array('127.0.0.1', false, '127.0.0.1', '88.88.88.88'),
array('88.88.88.88', true, '127.0.0.1', '88.88.88.88'),
array('::1', false, '::1', null),
array('2620:0:1cfe:face:b00c::3', true, '::1', '2620:0:1cfe:face:b00c::3, ::1'),
array('88.88.88.88', true, '123.45.67.89', '88.88.88.88, 87.65.43.21, 127.0.0.1'),
array('88.88.88.88', false, '88.88.88.88', null, null),
array('127.0.0.1', false, '127.0.0.1', null, null),
array('::1', false, '::1', null, null),
array('127.0.0.1', false, '127.0.0.1', '88.88.88.88', null),
array('88.88.88.88', true, '127.0.0.1', '88.88.88.88', null),
array('2620:0:1cfe:face:b00c::3', true, '::1', '2620:0:1cfe:face:b00c::3', null),
array('88.88.88.88', true, '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', null),
array('87.65.43.21', true, '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89', '88.88.88.88')),
array('87.65.43.21', false, '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89', '88.88.88.88')),
);
}

Expand Down

0 comments on commit b45873a

Please sign in to comment.