From 6d924bd10208d285b577b093fb2e83550069a330 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Wed, 27 Mar 2024 14:19:11 +0200 Subject: [PATCH 1/2] Preserve libxml errors after failed parsing --- src/Sanitizer.php | 18 ++++++++++++++++++ tests/SanitizerTest.php | 15 ++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Sanitizer.php b/src/Sanitizer.php index 60072fa..731b8c7 100644 --- a/src/Sanitizer.php +++ b/src/Sanitizer.php @@ -230,6 +230,7 @@ public function sanitize($dirty) // If we couldn't parse the XML then we go no further. Reset and return false if (!$loaded) { + $this->xmlIssues = self::getXmlErrors(); $this->resetAfter(); return false; } @@ -698,4 +699,21 @@ protected function cleanUnsafeNodes(\DOMNode $currentElement) { } } } + + /** + * Retrieve array of errors + * @return array + */ + private static function getXmlErrors() + { + $errors = []; + foreach (libxml_get_errors() as $error) { + $errors[] = [ + 'message' => trim($error->message), + 'line' => $error->line, + ]; + } + + return $errors; + } } diff --git a/tests/SanitizerTest.php b/tests/SanitizerTest.php index e031a5e..b9fa379 100644 --- a/tests/SanitizerTest.php +++ b/tests/SanitizerTest.php @@ -100,7 +100,20 @@ public function testBadXMLReturnsFalse() $sanitizer = new Sanitizer(); $cleanData = $sanitizer->sanitize($initialData); - self::assertSame(false, $cleanData); + self::assertFalse($cleanData); + self::assertEquals( + [ + [ + 'message' => 'Opening and ending tag mismatch: line line 8 and svg', + 'line' => 15, + ], + [ + 'message' => 'Premature end of data in tag svg line 4', + 'line' => 16, + ], + ], + $sanitizer->getXmlIssues(), + ); } /** From 9648e156195c67074734c2ed176a5b930465a917 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 18 Jun 2024 11:25:54 +0100 Subject: [PATCH 2/2] Remove trailing comma --- tests/SanitizerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/SanitizerTest.php b/tests/SanitizerTest.php index b9fa379..97514f0 100644 --- a/tests/SanitizerTest.php +++ b/tests/SanitizerTest.php @@ -112,7 +112,7 @@ public function testBadXMLReturnsFalse() 'line' => 16, ], ], - $sanitizer->getXmlIssues(), + $sanitizer->getXmlIssues() ); }