@@ -366,26 +366,28 @@ export class SignedXml {
366
366
}
367
367
}
368
368
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
+
370
379
for ( const ref of this . getReferences ( ) ) {
371
380
const uri = ref . uri ?. [ 0 ] === "#" ? ref . uri . substring ( 1 ) : ref . uri ;
372
- let targetElem : xpath . SelectSingleReturnType ;
373
381
374
382
for ( const attr of this . idAttributes ) {
375
383
const elemId = elem . getAttribute ( attr ) ;
376
384
if ( uri === elemId ) {
377
- targetElem = elem ;
378
385
ref . xpath = `//*[@*[local-name(.)='${ attr } ']='${ uri } ']` ;
379
386
break ; // found the correct element, no need to check further
380
387
}
381
388
}
382
389
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 ) ;
389
391
const hash = this . findHashAlgorithm ( ref . digestAlgorithm ) ;
390
392
const digest = hash . getHash ( canonXml ) ;
391
393
@@ -399,7 +401,7 @@ export class SignedXml {
399
401
400
402
private validateReference ( ref : Reference , doc : Document ) {
401
403
const uri = ref . uri ?. [ 0 ] === "#" ? ref . uri . substring ( 1 ) : ref . uri ;
402
- let elem : xpath . SelectSingleReturnType ;
404
+ let elem : xpath . SelectSingleReturnType = null ;
403
405
404
406
if ( uri === "" ) {
405
407
elem = xpath . select1 ( "//*" , doc ) ;
@@ -428,7 +430,6 @@ export class SignedXml {
428
430
}
429
431
}
430
432
431
- // @ts -expect-error FIXME: xpath types are wrong
432
433
if ( ! isDomNode . isNodeLike ( elem ) ) {
433
434
const validationError = new Error (
434
435
`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 {
453
454
return true ;
454
455
}
455
456
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 ,
461
461
) ;
462
+
463
+ return isDomNode . isArrayOfNodes ( nodes ) ? nodes : [ ] ;
462
464
}
463
465
464
466
/**
@@ -475,16 +477,16 @@ export class SignedXml {
475
477
476
478
this . signatureXml = signatureNode . toString ( ) ;
477
479
478
- const nodes = xpath . select (
480
+ const node = xpath . select1 (
479
481
".//*[local-name(.)='CanonicalizationMethod']/@Algorithm" ,
480
482
signatureNode ,
481
483
) ;
482
- if ( ! utils . isArrayHasLength ( nodes ) ) {
484
+ if ( ! isDomNode . isNodeLike ( node ) ) {
483
485
throw new Error ( "could not find CanonicalizationMethod/@Algorithm element" ) ;
484
486
}
485
487
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 ;
488
490
}
489
491
490
492
const signatureAlgorithm = xpath . select1 (
0 commit comments