Skip to content

Commit 38c552b

Browse files
committed
refactor: utils import, make it clear why we use signatureDoc in processSignatureReferences
1 parent 04a1de4 commit 38c552b

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/signed-xml.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import * as execC14n from "./exclusive-canonicalization";
2626
import * as hashAlgorithms from "./hash-algorithms";
2727
import * as signatureAlgorithms from "./signature-algorithms";
2828
import * as utils from "./utils";
29-
import { isDescendantOf } from "./utils";
3029

3130
export class SignedXml {
3231
idMode?: "wssecurity";
@@ -1347,6 +1346,10 @@ export class SignedXml {
13471346
throw new Error("Could not find SignedInfo element in signature");
13481347
}
13491348

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+
13501353
// Process each unprocessed reference
13511354
for (const ref of unprocessedReferences) {
13521355
const nodes = xpath.selectWithResolver(ref.xpath ?? "", doc, this.namespaceResolver);
@@ -1363,15 +1366,15 @@ export class SignedXml {
13631366
if (
13641367
node === signatureElem ||
13651368
node === signedInfoNode ||
1366-
isDescendantOf(node, signedInfoNode)
1369+
utils.isDescendantOf(node, signedInfoNode)
13671370
) {
13681371
throw new Error(
13691372
`Cannot sign a reference to the Signature or SignedInfo element itself: ${ref.xpath}`,
13701373
);
13711374
}
13721375

13731376
// Create the reference element directly using DOM methods to avoid namespace issues
1374-
const referenceElem = signatureElem.ownerDocument.createElementNS(
1377+
const referenceElem = signatureDoc.createElementNS(
13751378
signatureNamespace,
13761379
`${prefix}Reference`,
13771380
);
@@ -1391,15 +1394,21 @@ export class SignedXml {
13911394
referenceElem.setAttribute("Type", ref.type);
13921395
}
13931396

1394-
const transformsElem = doc.createElementNS(signatureNamespace, `${prefix}Transforms`);
1397+
const transformsElem = signatureDoc.createElementNS(
1398+
signatureNamespace,
1399+
`${prefix}Transforms`,
1400+
);
13951401

13961402
for (const trans of ref.transforms || []) {
13971403
const transform = this.findCanonicalizationAlgorithm(trans);
1398-
const transformElem = doc.createElementNS(signatureNamespace, `${prefix}Transform`);
1404+
const transformElem = signatureDoc.createElementNS(
1405+
signatureNamespace,
1406+
`${prefix}Transform`,
1407+
);
13991408
transformElem.setAttribute("Algorithm", transform.getAlgorithmName());
14001409

14011410
if (utils.isArrayHasLength(ref.inclusiveNamespacesPrefixList)) {
1402-
const inclusiveNamespacesElem = doc.createElementNS(
1411+
const inclusiveNamespacesElem = signatureDoc.createElementNS(
14031412
transform.getAlgorithmName(),
14041413
"InclusiveNamespaces",
14051414
);
@@ -1419,10 +1428,16 @@ export class SignedXml {
14191428
// Get the digest algorithm and compute the digest value
14201429
const digestAlgorithm = this.findHashAlgorithm(ref.digestAlgorithm);
14211430

1422-
const digestMethodElem = doc.createElementNS(signatureNamespace, `${prefix}DigestMethod`);
1431+
const digestMethodElem = signatureDoc.createElementNS(
1432+
signatureNamespace,
1433+
`${prefix}DigestMethod`,
1434+
);
14231435
digestMethodElem.setAttribute("Algorithm", digestAlgorithm.getAlgorithmName());
14241436

1425-
const digestValueElem = doc.createElementNS(signatureNamespace, `${prefix}DigestValue`);
1437+
const digestValueElem = signatureDoc.createElementNS(
1438+
signatureNamespace,
1439+
`${prefix}DigestValue`,
1440+
);
14261441
digestValueElem.textContent = digestAlgorithm.getHash(canonXml);
14271442

14281443
referenceElem.appendChild(transformsElem);

0 commit comments

Comments
 (0)