aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/request.php
diff options
context:
space:
mode:
authorLukas Reschke <lukas@owncloud.com>2014-08-31 15:21:09 +0200
committerLukas Reschke <lukas@owncloud.com>2014-08-31 15:21:09 +0200
commitae3425d2dadbac69e7dcb0ba577db20e63e7d91f (patch)
treef483759739cd951c33ef1c61ade8694f9e1c95e3 /lib/private/request.php
parent3329e0f2b22207a24ddb4953bbf11964b23682d9 (diff)
parent73685892ed6f255a916512863cd5549914d071e1 (diff)
downloadnextcloud-server-ae3425d2dadbac69e7dcb0ba577db20e63e7d91f.tar.gz
nextcloud-server-ae3425d2dadbac69e7dcb0ba577db20e63e7d91f.zip
Merge branch 'master' into securityutils
Conflicts: lib/private/util.php
Diffstat (limited to 'lib/private/request.php')
-rwxr-xr-xlib/private/request.php28
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