mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-11 01:33:11 +08:00
Enforce a schema to be provided when short URLs are created
This commit is contained in:
@@ -33,6 +33,7 @@ use function Shlinkio\Shlink\Core\enumValues;
|
||||
use function Shlinkio\Shlink\Core\generateRandomShortCode;
|
||||
use function Shlinkio\Shlink\Core\normalizeDate;
|
||||
use function Shlinkio\Shlink\Core\normalizeOptionalDate;
|
||||
use function str_contains;
|
||||
|
||||
class ShortUrl extends AbstractEntity
|
||||
{
|
||||
@@ -68,7 +69,7 @@ class ShortUrl extends AbstractEntity
|
||||
*/
|
||||
public static function createFake(): self
|
||||
{
|
||||
return self::withLongUrl('foo');
|
||||
return self::withLongUrl('https://foo');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,8 +12,10 @@ use Shlinkio\Shlink\Common\Validation;
|
||||
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
use function preg_match;
|
||||
use function substr;
|
||||
|
||||
use const Shlinkio\Shlink\LOOSE_URI_MATCHER;
|
||||
use const Shlinkio\Shlink\MIN_SHORT_CODES_LENGTH;
|
||||
|
||||
/**
|
||||
@@ -59,27 +61,13 @@ class ShortUrlInputFilter extends InputFilter
|
||||
|
||||
private function initialize(bool $requireLongUrl, UrlShortenerOptions $options): void
|
||||
{
|
||||
$longUrlNotEmptyCommonOptions = [
|
||||
Validator\NotEmpty::OBJECT,
|
||||
Validator\NotEmpty::SPACE,
|
||||
Validator\NotEmpty::EMPTY_ARRAY,
|
||||
Validator\NotEmpty::BOOLEAN,
|
||||
Validator\NotEmpty::STRING,
|
||||
];
|
||||
|
||||
$longUrlInput = $this->createInput(self::LONG_URL, $requireLongUrl);
|
||||
$longUrlInput->getValidatorChain()->attach(new Validator\NotEmpty([
|
||||
...$longUrlNotEmptyCommonOptions,
|
||||
Validator\NotEmpty::NULL,
|
||||
]));
|
||||
$longUrlInput->getValidatorChain()->merge($this->longUrlValidators());
|
||||
$this->add($longUrlInput);
|
||||
|
||||
$deviceLongUrlsInput = $this->createInput(self::DEVICE_LONG_URLS, false);
|
||||
$deviceLongUrlsInput->getValidatorChain()->attach(
|
||||
new DeviceLongUrlsValidator(new Validator\NotEmpty([
|
||||
...$longUrlNotEmptyCommonOptions,
|
||||
...($requireLongUrl ? [Validator\NotEmpty::NULL] : []),
|
||||
])),
|
||||
new DeviceLongUrlsValidator($this->longUrlValidators(allowNull: ! $requireLongUrl))
|
||||
);
|
||||
$this->add($deviceLongUrlsInput);
|
||||
|
||||
@@ -129,4 +117,24 @@ class ShortUrlInputFilter extends InputFilter
|
||||
|
||||
$this->add($this->createBooleanInput(self::CRAWLABLE, false));
|
||||
}
|
||||
|
||||
private function longUrlValidators(bool $allowNull = false): Validator\ValidatorChain
|
||||
{
|
||||
$emptyModifiers = [
|
||||
Validator\NotEmpty::OBJECT,
|
||||
Validator\NotEmpty::SPACE,
|
||||
Validator\NotEmpty::EMPTY_ARRAY,
|
||||
Validator\NotEmpty::BOOLEAN,
|
||||
Validator\NotEmpty::STRING,
|
||||
];
|
||||
if (! $allowNull) {
|
||||
$emptyModifiers[] = Validator\NotEmpty::NULL;
|
||||
}
|
||||
|
||||
return (new Validator\ValidatorChain())
|
||||
->attach(new Validator\NotEmpty($emptyModifiers))
|
||||
->attach(new Validator\Callback(
|
||||
fn (?string $value) => ($allowNull && $value === null) || preg_match(LOOSE_URI_MATCHER, $value) === 1
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user