merged branch denvned/getClientIp-fix (PR #7472)

This PR was merged into the 2.1 branch.

Discussion
----------

[HttpFoundation] getClientIp is fixed.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | no

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.

Commits
-------

c4da2d9 [HttpFoundation] getClientIp is fixed.
This commit is contained in:
Fabien Potencier 2013-04-09 18:02:56 +02:00
commit c8bd45b2c7
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')),
);
}