[HttpFoundation] getClientIp is fixed.

The getClientIp now returns ip of the earliest server in a proxy chain when all the servers in the chain are trusted proxies. Before this patch the getClientIp used to return null at such condition.
Some appropriate tests are added.
This commit is contained in:
dened 2013-03-25 11:05:20 +04:00
parent d9009cb3c6
commit c4da2d9892
2 changed files with 5 additions and 1 deletions

View File

@ -677,9 +677,10 @@ class Request
$clientIps[] = $ip;
$trustedProxies = self::$trustProxy && !self::$trustedProxies ? array($ip) : self::$trustedProxies;
$ip = $clientIps[0];
$clientIps = array_diff($clientIps, $trustedProxies);
return array_pop($clientIps);
return $clientIps ? array_pop($clientIps) : $ip;
}
/**

View File

@ -660,6 +660,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
{
return array(
array('88.88.88.88', false, '88.88.88.88', null, null),
array('88.88.88.88', true, '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),
@ -668,6 +669,8 @@ class RequestTest extends \PHPUnit_Framework_TestCase
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')),
array('88.88.88.88', true, '123.45.67.89', '88.88.88.88', array('123.45.67.89', '88.88.88.88')),
array('88.88.88.88', false, '123.45.67.89', '88.88.88.88', array('123.45.67.89', '88.88.88.88')),
);
}