From 562763199a26aef992e300b0d2a3c448a6febe59 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 7 Jan 2022 13:13:45 +0100 Subject: [PATCH 1/3] Ensured URL titles are trimmed to avoid error when persisted in database --- composer.json | 8 +++++- .../src/Validation/ShortUrlInputFilter.php | 9 +++++- module/Core/test/Model/ShortUrlMetaTest.php | 28 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b8ff3349..8e55c3c4 100644 --- a/composer.json +++ b/composer.json @@ -192,6 +192,12 @@ }, "config": { "sort-packages": true, - "platform-check": false + "platform-check": false, + "allow-plugins": { + "composer/package-versions-deprecated": true, + "dealerdirect/phpcodesniffer-composer-installer": true, + "infection/extension-installer": true, + "veewee/composer-run-parallel": true + } } } diff --git a/module/Core/src/Validation/ShortUrlInputFilter.php b/module/Core/src/Validation/ShortUrlInputFilter.php index 47f6f8ac..2497f85d 100644 --- a/module/Core/src/Validation/ShortUrlInputFilter.php +++ b/module/Core/src/Validation/ShortUrlInputFilter.php @@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Validation; use Cocur\Slugify\Slugify; use DateTime; +use Laminas\Filter; use Laminas\InputFilter\Input; use Laminas\InputFilter\InputFilter; use Laminas\Validator; @@ -13,6 +14,8 @@ use Shlinkio\Shlink\Common\Validation; use Shlinkio\Shlink\Core\Util\CocurSymfonySluggerBridge; use Shlinkio\Shlink\Rest\Entity\ApiKey; +use function substr; + use const Shlinkio\Shlink\CUSTOM_SLUGS_REGEXP; use const Shlinkio\Shlink\MIN_SHORT_CODES_LENGTH; @@ -107,7 +110,11 @@ class ShortUrlInputFilter extends InputFilter $this->add($this->createTagsInput(self::TAGS, false)); - $this->add($this->createInput(self::TITLE, false)); + $title = $this->createInput(self::TITLE, false); + $title->getFilterChain()->attach(new Filter\Callback( + static fn (?string $value) => $value === null ? $value : substr($value, 0, 512), + )); + $this->add($title); $this->add($this->createBooleanInput(self::CRAWLABLE, false)); } diff --git a/module/Core/test/Model/ShortUrlMetaTest.php b/module/Core/test/Model/ShortUrlMetaTest.php index 2b57987b..5fef14d8 100644 --- a/module/Core/test/Model/ShortUrlMetaTest.php +++ b/module/Core/test/Model/ShortUrlMetaTest.php @@ -11,6 +11,10 @@ use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter; use stdClass; +use function str_pad; + +use const STR_PAD_BOTH; + class ShortUrlMetaTest extends TestCase { /** @@ -99,4 +103,28 @@ class ShortUrlMetaTest extends TestCase yield ['谷歌', '谷歌']; yield ['гугл', 'гугл']; } + + /** + * @test + * @dataProvider provideTitles + */ + public function titleIsCroppedIfTooLong(?string $title, ?string $expectedTitle): void + { + $meta = ShortUrlMeta::fromRawData([ + 'title' => $title, + 'longUrl' => '', + ]); + + self::assertEquals($expectedTitle, $meta->getTitle()); + } + + public function provideTitles(): iterable + { + yield [null, null]; + yield ['foo', 'foo']; + yield [str_pad('bar', 600, ' ', STR_PAD_BOTH), 'bar']; + yield [str_pad('', 512, 'a'), str_pad('', 512, 'a')]; + yield [str_pad('', 600, 'b'), str_pad('', 512, 'b')]; + yield [str_pad('', 800, 'b'), str_pad('', 512, 'b')]; + } } From cbdc5f121e6e97b20773663d56855616ed47f118 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 7 Jan 2022 14:04:21 +0100 Subject: [PATCH 2/3] Updated changelog --- CHANGELOG.md | 17 +++++++++++++++++ config/autoload/entity-manager.local.php.dist | 1 - config/test/test_config.global.php | 2 -- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 311479ed..2831c237 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). +## [Unreleased] +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#1293](https://github.com/shlinkio/shlink/issues/1293) Fixed error when trying to create/import short URLs with a too long title. + + ## [2.10.1] - 2021-12-21 ### Added * *Nothing* diff --git a/config/autoload/entity-manager.local.php.dist b/config/autoload/entity-manager.local.php.dist index ef5cabf8..0624aa51 100644 --- a/config/autoload/entity-manager.local.php.dist +++ b/config/autoload/entity-manager.local.php.dist @@ -11,7 +11,6 @@ return [ 'driver' => 'pdo_mysql', 'host' => 'shlink_db_mysql', 'dbname' => 'shlink', - 'charset' => 'utf8', ], ], diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index 0898c732..f1a983fe 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -55,7 +55,6 @@ $buildDbConnection = static function (): array { 'user' => 'postgres', 'password' => 'root', 'dbname' => 'shlink_test', - 'charset' => 'utf8', ], 'mssql' => [ 'driver' => 'pdo_sqlsrv', @@ -71,7 +70,6 @@ $buildDbConnection = static function (): array { 'user' => 'root', 'password' => 'root', 'dbname' => 'shlink_test', - 'charset' => 'utf8', ], }; }; From 38bea6c086b2df06d681d455703ce16a889da80d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 7 Jan 2022 14:07:07 +0100 Subject: [PATCH 3/3] Added edge case tests for SHortUrlMetaTest on title field --- module/Core/test/Model/ShortUrlMetaTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/module/Core/test/Model/ShortUrlMetaTest.php b/module/Core/test/Model/ShortUrlMetaTest.php index 5fef14d8..9a5eac72 100644 --- a/module/Core/test/Model/ShortUrlMetaTest.php +++ b/module/Core/test/Model/ShortUrlMetaTest.php @@ -123,8 +123,10 @@ class ShortUrlMetaTest extends TestCase yield [null, null]; yield ['foo', 'foo']; yield [str_pad('bar', 600, ' ', STR_PAD_BOTH), 'bar']; - yield [str_pad('', 512, 'a'), str_pad('', 512, 'a')]; - yield [str_pad('', 600, 'b'), str_pad('', 512, 'b')]; - yield [str_pad('', 800, 'b'), str_pad('', 512, 'b')]; + yield [str_pad('', 511, 'a'), str_pad('', 511, 'a')]; + yield [str_pad('', 512, 'b'), str_pad('', 512, 'b')]; + yield [str_pad('', 513, 'c'), str_pad('', 512, 'c')]; + yield [str_pad('', 600, 'd'), str_pad('', 512, 'd')]; + yield [str_pad('', 800, 'e'), str_pad('', 512, 'e')]; } }