Skip to content

Commit f0237e9

Browse files
authored
Improve code clarity; remove unused functions (#397)
1 parent d98128a commit f0237e9

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/signed-xml.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -366,26 +366,28 @@ export class SignedXml {
366366
}
367367
}
368368

369-
validateElementAgainstReferences(elem: Element, doc: Document): Reference {
369+
validateElementAgainstReferences(elemOrXpath: Element | string, doc: Document): Reference {
370+
let elem: Element;
371+
if (typeof elemOrXpath === "string") {
372+
const firstElem = xpath.select1(elemOrXpath, doc);
373+
isDomNode.assertIsElementNode(firstElem);
374+
elem = firstElem;
375+
} else {
376+
elem = elemOrXpath;
377+
}
378+
370379
for (const ref of this.getReferences()) {
371380
const uri = ref.uri?.[0] === "#" ? ref.uri.substring(1) : ref.uri;
372-
let targetElem: xpath.SelectSingleReturnType;
373381

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

383-
// @ts-expect-error FIXME: xpath types are wrong
384-
if (!isDomNode.isNodeLike(targetElem)) {
385-
continue;
386-
}
387-
388-
const canonXml = this.getCanonReferenceXml(doc, ref, targetElem);
390+
const canonXml = this.getCanonReferenceXml(doc, ref, elem);
389391
const hash = this.findHashAlgorithm(ref.digestAlgorithm);
390392
const digest = hash.getHash(canonXml);
391393

@@ -399,7 +401,7 @@ export class SignedXml {
399401

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

404406
if (uri === "") {
405407
elem = xpath.select1("//*", doc);
@@ -428,7 +430,6 @@ export class SignedXml {
428430
}
429431
}
430432

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

456-
validateReferences(doc: Document) {
457-
return (
458-
Array.isArray(this.references) &&
459-
this.references.length > 0 &&
460-
this.references.every((ref) => this.validateReference(ref, doc))
457+
findSignatures(doc: Node): Node[] {
458+
const nodes = xpath.select(
459+
"//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
460+
doc,
461461
);
462+
463+
return isDomNode.isArrayOfNodes(nodes) ? nodes : [];
462464
}
463465

464466
/**
@@ -475,16 +477,16 @@ export class SignedXml {
475477

476478
this.signatureXml = signatureNode.toString();
477479

478-
const nodes = xpath.select(
480+
const node = xpath.select1(
479481
".//*[local-name(.)='CanonicalizationMethod']/@Algorithm",
480482
signatureNode,
481483
);
482-
if (!utils.isArrayHasLength(nodes)) {
484+
if (!isDomNode.isNodeLike(node)) {
483485
throw new Error("could not find CanonicalizationMethod/@Algorithm element");
484486
}
485487

486-
if (isDomNode.isAttributeNode(nodes[0])) {
487-
this.canonicalizationAlgorithm = nodes[0].value as CanonicalizationAlgorithmType;
488+
if (isDomNode.isAttributeNode(node)) {
489+
this.canonicalizationAlgorithm = node.value as CanonicalizationAlgorithmType;
488490
}
489491

490492
const signatureAlgorithm = xpath.select1(

test/document-tests.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ describe("Document tests", function () {
1515
);
1616

1717
isDomNode.assertIsNodeLike(node);
18-
const signature = new xmldom.DOMParser().parseFromString(node.toString());
1918
const sig = new SignedXml();
2019
sig.publicCert = fs.readFileSync("./test/static/feide_public.pem");
21-
sig.loadSignature(signature);
20+
sig.loadSignature(node);
2221
const result = sig.checkSignature(xml);
2322

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

3534
isDomNode.assertIsNodeLike(node);
36-
const signature = new xmldom.DOMParser().parseFromString(node.toString());
3735
const sig = new SignedXml();
3836
const feidePublicCert = fs.readFileSync("./test/static/feide_public.pem");
3937
sig.publicCert = feidePublicCert;
40-
sig.loadSignature(signature);
38+
sig.loadSignature(node);
4139
const result = sig.checkSignature(xml);
4240

4341
expect(result).to.be.true;

0 commit comments

Comments
 (0)