Skip to content

Commit a00a3df

Browse files
committed
[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.
1 parent 437a7c2 commit a00a3df

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

Request.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,10 @@ public function getClientIp()
677677
$clientIps[] = $ip;
678678

679679
$trustedProxies = self::$trustProxy && !self::$trustedProxies ? array($ip) : self::$trustedProxies;
680+
$ip = $clientIps[0];
680681
$clientIps = array_diff($clientIps, $trustedProxies);
681682

682-
return array_pop($clientIps);
683+
return $clientIps ? array_pop($clientIps) : $ip;
683684
}
684685

685686
/**

Tests/RequestTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ public function testGetClientIpProvider()
660660
{
661661
return array(
662662
array('88.88.88.88', false, '88.88.88.88', null, null),
663+
array('88.88.88.88', true, '88.88.88.88', null, null),
663664
array('127.0.0.1', false, '127.0.0.1', null, null),
664665
array('::1', false, '::1', null, null),
665666
array('127.0.0.1', false, '127.0.0.1', '88.88.88.88', null),
@@ -668,6 +669,8 @@ public function testGetClientIpProvider()
668669
array('88.88.88.88', true, '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', null),
669670
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')),
670671
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')),
672+
array('88.88.88.88', true, '123.45.67.89', '88.88.88.88', array('123.45.67.89', '88.88.88.88')),
673+
array('88.88.88.88', false, '123.45.67.89', '88.88.88.88', array('123.45.67.89', '88.88.88.88')),
671674
);
672675
}
673676

0 commit comments

Comments
 (0)