Skip to content

Commit

Permalink
Merge branch 'refs/heads/feature/url-schemes' into allowed-url-schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
ash-jc-allen committed Jul 2, 2024
2 parents 7038c04 + d95b881 commit dab76bb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
11 changes: 7 additions & 4 deletions config/short-url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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://',
],

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 2 additions & 4 deletions src/Classes/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
1 change: 1 addition & 0 deletions src/Classes/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
],
]);

Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Classes/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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://');
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Classes/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit dab76bb

Please sign in to comment.