77use DOMElement ;
88use SimpleSAML \XMLSecurity \Constants as C ;
99use SimpleSAML \XMLSecurity \Exception \CanonicalizationFailedException ;
10+ use SimpleSAML \XMLSecurity \Exception \ReferenceValidationFailedException ;
11+ use SimpleSAML \XMLSecurity \Utils \XPath ;
1012use SimpleSAML \XMLSecurity \XML \ds \Transforms ;
1113use SimpleSAML \XPath \Constants as XPATH_C ;
1214
@@ -121,6 +123,7 @@ public function processTransforms(
121123 $ canonicalMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS ;
122124 $ arXPath = null ;
123125 $ prefixList = null ;
126+
124127 foreach ($ transforms ->getTransform () as $ transform ) {
125128 $ canonicalMethod = $ transform ->getAlgorithm ()->getValue ();
126129 switch ($ canonicalMethod ) {
@@ -140,15 +143,22 @@ public function processTransforms(
140143 $ arXPath = [];
141144 $ xpathValue = $ xpath ->getContent ()->getValue ();
142145 $ arXPath ['query ' ] = '(.//. | .//@* | .//namespace::*)[ ' . $ xpathValue . '] ' ;
143-
144- // $arXpath['namespaces'] = $xpath->getNamespaces();
145- // TODO: review if $nsnode->localName is equivalent to the keys in getNamespaces()
146- // $nslist = $xp->query('./namespace::*', $node);
147- // foreach ($nslist as $nsnode) {
148- // if ($nsnode->localName != "xml") {
149- // $arXPath['namespaces'][$nsnode->localName] = $nsnode->nodeValue;
150- // }
151- // }
146+ $ arXpath ['namespaces ' ] = $ xpath ->getNamespaces ();
147+
148+ $ xpCache = XPath::getXPath ($ transform ->toXML ());
149+ $ nslist = $ xpCache ->query ('./namespace::* ' , $ xpath ->toXML ());
150+ Assert::lessThanEq (
151+ $ nslist ->count (),
152+ C::MAX_XPATH_NAMESPACES ,
153+ ReferenceValidationFailedException::class,
154+ 'Too many namespaces. ' ,
155+ );
156+
157+ foreach ($ nslist as $ nsnode ) {
158+ if ($ nsnode ->localName != "xml " ) {
159+ $ arXPath ['namespaces ' ][$ nsnode ->localName ] = $ nsnode ->nodeValue ;
160+ }
161+ }
152162 }
153163 break ;
154164 }
0 commit comments