From 3de218f2870c01e39131f1a1c6c747e31a7c6de0 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Thu, 25 Jul 2024 12:58:40 +0200 Subject: [PATCH] Clone document before manipulating it --- src/XML/SignedElementTrait.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/XML/SignedElementTrait.php b/src/XML/SignedElementTrait.php index 77c925cb..de6c729a 100644 --- a/src/XML/SignedElementTrait.php +++ b/src/XML/SignedElementTrait.php @@ -136,13 +136,18 @@ private function validateReference(SignedInfo $signedInfo): SignedElementInterfa $this->validateReferenceUri($reference, $xml); } - $xp = XPath::getXPath($xml->ownerDocument); - $sigNode = XPath::xpQuery($xml, 'child::ds:Signature', $xp); + // Clone the document so we don't mess up the original DOMDocument + $doc = DOMDocumentFactory::create(); + $node = $doc->importNode($xml->ownerDocument->documentElement, true); + $doc->appendChild($node); + + $xp = XPath::getXPath($doc); + $sigNode = XPath::xpQuery($doc->documentElement, 'child::ds:Signature', $xp); Assert::minCount($sigNode, 1, NoSignatureFoundException::class); Assert::maxCount($sigNode, 1, 'More than one signature found in object.', TooManyElementsException::class); - $xml->removeChild($sigNode[0]); - $data = XML::processTransforms($reference->getTransforms(), $xml); + $doc->documentElement->removeChild($sigNode[0]); + $data = XML::processTransforms($reference->getTransforms(), $doc->documentElement); $algo = $reference->getDigestMethod()->getAlgorithm(); Assert::keyExists( C::$DIGEST_ALGORITHMS,