]> source.dussan.org Git - nextcloud-server.git/commitdiff
Trim port from domain
authorLukas Reschke <lukas@owncloud.com>
Wed, 3 Dec 2014 18:54:48 +0000 (19:54 +0100)
committerLukas Reschke <lukas@owncloud.com>
Wed, 3 Dec 2014 18:59:50 +0000 (19:59 +0100)
Depending on the used environment the port might be appended to the host header resulting in an inaccessible instance when initially setting up on a system with a different HTTP or HTTPS port. (for example test:500)

To test this setup ownCloud under a different port with and without this patch. (heads-up: localhost is always white-listed, so use a different domain)

lib/private/request.php
lib/private/setup.php
tests/lib/request.php

index d079dc110d1f9dab1f9477fc4cacf23624f3ce03..794b566ce58aaf696e004eac777fa5200caba149 100644 (file)
@@ -65,6 +65,22 @@ class OC_Request {
                        or ($type !== 'protocol' and OC_Config::getValue('forcessl', false));
        }
 
+       /**
+        * Strips a potential port from a domain (in format domain:port)
+        * @param $host
+        * @return string $host without appended port
+        */
+       public static function getDomainWithoutPort($host) {
+               $pos = strrpos($host, ':');
+               if ($pos !== false) {
+                       $port = substr($host, $pos + 1);
+                       if (is_numeric($port)) {
+                               $host = substr($host, 0, $pos);
+                       }
+               }
+               return $host;
+       }
+
        /**
         * Checks whether a domain is considered as trusted from the list
         * of trusted domains. If no trusted domains have been configured, returns
@@ -76,13 +92,7 @@ class OC_Request {
         */
        public static function isTrustedDomain($domain) {
                // Extract port from domain if needed
-               $pos = strrpos($domain, ':');
-               if ($pos !== false) {
-                       $port = substr($domain, $pos + 1);
-                       if (is_numeric($port)) {
-                               $domain = substr($domain, 0, $pos);
-                       }
-               }
+               $domain = self::getDomainWithoutPort($domain);
 
                // FIXME: Empty config array defaults to true for now. - Deprecate this behaviour with ownCloud 8.
                $trustedList = \OC::$server->getConfig()->getSystemValue('trusted_domains', array());
index 1443de185469f6cbed954543ea8bb565b6bfb506..e5eb2bac1945e402db32b2818c5af6db0b797815 100644 (file)
@@ -162,7 +162,7 @@ class OC_Setup {
                    && is_array($options['trusted_domains'])) {
                        $trustedDomains = $options['trusted_domains'];
                } else {
-                       $trustedDomains = array(OC_Request::serverHost());
+                       $trustedDomains = array(\OC_Request::getDomainWithoutPort(\OC_Request::serverHost()));
                }
 
                if (OC_Util::runningOnWindows()) {
index 254048723e75a971aa5516cd27319b7bf6814ade..3b70ed0ba2503470f2ae0d9edd9a911a4d5d4498 100644 (file)
@@ -228,6 +228,22 @@ class Test_Request extends \Test\TestCase {
                OC_Config::deleteKey('overwritehost');
        }
 
+       public function hostWithPortProvider() {
+               return array(
+                       array('localhost:500', 'localhost'),
+                       array('foo.com', 'foo.com'),
+                       array('[1fff:0:a88:85a3::ac1f]:801', '[1fff:0:a88:85a3::ac1f]')
+               );
+       }
+
+       /**
+        * @dataProvider hostWithPortProvider
+        */
+       public function testGetDomainWithoutPort($hostWithPort, $host) {
+               $this->assertEquals($host, OC_Request::getDomainWithoutPort($hostWithPort));
+
+       }
+
        /**
         * @dataProvider trustedDomainDataProvider
         */