Skip to content

Commit 52aa883

Browse files
authored
Merge pull request #218 from paulish/master
fix for #201
2 parents 66fbb50 + 4e8d41e commit 52aa883

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

lib/signed-xml.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,11 @@ function HMACSHA1() {
211211
*
212212
* @param {object} doc - Usually a product from `new DOMParser().parseFromString()`
213213
* @param {string} docSubsetXpath - xpath query to get document subset being canonicalized
214+
* @param {object} namespaceResolver - xpath namespace resolver
214215
* @returns {Array} i.e. [{prefix: "saml", namespaceURI: "urn:oasis:names:tc:SAML:2.0:assertion"}]
215216
*/
216-
function findAncestorNs(doc, docSubsetXpath){
217-
var docSubset = xpath.select(docSubsetXpath, doc);
217+
function findAncestorNs(doc, docSubsetXpath, namespaceResolver){
218+
var docSubset = xpath.selectWithResolver(docSubsetXpath, doc, namespaceResolver);
218219

219220
if(!Array.isArray(docSubset) || docSubset.length < 1){
220221
return [];
@@ -442,7 +443,7 @@ SignedXml.prototype.getCanonReferenceXml = function(doc, ref, node) {
442443
* Search for ancestor namespaces before canonicalization.
443444
*/
444445
if(Array.isArray(ref.transforms)){
445-
ref.ancestorNamespaces = findAncestorNs(doc, ref.xpath)
446+
ref.ancestorNamespaces = findAncestorNs(doc, ref.xpath, this.namespaceResolver)
446447
}
447448

448449
var c14nOptions = {
@@ -732,6 +733,13 @@ SignedXml.prototype.computeSignature = function(xml, opts, callback) {
732733
attrs = opts.attrs || {};
733734
location = opts.location || {};
734735
var existingPrefixes = opts.existingPrefixes || {};
736+
737+
this.namespaceResolver = {
738+
lookupNamespaceURI: function(prefix) {
739+
return existingPrefixes[prefix];
740+
}
741+
}
742+
735743
// defaults to the root node
736744
location.reference = location.reference || "/*";
737745
// defaults to append action
@@ -874,7 +882,7 @@ SignedXml.prototype.createReferences = function(doc, prefix) {
874882
if (!this.references.hasOwnProperty(n)) continue;
875883

876884
var ref = this.references[n]
877-
, nodes = xpath.select(ref.xpath, doc)
885+
, nodes = xpath.selectWithResolver(ref.xpath, doc, this.namespaceResolver)
878886

879887
if (nodes.length==0) {
880888
throw new Error('the following xpath cannot be signed because it was not found: ' + ref.xpath)

test/signature-unit-tests.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ module.exports = {
1313
test.done();
1414
},
1515

16+
"signer adds references with namespaces": function(test) {
17+
verifyReferenceNS(test);
18+
test.done();
19+
},
20+
1621
"signer does not duplicate existing id attributes": function (test) {
1722
verifyDoesNotDuplicateIdAttributes(test, null, "")
1823
verifyDoesNotDuplicateIdAttributes(test, "wssecurity", "wsu:")
@@ -778,6 +783,26 @@ function verifyAddsAttrs(test) {
778783
test.strictEqual(signatureNode.getAttribute("xmlns"), "http://www.w3.org/2000/09/xmldsig#", "xmlns attribute is not equal to the expected value: \"http://www.w3.org/2000/09/xmldsig#\"")
779784
}
780785

786+
function verifyReferenceNS(test) {
787+
var xml = "<root xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><name wsu:Id=\"_1\">xml-crypto</name><repository wsu:Id=\"_2\">github</repository></root>"
788+
var sig = new SignedXml("wssecurity")
789+
790+
sig.signingKey = fs.readFileSync("./test/static/client.pem")
791+
792+
sig.addReference("//*[@wsu:Id]")
793+
794+
sig.computeSignature(xml, {
795+
existingPrefixes: {
796+
wsu: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
797+
}
798+
})
799+
800+
var signedXml = sig.getSignatureXml()
801+
var doc = new dom().parseFromString(signedXml)
802+
var references = select("//*[local-name(.)='Reference']", doc)
803+
test.equal(references.length, 2)
804+
}
805+
781806
function nodeExists(test, doc, xpath) {
782807
if (!doc && !xpath) return
783808
var node = select(xpath, doc)

0 commit comments

Comments
 (0)