Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust code to pass eslint no-var #311

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"no-use-before-define": "error",
"curly": "error",
"eqeqeq": ["warn", "smart"],
"no-var": "warn",
"no-var": "error",
"prefer-const": "warn"
}
}
24 changes: 12 additions & 12 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
/* eslint-disable no-console */

var select = require("xml-crypto").xpath;
var dom = require("@xmldom/xmldom").DOMParser;
var SignedXml = require("xml-crypto").SignedXml;
var FileKeyInfo = require("xml-crypto").FileKeyInfo;
var fs = require("fs");
const select = require("xml-crypto").xpath;
const dom = require("@xmldom/xmldom").DOMParser;
const SignedXml = require("xml-crypto").SignedXml;
const FileKeyInfo = require("xml-crypto").FileKeyInfo;
const fs = require("fs");

function signXml(xml, xpath, key, dest) {
var sig = new SignedXml();
const sig = new SignedXml();
sig.signingKey = fs.readFileSync(key);
sig.addReference(xpath);
sig.computeSignature(xml);
fs.writeFileSync(dest, sig.getSignedXml());
}

function validateXml(xml, key) {
var doc = new dom().parseFromString(xml);
var signature = select(
const doc = new dom().parseFromString(xml);
const signature = select(
"/*/*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
doc
)[0];
var sig = new SignedXml();
const sig = new SignedXml();
sig.keyInfoProvider = new FileKeyInfo(key);
sig.loadSignature(signature.toString());
var res = sig.checkSignature(xml);
const res = sig.checkSignature(xml);
if (!res) {
console.log(sig.validationErrors);
}
return res;
}

var xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";
const xml = "<library>" + "<book>" + "<name>Harry Potter</name>" + "</book>" + "</library>";

//sign an xml document
signXml(xml, "//*[local-name(.)='book']", "client.pem", "result.xml");

console.log("xml signed successfully");

var signedXml = fs.readFileSync("result.xml").toString();
const signedXml = fs.readFileSync("result.xml").toString();
console.log("validating signature...");

//validate an xml document
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var select = require("xpath").select;
const select = require("xpath").select;

module.exports = require("./lib/signed-xml");
module.exports.xpath = function (node, xpath) {
Expand Down
78 changes: 42 additions & 36 deletions lib/c14n-canonicalization.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* jshint laxcomma: true */
var utils = require("./utils");
const utils = require("./utils");

function C14nCanonicalization() {
this.includeComments = false;
Expand All @@ -13,8 +13,8 @@ C14nCanonicalization.prototype.attrCompare = function (a, b) {
return 1;
}

var left = a.namespaceURI + a.localName;
var right = b.namespaceURI + b.localName;
const left = a.namespaceURI + a.localName;
const right = b.namespaceURI + b.localName;

if (left === right) {
return 0;
Expand All @@ -26,20 +26,20 @@ C14nCanonicalization.prototype.attrCompare = function (a, b) {
};

C14nCanonicalization.prototype.nsCompare = function (a, b) {
var attr1 = a.prefix;
var attr2 = b.prefix;
const attr1 = a.prefix;
const attr2 = b.prefix;
if (attr1 == attr2) {
return 0;
}
return attr1.localeCompare(attr2);
};

C14nCanonicalization.prototype.renderAttrs = function (node, defaultNS) {
var a;
var i;
var attr;
var res = [];
var attrListToRender = [];
let a;
let i;
let attr;
const res = [];
const attrListToRender = [];

if (node.nodeType === 8) {
return this.renderComment(node);
Expand Down Expand Up @@ -89,14 +89,14 @@ C14nCanonicalization.prototype.renderNs = function (
defaultNsForPrefix,
ancestorNamespaces
) {
var a;
var i;
var p;
var attr;
var res = [];
var newDefaultNs = defaultNs;
var nsListToRender = [];
var currNs = node.namespaceURI || "";
let a;
let i;
let p;
let attr;
const res = [];
let newDefaultNs = defaultNs;
const nsListToRender = [];
const currNs = node.namespaceURI || "";

//handle the namespaceof the node itself
if (node.prefix) {
Expand Down Expand Up @@ -141,12 +141,12 @@ C14nCanonicalization.prototype.renderNs = function (

if (Array.isArray(ancestorNamespaces) && ancestorNamespaces.length > 0) {
// Remove namespaces which are already present in nsListToRender
for (var p1 in ancestorNamespaces) {
for (const p1 in ancestorNamespaces) {
if (!ancestorNamespaces.hasOwnProperty(p1)) {
continue;
}
var alreadyListed = false;
for (var p2 in nsListToRender) {
let alreadyListed = false;
for (const p2 in nsListToRender) {
if (
nsListToRender[p2].prefix === ancestorNamespaces[p1].prefix &&
nsListToRender[p2].namespaceURI === ancestorNamespaces[p1].namespaceURI
Expand Down Expand Up @@ -190,10 +190,16 @@ C14nCanonicalization.prototype.processInner = function (
return utils.encodeSpecialCharactersInText(node.data);
}

var i;
var pfxCopy;
var ns = this.renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces);
var res = ["<", node.tagName, ns.rendered, this.renderAttrs(node, ns.newDefaultNs), ">"];
let i;
let pfxCopy;
const ns = this.renderNs(
node,
prefixesInScope,
defaultNs,
defaultNsForPrefix,
ancestorNamespaces
);
const res = ["<", node.tagName, ns.rendered, this.renderAttrs(node, ns.newDefaultNs), ">"];

for (i = 0; i < node.childNodes.length; ++i) {
pfxCopy = prefixesInScope.slice(0);
Expand All @@ -212,13 +218,13 @@ C14nCanonicalization.prototype.renderComment = function (node) {
return "";
}

var isOutsideDocument = node.ownerDocument === node.parentNode;
var isBeforeDocument = null;
var isAfterDocument = null;
const isOutsideDocument = node.ownerDocument === node.parentNode;
let isBeforeDocument = null;
let isAfterDocument = null;

if (isOutsideDocument) {
var nextNode = node;
var previousNode = node;
let nextNode = node;
let previousNode = node;

while (nextNode !== null) {
if (nextNode === node.ownerDocument.documentElement) {
Expand Down Expand Up @@ -257,16 +263,16 @@ C14nCanonicalization.prototype.renderComment = function (node) {
*/
C14nCanonicalization.prototype.process = function (node, options) {
options = options || {};
var defaultNs = options.defaultNs || "";
var defaultNsForPrefix = options.defaultNsForPrefix || {};
var ancestorNamespaces = options.ancestorNamespaces || [];
const defaultNs = options.defaultNs || "";
const defaultNsForPrefix = options.defaultNsForPrefix || {};
const ancestorNamespaces = options.ancestorNamespaces || [];

var prefixesInScope = [];
for (var i = 0; i < ancestorNamespaces.length; i++) {
const prefixesInScope = [];
for (let i = 0; i < ancestorNamespaces.length; i++) {
prefixesInScope.push(ancestorNamespaces[i].prefix);
}

var res = this.processInner(
const res = this.processInner(
node,
prefixesInScope,
defaultNs,
Expand Down
18 changes: 9 additions & 9 deletions lib/enveloped-signature.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var xpath = require("xpath");
var utils = require("./utils");
const xpath = require("xpath");
const utils = require("./utils");

function EnvelopedSignature() {}

EnvelopedSignature.prototype.process = function (node, options) {
if (null == options.signatureNode) {
// leave this for the moment...
var signature = xpath.select(
const signature = xpath.select(
"./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
node
)[0];
Expand All @@ -15,21 +15,21 @@ EnvelopedSignature.prototype.process = function (node, options) {
}
return node;
}
var signatureNode = options.signatureNode;
var expectedSignatureValue = utils.findFirst(
const signatureNode = options.signatureNode;
const expectedSignatureValue = utils.findFirst(
signatureNode,
".//*[local-name(.)='SignatureValue']/text()"
).data;
var signatures = xpath.select(
const signatures = xpath.select(
".//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
node
);
for (var h in signatures) {
for (const h in signatures) {
if (!signatures.hasOwnProperty(h)) {
continue;
}
var nodeSignature = signatures[h];
var signatureValue = utils.findFirst(
const nodeSignature = signatures[h];
const signatureValue = utils.findFirst(
nodeSignature,
".//*[local-name(.)='SignatureValue']/text()"
).data;
Expand Down
Loading