diff --git a/src/Controller/SystemConfigurationController.php b/src/Controller/SystemConfigurationController.php index 729414bc..36a1e355 100644 --- a/src/Controller/SystemConfigurationController.php +++ b/src/Controller/SystemConfigurationController.php @@ -23,6 +23,7 @@ use App\Form\Type\TrackingModeType; use App\Form\Type\WeekDaysType; use App\Form\Type\YesNoType; use App\Repository\ConfigurationRepository; +use App\Validator\Constraints\AllowedHtmlTags; use App\Validator\Constraints\DateTimeFormat; use App\Validator\Constraints\TimeFormat; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; @@ -438,12 +439,14 @@ final class SystemConfigurationController extends AbstractController ->setName('theme.branding.company') ->setTranslationDomain('system-configuration') ->setRequired(false) - ->setType(TextType::class), + ->setType(TextType::class) + ->setConstraints([new AllowedHtmlTags(['tags' => ''])]), (new Configuration()) ->setName('theme.branding.mini') ->setTranslationDomain('system-configuration') ->setRequired(false) - ->setType(TextType::class), + ->setType(TextType::class) + ->setConstraints([new AllowedHtmlTags(['tags' => ''])]), (new Configuration()) ->setName('theme.branding.title') ->setTranslationDomain('system-configuration') diff --git a/src/Validator/Constraints/AllowedHtmlTags.php b/src/Validator/Constraints/AllowedHtmlTags.php new file mode 100644 index 00000000..5ac32eb0 --- /dev/null +++ b/src/Validator/Constraints/AllowedHtmlTags.php @@ -0,0 +1,45 @@ + 'The given value contains disallowed HTML tags.', + ]; + + public $message = 'This string contains invalid HTML tags.'; + + /** + * {@inheritdoc} + */ + public function getDefaultOption() + { + return 'tags'; + } + + /** + * {@inheritdoc} + */ + public function getRequiredOptions() + { + return ['tags']; + } +} diff --git a/src/Validator/Constraints/AllowedHtmlTagsValidator.php b/src/Validator/Constraints/AllowedHtmlTagsValidator.php new file mode 100644 index 00000000..353fcb8c --- /dev/null +++ b/src/Validator/Constraints/AllowedHtmlTagsValidator.php @@ -0,0 +1,47 @@ +tags) !== $value) { + $this->context->buildViolation('This string contains invalid HTML tags.') + ->setTranslationDomain('validators') + ->setParameter('{{ value }}', $this->formatValue($value)) + ->setCode(AllowedHtmlTags::DISALLOWED_TAGS_FOUND) + ->addViolation(); + } + } +} diff --git a/templates/base.html.twig b/templates/base.html.twig index e491ff95..897e15e5 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -72,7 +72,7 @@ {% block logo_mini %} {% if not kimai_context.branding.mini is empty %} - {{ kimai_context.branding.mini|striptags('')|raw }} + {{ kimai_context.branding.mini|raw }} {% else %} KTT {% endif %} @@ -80,7 +80,7 @@ {% block logo_large %} {% if not kimai_context.branding.company is empty %} - {{ kimai_context.branding.company|striptags('')|raw }} + {{ kimai_context.branding.company|raw }} {% else %} Kimai - Time Tracking {% endif %} diff --git a/tests/Validator/Constraints/AllowedHtmlTagsTest.php b/tests/Validator/Constraints/AllowedHtmlTagsTest.php new file mode 100644 index 00000000..894d9646 --- /dev/null +++ b/tests/Validator/Constraints/AllowedHtmlTagsTest.php @@ -0,0 +1,101 @@ +expectException(UnexpectedTypeException::class); + + $this->validator->validate('foo', new NotBlank()); + } + + public function testConstraintIsInvalidObject() + { + $this->expectException(UnexpectedTypeException::class); + + $constraint = new AllowedHtmlTags(['tags' => '']); + $this->validator->validate(new \stdClass(), $constraint); + } + + /** + * @dataProvider getValidValues + * @param string $allowedTags + * @param string $testString + */ + public function testConstraintWithValidValue(string $allowedTags, string $testString) + { + $constraint = new AllowedHtmlTags(['tags' => $allowedTags]); + $this->validator->validate($testString, $constraint); + $this->assertNoViolation(); + } + + public function testNullIsInvalid() + { + $this->validator->validate(null, new AllowedHtmlTags(['tags' => '', 'message' => 'myMessage'])); + + $this->assertNoViolation(); + } + + public function getValidValues() + { + return [ + ['', 'foo'], + ['', ''], + ['', 'fookjhg'], + ['', 'fookjhg'], + ['', 'fookjhgkjhgk'], + ]; + } + + public function getInvalidValues() + { + return [ + ['', 'fookjhg'], + ['', 'fookjhg'], + ['', 'fookjhg'], + ]; + } + + /** + * @dataProvider getInvalidValues + * @param string $allowedTags + * @param string $testString + */ + public function testValidationError(string $allowedTags, string $testString) + { + $constraint = new AllowedHtmlTags([ + 'tags' => $allowedTags, + ]); + + $this->validator->validate($testString, $constraint); + + $this->buildViolation('This string contains invalid HTML tags.') + ->setParameter('{{ value }}', '"' . $testString . '"') + ->setCode(AllowedHtmlTags::DISALLOWED_TAGS_FOUND) + ->assertRaised(); + } +}