diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2016-01-11 05:56:12 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2016-01-11 06:43:58 +0300 |
commit | ae71bc82304f7d25490acce6f1e55a6d97a37206 (patch) | |
tree | 2306039989ae6d5b6b9faee4a00b9bf6de0b78f0 | |
parent | 41b743af65b1b67c49022790f9b1afae79192e3f (diff) |
Do not overwrite piwikUrl when host is localhost
-rw-r--r-- | core/Http.php | 3 | ||||
-rw-r--r-- | core/Mail.php | 2 | ||||
-rw-r--r-- | core/SettingsPiwik.php | 5 | ||||
-rw-r--r-- | core/Url.php | 16 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/UrlTest.php | 46 |
5 files changed, 45 insertions, 27 deletions
diff --git a/core/Http.php b/core/Http.php index b5533262e4..bf3ecc17d2 100644 --- a/core/Http.php +++ b/core/Http.php @@ -839,9 +839,8 @@ class Http private static function getProxyConfiguration($url) { $hostname = UrlHelper::getHostFromUrl($url); - $localHostnames = Url::getLocalHostnames(); - if(in_array($hostname, $localHostnames)) { + if (Url::isLocalHost($hostname)) { return array(null, null, null, null); } diff --git a/core/Mail.php b/core/Mail.php index 22843b5a76..454fcd377c 100644 --- a/core/Mail.php +++ b/core/Mail.php @@ -152,6 +152,6 @@ class Mail extends Zend_Mail */ protected function isHostDefinedAndNotLocal($url) { - return isset($url['host']) && !in_array($url['host'], Url::getLocalHostnames(), true); + return isset($url['host']) && !Url::isLocalHost($url['host']); } } diff --git a/core/SettingsPiwik.php b/core/SettingsPiwik.php index 7a50263e39..b0df27b2b6 100644 --- a/core/SettingsPiwik.php +++ b/core/SettingsPiwik.php @@ -186,7 +186,10 @@ class SettingsPiwik // if URL changes, always update the cache || $currentUrl != $url ) { - if (strlen($currentUrl) >= strlen('http://a/')) { + $host = Url::getHostFromUrl($url); + + if (strlen($currentUrl) >= strlen('http://a/') + && !Url::isLocalHost($host)) { self::overwritePiwikUrl($currentUrl); } $url = $currentUrl; diff --git a/core/Url.php b/core/Url.php index a86978286a..2a301a9b46 100644 --- a/core/Url.php +++ b/core/Url.php @@ -551,6 +551,21 @@ class Url && in_array($parsedUrl['scheme'], array('http', 'https')); } + /** + * Checks whether the given host is a local host like `127.0.0.1` or `localhost`. + * + * @param string $host + * @return bool + */ + public static function isLocalHost($host) + { + if (empty($host)) { + return false; + } + + return in_array($host, Url::getLocalHostnames(), true); + } + public static function getTrustedHostsFromConfig() { $hosts = self::getHostsFromConfig('General', 'trusted_hosts'); @@ -677,7 +692,6 @@ class Url return array('localhost', '127.0.0.1', '::1', '[::1]'); } - /** * @return bool */ diff --git a/tests/PHPUnit/Unit/UrlTest.php b/tests/PHPUnit/Unit/UrlTest.php index 3153e43acc..6f9c50d614 100644 --- a/tests/PHPUnit/Unit/UrlTest.php +++ b/tests/PHPUnit/Unit/UrlTest.php @@ -13,12 +13,10 @@ use Piwik\Url; /** * @backupGlobals enabled + * @group Core */ class UrlTest extends \PHPUnit_Framework_TestCase { - /** - * @group Core - */ public function testAllMethods() { $this->assertEquals(Url::getCurrentQueryStringWithParametersModified(array()), Url::getCurrentQueryString()); @@ -63,7 +61,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getCurrentHosts - * @group Core */ public function testGetCurrentHost($description, $test) { @@ -132,8 +129,30 @@ class UrlTest extends \PHPUnit_Framework_TestCase } /** + * @dataProvider getIsLocalHost + */ + public function test_isLocalHost($expectedIsLocal, $host) + { + $this->assertSame($expectedIsLocal, Url::isLocalHost($host)); + } + + public function getIsLocalHost() + { + return array( + array($isLocal = false, '127.0.0.2'), + array($isLocal = false, '192.168.1.1'), + array($isLocal = false, '10.1.1.1'), + array($isLocal = false, '172.30.1.1'), + + array($isLocal = true, 'localhost'), + array($isLocal = true, '127.0.0.1'), + array($isLocal = true, '::1'), + array($isLocal = true, '[::1]'), + ); + } + + /** * @dataProvider getLocalUrls - * @group Core */ public function testIsLocalUrl($httphost, $scripturi, $requesturi, $testurl, $result) { @@ -161,7 +180,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getCurrentUrlWithoutFilename - * @group Core */ public function testGetCurrentUrlWithoutFilename($expected, $https, $host, $path) { @@ -182,9 +200,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $url); } - /** - * @group Core - */ public function test_getCurrentScriptName() { $this->resetGlobalVariables(); @@ -238,7 +253,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getValidHostData - * @group Core */ public function testIsValidHost($expected, $host, $trustedHosts, $description) { @@ -247,9 +261,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, Url::isValidHost($host), $description); } - /** - * @group Core - */ public function testGetReferrer() { $_SERVER['HTTP_REFERER'] = 'http://www.piwik.org'; @@ -257,8 +268,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase } /** - * @group Core - * * @dataProvider getQueryParameters */ public function testGetQueryStringFromParameters($params, $queryString) @@ -281,8 +290,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase } /** - * @group Core - * * @dataProvider getHostsFromUrl */ public function testGetHostsFromUrl($url, $expectedHost) @@ -304,8 +311,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase } /** - * @group Core - * * @dataProvider getIsHostInUrls */ public function testIsHostInUrlsl($isHost, $host, $urls) @@ -348,7 +353,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase } /** - * @group Core * @dataProvider urlProvider */ public function testGetCurrentUrl($url, $pathInfo = null) @@ -372,7 +376,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase } /** - * @group Core * @dataProvider urlWithoutQueryStringProvider */ public function testGetCurrentUrlWithoutQueryString($url, $expected, $pathInfo = null) @@ -387,7 +390,6 @@ class UrlTest extends \PHPUnit_Framework_TestCase * Tests a use case that was reported by some users: Nginx is not properly configured and passes * incorrect PATH_INFO values in $_SERVER. * @link https://github.com/piwik/piwik/issues/6491 - * @group Core */ public function testMisconfiguredNginxPathInfo() { |