From 57749892c8d43aa8766ac0c9c492c2c106e1fdfe Mon Sep 17 00:00:00 2001 From: Stanislas Date: Thu, 9 Nov 2023 08:13:06 +0100 Subject: [PATCH] fix(UserMention): fix fatal error on null --- src/RichText/UserMention.php | 9 +++++++- .../functional/Glpi/RichText/UserMention.php | 21 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/RichText/UserMention.php b/src/RichText/UserMention.php index f99cbe18d8a..babb1b80b68 100644 --- a/src/RichText/UserMention.php +++ b/src/RichText/UserMention.php @@ -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'}; diff --git a/tests/functional/Glpi/RichText/UserMention.php b/tests/functional/Glpi/RichText/UserMention.php index 1fcd989a1dc..31bd375111e 100644 --- a/tests/functional/Glpi/RichText/UserMention.php +++ b/tests/functional/Glpi/RichText/UserMention.php @@ -132,7 +132,6 @@ protected function itilProvider() 'update_expected_observers' => [], 'update_expected_notified' => [], ]; - yield [ 'itemtype' => $itemtype, 'main_itemtype' => $main_type, @@ -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 + , + 'add_expected_observers' => [], + 'add_expected_notified' => [], + + // update bad HTML => no users are notified + 'update_content' => <<

+HTML + , + 'update_expected_observers' => [], + 'update_expected_notified' => [], + ]; } } }