Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix HTML filter issue when the text contains malformed HTML tags #27

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Source/Filter/HtmlFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -258,6 +258,10 @@ function ($match) {
*/
private function isIgnoredTag(?string $name): bool
{
if ($name === null) {
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tag should be ignored if its malformed. So this should return true.
Or else you will get a array access on null in

} elseif ('/' === $tagName[0]) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @icanhazstring .
Actually if the tag is ignored, the result of the filter becomes foo/ because the state is not restored and the text after the character > is replaced by spaces. Do you have any idea to solve it?

Copy link
Collaborator

@icanhazstring icanhazstring Aug 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base problem here I think is: The script "thinks" it is parsing a closing tag with > but it shouldn't. Because it never started one using <.

Maybe there is a total underlying problem here with the parsing method.
So I would go with your initial solution as it is the more correct one.

So your case with $tagName !== null should be in here.
Maybe we can think of some other solution someday :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your help

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing :)

}

foreach (self::$ignoreTags as $tag) {
if (strcasecmp($tag, $name) === 0) {
return true;
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Source/Filter/HtmlFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<br><br/>";
$text = "foo/ bar ";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the tag will be ignored the outcome should be foo/>bar

static::assertEquals($text, $filter->filter($html));
}
}