Skip to content

Commit

Permalink
[HttpFoundation] changed the order of IP addresses returned by Reques…
Browse files Browse the repository at this point in the history
…t::getClientIps()
  • Loading branch information
fabpot committed Apr 20, 2013
1 parent deccb76 commit 75db8eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -661,6 +661,12 @@ public function setSession(SessionInterface $session)
/**
* Returns the client IP addresses.
*
* The most trusted IP address is first, and the less trusted one last.
* The "real" client IP address is the last one, but this is also the
* less trusted one.
*
* Use this method carefully; you should use getClientIp() instead.
*
* @return array The client IP addresses
*
* @see getClientIp()
Expand All @@ -684,7 +690,7 @@ public function getClientIps()
$ip = $clientIps[0];
$clientIps = array_values(array_diff($clientIps, $trustedProxies));

return $clientIps ? $clientIps : array($ip);
return $clientIps ? array_reverse($clientIps) : array($ip);
}

/**
Expand All @@ -711,7 +717,7 @@ public function getClientIp()
{
$ipAddresses = $this->getClientIps();

return array_pop($ipAddresses);
return $ipAddresses[0];
}

/**
Expand Down
16 changes: 10 additions & 6 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Expand Up @@ -744,7 +744,7 @@ public function testGetClientIp($expected, $remoteAddr, $httpForwardedFor, $trus
{
$request = $this->getRequestInstanceForClientIpTests($remoteAddr, $httpForwardedFor, $trustedProxies);

$this->assertEquals(array_pop($expected), $request->getClientIp());
$this->assertEquals($expected[0], $request->getClientIp());

Request::setTrustedProxies(array());
}
Expand Down Expand Up @@ -779,25 +779,29 @@ public function testGetClientIpsProvider()
array(array('127.0.0.1'), '127.0.0.1', '88.88.88.88', null),
// forwarded for with remote IPv4 addr trusted
array(array('88.88.88.88'), '127.0.0.1', '88.88.88.88', array('127.0.0.1')),
// forwarded for with remote IPv4 and all FF addrs trusted
array(array('88.88.88.88'), '127.0.0.1', '88.88.88.88', array('127.0.0.1', '88.88.88.88')),

// forwarded for with remote IPv6 addr not trusted
array(array('1620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '2620:0:1cfe:face:b00c::3', null),
// forwarded for with remote IPv6 addr trusted
array(array('2620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3')),

// multiple forwarded for with remote IPv4 addr trusted
array(array('127.0.0.1', '87.65.43.21', '88.88.88.88'), '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89')),
array(array('88.88.88.88', '87.65.43.21', '127.0.0.1'), '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89')),
// multiple forwarded for with remote IPv4 addr and some reverse proxies trusted
array(array('127.0.0.1', '87.65.43.21'), '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(array('87.65.43.21', '127.0.0.1'), '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')),
// multiple forwarded for with remote IPv4 addr and some reverse proxies trusted but in the middle
array(array('127.0.0.1', '88.88.88.88'), '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89', '87.65.43.21')),
array(array('88.88.88.88', '127.0.0.1'), '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89', '87.65.43.21')),
// multiple forwarded for with remote IPv4 addr and all reverse proxies trusted
array(array('127.0.0.1'), '123.45.67.89', '127.0.0.1, 87.65.43.21, 88.88.88.88', array('123.45.67.89', '87.65.43.21', '88.88.88.88', '127.0.0.1')),

// multiple forwarded for with remote IPv6 addr trusted
array(array('3620:0:1cfe:face:b00c::3', '2620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3')),
array(array('2620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3')),
// multiple forwarded for with remote IPv6 addr and some reverse proxies trusted
array(array('3620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3', '2620:0:1cfe:face:b00c::3')),
// multiple forwarded for with remote IPv4 addr and some reverse proxies trusted but in the middle
array(array('4620:0:1cfe:face:b00c::3', '2620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '4620:0:1cfe:face:b00c::3,3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3')),
array(array('2620:0:1cfe:face:b00c::3', '4620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '4620:0:1cfe:face:b00c::3,3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3')),
);
}

Expand Down

0 comments on commit 75db8eb

Please sign in to comment.