Skip to content

Commit

Permalink
Don't trust CLIENT_IP
Browse files Browse the repository at this point in the history
The client_ip header can easily be forged. In 'safe' modes we should
only trust the remote_addr which comes from the sapi. Remove support for
http_clientaddress as I can't seem to find where this ever came from in
PHP on the http specs.
  • Loading branch information
markstory committed Mar 14, 2016
1 parent 64c8f8b commit 4ed8d35
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
16 changes: 3 additions & 13 deletions lib/Cake/Network/CakeRequest.php
Expand Up @@ -393,20 +393,10 @@ protected function _processFileData($path, $data, $field) {
public function clientIp($safe = true) {
if (!$safe && env('HTTP_X_FORWARDED_FOR')) {
$ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR'));
} elseif (!$safe && env('HTTP_CLIENT_IP')) {
$ipaddr = env('HTTP_CLIENT_IP');
} else {
if (env('HTTP_CLIENT_IP')) {
$ipaddr = env('HTTP_CLIENT_IP');
} else {
$ipaddr = env('REMOTE_ADDR');
}
}

if (env('HTTP_CLIENTADDRESS')) {
$tmpipaddr = env('HTTP_CLIENTADDRESS');

if (!empty($tmpipaddr)) {
$ipaddr = preg_replace('/(?:,.*)/', '', $tmpipaddr);
}
$ipaddr = env('REMOTE_ADDR');
}
return trim($ipaddr);
}
Expand Down
14 changes: 7 additions & 7 deletions lib/Cake/Test/Case/Network/CakeRequestTest.php
Expand Up @@ -658,18 +658,18 @@ public function testclientIp() {
$_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
$_SERVER['REMOTE_ADDR'] = '192.168.1.3';

$request = new CakeRequest('some/path');
$this->assertEquals('192.168.1.5', $request->clientIp(false));
$this->assertEquals('192.168.1.2', $request->clientIp());
$this->assertEquals('192.168.1.3', $request->clientIp(), 'Use remote_addr in safe mode');
$this->assertEquals('192.168.1.5', $request->clientIp(false), 'Use x-forwarded');

unset($_SERVER['HTTP_X_FORWARDED_FOR']);
$this->assertEquals('192.168.1.2', $request->clientIp());
$this->assertEquals('192.168.1.3', $request->clientIp(), 'safe uses remote_addr');
$this->assertEquals('192.168.1.2', $request->clientIp(false), 'unsafe reads from client_ip');

unset($_SERVER['HTTP_CLIENT_IP']);
$this->assertEquals('192.168.1.3', $request->clientIp());

$_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
$this->assertEquals('10.0.1.2', $request->clientIp());
$this->assertEquals('192.168.1.3', $request->clientIp(), 'use remote_addr');
$this->assertEquals('192.168.1.3', $request->clientIp(false), 'use remote_addr');
}

/**
Expand Down

0 comments on commit 4ed8d35

Please sign in to comment.