@@ -26,7 +26,6 @@ import * as execC14n from "./exclusive-canonicalization";
26
26
import * as hashAlgorithms from "./hash-algorithms" ;
27
27
import * as signatureAlgorithms from "./signature-algorithms" ;
28
28
import * as utils from "./utils" ;
29
- import { isDescendantOf } from "./utils" ;
30
29
31
30
export class SignedXml {
32
31
idMode ?: "wssecurity" ;
@@ -1347,6 +1346,10 @@ export class SignedXml {
1347
1346
throw new Error ( "Could not find SignedInfo element in signature" ) ;
1348
1347
}
1349
1348
1349
+ // Signature document is technically the same document as the one we are signing,
1350
+ // but we will extract it here for clarity (and also make it support detached signatures in the future)
1351
+ const signatureDoc = signatureElem . ownerDocument ;
1352
+
1350
1353
// Process each unprocessed reference
1351
1354
for ( const ref of unprocessedReferences ) {
1352
1355
const nodes = xpath . selectWithResolver ( ref . xpath ?? "" , doc , this . namespaceResolver ) ;
@@ -1363,15 +1366,15 @@ export class SignedXml {
1363
1366
if (
1364
1367
node === signatureElem ||
1365
1368
node === signedInfoNode ||
1366
- isDescendantOf ( node , signedInfoNode )
1369
+ utils . isDescendantOf ( node , signedInfoNode )
1367
1370
) {
1368
1371
throw new Error (
1369
1372
`Cannot sign a reference to the Signature or SignedInfo element itself: ${ ref . xpath } ` ,
1370
1373
) ;
1371
1374
}
1372
1375
1373
1376
// Create the reference element directly using DOM methods to avoid namespace issues
1374
- const referenceElem = signatureElem . ownerDocument . createElementNS (
1377
+ const referenceElem = signatureDoc . createElementNS (
1375
1378
signatureNamespace ,
1376
1379
`${ prefix } Reference` ,
1377
1380
) ;
@@ -1391,15 +1394,21 @@ export class SignedXml {
1391
1394
referenceElem . setAttribute ( "Type" , ref . type ) ;
1392
1395
}
1393
1396
1394
- const transformsElem = doc . createElementNS ( signatureNamespace , `${ prefix } Transforms` ) ;
1397
+ const transformsElem = signatureDoc . createElementNS (
1398
+ signatureNamespace ,
1399
+ `${ prefix } Transforms` ,
1400
+ ) ;
1395
1401
1396
1402
for ( const trans of ref . transforms || [ ] ) {
1397
1403
const transform = this . findCanonicalizationAlgorithm ( trans ) ;
1398
- const transformElem = doc . createElementNS ( signatureNamespace , `${ prefix } Transform` ) ;
1404
+ const transformElem = signatureDoc . createElementNS (
1405
+ signatureNamespace ,
1406
+ `${ prefix } Transform` ,
1407
+ ) ;
1399
1408
transformElem . setAttribute ( "Algorithm" , transform . getAlgorithmName ( ) ) ;
1400
1409
1401
1410
if ( utils . isArrayHasLength ( ref . inclusiveNamespacesPrefixList ) ) {
1402
- const inclusiveNamespacesElem = doc . createElementNS (
1411
+ const inclusiveNamespacesElem = signatureDoc . createElementNS (
1403
1412
transform . getAlgorithmName ( ) ,
1404
1413
"InclusiveNamespaces" ,
1405
1414
) ;
@@ -1419,10 +1428,16 @@ export class SignedXml {
1419
1428
// Get the digest algorithm and compute the digest value
1420
1429
const digestAlgorithm = this . findHashAlgorithm ( ref . digestAlgorithm ) ;
1421
1430
1422
- const digestMethodElem = doc . createElementNS ( signatureNamespace , `${ prefix } DigestMethod` ) ;
1431
+ const digestMethodElem = signatureDoc . createElementNS (
1432
+ signatureNamespace ,
1433
+ `${ prefix } DigestMethod` ,
1434
+ ) ;
1423
1435
digestMethodElem . setAttribute ( "Algorithm" , digestAlgorithm . getAlgorithmName ( ) ) ;
1424
1436
1425
- const digestValueElem = doc . createElementNS ( signatureNamespace , `${ prefix } DigestValue` ) ;
1437
+ const digestValueElem = signatureDoc . createElementNS (
1438
+ signatureNamespace ,
1439
+ `${ prefix } DigestValue` ,
1440
+ ) ;
1426
1441
digestValueElem . textContent = digestAlgorithm . getHash ( canonXml ) ;
1427
1442
1428
1443
referenceElem . appendChild ( transformsElem ) ;
0 commit comments