Skip to content

Commit 1027f79

Browse files
committed
Replace xpath.js with xpath
xpath.js was forked and has since been more actively maintained than the original, so switch to using that instead This commit seeks to resolve the issues found in #165 by using the abovementioned package to avoid a possible bug with xpath.js. See #165 for further information
1 parent 8d31ffd commit 1027f79

18 files changed

+66
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ You might find it difficult to guess such transforms, but there are typical tran
179179

180180
### xpath
181181

182-
See [xpath.js](https://github.com/yaronn/xpath.js) for usage
182+
See [xpath](https://github.com/goto100/xpath) for usage
183183

184184
### SignedXml
185185

example/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function signXml(xml, xpath, key, dest)
1616
function validateXml(xml, key)
1717
{
1818
var doc = new dom().parseFromString(xml)
19-
var signature = select(doc, "/*/*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']")[0]
19+
var signature = select("/*/*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']", doc)[0]
2020
var sig = new SignedXml()
2121
sig.keyInfoProvider = new FileKeyInfo(key)
2222
sig.loadSignature(signature.toString())

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
module.exports = require('./lib/signed-xml')
2-
module.exports.xpath = require('xpath.js')
2+
module.exports.xpath = require('xpath').select

lib/enveloped-signature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var xpath = require('xpath.js');
1+
var xpath = require('xpath');
22

33
exports.EnvelopedSignature = EnvelopedSignature;
44

55
function EnvelopedSignature() {
66
}
77

88
EnvelopedSignature.prototype.process = function (node) {
9-
var signature = xpath(node, "./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']")[0];
9+
var signature = xpath.select("./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']", node)[0];
1010
if (signature) signature.parentNode.removeChild(signature);
1111
return node;
1212
};

lib/signed-xml.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
var select = require('xpath.js')
1+
var xpath = require('xpath')
22
, Dom = require('xmldom').DOMParser
33
, utils = require('./utils')
44
, c14n = require('./c14n-canonicalization')
55
, execC14n = require('./exclusive-canonicalization')
66
, EnvelopedSignature = require('./enveloped-signature').EnvelopedSignature
77
, crypto = require('crypto')
88
, fs = require('fs')
9-
, xpath = require('xpath.js')
109

1110
exports.SignedXml = SignedXml
1211
exports.FileKeyInfo = FileKeyInfo
@@ -209,7 +208,7 @@ function HMACSHA1() {
209208
* @returns {Array} i.e. [{prefix: "saml", namespaceURI: "urn:oasis:names:tc:SAML:2.0:assertion"}]
210209
*/
211210
function findAncestorNs(doc, docSubsetXpath){
212-
var docSubset = xpath(doc, docSubsetXpath);
211+
var docSubset = xpath.select(docSubsetXpath, doc);
213212

214213
if(!Array.isArray(docSubset) || docSubset.length < 1){
215214
return [];
@@ -418,7 +417,7 @@ SignedXml.prototype.validateReferences = function(doc) {
418417
var elemXpath;
419418

420419
if (uri=="") {
421-
elem = select(doc, "//*")
420+
elem = xpath.select("//*", doc)
422421
}
423422
else if (uri.indexOf("'") != -1) {
424423
// xpath injection
@@ -429,7 +428,7 @@ SignedXml.prototype.validateReferences = function(doc) {
429428
for (var index in this.idAttributes) {
430429
if (!this.idAttributes.hasOwnProperty(index)) continue;
431430
var tmp_elemXpath = "//*[@*[local-name(.)='" + this.idAttributes[index] + "']='" + uri + "']";
432-
var tmp_elem = select(doc, tmp_elemXpath)
431+
var tmp_elem = xpath.select(tmp_elemXpath, doc)
433432
num_elements_for_id += tmp_elem.length;
434433
if (tmp_elem.length > 0) {
435434
elem = tmp_elem;
@@ -519,15 +518,15 @@ SignedXml.prototype.loadSignature = function(signatureNode) {
519518

520519
this.signatureXml = signatureNode.toString();
521520

522-
var nodes = select(signatureNode, ".//*[local-name(.)='CanonicalizationMethod']/@Algorithm")
521+
var nodes = xpath.select(".//*[local-name(.)='CanonicalizationMethod']/@Algorithm", signatureNode)
523522
if (nodes.length==0) throw new Error("could not find CanonicalizationMethod/@Algorithm element")
524523
this.canonicalizationAlgorithm = nodes[0].value
525524

526525
this.signatureAlgorithm =
527526
utils.findFirst(signatureNode, ".//*[local-name(.)='SignatureMethod']/@Algorithm").value
528527

529528
this.references = []
530-
var references = select(signatureNode, ".//*[local-name(.)='SignedInfo']/*[local-name(.)='Reference']")
529+
var references = xpath.select(".//*[local-name(.)='SignedInfo']/*[local-name(.)='Reference']", signatureNode)
531530
if (references.length == 0) throw new Error("could not find any Reference elements")
532531

533532
for (var i in references) {
@@ -539,7 +538,7 @@ SignedXml.prototype.loadSignature = function(signatureNode) {
539538
this.signatureValue =
540539
utils.findFirst(signatureNode, ".//*[local-name(.)='SignatureValue']/text()").data.replace(/\r?\n/g, '')
541540

542-
this.keyInfo = select(signatureNode, ".//*[local-name(.)='KeyInfo']")
541+
this.keyInfo = xpath.select(".//*[local-name(.)='KeyInfo']", signatureNode)
543542
}
544543

545544
/**
@@ -576,7 +575,7 @@ SignedXml.prototype.loadReference = function(ref) {
576575
transforms.push(utils.findAttr(trans, "Algorithm").value)
577576
}
578577

579-
var inclusiveNamespaces = select(transformsNode, "//*[local-name(.)='InclusiveNamespaces']");
578+
var inclusiveNamespaces = xpath.select("//*[local-name(.)='InclusiveNamespaces']", transformsNode);
580579
if (inclusiveNamespaces.length > 0) {
581580
inclusiveNamespacesPrefixList = inclusiveNamespaces[0].getAttribute('PrefixList');
582581
}
@@ -685,7 +684,7 @@ SignedXml.prototype.computeSignature = function(xml, opts) {
685684

686685
var signatureDoc = new Dom().parseFromString(this.signatureXml)
687686

688-
var referenceNode = select(doc, location.reference);
687+
var referenceNode = xpath.select(location.reference, doc);
689688

690689
if (!referenceNode || referenceNode.length === 0) {
691690
throw new Error("the following xpath cannot be used because it was not found: " + location.reference);
@@ -736,7 +735,7 @@ SignedXml.prototype.createReferences = function(doc, prefix) {
736735
if (!this.references.hasOwnProperty(n)) continue;
737736

738737
var ref = this.references[n]
739-
, nodes = select(doc, ref.xpath)
738+
, nodes = xpath.select(ref.xpath, doc)
740739

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

lib/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var select = require('xpath.js');
1+
var select = require('xpath').select
22

33
function findAttr(node, localName, namespace) {
44
for (var i = 0; i<node.attributes.length; i++) {
@@ -12,7 +12,7 @@ function findAttr(node, localName, namespace) {
1212
}
1313

1414
function findFirst(doc, xpath) {
15-
var nodes = select(doc, xpath)
15+
var nodes = select(xpath, doc)
1616
if (nodes.length==0) throw "could not find xpath " + xpath
1717
return nodes[0]
1818
}

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"dependencies": {
1313
"xmldom": "0.1.27",
14-
"xpath.js": ">=0.0.3"
14+
"xpath": "0.0.27"
1515
},
1616
"devDependencies": {
1717
"nodeunit": "^0.11.3"

test/c14n-non-exclusive-unit-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
var C14nCanonicalization = require("../lib/c14n-canonicalization").C14nCanonicalization
22
, Dom = require('xmldom').DOMParser
3-
, select = require('xpath.js')
3+
, select = require('xpath').select
44
, findAncestorNs = require('../lib/signed-xml').SignedXml.findAncestorNs
55

66
var test_C14nCanonicalization = function(test, xml, xpath, expected) {
77
var doc = new Dom().parseFromString(xml);
8-
var elem = select(doc, xpath)[0];
8+
var elem = select(xpath, doc)[0];
99
var can = new C14nCanonicalization();
1010
var result = can.process(elem, {
1111
ancestorNamespaces: findAncestorNs(doc, xpath)

test/c14nWithComments-unit-tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
var c14nWithComments = require("../lib/exclusive-canonicalization").ExclusiveCanonicalizationWithComments
22
, Dom = require('xmldom').DOMParser
3-
, select = require('xpath.js')
3+
, select = require('xpath').select
44
, SignedXml = require('../lib/signed-xml.js').SignedXml
55

66

77
var compare = function(test, xml, xpath, expected, inclusiveNamespacesPrefixList) {
88
test.expect(1)
99
var doc = new Dom().parseFromString(xml)
10-
var elem = select(doc, xpath)[0]
10+
var elem = select(xpath, doc)[0]
1111
var can = new c14nWithComments()
1212
var result = can.process(elem, { inclusiveNamespacesPrefixList: inclusiveNamespacesPrefixList }).toString()
1313

@@ -345,7 +345,7 @@ module.exports = {
345345
"Multiple Canonicalization with namespace definition outside of signed element": function (test) {
346346
//var doc = new Dom().parseFromString("<x xmlns:p=\"myns\"><p:y><ds:Signature xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\"></ds:Signature></p:y></x>")
347347
var doc = new Dom().parseFromString("<x xmlns:p=\"myns\"><p:y></p:y></x>")
348-
var node = select(doc, "//*[local-name(.)='y']")[0]
348+
var node = select("//*[local-name(.)='y']", doc)[0]
349349
var sig = new SignedXml()
350350
var res = sig.getCanonXml(["http://www.w3.org/2000/09/xmldsig#enveloped-signature", "http://www.w3.org/2001/10/xml-exc-c14n#"], node)
351351
test.equal("<p:y xmlns:p=\"myns\"></p:y>", res)
@@ -358,7 +358,7 @@ module.exports = {
358358
// in a document.
359359
var xml = '<x><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" /><y><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" /></y></x>';
360360
var doc = new Dom().parseFromString(xml);
361-
var node = select(doc, "//*[local-name(.)='y']")[0];
361+
var node = select("//*[local-name(.)='y']", doc)[0];
362362
var sig = new SignedXml();
363363
var transforms = ["http://www.w3.org/2000/09/xmldsig#enveloped-signature"];
364364
var res = sig.getCanonXml(transforms, node);

0 commit comments

Comments
 (0)