From d227a82056def4b9a59aaee217f3721183e87b0e Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 27 May 2019 11:58:31 +0300 Subject: [PATCH 1/3] Fixed regresssion issue of `Utils::Url()` not returning `false` on failure #2524 --- CHANGELOG.md | 1 + system/src/Grav/Common/Utils.php | 31 +++++++++++++++++-------- tests/unit/Grav/Common/UtilsTest.php | 34 ++++++++++++++++++++++------ 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 877e073f3..319db5032 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Fixed bitwise operator in `TwigExtension::exifFunc()` [#2518](https://github.com/getgrav/grav/issues/2518) * Fixed issue with lang prefix incorrectly identifying as admin [#2511](https://github.com/getgrav/grav/issues/2511) * Fixed issue with `U0ils::pathPrefixedBYLanguageCode()` and trailing slash [#2510](https://github.com/getgrav/grav/issues/2511) + * Fixed regresssion issue of `Utils::Url()` not returning `false` on failure. Added new optional `fail_gracefully` 3rd attribute to return string that caused failure [#2524](https://github.com/getgrav/grav/issues/2524) # v1.6.9 ## 05/09/2019 diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 32da72b81..a19373585 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -30,19 +30,24 @@ abstract class Utils * * @param string $input * @param bool $domain + * @param bool $fail_gracefully * @return bool|null|string */ - public static function url($input, $domain = false) + public static function url($input, $domain = false, $fail_gracefully = false) { - if (!trim((string)$input)) { - $input = '/'; + if ((!is_string($input) && !method_exists($input, '__toString')) || !trim($input)) { + if ($fail_gracefully) { + $input = '/'; + } else { + return false; + } } if (Grav::instance()['config']->get('system.absolute_urls', false)) { $domain = true; } - if (Grav::instance()['uri']->isExternal($input)) { + if (Uri::isExternal($input)) { return $input; } @@ -57,13 +62,20 @@ abstract class Utils if (Utils::contains((string)$input, '://')) { /** @var UniformResourceLocator $locator */ $locator = Grav::instance()['locator']; - $parts = Uri::parseUrl($input); if ($parts) { - $resource = $locator->findResource("{$parts['scheme']}://{$parts['host']}{$parts['path']}", false); + try { + $resource = $locator->findResource("{$parts['scheme']}://{$parts['host']}{$parts['path']}", false); + } catch (\Exception $e) { + if ($fail_gracefully) { + return $input; + } else { + return false; + } + } - if (isset($parts['query'])) { + if ($resource && isset($parts['query'])) { $resource = $resource . '?' . $parts['query']; } } else { @@ -71,12 +83,13 @@ abstract class Utils $resource = $locator->findResource($input, false); } - } else { $resource = $input; } - + if (!$fail_gracefully && $resource === false) { + return false; + } return rtrim($uri->rootUrl($domain), '/') . '/' . ($resource ?? ''); } diff --git a/tests/unit/Grav/Common/UtilsTest.php b/tests/unit/Grav/Common/UtilsTest.php index f3b2a9707..e5be6394b 100644 --- a/tests/unit/Grav/Common/UtilsTest.php +++ b/tests/unit/Grav/Common/UtilsTest.php @@ -379,14 +379,25 @@ class UtilsTest extends \Codeception\TestCase\Test { $this->uri->initializeWithUrl('http://testing.dev/path1/path2')->init(); - $this->assertSame('http://testing.dev/', Utils::url('/', true)); - $this->assertSame('http://testing.dev/', Utils::url('', true)); - $this->assertSame('http://testing.dev/path1', Utils::url('/path1', true)); + // Fail hard + $this->assertSame(false, Utils::url('', true)); + $this->assertSame(false, Utils::url('')); + $this->assertSame(false, Utils::url('foo://bar/baz')); + $this->assertSame(false, Utils::url(new stdClass())); + $this->assertSame(false, Utils::url(['foo','bar','baz'])); + + // Fail Gracefully + $this->assertSame('/', Utils::url('/', false, true)); + $this->assertSame('/', Utils::url('', false, true)); + $this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz', false, true)); + $this->assertSame('/', Utils::url(new stdClass(), false, true)); + $this->assertSame('/', Utils::url(['foo','bar','baz'], false, true)); + $this->assertSame('/', Utils::url('/')); - $this->assertSame('/', Utils::url('')); + $this->assertSame('http://testing.dev/', Utils::url('/', true)); + $this->assertSame('http://testing.dev/path1', Utils::url('/path1', true)); $this->assertSame('/path1', Utils::url('/path1')); $this->assertSame('/path1/path2', Utils::url('/path1/path2')); - $this->assertSame('http://testing.dev/foobar.jpg', Utils::url('foobar.jpg', true)); $this->assertSame('http://testing.dev/foobar.jpg', Utils::url('/foobar.jpg', true)); $this->assertSame('http://testing.dev/path1/foobar.jpg', Utils::url('/path1/foobar.jpg', true)); @@ -394,18 +405,27 @@ class UtilsTest extends \Codeception\TestCase\Test $this->assertSame('/foobar.jpg', Utils::url('foobar.jpg')); $this->assertSame('/path1/foobar.jpg', Utils::url('/path1/foobar.jpg')); $this->assertSame('/path1/path2/foobar.jpg', Utils::url('/path1/path2/foobar.jpg')); + } public function testUrlWithRoot() { $this->uri->initializeWithUrlAndRootPath('http://testing.dev/subdir/path1/path2', '/subdir')->init(); + // Fail hard + $this->assertSame(false, Utils::url('', true)); + $this->assertSame(false, Utils::url('')); + $this->assertSame(false, Utils::url('foo://bar/baz')); + + // Fail Gracefully + $this->assertSame('/subdir/', Utils::url('/', false, true)); + $this->assertSame('/subdir/', Utils::url('', false, true)); + $this->assertSame('foo://bar/baz', Utils::url('foo://bar/baz', false, true)); + $this->assertSame('http://testing.dev/subdir/', Utils::url('/', true)); - $this->assertSame('http://testing.dev/subdir/', Utils::url('', true)); $this->assertSame('http://testing.dev/subdir/path1', Utils::url('/path1', true)); $this->assertSame('http://testing.dev/subdir/path1', Utils::url('/subdir/path1', true)); $this->assertSame('/subdir/', Utils::url('/')); - $this->assertSame('/subdir/', Utils::url('')); $this->assertSame('/subdir/path1', Utils::url('/path1')); $this->assertSame('/subdir/path1/path2', Utils::url('/path1/path2')); $this->assertSame('/subdir/path1/path2', Utils::url('/subdir/path1/path2')); From 30cfe3bdfa03082862d859dbdcb4449d72a604b0 Mon Sep 17 00:00:00 2001 From: Tracerneo <660285+Tracerneo@users.noreply.github.com> Date: Tue, 11 Jun 2019 16:39:26 +0200 Subject: [PATCH 2/3] Change minimal port number to 0 (unix socket) (#2452) --- system/src/Grav/Framework/Uri/UriPartsFilter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/src/Grav/Framework/Uri/UriPartsFilter.php b/system/src/Grav/Framework/Uri/UriPartsFilter.php index 88fe3e64e..51a971ab4 100644 --- a/system/src/Grav/Framework/Uri/UriPartsFilter.php +++ b/system/src/Grav/Framework/Uri/UriPartsFilter.php @@ -84,11 +84,11 @@ class UriPartsFilter */ public static function filterPort($port = null) { - if (null === $port || (\is_int($port) && ($port >= 1 && $port <= 65535))) { + if (null === $port || (\is_int($port) && ($port >= 0 && $port <= 65535))) { return $port; } - throw new \InvalidArgumentException('Uri port must be null or an integer between 1 and 65535'); + throw new \InvalidArgumentException('Uri port must be null or an integer between 0 and 65535'); } /** From a222e353bada101ed58c416a9d89437b487eaf72 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 14 Jun 2019 13:59:01 -0600 Subject: [PATCH 3/3] prepare for release --- CHANGELOG.md | 3 ++- system/defines.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 319db5032..bb00c69ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ # v1.6.10 -## mm/dd/2019 +## 06/14/2019 1. [](#improved) * Added **page blueprints** to `YamlLinter` CLI and Admin reports * Removed `Gitter` and `Slack` [#2502](https://github.com/getgrav/grav/issues/2502) * Optimizations for Plugin/Theme loading * Generalized markdown classes so they can be used outside of `Page` scope with a custom `Excerpts` class instance + * Change minimal port number to 0 (unix socket) [#2452](https://github.com/getgrav/grav/issues/2452) 1. [](#bugfix) * Force question to install demo content in theme update [#2493](https://github.com/getgrav/grav/issues/2493) * Fixed GPM errors from blueprints not being logged [#2505](https://github.com/getgrav/grav/issues/2505) diff --git a/system/defines.php b/system/defines.php index 7a4df5cce..405b68062 100644 --- a/system/defines.php +++ b/system/defines.php @@ -8,7 +8,7 @@ // Some standard defines define('GRAV', true); -define('GRAV_VERSION', '1.6.9'); +define('GRAV_VERSION', '1.6.10'); define('GRAV_TESTING', false); define('DS', '/');