Skip to content

Commit

Permalink
Automatically prevent folder/file names that are in public folder fro…
Browse files Browse the repository at this point in the history
…m being used as keywords (#977)
  • Loading branch information
realodix authored May 5, 2024
1 parent 2641ec2 commit 9ba217e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/Services/KeyGeneratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ public function ensureStringCanBeUsedAsKey(string $value): bool
$alreadyInUse = Url::whereKeyword($value)->exists();
$isReservedKeyword = in_array($value, config('urlhub.reserved_keyword'));
$isRoute = in_array($value, $route);
$isPublicPath = in_array($value, scandir(public_path()));

if ($alreadyInUse || $isReservedKeyword || $isRoute) {
if ($alreadyInUse || $isReservedKeyword || $isRoute || $isPublicPath) {
return false;
}

Expand Down
7 changes: 1 addition & 6 deletions config/urlhub.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@
* example rude words.
*/
'reserved_keyword' => [
'css',
'images',
'img',
'fonts',
'js',
'svg',
'images', 'img', 'fonts',
],

'web_title' => env('UH_WEB_TITLE', true),
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/FrontPage/ShortenUrl/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function customKeyFailProvider(): array
return [
['fooBar'],
['foo_bar'],
['css'], // reserved keyword
['fonts'], // reserved keyword
['login'], // registered route
];
}
Expand Down
6 changes: 5 additions & 1 deletion tests/Unit/Rule/NotBlacklistedKeywordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static function registeredRouteDataProvider(): array
return [
['login'],
['register'],

// in public folder
['svg'], // folder
['build'], // vite folder
];
}

Expand All @@ -68,7 +72,7 @@ public function testCustomKeywordIsRegisteredRoute($value): void
#[Group('u-rule')]
public function testCustomKeywordIsReservedKeyword(): void
{
$value = 'css';
$value = 'reserved_keyword';
config(['urlhub.reserved_keyword' => [$value]]);

$val = Helper::validator(['foo' => $value], ['foo' => new NotBlacklistedKeyword]);
Expand Down

0 comments on commit 9ba217e

Please sign in to comment.