Skip to content

Commit

Permalink
HTTP_X_FORWARDED_FOR can be spoofed, proxies append to the list, so u…
Browse files Browse the repository at this point in the history
…se last ip
  • Loading branch information
ceeram committed Aug 24, 2017
1 parent 91274dd commit 42c2d48
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Http/ServerRequest.php
Expand Up @@ -550,7 +550,8 @@ public function session(Session $session = null)
public function clientIp()
{
if ($this->trustProxy && $this->getEnv('HTTP_X_FORWARDED_FOR')) {
$ipaddr = preg_replace('/(?:,.*)/', '', $this->getEnv('HTTP_X_FORWARDED_FOR'));
$addresses = explode(',', $this->getEnv('HTTP_X_FORWARDED_FOR'));
$ipaddr = end($addresses);
} elseif ($this->trustProxy && $this->getEnv('HTTP_CLIENT_IP')) {
$ipaddr = $this->getEnv('HTTP_CLIENT_IP');
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Http/ServerRequestTest.php
Expand Up @@ -688,13 +688,13 @@ public function testDefaultEnvValue()
public function testClientIp()
{
$request = new ServerRequest(['environment' => [
'HTTP_X_FORWARDED_FOR' => '192.168.1.5, 10.0.1.1, proxy.com',
'HTTP_X_FORWARDED_FOR' => '192.168.1.5, 10.0.1.1, proxy.com, real.ip',
'HTTP_CLIENT_IP' => '192.168.1.2',
'REMOTE_ADDR' => '192.168.1.3'
]]);

$request->trustProxy = true;
$this->assertEquals('192.168.1.5', $request->clientIp());
$this->assertEquals('real.ip', $request->clientIp());

$request->env('HTTP_X_FORWARDED_FOR', '');
$this->assertEquals('192.168.1.2', $request->clientIp());
Expand Down

0 comments on commit 42c2d48

Please sign in to comment.