Skip to content

Commit 08e4eff

Browse files
committed
Refactor inclusive namespaces to happen in canonicalization code
1 parent ddb6507 commit 08e4eff

File tree

2 files changed

+31
-41
lines changed

2 files changed

+31
-41
lines changed

lib/exclusive-canonicalization.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,36 @@ ExclusiveCanonicalization.prototype.process = function(node, options) {
199199
var defaultNsForPrefix = options.defaultNsForPrefix || {};
200200
if (!(inclusiveNamespacesPrefixList instanceof Array)) { inclusiveNamespacesPrefixList = inclusiveNamespacesPrefixList.split(' '); }
201201

202+
var ancestorNamespaces = options.ancestorNamespaces || [];
203+
204+
/**
205+
* Get the InclusiveNamespaces PrefixList
206+
*/
207+
var inclusiveNamespacesPrefixList = options.inclusiveNamespacesPrefixList || [];
208+
var CanonicalizationMethod = utils.findChilds(node, "CanonicalizationMethod")
209+
if (CanonicalizationMethod.length!=0) {
210+
var inclusiveNamespaces = utils.findChilds(CanonicalizationMethod[0], "InclusiveNamespaces")
211+
if (inclusiveNamespaces.length!=0) {
212+
inclusiveNamespacesPrefixList = inclusiveNamespaces[0].getAttribute('PrefixList').split(" ");
213+
}
214+
}
215+
216+
/**
217+
* If you have a PrefixList then use it and the ancestors to add the necessary namespaces
218+
*/
219+
if (inclusiveNamespacesPrefixList) {
220+
var prefixList = inclusiveNamespacesPrefixList instanceof Array ? inclusiveNamespacesPrefixList : inclusiveNamespacesPrefixList.split(' ');
221+
prefixList.forEach(function (prefix) {
222+
if (ancestorNamespaces) {
223+
ancestorNamespaces.forEach(function (ancestorNamespace) {
224+
if (prefix == ancestorNamespace.prefix) {
225+
node.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' + prefix, ancestorNamespace.namespaceURI);
226+
}
227+
})
228+
}
229+
})
230+
}
231+
202232
var res = this.processInner(node, [], defaultNs, defaultNsForPrefix, inclusiveNamespacesPrefixList);
203233
return res;
204234
};

lib/signed-xml.js

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -368,45 +368,18 @@ SignedXml.prototype.validateSignatureValue = function(doc) {
368368
throw new Error("When canonicalization method is non-exclusive, whole xml dom must be provided as an argument");
369369
}
370370
}
371-
372-
/**
373-
* Get the InclusiveNamespaces PrefixList
374-
*/
375-
var inclusiveNamespacesPrefixList
376-
var CanonicalizationMethod = utils.findChilds(signedInfo[0], "CanonicalizationMethod")
377-
if (CanonicalizationMethod.length!=0) {
378-
var inclusiveNamespaces = utils.findChilds(CanonicalizationMethod[0], "InclusiveNamespaces")
379-
if (inclusiveNamespaces.length!=0) {
380-
inclusiveNamespacesPrefixList = inclusiveNamespaces[0].getAttribute('PrefixList').split(" ");
381-
}
382-
}
383371

384372
/**
385373
* Search for ancestor namespaces before validating signature.
386374
*/
387375
var ancestorNamespaces = [];
388376
ancestorNamespaces = findAncestorNs(doc, "//*[local-name()='SignedInfo']");
389377

390-
/**
391-
* If you have a PrefixList then use it and the ancestors to add the necessary namespaces
392-
*/
393-
if (inclusiveNamespacesPrefixList) {
394-
inclusiveNamespacesPrefixList.forEach(function(inclusiveNamespacesPrefix) {
395-
ancestorNamespaces.forEach(function (ancestorNamespace) {
396-
if (inclusiveNamespacesPrefix == ancestorNamespace.prefix) {
397-
signedInfo[0].setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' + ancestorNamespace.prefix, ancestorNamespace.namespaceURI);
398-
}
399-
})
400-
})
401-
}
402-
403378
var c14nOptions = {
404-
ancestorNamespaces: ancestorNamespaces,
405-
inclusiveNamespacesPrefixList: inclusiveNamespacesPrefixList
379+
ancestorNamespaces: ancestorNamespaces
406380
};
407381
var signedInfoCanon = this.getCanonXml([this.canonicalizationAlgorithm], signedInfo[0], c14nOptions)
408382
var signer = this.findSignatureAlgorithm(this.signatureAlgorithm)
409-
410383
var res = signer.verifySignature(signedInfoCanon, this.signingKey, this.signatureValue)
411384
if (!res) this.validationErrors.push("invalid signature: the signature value " +
412385
this.signatureValue + " is incorrect")
@@ -486,19 +459,6 @@ SignedXml.prototype.validateReferences = function(doc) {
486459
inclusiveNamespacesPrefixList: ref.inclusiveNamespacesPrefixList,
487460
ancestorNamespaces: ref.ancestorNamespaces
488461
};
489-
490-
if (ref.inclusiveNamespacesPrefixList) {
491-
var prefixList = ref.inclusiveNamespacesPrefixList instanceof Array ? ref.inclusiveNamespacesPrefixList : ref.inclusiveNamespacesPrefixList.split(' ');
492-
prefixList.forEach(function (prefix) {
493-
if (ref.ancestorNamespaces) {
494-
ref.ancestorNamespaces.forEach(function (ancestorNamespace) {
495-
if (prefix == ancestorNamespace.prefix) {
496-
elem[0].setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' + prefix, ancestorNamespace.namespaceURI);
497-
}
498-
})
499-
}
500-
})
501-
}
502462

503463
var canonXml = this.getCanonXml(ref.transforms, elem[0], c14nOptions);
504464

0 commit comments

Comments
 (0)