diff options
author | Lukas Reschke <lukas@owncloud.com> | 2014-09-03 11:04:49 +0200 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2014-09-03 11:04:49 +0200 |
commit | 77c0adb520adcb100d2ef4fd0562330b209ee9ea (patch) | |
tree | 9bae923fa05f312c033722ad2c711cec01758ab0 /lib/private/request.php | |
parent | 50b430ee7cadd6be1520d63acdac27bc06581e09 (diff) | |
parent | fe74b397a53b8a568c15d1ccf779bc8b0425b3c5 (diff) | |
download | nextcloud-server-77c0adb520adcb100d2ef4fd0562330b209ee9ea.tar.gz nextcloud-server-77c0adb520adcb100d2ef4fd0562330b209ee9ea.zip |
Merge branch 'securityutils' of https://github.com/owncloud/core into securityutils
Diffstat (limited to 'lib/private/request.php')
-rwxr-xr-x | lib/private/request.php | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/private/request.php b/lib/private/request.php index 5fd5b3a7197..b063c1f5967 100755 --- a/lib/private/request.php +++ b/lib/private/request.php @@ -16,6 +16,34 @@ class OC_Request { const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost)(:[0-9]+|)$/'; /** + * Returns the remote address, if the connection came from a trusted proxy and `forwarded_for_headers` has been configured + * then the IP address specified in this header will be returned instead. + * Do always use this instead of $_SERVER['REMOTE_ADDR'] + * @return string IP address + */ + public static function getRemoteAddress() { + $remoteAddress = $_SERVER['REMOTE_ADDR']; + $trustedProxies = \OC::$server->getConfig()->getSystemValue('trusted_proxies', array()); + + if(is_array($trustedProxies) && in_array($remoteAddress, $trustedProxies)) { + $forwardedForHeaders = \OC::$server->getConfig()->getSystemValue('forwarded_for_headers', array()); + + foreach($forwardedForHeaders as $header) { + if (array_key_exists($header, $_SERVER) === true) { + foreach (explode(',', $_SERVER[$header]) as $IP) { + $IP = trim($IP); + if (filter_var($IP, FILTER_VALIDATE_IP) !== false) { + return $IP; + } + } + } + } + } + + return $remoteAddress; + } + + /** * Check overwrite condition * @param string $type * @return bool |