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

Improve code clarity; remove unused functions #397

Merged
merged 2 commits into from
Oct 6, 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
42 changes: 22 additions & 20 deletions src/signed-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,26 +366,28 @@
}
}

validateElementAgainstReferences(elem: Element, doc: Document): Reference {
validateElementAgainstReferences(elemOrXpath: Element | string, doc: Document): Reference {
let elem: Element;
if (typeof elemOrXpath === "string") {
const firstElem = xpath.select1(elemOrXpath, doc);
isDomNode.assertIsElementNode(firstElem);
elem = firstElem;

Check warning on line 374 in src/signed-xml.ts

View check run for this annotation

Codecov / codecov/patch

src/signed-xml.ts#L372-L374

Added lines #L372 - L374 were not covered by tests
} else {
elem = elemOrXpath;
}

for (const ref of this.getReferences()) {
const uri = ref.uri?.[0] === "#" ? ref.uri.substring(1) : ref.uri;
let targetElem: xpath.SelectSingleReturnType;

for (const attr of this.idAttributes) {
const elemId = elem.getAttribute(attr);
if (uri === elemId) {
targetElem = elem;
ref.xpath = `//*[@*[local-name(.)='${attr}']='${uri}']`;
break; // found the correct element, no need to check further
}
}

// @ts-expect-error FIXME: xpath types are wrong
if (!isDomNode.isNodeLike(targetElem)) {
continue;
}

const canonXml = this.getCanonReferenceXml(doc, ref, targetElem);
const canonXml = this.getCanonReferenceXml(doc, ref, elem);
const hash = this.findHashAlgorithm(ref.digestAlgorithm);
const digest = hash.getHash(canonXml);

Expand All @@ -399,7 +401,7 @@

private validateReference(ref: Reference, doc: Document) {
const uri = ref.uri?.[0] === "#" ? ref.uri.substring(1) : ref.uri;
let elem: xpath.SelectSingleReturnType;
let elem: xpath.SelectSingleReturnType = null;

if (uri === "") {
elem = xpath.select1("//*", doc);
Expand Down Expand Up @@ -428,7 +430,6 @@
}
}

// @ts-expect-error FIXME: xpath types are wrong
if (!isDomNode.isNodeLike(elem)) {
const validationError = new Error(
`invalid signature: the signature references an element with uri ${ref.uri} but could not find such element in the xml`,
Expand All @@ -453,12 +454,13 @@
return true;
}

validateReferences(doc: Document) {
return (
Array.isArray(this.references) &&
this.references.length > 0 &&
this.references.every((ref) => this.validateReference(ref, doc))
findSignatures(doc: Node): Node[] {
const nodes = xpath.select(

Check warning on line 458 in src/signed-xml.ts

View check run for this annotation

Codecov / codecov/patch

src/signed-xml.ts#L458

Added line #L458 was not covered by tests
"//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
doc,
);

return isDomNode.isArrayOfNodes(nodes) ? nodes : [];
}

/**
Expand All @@ -475,16 +477,16 @@

this.signatureXml = signatureNode.toString();

const nodes = xpath.select(
const node = xpath.select1(
".//*[local-name(.)='CanonicalizationMethod']/@Algorithm",
signatureNode,
);
if (!utils.isArrayHasLength(nodes)) {
if (!isDomNode.isNodeLike(node)) {
throw new Error("could not find CanonicalizationMethod/@Algorithm element");
}

if (isDomNode.isAttributeNode(nodes[0])) {
this.canonicalizationAlgorithm = nodes[0].value as CanonicalizationAlgorithmType;
if (isDomNode.isAttributeNode(node)) {
this.canonicalizationAlgorithm = node.value as CanonicalizationAlgorithmType;
}

const signatureAlgorithm = xpath.select1(
Expand Down
6 changes: 2 additions & 4 deletions test/document-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ describe("Document tests", function () {
);

isDomNode.assertIsNodeLike(node);
const signature = new xmldom.DOMParser().parseFromString(node.toString());
const sig = new SignedXml();
sig.publicCert = fs.readFileSync("./test/static/feide_public.pem");
sig.loadSignature(signature);
sig.loadSignature(node);
const result = sig.checkSignature(xml);

expect(result).to.be.true;
Expand All @@ -33,11 +32,10 @@ describe("Document tests", function () {
);

isDomNode.assertIsNodeLike(node);
const signature = new xmldom.DOMParser().parseFromString(node.toString());
const sig = new SignedXml();
const feidePublicCert = fs.readFileSync("./test/static/feide_public.pem");
sig.publicCert = feidePublicCert;
sig.loadSignature(signature);
sig.loadSignature(node);
const result = sig.checkSignature(xml);

expect(result).to.be.true;
Expand Down