[HttpFoundation] changed the order of IP addresses returned by Request::getClientIps()

This commit is contained in:
Fabien Potencier 2013-04-20 17:09:51 +02:00
parent deccb7651d
commit 75db8eb8a7
2 changed files with 18 additions and 8 deletions

View File

@ -661,6 +661,12 @@ class Request
/**
* 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()
@ -684,7 +690,7 @@ class Request
$ip = $clientIps[0];
$clientIps = array_values(array_diff($clientIps, $trustedProxies));
return $clientIps ? $clientIps : array($ip);
return $clientIps ? array_reverse($clientIps) : array($ip);
}
/**
@ -711,7 +717,7 @@ class Request
{
$ipAddresses = $this->getClientIps();
return array_pop($ipAddresses);
return $ipAddresses[0];
}
/**

View File

@ -744,7 +744,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
{
$request = $this->getRequestInstanceForClientIpTests($remoteAddr, $httpForwardedFor, $trustedProxies);
$this->assertEquals(array_pop($expected), $request->getClientIp());
$this->assertEquals($expected[0], $request->getClientIp());
Request::setTrustedProxies(array());
}
@ -779,6 +779,8 @@ class RequestTest extends \PHPUnit_Framework_TestCase
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),
@ -786,18 +788,20 @@ class RequestTest extends \PHPUnit_Framework_TestCase
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')),
);
}