This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'security/session-ip-validator'
Fixes issues with proxy server/ip detection. Fixes zendframework/zendframework#3095 - However, a different approach is taken than that used in that pull request.
- Loading branch information
87 parents
03ab3c8
+
6bce797
+
6ad2b62
+
1e41fbd
+
a0078e6
+
9db875b
+
2879406
+
9acc850
+
eddeb7d
+
6de3f68
+
cb8f354
+
5a77057
+
33c15ff
+
9f13ebf
+
a2ad2a5
+
c0ba21c
+
94e82a8
+
459f1f9
+
e0c8424
+
5ef6a39
+
58fd018
+
65e3b0b
+
d21655a
+
1d20fd1
+
fe81bc5
+
96fadae
+
072c64d
+
d3c9780
+
9c15ae8
+
66a4342
+
85c9491
+
8ec8384
+
004366f
+
55a086d
+
ac7c7af
+
f02a226
+
00c4ac3
+
3f52720
+
5508474
+
49ed2f6
+
d562686
+
67b42b2
+
bdb1dae
+
9809630
+
7304e37
+
752a5af
+
8181c8f
+
53bdac2
+
c2f9414
+
f6341e9
+
57cde95
+
c750616
+
293054e
+
7432649
+
63f13f6
+
927c00d
+
44e0d4b
+
bc03833
+
1fd7c61
+
de0cb77
+
66e902f
+
24354dc
+
9f886a2
+
845333c
+
96e9a1e
+
52fbeb6
+
7f76d90
+
fec4cec
+
24efdcc
+
219c9ad
+
3025666
+
17d48b4
+
00f4506
+
0800032
+
fc89677
+
e89d79b
+
f329014
+
a1bfde3
+
7dec05c
+
734f9f2
+
2756031
+
408f714
+
fd53447
+
f1f6e0d
+
e193ac7
+
dad06c3
+
31a4171
commit e47995d
Showing
1 changed file
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Http | ||
*/ | ||
|
||
namespace Zend\Http\PhpEnvironment; | ||
|
||
/** | ||
* Functionality for determining client IP address. | ||
* | ||
* @category Zend | ||
* @package Zend_Http | ||
*/ | ||
class RemoteAddress | ||
{ | ||
/** | ||
* Whether to use proxy addresses or not. | ||
* | ||
* As default this setting is disabled - IP address is mostly needed to increase | ||
* security. HTTP_* are not reliable since can easily be spoofed. It can be enabled | ||
* just for more flexibility, but if user uses proxy to connect to trusted services | ||
* it's his/her own risk, only reliable field for IP address is $_SERVER['REMOTE_ADDR']. | ||
* | ||
* @var bool | ||
*/ | ||
protected $useProxy = false; | ||
|
||
/** | ||
* List of trusted proxy IP addresses | ||
* | ||
* @var array | ||
*/ | ||
protected $trustedProxies = array(); | ||
|
||
/** | ||
* HTTP header to introspect for proxies | ||
* | ||
* @var string | ||
*/ | ||
protected $proxyHeader = 'HTTP_X_FORWARDED_FOR'; | ||
|
||
|
||
/** | ||
* Changes proxy handling setting. | ||
* | ||
* This must be static method, since validators are recovered automatically | ||
* at session read, so this is the only way to switch setting. | ||
* | ||
* @param bool $useProxy Whether to check also proxied IP addresses. | ||
* @return RemoteAddress | ||
*/ | ||
public function setUseProxy($useProxy = true) | ||
{ | ||
$this->useProxy = $useProxy; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Checks proxy handling setting. | ||
* | ||
* @return bool Current setting value. | ||
*/ | ||
public function getUseProxy() | ||
{ | ||
return $this->useProxy; | ||
} | ||
|
||
/** | ||
* Set list of trusted proxy addresses | ||
* | ||
* @param array $trustedProxies | ||
* @return RemoteAddress | ||
*/ | ||
public function setTrustedProxies(array $trustedProxies) | ||
{ | ||
$this->trustedProxies = $trustedProxies; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set the header to introspect for proxy IPs | ||
* | ||
* @param string $header | ||
* @return RemoteAddress | ||
*/ | ||
public function setProxyHeader($header = 'X-Forwarded-For') | ||
{ | ||
$this->proxyHeader = $this->normalizeProxyHeader($header); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Returns client IP address. | ||
* | ||
* @return string IP address. | ||
*/ | ||
public function getIpAddress() | ||
{ | ||
$ip = $this->getIpAddressFromProxy(); | ||
if ($ip) { | ||
return $ip; | ||
} | ||
|
||
// direct IP address | ||
if (isset($_SERVER['REMOTE_ADDR'])) { | ||
return $_SERVER['REMOTE_ADDR']; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
/** | ||
* Attempt to get the IP address for a proxied client | ||
* | ||
* @return false|string | ||
*/ | ||
protected function getIpAddressFromProxy() | ||
{ | ||
if (!$this->useProxy) { | ||
return false; | ||
} | ||
|
||
$header = $this->proxyHeader; | ||
|
||
if (!isset($_SERVER[$header]) || empty($_SERVER[$header])) { | ||
return false; | ||
} | ||
|
||
// Extract IPs | ||
$ips = explode(',', $_SERVER[$header]); | ||
// trim, so we can compare against trusted proxies properly | ||
$ips = array_map('trim', $ips); | ||
// remove trusted proxy IPs | ||
$ips = array_diff($ips, $this->trustedProxies); | ||
|
||
// Any left? | ||
if (empty($ips)) { | ||
return false; | ||
} | ||
|
||
// Return right-most | ||
$ip = array_pop($ips); | ||
return $ip; | ||
} | ||
|
||
|
||
/** | ||
* Normalize a header string | ||
* | ||
* Normalizes a header string to a format that is compatible with | ||
* $_SERVER | ||
* | ||
* @param string $header | ||
* @return string | ||
*/ | ||
protected function normalizeProxyHeader($header) | ||
{ | ||
$header = strtoupper($header); | ||
$header = str_replace('-', '_', $header); | ||
if (0 !== strpos($header, 'HTTP_')) { | ||
$header = 'HTTP_' . $header; | ||
} | ||
return $header; | ||
} | ||
} |