Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/Factories/LinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions app/Helpers/LinkHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 16 additions & 0 deletions tests/LinkFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php


use App\Factories\LinkFactory;

class LinkFactoryTest extends TestCase
{
public function testLinkIsProhibitedEnding()
{
$this->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);
}
}