Skip to content

Commit 0dc9d9c

Browse files
committed
improve reference URI verification and signature validation test
1 parent 777914a commit 0dc9d9c

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

test/signature-object-tests.spec.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ describe("Valid signatures with ds:Object elements", function () {
367367
expect(valid, errorMessage).to.be.true;
368368
});
369369

370-
it("should generate Id attribute for ds:Object when not provided", function () {
370+
it("should create valid signature and generate Id attribute for ds:Object when not provided", function () {
371371
const xml = "<root></root>";
372372
const sig = new SignedXml({
373373
privateKey,
@@ -393,14 +393,20 @@ describe("Valid signatures with ds:Object elements", function () {
393393
const signedXml = sig.getSignedXml();
394394
const doc = new xmldom.DOMParser().parseFromString(signedXml);
395395

396-
// Verify that Data element got an Id
396+
// Find the ds:Object/Data element and get the value of its Id attribute (ensuring it was generated)
397397
const dataEl = select1Ns("/root/ds:Signature/ds:Object/Data[@Id]", doc);
398398
isDomNode.assertIsElementNode(dataEl);
399+
const idValue = dataEl.getAttribute("Id");
400+
expect(idValue).to.be.a("string").that.is.not.empty;
399401

400-
// Verify Reference URI points to the generated Id
401-
const refEl = select1Ns("/root/ds:Signature/ds:SignedInfo/ds:Reference", doc);
402+
// Verify that there is a Reference pointing to the generated Id
403+
const uri = `#${idValue}`;
404+
const refEl = select1Ns(`/root/ds:Signature/ds:SignedInfo/ds:Reference[@URI='${uri}']`, doc);
402405
isDomNode.assertIsElementNode(refEl);
403-
expect(refEl.getAttribute("URI")).to.match(/^#_\d+$/);
406+
407+
// Verify that the signature is valid
408+
const { valid, errorMessage } = checkSignature(signedXml, doc);
409+
expect(valid, errorMessage).to.be.true;
404410
});
405411
});
406412

@@ -466,8 +472,18 @@ describe("Should successfuly sign references to ds:KeyInfo elements", function (
466472

467473
const doc = new xmldom.DOMParser().parseFromString(signedXml);
468474

469-
// Verify that there is a Reference to KeyInfo
470-
const referenceEl = select1Ns("/root/ds:Signature/ds:SignedInfo/ds:Reference[@URI='#_0']", doc);
475+
// Find the KeyInfo element and get the value of its Id attribute (ensuring it was generated)
476+
const keyInfoEl = select1Ns("/root/ds:Signature/ds:KeyInfo[@Id]", doc);
477+
isDomNode.assertIsElementNode(keyInfoEl);
478+
const idValue = keyInfoEl.getAttribute("Id");
479+
expect(idValue).to.be.a("string").that.is.not.empty;
480+
481+
// Find a Reference with URI=`#${idValue}`
482+
const uri = `#${idValue}`;
483+
const referenceEl = select1Ns(
484+
`/root/ds:Signature/ds:SignedInfo/ds:Reference[@URI='${uri}']`,
485+
doc,
486+
);
471487
isDomNode.assertIsElementNode(referenceEl);
472488

473489
// Verify that the signature is valid

0 commit comments

Comments
 (0)