From 629e86d8cc5b709924e92e3a4084d9629dc40351 Mon Sep 17 00:00:00 2001 From: Olivier Pontier Date: Thu, 20 Aug 2020 16:59:32 +0200 Subject: [PATCH 1/2] fix HTML filter issue when the text contains malformed HTML tags --- src/Source/Filter/HtmlFilter.php | 4 ++++ tests/Unit/Source/Filter/HtmlFilterTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Source/Filter/HtmlFilter.php b/src/Source/Filter/HtmlFilter.php index 4e3c756..13f6f6f 100644 --- a/src/Source/Filter/HtmlFilter.php +++ b/src/Source/Filter/HtmlFilter.php @@ -258,6 +258,10 @@ function ($match) { */ private function isIgnoredTag(?string $name): bool { + if ($name === null) { + return false; + } + foreach (self::$ignoreTags as $tag) { if (strcasecmp($tag, $name) === 0) { return true; diff --git a/tests/Unit/Source/Filter/HtmlFilterTest.php b/tests/Unit/Source/Filter/HtmlFilterTest.php index fa2fb92..7bf4d55 100644 --- a/tests/Unit/Source/Filter/HtmlFilterTest.php +++ b/tests/Unit/Source/Filter/HtmlFilterTest.php @@ -90,4 +90,12 @@ public function testMalformedAttribute(): void $text = ' test '; static::assertEquals($text, $filter->filter($html)); } + + public function testMalformedTags(): void + { + $filter = new HtmlFilter(); + $html = "foo/>bar

"; + $text = "foo/ bar "; + static::assertEquals($text, $filter->filter($html)); + } } From 880b59392bef4b27f82e7a5c65800c4854bb55b1 Mon Sep 17 00:00:00 2001 From: Olivier Pontier Date: Thu, 20 Aug 2020 17:10:46 +0200 Subject: [PATCH 2/2] check if tagName is null before use it as an array of chars --- src/Source/Filter/HtmlFilter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Source/Filter/HtmlFilter.php b/src/Source/Filter/HtmlFilter.php index 13f6f6f..0a226fc 100644 --- a/src/Source/Filter/HtmlFilter.php +++ b/src/Source/Filter/HtmlFilter.php @@ -120,7 +120,7 @@ public function filter(string $string): string case '>' === $char: if ($this->isIgnoredTag($tagName)) { $ignoreTagContent = true; - } elseif ('/' === $tagName[0]) { + } elseif ($tagName === null || '/' === $tagName[0]) { $ignoreTagContent = false; // Restore to default state. } $context = self::CTX_TAG_CONTENT;