forked from shlinkio/shlink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not allow URL reserved characters in custom slugs
- Loading branch information
Showing
6 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
module/Core/src/ShortUrl/Model/Validation/CustomSlugValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\Core\ShortUrl\Model\Validation; | ||
|
||
use Laminas\Validator\AbstractValidator; | ||
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions; | ||
|
||
use function is_string; | ||
use function strpbrk; | ||
|
||
class CustomSlugValidator extends AbstractValidator | ||
{ | ||
private const NOT_STRING = 'NOT_STRING'; | ||
private const CONTAINS_URL_CHARACTERS = 'CONTAINS_URL_CHARACTERS'; | ||
|
||
protected array $messageTemplates = [ | ||
self::NOT_STRING => 'Provided value is not a string.', | ||
self::CONTAINS_URL_CHARACTERS => 'URL-reserved characters cannot be used in a custom slug.', | ||
]; | ||
|
||
private UrlShortenerOptions $options; | ||
|
||
private function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public static function forUrlShortenerOptions(UrlShortenerOptions $options): self | ||
{ | ||
$instance = new self(); | ||
$instance->options = $options; | ||
|
||
return $instance; | ||
} | ||
|
||
public function isValid(mixed $value): bool | ||
{ | ||
if ($value === null) { | ||
return true; | ||
} | ||
|
||
if (! is_string($value)) { | ||
$this->error(self::NOT_STRING); | ||
return false; | ||
} | ||
|
||
// URL reserved characters: https://datatracker.ietf.org/doc/html/rfc3986#section-2.2 | ||
$reservedChars = "!*'();:@&=+$,?%#[]"; | ||
if (! $this->options->multiSegmentSlugsEnabled) { | ||
// Slashes should be allowed for multi-segment slugs | ||
$reservedChars .= '/'; | ||
} | ||
|
||
if (strpbrk($value, $reservedChars) !== false) { | ||
$this->error(self::CONTAINS_URL_CHARACTERS); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
module/Core/test/ShortUrl/Model/Validation/CustomSlugValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\Core\ShortUrl\Model\Validation; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions; | ||
use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\CustomSlugValidator; | ||
use stdClass; | ||
|
||
class CustomSlugValidatorTest extends TestCase | ||
{ | ||
#[Test] | ||
public function nullIsValid(): void | ||
{ | ||
$validator = $this->createValidator(); | ||
self::assertTrue($validator->isValid(null)); | ||
} | ||
|
||
#[Test, DataProvider('provideNonStringValues')] | ||
public function nonStringValuesAreInvalid(mixed $value): void | ||
{ | ||
$validator = $this->createValidator(); | ||
|
||
self::assertFalse($validator->isValid($value)); | ||
self::assertEquals(['NOT_STRING' => 'Provided value is not a string.'], $validator->getMessages()); | ||
} | ||
|
||
public static function provideNonStringValues(): iterable | ||
{ | ||
yield [123]; | ||
yield [new stdClass()]; | ||
yield [true]; | ||
} | ||
|
||
#[Test] | ||
public function slashesAreAllowedWhenMultiSegmentSlugsAreEnabled(): void | ||
{ | ||
$slugWithSlashes = 'foo/bar/baz'; | ||
|
||
self::assertTrue($this->createValidator(multiSegmentSlugsEnabled: true)->isValid($slugWithSlashes)); | ||
self::assertFalse($this->createValidator(multiSegmentSlugsEnabled: false)->isValid($slugWithSlashes)); | ||
} | ||
|
||
#[Test, DataProvider('provideInvalidValues')] | ||
public function valuesWithReservedCharsAreInvalid(string $value): void | ||
{ | ||
$validator = $this->createValidator(); | ||
|
||
self::assertFalse($validator->isValid($value)); | ||
self::assertEquals( | ||
['CONTAINS_URL_CHARACTERS' => 'URL-reserved characters cannot be used in a custom slug.'], | ||
$validator->getMessages(), | ||
); | ||
} | ||
|
||
public static function provideInvalidValues(): iterable | ||
{ | ||
yield ['foo?bar=baz']; | ||
yield ['some-thing#foo']; | ||
yield ['call()']; | ||
yield ['array[]']; | ||
yield ['email@example.com']; | ||
yield ['wildcard*']; | ||
yield ['$500']; | ||
} | ||
|
||
public function createValidator(bool $multiSegmentSlugsEnabled = false): CustomSlugValidator | ||
{ | ||
return CustomSlugValidator::forUrlShortenerOptions( | ||
new UrlShortenerOptions(multiSegmentSlugsEnabled: $multiSegmentSlugsEnabled), | ||
); | ||
} | ||
} |