From 0a220bbc7aad0ffab647b9d8d6bbe6e6ed324f50 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 1 Aug 2022 10:12:08 +0200 Subject: [PATCH] Allowed slashes on custom slugs during short URL creation --- module/Core/src/Validation/ShortUrlInputFilter.php | 3 ++- module/Core/test/Model/ShortUrlMetaTest.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/module/Core/src/Validation/ShortUrlInputFilter.php b/module/Core/src/Validation/ShortUrlInputFilter.php index 6cd578fb..d4d0f818 100644 --- a/module/Core/src/Validation/ShortUrlInputFilter.php +++ b/module/Core/src/Validation/ShortUrlInputFilter.php @@ -13,6 +13,7 @@ use Shlinkio\Shlink\Common\Validation; use Shlinkio\Shlink\Rest\Entity\ApiKey; use function is_string; +use function ltrim; use function str_replace; use function substr; @@ -77,7 +78,7 @@ class ShortUrlInputFilter extends InputFilter // empty, is by using the deprecated setContinueIfEmpty $customSlug = $this->createInput(self::CUSTOM_SLUG, false)->setContinueIfEmpty(true); $customSlug->getFilterChain()->attach(new Filter\Callback( - static fn (mixed $value) => is_string($value) ? str_replace([' ', '/'], '-', $value) : $value, + static fn (mixed $value) => is_string($value) ? ltrim(str_replace(' ', '-', $value), '/') : $value, )); $customSlug->getValidatorChain()->attach(new Validator\NotEmpty([ Validator\NotEmpty::STRING, diff --git a/module/Core/test/Model/ShortUrlMetaTest.php b/module/Core/test/Model/ShortUrlMetaTest.php index 1933b3b6..833a25bb 100644 --- a/module/Core/test/Model/ShortUrlMetaTest.php +++ b/module/Core/test/Model/ShortUrlMetaTest.php @@ -103,7 +103,8 @@ class ShortUrlMetaTest extends TestCase yield ['foo bar', 'foo-bar']; yield ['foo bar baz', 'foo-bar-baz']; yield ['foo bar-baz', 'foo-bar-baz']; - yield ['foo/bar/baz', 'foo-bar-baz']; + yield ['foo/bar/baz', 'foo/bar/baz']; + yield ['/foo/bar/baz', 'foo/bar/baz']; yield ['wp-admin.php', 'wp-admin.php']; yield ['UPPER_lower', 'UPPER_lower']; yield ['more~url_special.chars', 'more~url_special.chars'];