Skip to content

Commit

Permalink
Clone document before manipulating it
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jul 25, 2024
1 parent 5bf9bdd commit 3de218f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/XML/SignedElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3de218f

Please sign in to comment.