Skip to content

Commit 9038099

Browse files
authored
Lint code for new linting rules (#300)
1 parent dedb9c3 commit 9038099

9 files changed

+293
-296
lines changed

.eslintrc.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"no-duplicate-imports": "error",
1818
"no-use-before-define": "error",
1919
"curly": "warn",
20-
"eqeqeq": ["error", "smart"],
20+
"eqeqeq": ["warn", "smart"],
2121
"no-var": "warn",
22-
"prefer-const":"warn"
22+
"prefer-const": "warn"
2323
}
24-
}
24+
}

lib/c14n-canonicalization.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* jshint laxcomma: true */
22
var utils = require("./utils");
33

4-
exports.C14nCanonicalization = C14nCanonicalization;
5-
exports.C14nCanonicalizationWithComments = C14nCanonicalizationWithComments;
6-
74
function C14nCanonicalization() {
85
this.includeComments = false;
96
}
@@ -278,8 +275,6 @@ C14nCanonicalization.prototype.getAlgorithmName = function () {
278275
};
279276

280277
// Add c14n#WithComments here (very simple subclass)
281-
exports.C14nCanonicalizationWithComments = C14nCanonicalizationWithComments;
282-
283278
function C14nCanonicalizationWithComments() {
284279
C14nCanonicalization.call(this);
285280
this.includeComments = true;
@@ -292,3 +287,6 @@ C14nCanonicalizationWithComments.prototype.constructor = C14nCanonicalizationWit
292287
C14nCanonicalizationWithComments.prototype.getAlgorithmName = function () {
293288
return "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
294289
};
290+
291+
exports.C14nCanonicalization = C14nCanonicalization;
292+
exports.C14nCanonicalizationWithComments = C14nCanonicalizationWithComments;

lib/enveloped-signature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
var xpath = require("xpath");
22
var utils = require("./utils");
33

4-
exports.EnvelopedSignature = EnvelopedSignature;
5-
64
function EnvelopedSignature() {}
75

86
EnvelopedSignature.prototype.process = function (node, options) {
@@ -41,3 +39,5 @@ EnvelopedSignature.prototype.process = function (node, options) {
4139
EnvelopedSignature.prototype.getAlgorithmName = function () {
4240
return "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
4341
};
42+
43+
exports.EnvelopedSignature = EnvelopedSignature;

lib/exclusive-canonicalization.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* jshint laxcomma: true */
22
var utils = require("./utils");
33

4-
exports.ExclusiveCanonicalization = ExclusiveCanonicalization;
5-
exports.ExclusiveCanonicalizationWithComments = ExclusiveCanonicalizationWithComments;
6-
74
function ExclusiveCanonicalization() {
85
this.includeComments = false;
96
}
@@ -330,8 +327,6 @@ ExclusiveCanonicalization.prototype.getAlgorithmName = function () {
330327
};
331328

332329
// Add c14n#WithComments here (very simple subclass)
333-
exports.ExclusiveCanonicalizationWithComments = ExclusiveCanonicalizationWithComments;
334-
335330
function ExclusiveCanonicalizationWithComments() {
336331
ExclusiveCanonicalization.call(this);
337332
this.includeComments = true;
@@ -346,3 +341,6 @@ ExclusiveCanonicalizationWithComments.prototype.constructor = ExclusiveCanonical
346341
ExclusiveCanonicalizationWithComments.prototype.getAlgorithmName = function () {
347342
return "http://www.w3.org/2001/10/xml-exc-c14n#WithComments";
348343
};
344+
345+
exports.ExclusiveCanonicalization = ExclusiveCanonicalization;
346+
exports.ExclusiveCanonicalizationWithComments = ExclusiveCanonicalizationWithComments;

lib/signed-xml.js

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ var xpath = require("xpath"),
88
FileKeyInfo = require("./file-key-info"),
99
crypto = require("crypto");
1010

11-
exports.SignedXml = SignedXml;
12-
exports.StringKeyInfo = StringKeyInfo;
13-
exports.FileKeyInfo = FileKeyInfo;
14-
1511
/**
1612
* Hash algorithm implementation
1713
*
@@ -177,6 +173,32 @@ function HMACSHA1() {
177173
};
178174
}
179175

176+
function collectAncestorNamespaces(node, nsArray) {
177+
if (!nsArray) {
178+
nsArray = [];
179+
}
180+
181+
var parent = node.parentNode;
182+
183+
if (!parent) {
184+
return nsArray;
185+
}
186+
187+
if (parent.attributes && parent.attributes.length > 0) {
188+
for (var i = 0; i < parent.attributes.length; i++) {
189+
var attr = parent.attributes[i];
190+
if (attr && attr.nodeName && attr.nodeName.search(/^xmlns:/) !== -1) {
191+
nsArray.push({
192+
prefix: attr.nodeName.replace(/^xmlns:/, ""),
193+
namespaceURI: attr.nodeValue,
194+
});
195+
}
196+
}
197+
}
198+
199+
return collectAncestorNamespaces(parent, nsArray);
200+
}
201+
180202
/**
181203
* Extract ancestor namespaces in order to import it to root of document subset
182204
* which is being canonicalized for non-exclusive c14n.
@@ -233,30 +255,36 @@ function findAncestorNs(doc, docSubsetXpath, namespaceResolver) {
233255
return returningNs;
234256
}
235257

236-
function collectAncestorNamespaces(node, nsArray) {
237-
if (!nsArray) {
238-
nsArray = [];
258+
function validateDigestValue(digest, expectedDigest) {
259+
var buffer, expectedBuffer;
260+
261+
var majorVersion = /^v(\d+)/.exec(process.version)[1];
262+
263+
if (+majorVersion >= 6) {
264+
buffer = Buffer.from(digest, "base64");
265+
expectedBuffer = Buffer.from(expectedDigest, "base64");
266+
} else {
267+
// Compatibility with Node < 5.10.0
268+
buffer = new Buffer(digest, "base64");
269+
expectedBuffer = new Buffer(expectedDigest, "base64");
239270
}
240271

241-
var parent = node.parentNode;
272+
if (typeof buffer.equals === "function") {
273+
return buffer.equals(expectedBuffer);
274+
}
242275

243-
if (!parent) {
244-
return nsArray;
276+
// Compatibility with Node < 0.11.13
277+
if (buffer.length !== expectedBuffer.length) {
278+
return false;
245279
}
246280

247-
if (parent.attributes && parent.attributes.length > 0) {
248-
for (var i = 0; i < parent.attributes.length; i++) {
249-
var attr = parent.attributes[i];
250-
if (attr && attr.nodeName && attr.nodeName.search(/^xmlns:/) !== -1) {
251-
nsArray.push({
252-
prefix: attr.nodeName.replace(/^xmlns:/, ""),
253-
namespaceURI: attr.nodeValue,
254-
});
255-
}
281+
for (var i = 0; i < buffer.length; i++) {
282+
if (buffer[i] !== expectedBuffer[i]) {
283+
return false;
256284
}
257285
}
258286

259-
return collectAncestorNamespaces(parent, nsArray);
287+
return true;
260288
}
261289

262290
/**
@@ -540,38 +568,6 @@ SignedXml.prototype.validateReferences = function (doc) {
540568
return true;
541569
};
542570

543-
function validateDigestValue(digest, expectedDigest) {
544-
var buffer, expectedBuffer;
545-
546-
var majorVersion = /^v(\d+)/.exec(process.version)[1];
547-
548-
if (+majorVersion >= 6) {
549-
buffer = Buffer.from(digest, "base64");
550-
expectedBuffer = Buffer.from(expectedDigest, "base64");
551-
} else {
552-
// Compatibility with Node < 5.10.0
553-
buffer = new Buffer(digest, "base64");
554-
expectedBuffer = new Buffer(expectedDigest, "base64");
555-
}
556-
557-
if (typeof buffer.equals === "function") {
558-
return buffer.equals(expectedBuffer);
559-
}
560-
561-
// Compatibility with Node < 0.11.13
562-
if (buffer.length !== expectedBuffer.length) {
563-
return false;
564-
}
565-
566-
for (var i = 0; i < buffer.length; i++) {
567-
if (buffer[i] !== expectedBuffer[i]) {
568-
return false;
569-
}
570-
}
571-
572-
return true;
573-
}
574-
575571
SignedXml.prototype.loadSignature = function (signatureNode) {
576572
if (typeof signatureNode === "string") {
577573
this.signatureNode = signatureNode = new Dom().parseFromString(signatureNode);
@@ -1132,3 +1128,7 @@ SignedXml.prototype.getOriginalXmlWithIds = function () {
11321128
SignedXml.prototype.getSignedXml = function () {
11331129
return this.signedXml;
11341130
};
1131+
1132+
exports.SignedXml = SignedXml;
1133+
exports.StringKeyInfo = StringKeyInfo;
1134+
exports.FileKeyInfo = FileKeyInfo;

lib/utils.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
var select = require("xpath").select;
22

3+
function attrEqualsExplicitly(attr, localName, namespace) {
4+
return attr.localName == localName && (attr.namespaceURI == namespace || !namespace);
5+
}
6+
7+
function attrEqualsImplicitly(attr, localName, namespace, node) {
8+
return (
9+
attr.localName == localName &&
10+
((!attr.namespaceURI && node.namespaceURI == namespace) || !namespace)
11+
);
12+
}
13+
314
function findAttr(node, localName, namespace) {
415
for (var i = 0; i < node.attributes.length; i++) {
516
var attr = node.attributes[i];
@@ -32,17 +43,6 @@ function findChilds(node, localName, namespace) {
3243
return res;
3344
}
3445

35-
function attrEqualsExplicitly(attr, localName, namespace) {
36-
return attr.localName == localName && (attr.namespaceURI == namespace || !namespace);
37-
}
38-
39-
function attrEqualsImplicitly(attr, localName, namespace, node) {
40-
return (
41-
attr.localName == localName &&
42-
((!attr.namespaceURI && node.namespaceURI == namespace) || !namespace)
43-
);
44-
}
45-
4646
var xml_special_to_encoded_attribute = {
4747
"&": "&amp;",
4848
"<": "&lt;",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
},
2727
"scripts": {
2828
"changelog": "gren changelog --override --generate --head master",
29-
"lint": "eslint --ext .js \"**/*.js\" --cache && npm run prettier-check",
30-
"lint:fix": "eslint --ext .js --fix \"**/*.js\" && npm run prettier-format",
29+
"lint": "eslint --ext .js \"{lib,test}/*.js\" --cache && npm run prettier-check",
30+
"lint:fix": "eslint --ext .js --fix \"{lib,test}/*.js\" && npm run prettier-format",
3131
"prettier-check": "prettier --config .prettierrc.json --check .",
3232
"prettier-format": "prettier --config .prettierrc.json --write .",
3333
"prerelease": "git clean -xfd && npm ci && npm test",

test/signature-integration-tests.js

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,40 @@ var xpath = require("xpath"),
66
var expect = require("chai").expect;
77

88
describe("Signature integration tests", function () {
9+
function verifySignature(xml, expected, xpath) {
10+
var sig = new SignedXml();
11+
sig.signingKey = fs.readFileSync("./test/static/client.pem");
12+
sig.keyInfo = null;
13+
14+
xpath.map(function (n) {
15+
sig.addReference(n);
16+
});
17+
18+
sig.computeSignature(xml);
19+
var signed = sig.getSignedXml();
20+
21+
//fs.writeFileSync("./test/validators/XmlCryptoUtilities/XmlCryptoUtilities/bin/Debug/signedExample.xml", signed)
22+
var expectedContent = fs.readFileSync(expected).toString();
23+
expect(signed, "signature xml different than expected").to.equal(expectedContent);
24+
/*
25+
var spawn = require('child_process').spawn
26+
var proc = spawn('./test/validators/XmlCryptoUtilities/XmlCryptoUtilities/bin/Debug/XmlCryptoUtilities.exe', ['verify'])
27+
28+
proc.stdout.on('data', function (data) {
29+
console.log('stdout: ' + data);
30+
});
31+
32+
proc.stderr.on('data', function (data) {
33+
console.log('stderr: ' + data);
34+
});
35+
36+
proc.on('exit', function (code) {
37+
test.equal(0, code, "signature validation failed")
38+
test.done()
39+
});
40+
*/
41+
}
42+
943
it("verify signature", function () {
1044
var xml =
1145
'<root><x xmlns="ns"></x><y z_attr="value" a_attr1="foo"></y><z><ns:w ns:attr="value" xmlns:ns="myns"></ns:w></z></root>';
@@ -188,37 +222,3 @@ describe("Signature integration tests", function () {
188222
).to.equal(2);
189223
});
190224
});
191-
192-
function verifySignature(xml, expected, xpath) {
193-
var sig = new SignedXml();
194-
sig.signingKey = fs.readFileSync("./test/static/client.pem");
195-
sig.keyInfo = null;
196-
197-
xpath.map(function (n) {
198-
sig.addReference(n);
199-
});
200-
201-
sig.computeSignature(xml);
202-
var signed = sig.getSignedXml();
203-
204-
//fs.writeFileSync("./test/validators/XmlCryptoUtilities/XmlCryptoUtilities/bin/Debug/signedExample.xml", signed)
205-
var expectedContent = fs.readFileSync(expected).toString();
206-
expect(signed, "signature xml different than expected").to.equal(expectedContent);
207-
/*
208-
var spawn = require('child_process').spawn
209-
var proc = spawn('./test/validators/XmlCryptoUtilities/XmlCryptoUtilities/bin/Debug/XmlCryptoUtilities.exe', ['verify'])
210-
211-
proc.stdout.on('data', function (data) {
212-
console.log('stdout: ' + data);
213-
});
214-
215-
proc.stderr.on('data', function (data) {
216-
console.log('stderr: ' + data);
217-
});
218-
219-
proc.on('exit', function (code) {
220-
test.equal(0, code, "signature validation failed")
221-
test.done()
222-
});
223-
*/
224-
}

0 commit comments

Comments
 (0)