Skip to content

Commit 419be3e

Browse files
committed
Reduce duplicate namespaces
1 parent 44d4187 commit 419be3e

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/XML/CanonicalizableElementTrait.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use DOMElement;
88
use SimpleSAML\XMLSecurity\Constants as C;
99
use SimpleSAML\XMLSecurity\Exception\CanonicalizationFailedException;
10+
use SimpleSAML\XMLSecurity\Exception\ReferenceValidationFailedException;
11+
use SimpleSAML\XMLSecurity\Utils\XPath;
1012
use SimpleSAML\XMLSecurity\XML\ds\Transforms;
1113
use 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

Comments
 (0)