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

Lint code for new linting rules #300

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -17,7 +17,7 @@
"no-duplicate-imports": "error",
"no-use-before-define": "error",
"curly": "warn",
"eqeqeq": ["error", "smart"],
"eqeqeq": ["warn", "smart"],
"no-var": "warn",
"prefer-const":"warn"
}
Expand Down
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;
5 changes: 3 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,6 @@ EnvelopedSignature.prototype.process = function (node, options) {
EnvelopedSignature.prototype.getAlgorithmName = function () {
return "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
};

exports.EnvelopedSignature = EnvelopedSignature;

9 changes: 4 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,7 @@ 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;

106 changes: 54 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,9 @@ 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