diff --git a/config/short-url.php b/config/short-url.php index bbcf3b9..ba2d0cb 100644 --- a/config/short-url.php +++ b/config/short-url.php @@ -107,14 +107,17 @@ /* |-------------------------------------------------------------------------- - | Additional URL Schemes + | Allowed URL Schemes |-------------------------------------------------------------------------- | - | Here you may specify additional URL schemes to shorten, eg 'mailto://' or - | ones for your own app, eg 'whatsapp://', 'yourapp://' + | Here you may specify the allowed URL schemes to shorten, eg 'mailto://' + | or ones for your own app, eg 'whatsapp://', 'yourapp://'. | */ - 'additional_url_schemes' => [], + 'allowed_url_schemes' => [ + 'http://', + 'https://', + ], /* |-------------------------------------------------------------------------- diff --git a/src/Classes/Builder.php b/src/Classes/Builder.php index c050325..b55df51 100644 --- a/src/Classes/Builder.php +++ b/src/Classes/Builder.php @@ -181,11 +181,9 @@ public function routes(): void */ public function destinationUrl(string $url): self { - $defaultAllowedPrefixes = ['http://', 'https://']; - $additionalAllowedPrefixes = config('short-url.additional_url_schemes', []); - $allowedPrefixes = array_merge($defaultAllowedPrefixes, $additionalAllowedPrefixes); + $allowedPrefixes = config('short-url.allowed_url_schemes'); - if (! Str::startsWith($url, $allowedPrefixes)) { + if (! Str::startsWith($url, config('short-url.allowed_url_schemes'))) { throw new ShortURLException('The destination URL must begin with an allowed prefix: '.implode(', ', $allowedPrefixes)); } diff --git a/src/Classes/Validation.php b/src/Classes/Validation.php index 2219e91..bc9cab5 100644 --- a/src/Classes/Validation.php +++ b/src/Classes/Validation.php @@ -32,6 +32,7 @@ public function validateConfig(): bool Rule::make('enforce_https')->rules(['required', 'boolean']), Rule::make('forward_query_params')->rules(['required', 'boolean']), Rule::make('default_url')->rules(['nullable', 'string']), + Rule::make('allowed_url_schemes')->rules(['required', 'array']), ], ]); diff --git a/tests/Unit/Classes/BuilderTest.php b/tests/Unit/Classes/BuilderTest.php index 9cb90c4..5447c33 100644 --- a/tests/Unit/Classes/BuilderTest.php +++ b/tests/Unit/Classes/BuilderTest.php @@ -613,7 +613,7 @@ public function builder_works_when_the_date_facade_is_set_to_use_carbon_immutabl #[Test] public function custom_url_schemes_allowed_if_configured(): void { - Config::set('short-url.additional_url_schemes', ['whatsapp://']); + Config::set('short-url.allowed_url_schemes', ['http://', 'https://', 'whatsapp://']); $shortUrl = app(Builder::class) ->destinationUrl('whatsapp://callMe') @@ -625,12 +625,12 @@ public function custom_url_schemes_allowed_if_configured(): void #[Test] public function exception_is_thrown_if_invalid_scheme(): void { - Config::set('short-url.additional_url_schemes', ['whatsapp://']); + Config::set('short-url.allowed_url_schemes', ['https://', 'whatsapp://']); $this->expectException(ShortURLException::class); - $this->expectExceptionMessage('The destination URL must begin with an allowed prefix: http://, https://, whatsapp://'); + $this->expectExceptionMessage('The destination URL must begin with an allowed prefix: https://, whatsapp://'); $builder = app(Builder::class); - $builder->destinationUrl('INVALID'); + $builder->destinationUrl('phpstorm://'); } } diff --git a/tests/Unit/Classes/ValidationTest.php b/tests/Unit/Classes/ValidationTest.php index e44d32b..5070278 100644 --- a/tests/Unit/Classes/ValidationTest.php +++ b/tests/Unit/Classes/ValidationTest.php @@ -129,4 +129,16 @@ public function exception_is_thrown_if_the_default_url_is_not_a_string(): void $validation = new Validation(); $validation->validateConfig(); } + + #[Test] + public function exception_is_thrown_if_the_allowed_url_schemes_is_not_an_array(): void + { + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('The short-url.allowed_url_schemes field must be an array.'); + + Config::set('short-url.allowed_url_schemes', 'INVALID'); + + $validation = new Validation(); + $validation->validateConfig(); + } }