Skip to content

Commit

Permalink
fix(UserMention): fix fatal error on null
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz authored Nov 9, 2023
1 parent 41cd46e commit 5774989
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/RichText/UserMention.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,20 @@ public static function getUserIdsFromUserMentions(string $content)
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($content);
$content_as_xml = simplexml_import_dom($dom);
// TODO In GLPI 10.1, find a way to remove usage of this `@` operator
// that was added to prevent Error E_WARNING simplexml_import_dom(): Invalid Nodetype to import
// with bad HTML content.
$content_as_xml = @simplexml_import_dom($dom);
} catch (\Throwable $e) {
// Sanitize process does not handle correctly `<` and `>` chars that are not surrounding html tags.
// This generates invalid HTML that cannot be loaded by `SimpleXMLElement`.
return [];
}

if ($content_as_xml === null) {
return [];
}

$mention_elements = $content_as_xml->xpath('//*[@data-user-mention="true"]');
foreach ($mention_elements as $mention_element) {
$ids[] = (int)$mention_element->attributes()->{'data-user-id'};
Expand Down
21 changes: 20 additions & 1 deletion tests/functional/Glpi/RichText/UserMention.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ protected function itilProvider()
'update_expected_observers' => [],
'update_expected_notified' => [],
];

yield [
'itemtype' => $itemtype,
'main_itemtype' => $main_type,
Expand Down Expand Up @@ -185,6 +184,26 @@ protected function itilProvider()
'is_private' => true,
];
}
yield [
'itemtype' => $itemtype,
'main_itemtype' => $main_type,

// bad HTML no users are notified
'add_content' => <<<HTML
</span></p></div></body></html>
HTML
,
'add_expected_observers' => [],
'add_expected_notified' => [],

// update bad HTML => no users are notified
'update_content' => <<<HTML
</span></p></div></body></html>
HTML
,
'update_expected_observers' => [],
'update_expected_notified' => [],
];
}
}
}
Expand Down

0 comments on commit 5774989

Please sign in to comment.