From bba9f7cbf4a6ed89c22a67a8591fb5ce9f2e583a Mon Sep 17 00:00:00 2001 From: Chris Barth Date: Fri, 23 Jun 2023 04:46:38 -0400 Subject: [PATCH] Enforce eslint `eqeqeq` rule (#320) --- .eslintrc.json | 2 +- lib/c14n-canonicalization.js | 12 ++++++------ lib/exclusive-canonicalization.js | 16 ++++++++-------- lib/signed-xml.js | 30 +++++++++++++++--------------- lib/utils.js | 10 +++++----- test/signature-unit-tests.js | 2 +- 6 files changed, 36 insertions(+), 36 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 86e0ba60..efd4b104 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -15,7 +15,7 @@ "no-duplicate-imports": "error", "no-use-before-define": "error", "curly": "error", - "eqeqeq": ["warn", "smart"], + "eqeqeq": ["error", "smart"], "no-var": "error", "prefer-const": "error" } diff --git a/lib/c14n-canonicalization.js b/lib/c14n-canonicalization.js index aa18b03e..1a4d32d8 100644 --- a/lib/c14n-canonicalization.js +++ b/lib/c14n-canonicalization.js @@ -31,7 +31,7 @@ class C14nCanonicalization { nsCompare(a, b) { const attr1 = a.prefix; const attr2 = b.prefix; - if (attr1 == attr2) { + if (attr1 === attr2) { return 0; } return attr1.localeCompare(attr2); @@ -97,14 +97,14 @@ class C14nCanonicalization { //handle the namespaceof the node itself if (node.prefix) { - if (prefixesInScope.indexOf(node.prefix) == -1) { + if (prefixesInScope.indexOf(node.prefix) === -1) { nsListToRender.push({ prefix: node.prefix, namespaceURI: node.namespaceURI || defaultNsForPrefix[node.prefix], }); prefixesInScope.push(node.prefix); } - } else if (defaultNs != currNs) { + } else if (defaultNs !== currNs) { //new default ns newDefaultNs = node.namespaceURI; res.push(' xmlns="', newDefaultNs, '"'); @@ -126,9 +126,9 @@ class C14nCanonicalization { //the prefix is not defined already if ( attr.prefix && - prefixesInScope.indexOf(attr.prefix) == -1 && - attr.prefix != "xmlns" && - attr.prefix != "xml" + prefixesInScope.indexOf(attr.prefix) === -1 && + attr.prefix !== "xmlns" && + attr.prefix !== "xml" ) { nsListToRender.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI }); prefixesInScope.push(attr.prefix); diff --git a/lib/exclusive-canonicalization.js b/lib/exclusive-canonicalization.js index f21651ac..cdad671a 100644 --- a/lib/exclusive-canonicalization.js +++ b/lib/exclusive-canonicalization.js @@ -42,7 +42,7 @@ class ExclusiveCanonicalization { nsCompare(a, b) { const attr1 = a.prefix; const attr2 = b.prefix; - if (attr1 == attr2) { + if (attr1 === attr2) { return 0; } return attr1.localeCompare(attr2); @@ -122,7 +122,7 @@ class ExclusiveCanonicalization { namespaceURI: node.namespaceURI || defaultNsForPrefix[node.prefix], }); } - } else if (defaultNs != currNs) { + } else if (defaultNs !== currNs) { //new default ns newDefaultNs = node.namespaceURI; res.push(' xmlns="', newDefaultNs, '"'); @@ -149,8 +149,8 @@ class ExclusiveCanonicalization { if ( attr.prefix && !isPrefixInScope(prefixesInScope, attr.prefix, attr.namespaceURI) && - attr.prefix != "xmlns" && - attr.prefix != "xml" + attr.prefix !== "xmlns" && + attr.prefix !== "xml" ) { nsListToRender.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI }); prefixesInScope.push({ prefix: attr.prefix, namespaceURI: attr.namespaceURI }); @@ -278,14 +278,14 @@ class ExclusiveCanonicalization { /** * If the inclusiveNamespacesPrefixList has not been explicitly provided then look it up in CanonicalizationMethod/InclusiveNamespaces */ - if (inclusiveNamespacesPrefixList.length == 0) { + if (inclusiveNamespacesPrefixList.length === 0) { const CanonicalizationMethod = utils.findChilds(node, "CanonicalizationMethod"); - if (CanonicalizationMethod.length != 0) { + if (CanonicalizationMethod.length !== 0) { const inclusiveNamespaces = utils.findChilds( CanonicalizationMethod[0], "InclusiveNamespaces" ); - if (inclusiveNamespaces.length != 0) { + if (inclusiveNamespaces.length !== 0) { inclusiveNamespacesPrefixList = inclusiveNamespaces[0] .getAttribute("PrefixList") .split(" "); @@ -304,7 +304,7 @@ class ExclusiveCanonicalization { prefixList.forEach(function (prefix) { if (ancestorNamespaces) { ancestorNamespaces.forEach(function (ancestorNamespace) { - if (prefix == ancestorNamespace.prefix) { + if (prefix === ancestorNamespace.prefix) { node.setAttributeNS( "http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, diff --git a/lib/signed-xml.js b/lib/signed-xml.js index ba6a3b22..d75cce22 100644 --- a/lib/signed-xml.js +++ b/lib/signed-xml.js @@ -154,7 +154,7 @@ class SignedXml { getCanonSignedInfoXml(doc) { const signedInfo = utils.findChilds(this.signatureNode, "SignedInfo"); - if (signedInfo.length == 0) { + if (signedInfo.length === 0) { throw new Error("could not find SignedInfo element in the message"); } @@ -256,12 +256,12 @@ class SignedXml { const ref = this.references[r]; let elemXpath; - const uri = ref.uri[0] == "#" ? ref.uri.substring(1) : ref.uri; + const uri = ref.uri[0] === "#" ? ref.uri.substring(1) : ref.uri; let elem = []; - if (uri == "") { + if (uri === "") { elem = xpath.select("//*", doc); - } else if (uri.indexOf("'") != -1) { + } else if (uri.indexOf("'") !== -1) { // xpath injection throw new Error("Cannot validate a uri with quotes inside it"); } else { @@ -290,7 +290,7 @@ class SignedXml { ref.xpath = elemXpath; } - if (elem.length == 0) { + if (elem.length === 0) { this.validationErrors.push( "invalid signature: the signature references an element with uri " + ref.uri + @@ -333,7 +333,7 @@ class SignedXml { ".//*[local-name(.)='CanonicalizationMethod']/@Algorithm", signatureNode ); - if (nodes.length == 0) { + if (nodes.length === 0) { throw new Error("could not find CanonicalizationMethod/@Algorithm element"); } this.canonicalizationAlgorithm = nodes[0].value; @@ -348,7 +348,7 @@ class SignedXml { ".//*[local-name(.)='SignedInfo']/*[local-name(.)='Reference']", signatureNode ); - if (references.length == 0) { + if (references.length === 0) { throw new Error("could not find any Reference elements"); } @@ -373,7 +373,7 @@ class SignedXml { */ loadReference(ref) { let nodes = utils.findChilds(ref, "DigestMethod"); - if (nodes.length == 0) { + if (nodes.length === 0) { throw new Error("could not find DigestMethod in reference " + ref.toString()); } const digestAlgoNode = nodes[0]; @@ -385,10 +385,10 @@ class SignedXml { const digestAlgo = attr.value; nodes = utils.findChilds(ref, "DigestValue"); - if (nodes.length == 0) { + if (nodes.length === 0) { throw new Error("could not find DigestValue node in reference " + ref.toString()); } - if (nodes[0].childNodes.length == 0 || !nodes[0].firstChild.data) { + if (nodes[0].childNodes.length === 0 || !nodes[0].firstChild.data) { throw new Error("could not find the value of DigestValue in " + nodes[0].toString()); } const digestValue = nodes[0].firstChild.data; @@ -397,7 +397,7 @@ class SignedXml { let trans; let inclusiveNamespacesPrefixList; nodes = utils.findChilds(ref, "Transforms"); - if (nodes.length != 0) { + if (nodes.length !== 0) { const transformsNode = nodes[0]; const transformsAll = utils.findChilds(transformsNode, "Transform"); for (const t in transformsAll) { @@ -608,7 +608,7 @@ class SignedXml { this.signatureNode = signatureDoc; let signedInfoNode = utils.findChilds(this.signatureNode, "SignedInfo"); - if (signedInfoNode.length == 0) { + if (signedInfoNode.length === 0) { const err3 = new Error("could not find SignedInfo element in the message"); if (!callback) { throw err3; @@ -684,7 +684,7 @@ class SignedXml { const ref = this.references[n]; const nodes = xpath.selectWithResolver(ref.xpath, doc, this.namespaceResolver); - if (nodes.length == 0) { + if (nodes.length === 0) { throw new Error( "the following xpath cannot be signed because it was not found: " + ref.xpath ); @@ -786,7 +786,7 @@ class SignedXml { ensureHasId(node) { let attr; - if (this.idMode == "wssecurity") { + if (this.idMode === "wssecurity") { attr = utils.findAttr( node, "Id", @@ -812,7 +812,7 @@ class SignedXml { //add the attribute const id = "_" + this.id++; - if (this.idMode == "wssecurity") { + if (this.idMode === "wssecurity") { node.setAttributeNS( "http://www.w3.org/2000/xmlns/", "xmlns:wsu", diff --git a/lib/utils.js b/lib/utils.js index 16a5c762..e14ea3ed 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,13 +1,13 @@ const xpath = require("xpath"); function attrEqualsExplicitly(attr, localName, namespace) { - return attr.localName == localName && (attr.namespaceURI == namespace || !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) + attr.localName === localName && + ((!attr.namespaceURI && node.namespaceURI === namespace) || !namespace) ); } @@ -27,7 +27,7 @@ function findAttr(node, localName, namespace) { function findFirst(doc, path) { const nodes = xpath.select(path, doc); - if (nodes.length == 0) { + if (nodes.length === 0) { throw "could not find xpath " + path; } return nodes[0]; @@ -38,7 +38,7 @@ function findChilds(node, localName, namespace) { const res = []; for (let i = 0; i < node.childNodes.length; i++) { const child = node.childNodes[i]; - if (child.localName == localName && (child.namespaceURI == namespace || !namespace)) { + if (child.localName === localName && (child.namespaceURI === namespace || !namespace)) { res.push(child); } } diff --git a/test/signature-unit-tests.js b/test/signature-unit-tests.js index 2d52fdcf..738e6fdf 100644 --- a/test/signature-unit-tests.js +++ b/test/signature-unit-tests.js @@ -123,7 +123,7 @@ describe("Signature unit tests", function () { const signedXml = sig.getOriginalXmlWithIds(); const doc = new dom().parseFromString(signedXml); - const op = nsMode == "equal" ? "=" : "!="; + const op = nsMode === "equal" ? "=" : "!="; const xpath = "//*[local-name(.)='{elem}' and '_{id}' = @*[local-name(.)='Id' and namespace-uri(.)" +