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

Convert this project to TypeScript #325

Merged
merged 7 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
79 changes: 39 additions & 40 deletions src/c14n-canonicalization.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const utils = require("./utils");

/**
* @type { import("../index.d.ts").CanonicalizationOrTransformationAlgorithm}
*/
class C14nCanonicalization {
constructor() {
this.includeComments = false;
}
import {
LoneRifle marked this conversation as resolved.
Show resolved Hide resolved
CanonicalizationOrTransformationAlgorithm,
CanonicalizationOrTransformationAlgorithmProcessOptions,
NamespacePrefix,
RenderedNamespace,
} from "./types";
import * as utils from "./utils";
import * as xpath from "xpath";

export class C14nCanonicalization implements CanonicalizationOrTransformationAlgorithm {
includeComments = false;

attrCompare(a, b) {
if (!a.namespaceURI && b.namespaceURI) {
Expand Down Expand Up @@ -40,9 +42,9 @@ class C14nCanonicalization {
renderAttrs(node) {
let i;
let attr;
const attrListToRender = [];
const attrListToRender: Attr[] = [];

if (node.nodeType === 8) {
if (xpath.isComment(node)) {
return this.renderComment(node);
}

Expand All @@ -69,21 +71,25 @@ class C14nCanonicalization {
/**
* 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
* 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 node The node we now render
* @param prefixesInScope The prefixes defined on this node parents which are a part of the output set
* @param defaultNs The current default namespace
* @param defaultNsForPrefix
* @param ancestorNamespaces Import ancestor namespaces if it is specified
LoneRifle marked this conversation as resolved.
Show resolved Hide resolved
* @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 = [];
const res: string[] = [];
let newDefaultNs = defaultNs;
const nsListToRender = [];
const nsListToRender: { prefix: string; namespaceURI: string }[] = [];
const currNs = node.namespaceURI || "";

//handle the namespace of the node itself
Expand All @@ -97,7 +103,7 @@ class C14nCanonicalization {
}
} else if (defaultNs !== currNs) {
//new default ns
newDefaultNs = node.namespaceURI;
newDefaultNs = node.namespaceURI || "";
res.push(' xmlns="', newDefaultNs, '"');
}

Expand Down Expand Up @@ -127,7 +133,7 @@ class C14nCanonicalization {
}
}

if (Array.isArray(ancestorNamespaces) && ancestorNamespaces.length > 0) {
if (utils.isArrayHasLength(ancestorNamespaces)) {
// Remove namespaces which are already present in nsListToRender
for (const ancestorNamespace of ancestorNamespaces) {
let alreadyListed = false;
Expand Down Expand Up @@ -158,11 +164,11 @@ class C14nCanonicalization {
})
);

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

processInner(node, prefixesInScope, defaultNs, defaultNsForPrefix, ancestorNamespaces) {
if (node.nodeType === 8) {
if (xpath.isComment(node)) {
return this.renderComment(node);
}
if (node.data) {
Expand Down Expand Up @@ -192,18 +198,18 @@ class C14nCanonicalization {
}

// Thanks to deoxxa/xml-c14n for comment renderer
renderComment(node) {
renderComment(node: Comment) {
if (!this.includeComments) {
return "";
}

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

if (isOutsideDocument) {
let nextNode = node;
let previousNode = node;
let nextNode: ChildNode | null = node;
let previousNode: ChildNode | null = node;

while (nextNode !== null) {
if (nextNode === node.ownerDocument.documentElement) {
Expand Down Expand Up @@ -240,13 +246,13 @@ class C14nCanonicalization {
* @return {String}
* @api public
*/
process(node, options) {
process(node: Node, options: CanonicalizationOrTransformationAlgorithmProcessOptions) {
options = options || {};
const defaultNs = options.defaultNs || "";
const defaultNsForPrefix = options.defaultNsForPrefix || {};
const ancestorNamespaces = options.ancestorNamespaces || [];

const prefixesInScope = [];
const prefixesInScope: string[] = [];
for (let i = 0; i < ancestorNamespaces.length; i++) {
prefixesInScope.push(ancestorNamespaces[i].prefix);
}
Expand All @@ -268,10 +274,8 @@ class C14nCanonicalization {

/**
* Add c14n#WithComments here (very simple subclass)
*
* @type { import("../index.d.ts").CanonicalizationOrTransformationAlgorithm}
*/
class C14nCanonicalizationWithComments extends C14nCanonicalization {
export class C14nCanonicalizationWithComments extends C14nCanonicalization {
constructor() {
super();
this.includeComments = true;
Expand All @@ -281,8 +285,3 @@ class C14nCanonicalizationWithComments extends C14nCanonicalization {
return "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
}
}

module.exports = {
C14nCanonicalization,
C14nCanonicalizationWithComments,
};
66 changes: 37 additions & 29 deletions src/enveloped-signature.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,55 @@
const xpath = require("xpath");
const utils = require("./utils");
import * as xpath from "xpath";

/**
* @type { import("../index.d.ts").CanonicalizationOrTransformationAlgorithm}
*/
class EnvelopedSignature {
process(node, options) {
import {
CanonicalizationOrTransformationAlgorithm,
CanonicalizationOrTransformationAlgorithmProcessOptions,
CanonicalizationOrTransformAlgorithmType,
} from "./types";

export class EnvelopedSignature implements CanonicalizationOrTransformationAlgorithm {
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;
}
const signatureNode = options.signatureNode;
const expectedSignatureValue = utils.findFirst(
signatureNode,
".//*[local-name(.)='SignatureValue']/text()"
).data;
const signatures = xpath.select(
".//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
node
const expectedSignatureValue = xpath.select1(
".//*[local-name(.)='SignatureValue']/text()",
signatureNode
);
for (const nodeSignature of signatures) {
const signatureValue = utils.findFirst(
nodeSignature,
".//*[local-name(.)='SignatureValue']/text()"
).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;
}

getAlgorithmName() {
getAlgorithmName(): CanonicalizationOrTransformAlgorithmType {
return "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
}
}

module.exports = {
EnvelopedSignature,
};
Loading