Skip to content

Commit

Permalink
Lint code for new linting rules (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbarth authored Jun 8, 2023
1 parent dedb9c3 commit 9038099
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 296 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"no-duplicate-imports": "error",
"no-use-before-define": "error",
"curly": "warn",
"eqeqeq": ["error", "smart"],
"eqeqeq": ["warn", "smart"],
"no-var": "warn",
"prefer-const":"warn"
"prefer-const": "warn"
}
}
}
8 changes: 3 additions & 5 deletions lib/c14n-canonicalization.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* jshint laxcomma: true */
var utils = require("./utils");

exports.C14nCanonicalization = C14nCanonicalization;
exports.C14nCanonicalizationWithComments = C14nCanonicalizationWithComments;

function C14nCanonicalization() {
this.includeComments = false;
}
Expand Down Expand Up @@ -278,8 +275,6 @@ C14nCanonicalization.prototype.getAlgorithmName = function () {
};

// Add c14n#WithComments here (very simple subclass)
exports.C14nCanonicalizationWithComments = C14nCanonicalizationWithComments;

function C14nCanonicalizationWithComments() {
C14nCanonicalization.call(this);
this.includeComments = true;
Expand All @@ -292,3 +287,6 @@ C14nCanonicalizationWithComments.prototype.constructor = C14nCanonicalizationWit
C14nCanonicalizationWithComments.prototype.getAlgorithmName = function () {
return "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
};

exports.C14nCanonicalization = C14nCanonicalization;
exports.C14nCanonicalizationWithComments = C14nCanonicalizationWithComments;
4 changes: 2 additions & 2 deletions lib/enveloped-signature.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var xpath = require("xpath");
var utils = require("./utils");

exports.EnvelopedSignature = EnvelopedSignature;

function EnvelopedSignature() {}

EnvelopedSignature.prototype.process = function (node, options) {
Expand Down Expand Up @@ -41,3 +39,5 @@ EnvelopedSignature.prototype.process = function (node, options) {
EnvelopedSignature.prototype.getAlgorithmName = function () {
return "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
};

exports.EnvelopedSignature = EnvelopedSignature;
8 changes: 3 additions & 5 deletions lib/exclusive-canonicalization.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* jshint laxcomma: true */
var utils = require("./utils");

exports.ExclusiveCanonicalization = ExclusiveCanonicalization;
exports.ExclusiveCanonicalizationWithComments = ExclusiveCanonicalizationWithComments;

function ExclusiveCanonicalization() {
this.includeComments = false;
}
Expand Down Expand Up @@ -330,8 +327,6 @@ ExclusiveCanonicalization.prototype.getAlgorithmName = function () {
};

// Add c14n#WithComments here (very simple subclass)
exports.ExclusiveCanonicalizationWithComments = ExclusiveCanonicalizationWithComments;

function ExclusiveCanonicalizationWithComments() {
ExclusiveCanonicalization.call(this);
this.includeComments = true;
Expand All @@ -346,3 +341,6 @@ ExclusiveCanonicalizationWithComments.prototype.constructor = ExclusiveCanonical
ExclusiveCanonicalizationWithComments.prototype.getAlgorithmName = function () {
return "http://www.w3.org/2001/10/xml-exc-c14n#WithComments";
};

exports.ExclusiveCanonicalization = ExclusiveCanonicalization;
exports.ExclusiveCanonicalizationWithComments = ExclusiveCanonicalizationWithComments;
104 changes: 52 additions & 52 deletions lib/signed-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ var xpath = require("xpath"),
FileKeyInfo = require("./file-key-info"),
crypto = require("crypto");

exports.SignedXml = SignedXml;
exports.StringKeyInfo = StringKeyInfo;
exports.FileKeyInfo = FileKeyInfo;

/**
* Hash algorithm implementation
*
Expand Down Expand Up @@ -177,6 +173,32 @@ function HMACSHA1() {
};
}

function collectAncestorNamespaces(node, nsArray) {
if (!nsArray) {
nsArray = [];
}

var parent = node.parentNode;

if (!parent) {
return nsArray;
}

if (parent.attributes && parent.attributes.length > 0) {
for (var i = 0; i < parent.attributes.length; i++) {
var attr = parent.attributes[i];
if (attr && attr.nodeName && attr.nodeName.search(/^xmlns:/) !== -1) {
nsArray.push({
prefix: attr.nodeName.replace(/^xmlns:/, ""),
namespaceURI: attr.nodeValue,
});
}
}
}

return collectAncestorNamespaces(parent, nsArray);
}

/**
* Extract ancestor namespaces in order to import it to root of document subset
* which is being canonicalized for non-exclusive c14n.
Expand Down Expand Up @@ -233,30 +255,36 @@ function findAncestorNs(doc, docSubsetXpath, namespaceResolver) {
return returningNs;
}

function collectAncestorNamespaces(node, nsArray) {
if (!nsArray) {
nsArray = [];
function validateDigestValue(digest, expectedDigest) {
var buffer, expectedBuffer;

var majorVersion = /^v(\d+)/.exec(process.version)[1];

if (+majorVersion >= 6) {
buffer = Buffer.from(digest, "base64");
expectedBuffer = Buffer.from(expectedDigest, "base64");
} else {
// Compatibility with Node < 5.10.0
buffer = new Buffer(digest, "base64");
expectedBuffer = new Buffer(expectedDigest, "base64");
}

var parent = node.parentNode;
if (typeof buffer.equals === "function") {
return buffer.equals(expectedBuffer);
}

if (!parent) {
return nsArray;
// Compatibility with Node < 0.11.13
if (buffer.length !== expectedBuffer.length) {
return false;
}

if (parent.attributes && parent.attributes.length > 0) {
for (var i = 0; i < parent.attributes.length; i++) {
var attr = parent.attributes[i];
if (attr && attr.nodeName && attr.nodeName.search(/^xmlns:/) !== -1) {
nsArray.push({
prefix: attr.nodeName.replace(/^xmlns:/, ""),
namespaceURI: attr.nodeValue,
});
}
for (var i = 0; i < buffer.length; i++) {
if (buffer[i] !== expectedBuffer[i]) {
return false;
}
}

return collectAncestorNamespaces(parent, nsArray);
return true;
}

/**
Expand Down Expand Up @@ -540,38 +568,6 @@ SignedXml.prototype.validateReferences = function (doc) {
return true;
};

function validateDigestValue(digest, expectedDigest) {
var buffer, expectedBuffer;

var majorVersion = /^v(\d+)/.exec(process.version)[1];

if (+majorVersion >= 6) {
buffer = Buffer.from(digest, "base64");
expectedBuffer = Buffer.from(expectedDigest, "base64");
} else {
// Compatibility with Node < 5.10.0
buffer = new Buffer(digest, "base64");
expectedBuffer = new Buffer(expectedDigest, "base64");
}

if (typeof buffer.equals === "function") {
return buffer.equals(expectedBuffer);
}

// Compatibility with Node < 0.11.13
if (buffer.length !== expectedBuffer.length) {
return false;
}

for (var i = 0; i < buffer.length; i++) {
if (buffer[i] !== expectedBuffer[i]) {
return false;
}
}

return true;
}

SignedXml.prototype.loadSignature = function (signatureNode) {
if (typeof signatureNode === "string") {
this.signatureNode = signatureNode = new Dom().parseFromString(signatureNode);
Expand Down Expand Up @@ -1132,3 +1128,7 @@ SignedXml.prototype.getOriginalXmlWithIds = function () {
SignedXml.prototype.getSignedXml = function () {
return this.signedXml;
};

exports.SignedXml = SignedXml;
exports.StringKeyInfo = StringKeyInfo;
exports.FileKeyInfo = FileKeyInfo;
22 changes: 11 additions & 11 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
var select = require("xpath").select;

function attrEqualsExplicitly(attr, localName, namespace) {
return attr.localName == localName && (attr.namespaceURI == namespace || !namespace);
}

function attrEqualsImplicitly(attr, localName, namespace, node) {
return (
attr.localName == localName &&
((!attr.namespaceURI && node.namespaceURI == namespace) || !namespace)
);
}

function findAttr(node, localName, namespace) {
for (var i = 0; i < node.attributes.length; i++) {
var attr = node.attributes[i];
Expand Down Expand Up @@ -32,17 +43,6 @@ function findChilds(node, localName, namespace) {
return res;
}

function attrEqualsExplicitly(attr, localName, namespace) {
return attr.localName == localName && (attr.namespaceURI == namespace || !namespace);
}

function attrEqualsImplicitly(attr, localName, namespace, node) {
return (
attr.localName == localName &&
((!attr.namespaceURI && node.namespaceURI == namespace) || !namespace)
);
}

var xml_special_to_encoded_attribute = {
"&": "&amp;",
"<": "&lt;",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
},
"scripts": {
"changelog": "gren changelog --override --generate --head master",
"lint": "eslint --ext .js \"**/*.js\" --cache && npm run prettier-check",
"lint:fix": "eslint --ext .js --fix \"**/*.js\" && npm run prettier-format",
"lint": "eslint --ext .js \"{lib,test}/*.js\" --cache && npm run prettier-check",
"lint:fix": "eslint --ext .js --fix \"{lib,test}/*.js\" && npm run prettier-format",
"prettier-check": "prettier --config .prettierrc.json --check .",
"prettier-format": "prettier --config .prettierrc.json --write .",
"prerelease": "git clean -xfd && npm ci && npm test",
Expand Down
68 changes: 34 additions & 34 deletions test/signature-integration-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,40 @@ var xpath = require("xpath"),
var expect = require("chai").expect;

describe("Signature integration tests", function () {
function verifySignature(xml, expected, xpath) {
var sig = new SignedXml();
sig.signingKey = fs.readFileSync("./test/static/client.pem");
sig.keyInfo = null;

xpath.map(function (n) {
sig.addReference(n);
});

sig.computeSignature(xml);
var signed = sig.getSignedXml();

//fs.writeFileSync("./test/validators/XmlCryptoUtilities/XmlCryptoUtilities/bin/Debug/signedExample.xml", signed)
var expectedContent = fs.readFileSync(expected).toString();
expect(signed, "signature xml different than expected").to.equal(expectedContent);
/*
var spawn = require('child_process').spawn
var proc = spawn('./test/validators/XmlCryptoUtilities/XmlCryptoUtilities/bin/Debug/XmlCryptoUtilities.exe', ['verify'])
proc.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
proc.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
proc.on('exit', function (code) {
test.equal(0, code, "signature validation failed")
test.done()
});
*/
}

it("verify signature", function () {
var xml =
'<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>';
Expand Down Expand Up @@ -188,37 +222,3 @@ describe("Signature integration tests", function () {
).to.equal(2);
});
});

function verifySignature(xml, expected, xpath) {
var sig = new SignedXml();
sig.signingKey = fs.readFileSync("./test/static/client.pem");
sig.keyInfo = null;

xpath.map(function (n) {
sig.addReference(n);
});

sig.computeSignature(xml);
var signed = sig.getSignedXml();

//fs.writeFileSync("./test/validators/XmlCryptoUtilities/XmlCryptoUtilities/bin/Debug/signedExample.xml", signed)
var expectedContent = fs.readFileSync(expected).toString();
expect(signed, "signature xml different than expected").to.equal(expectedContent);
/*
var spawn = require('child_process').spawn
var proc = spawn('./test/validators/XmlCryptoUtilities/XmlCryptoUtilities/bin/Debug/XmlCryptoUtilities.exe', ['verify'])
proc.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
proc.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
proc.on('exit', function (code) {
test.equal(0, code, "signature validation failed")
test.done()
});
*/
}
Loading

0 comments on commit 9038099

Please sign in to comment.