diff --git a/app/Factories/LinkFactory.php b/app/Factories/LinkFactory.php index 7ff2bda5d..998b33f9e 100644 --- a/app/Factories/LinkFactory.php +++ b/app/Factories/LinkFactory.php @@ -48,6 +48,10 @@ public static function createLink($long_url, $is_secret=false, $custom_ending=nu maximum length allowed.'); } + if (LinkHelper::checkIfShortlinkIsRegisteredRoute($custom_ending)) { + throw new \Exception('Sorry, but your ending is a prohibited ending'); + } + $is_already_short = LinkHelper::checkIfAlreadyShortened($long_url); if ($is_already_short) { diff --git a/app/Helpers/LinkHelper.php b/app/Helpers/LinkHelper.php index 5abd67528..594266eaa 100644 --- a/app/Helpers/LinkHelper.php +++ b/app/Helpers/LinkHelper.php @@ -142,4 +142,25 @@ static public function findSuitableEnding() { return $base_x_val; } + + /** + * Checks if the ending is a registered route + * @param string $ending the requested ending + * @return bool true - it is a registered route; false - it is not + */ + static public function checkIfShortlinkIsRegisteredRoute($ending) + { + $publicDirectories = ['css', 'directives', 'fonts', 'img', 'js']; + if (in_array($ending, $publicDirectories, true)) { + return true; + } + $routes = property_exists(app(), 'router') ? app()->router->getRoutes() : app()->getRoutes(); + foreach ($routes as $route) { + $routeName = (isset($route['action']['as'])) ? $route['action']['as'] : ''; + if ($ending === $routeName) { + return true; + } + } + return false; + } } diff --git a/tests/LinkFactoryTest.php b/tests/LinkFactoryTest.php new file mode 100644 index 000000000..340e45101 --- /dev/null +++ b/tests/LinkFactoryTest.php @@ -0,0 +1,16 @@ +setExpectedException(\Exception::class, 'Sorry, but your ending is a prohibited ending'); + LinkFactory::createLink('https://example.org', true, 'login', '127.0.0.1', false, true); + + $this->setExpectedException(\Exception::class, 'Sorry, but your ending is a prohibited ending'); + LinkFactory::createLink('https://example.org', true, 'js', '127.0.0.1', false, true); + } +} \ No newline at end of file