Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbarth committed Jul 8, 2023
1 parent e1ead87 commit 52e4e08
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 72 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"rules": {
"no-console": "error",
"no-unused-vars": "error",
"no-unused-vars": "warn",
"no-prototype-builtins": "error",
"one-var": ["error", "never"],
"no-duplicate-imports": "error",
Expand All @@ -30,6 +30,7 @@
"prefer-const": "error",
"deprecation/deprecation": "warn",
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/no-unused-vars": "error"
"@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/no-this-alias": "warn"
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 21 additions & 13 deletions src/c14n-canonicalization.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { CanonicalizationOrTransformationAlgorithm, ProcessOptions } from "./types";
import {
CanonicalizationOrTransformationAlgorithm,
NamespacePrefix,
ProcessOptions,
RenderedNamespace,
} from "./types";
import * as utils from "./utils";

export class C14nCanonicalization implements CanonicalizationOrTransformationAlgorithm {
includeComments: boolean = false;

constructor() {}
includeComments = false;

attrCompare(a, b) {
if (!a.namespaceURI && b.namespaceURI) {
Expand Down Expand Up @@ -67,16 +70,21 @@ export class C14nCanonicalization implements CanonicalizationOrTransformationAlg
/**
* Create the string of all namespace declarations that should appear on this element
*
* @param {Node} node. The node we now render
* @param {Array} prefixesInScope. The prefixes defined on this node
* @param node. The node we now render
* @param prefixesInScope. The prefixes defined on this node
* parents which are a part of the output set
* @param {String} defaultNs. The current default namespace
* @param {String} defaultNsForPrefix.
* @param {String} ancestorNamespaces - Import ancestor namespaces if it is specified
* @return {String}
* @param defaultNs. The current default namespace
* @param defaultNsForPrefix.
* @param ancestorNamespaces - Import ancestor namespaces if it is specified
* @api private
*/
renderNs(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {
renderNs(
node: Element,
prefixesInScope: string[],
defaultNs: string,
defaultNsForPrefix: string,
ancestorNamespaces: NamespacePrefix[]
): RenderedNamespace {
let i;
let attr;
const res: string[] = [];
Expand All @@ -95,7 +103,7 @@ export class C14nCanonicalization implements CanonicalizationOrTransformationAlg
}
} else if (defaultNs !== currNs) {
//new default ns
newDefaultNs = node.namespaceURI;
newDefaultNs = node.namespaceURI || "";
res.push(' xmlns="', newDefaultNs, '"');
}

Expand Down Expand Up @@ -149,7 +157,7 @@ export class C14nCanonicalization implements CanonicalizationOrTransformationAlg
//render namespaces
res.push(...nsListToRender.map((attr) => ` xmlns:${attr.prefix}="${attr.namespaceURI}"`));

return { rendered: res.join(""), newDefaultNs: newDefaultNs };
return { rendered: res.join(""), newDefaultNs };
}

processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {
Expand Down
44 changes: 27 additions & 17 deletions src/enveloped-signature.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const xpath = require("xpath");
import * as xpath from "xpath";

import {
CanonicalizationOrTransformationAlgorithm,
CanonicalizationOrTransformationAlgorithmProcessOptions,
CanonicalizationOrTransformAlgorithmType,
} from "./types";
import * as utils from "./utils";

export class EnvelopedSignature implements CanonicalizationOrTransformationAlgorithm {
includeComments: boolean = false;
includeComments = false;
process(node: Node, options: CanonicalizationOrTransformationAlgorithmProcessOptions) {
if (null == options.signatureNode) {
const signature = xpath.select(
const signature = xpath.select1(
"./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
node
)[0];
if (signature) {
);
if (xpath.isNodeLike(signature) && signature.parentNode) {
signature.parentNode.removeChild(signature);
}
return node;
Expand All @@ -22,18 +23,27 @@ export class EnvelopedSignature implements CanonicalizationOrTransformationAlgor
const expectedSignatureValue = xpath.select1(
".//*[local-name(.)='SignatureValue']/text()",
signatureNode
).data;
const signatures = xpath.select(
".//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
node
);
for (const nodeSignature of signatures) {
const signatureValue = xpath.select1(
".//*[local-name(.)='SignatureValue']/text()",
nodeSignature
).data;
if (expectedSignatureValue === signatureValue) {
nodeSignature.parentNode.removeChild(nodeSignature);
if (xpath.isTextNode(expectedSignatureValue)) {
const expectedSignatureValueData = expectedSignatureValue.data;

const signatures = xpath.select(
".//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
node
);
for (const nodeSignature of Array.isArray(signatures) ? signatures : []) {
const signatureValue = xpath.select1(
".//*[local-name(.)='SignatureValue']/text()",
nodeSignature
);
if (xpath.isTextNode(signatureValue)) {
const signatureValueData = signatureValue.data;
if (expectedSignatureValueData === signatureValueData) {
if (nodeSignature.parentNode) {
nodeSignature.parentNode.removeChild(nodeSignature);
}
}
}
}
}
return node;
Expand Down
4 changes: 1 addition & 3 deletions src/exclusive-canonicalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ function isPrefixInScope(prefixesInScope, prefix, namespaceURI) {
}

export class ExclusiveCanonicalization implements CanonicalizationOrTransformationAlgorithm {
includeComments: boolean = false;

constructor() {}
includeComments = false;

attrCompare(a, b) {
if (!a.namespaceURI && b.namespaceURI) {
Expand Down
4 changes: 0 additions & 4 deletions src/hash-algorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as crypto from "crypto";
import { HashAlgorithm } from "./types";

export class Sha1 implements HashAlgorithm {
constructor() {}
getHash = function (xml) {
const shasum = crypto.createHash("sha1");
shasum.update(xml, "utf8");
Expand All @@ -16,8 +15,6 @@ export class Sha1 implements HashAlgorithm {
}

export class Sha256 implements HashAlgorithm {
constructor() {}

getHash = function (xml) {
const shasum = crypto.createHash("sha256");
shasum.update(xml, "utf8");
Expand All @@ -31,7 +28,6 @@ export class Sha256 implements HashAlgorithm {
}

export class Sha512 implements HashAlgorithm {
constructor() {}
getHash = function (xml) {
const shasum = crypto.createHash("sha512");
shasum.update(xml, "utf8");
Expand Down
5 changes: 0 additions & 5 deletions src/signature-algorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as crypto from "crypto";
import { SignatureAlgorithm, createOptionalCallbackFunction } from "./types";

export class RsaSha1 implements SignatureAlgorithm {
constructor() {}

getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
const signer = crypto.createSign("RSA-SHA1");
Expand All @@ -30,7 +28,6 @@ export class RsaSha1 implements SignatureAlgorithm {
}

export class RsaSha256 implements SignatureAlgorithm {
constructor() {}
getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
const signer = crypto.createSign("RSA-SHA256");
Expand All @@ -57,7 +54,6 @@ export class RsaSha256 implements SignatureAlgorithm {
}

export class RsaSha512 implements SignatureAlgorithm {
constructor() {}
getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
const signer = crypto.createSign("RSA-SHA512");
Expand All @@ -84,7 +80,6 @@ export class RsaSha512 implements SignatureAlgorithm {
}

export class HmacSha1 implements SignatureAlgorithm {
constructor() {}
getSignature = createOptionalCallbackFunction(
(signedInfo: crypto.BinaryLike, privateKey: crypto.KeyLike): string => {
const signer = crypto.createHmac("SHA1", privateKey);
Expand Down
19 changes: 9 additions & 10 deletions src/signed-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import {
ErrorFirstCallback,
} from "./types";

const xpath = require("xpath");
const xmldom = require("@xmldom/xmldom");
const Dom = require("@xmldom/xmldom").DOMParser;
import * as xpath from "xpath";
import { DOMParser as Dom } from "@xmldom/xmldom";
import * as utils from "./utils";
const c14n = require("./c14n-canonicalization");
const execC14n = require("./exclusive-canonicalization");
const envelopedSignatures = require("./enveloped-signature");
const hashAlgorithms = require("./hash-algorithms");
const signatureAlgorithms = require("./signature-algorithms");
import * as c14n from "./c14n-canonicalization";
import * as execC14n from "./exclusive-canonicalization";
import * as envelopedSignatures from "./enveloped-signature";
import * as hashAlgorithms from "./hash-algorithms";
import * as signatureAlgorithms from "./signature-algorithms";
import * as crypto from "crypto";

export class SignedXml {
Expand All @@ -44,7 +43,7 @@ export class SignedXml {
/**
* It specifies a list of namespace prefixes that should be considered "inclusive" during the canonicalization process.
*/
inclusiveNamespacesPrefixList: string = "";
inclusiveNamespacesPrefixList = "";
namespaceResolver: XPathNSResolver = {
lookupNamespaceURI: function (prefix) {
throw new Error("Not implemented");
Expand Down Expand Up @@ -786,7 +785,7 @@ export class SignedXml {
}

this.signatureNode = signatureDoc;
let signedInfoNodes = utils.findChilds(this.signatureNode, "SignedInfo");
const signedInfoNodes = utils.findChilds(this.signatureNode, "SignedInfo");
if (signedInfoNodes.length === 0) {
const err3 = new Error("could not find SignedInfo element in the message");
if (!callback) {
Expand Down
37 changes: 25 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
// Type definitions for @node-saml/xml-crypto
// Project: https://github.com/node-saml/xml-crypto#readme
// Original definitions by: Eric Heikes <https://github.com/eheikes>
Expand All @@ -7,6 +8,8 @@

import * as crypto from "crypto";

export type ErrorFirstCallback<T> = (err: Error | null, result?: T) => void;

export type CanonicalizationAlgorithmType =
| "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
| "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"
Expand All @@ -31,6 +34,15 @@ export type SignatureAlgorithmType =
| "http://www.w3.org/2000/09/xmldsig#hmac-sha1"
| string;

/**
* @param cert the certificate as a string or array of strings (see https://www.w3.org/TR/2008/REC-xmldsig-core-20080610/#sec-X509Data)
* @param prefix an optional namespace alias to be used for the generated XML
*/
export type GetKeyInfoContentArgs = {
publicCert?: crypto.KeyLike;
prefix?: string | null;
};

/**
* Options for the SignedXml constructor.
*/
Expand All @@ -52,15 +64,21 @@ export type NamespacePrefix = {
prefix: string;
namespaceURI: string;
};

export type ProcessOptions = {
defaultNs?: string;
defaultNsForPrefix?: NamespacePrefix;
ancestorNamespaces?: NamespacePrefix[];
};

export type RenderedNamespace = {
rendered: string;
newDefaultNs: string;
};

export type CanonicalizationOrTransformationAlgorithmProcessOptions = {
defaultNs?: string;
defaultForPrefix?: {};
defaultForPrefix?: object;
ancestorNamespaces?: [];
signatureNode: Node;
};
Expand Down Expand Up @@ -186,17 +204,6 @@ export interface TransformAlgorithm {
* - {@link SignedXml#validationErrors}
*/

/**
* @param cert the certificate as a string or array of strings (see https://www.w3.org/TR/2008/REC-xmldsig-core-20080610/#sec-X509Data)
* @param prefix an optional namespace alias to be used for the generated XML
*/
export type GetKeyInfoContentArgs = {
publicCert?: crypto.KeyLike;
prefix?: string | null;
};

export type ErrorFirstCallback<T> = (err: Error | null, result?: T) => void;

/**
* This function will add a callback version of a sync function.
*
Expand All @@ -223,3 +230,9 @@ export function createOptionalCallbackFunction<T, A extends any[]>(
}
}) as any;
}

declare global {
interface ArrayConstructor {
isArray(arg: unknown): arg is Array<unknown> | ReadonlyArray<unknown>;
}
}
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,15 @@ export function validateDigestValue(digest, expectedDigest) {
let buffer;
let expectedBuffer;

const majorVersion = /^v(\d+)/.exec(process.version)![1];
const majorVersion = (/^v(\d+)/.exec(process.version) || [0, 0])[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");
buffer = Buffer.from(digest, "base64");
expectedBuffer = Buffer.from(expectedDigest, "base64");
}

if (typeof buffer.equals === "function") {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.json",
"exclude": [],
"include": ["test"]
"include": ["test", "src"]
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"exclude": ["node_modules", "docs", "lib", "test", "coverage", "example"]
"exclude": ["node_modules", "docs", "lib", "test", "coverage", "example"],
"include": ["src"]
}

0 comments on commit 52e4e08

Please sign in to comment.