|
| 1 | +import test from "ava"; |
| 2 | +import { SignedXml } from "xml-crypto"; |
| 3 | +import libSaml from "../src/libsaml"; |
| 4 | + |
| 5 | +const fakeMetadataInvalid = { |
| 6 | + getX509Certificate: (_usage: string) => null, |
| 7 | +}; |
| 8 | + |
| 9 | +const fakeMetadataValid = { |
| 10 | + getX509Certificate: (_usage: string) => "VALIDCERT", |
| 11 | +}; |
| 12 | + |
| 13 | +test("throws ERR_UNDEFINED_SIGNATURE_VERIFIER_OPTIONS when neither keyFile nor metadata provided", (t) => { |
| 14 | + const xml = "<Response><Signature></Signature></Response>"; |
| 15 | + const error = t.throws(() => { |
| 16 | + libSaml.verifySignature(xml, { signatureAlgorithm: "dummy" } as any); |
| 17 | + }); |
| 18 | + t.is(error!.message, "ERR_UNDEFINED_SIGNATURE_VERIFIER_OPTIONS"); |
| 19 | +}); |
| 20 | + |
| 21 | +test("throws ERR_ZERO_SIGNATURE when no signature element exists", (t) => { |
| 22 | + const xml = "<Response></Response>"; |
| 23 | + const error = t.throws(() => { |
| 24 | + libSaml.verifySignature(xml, { |
| 25 | + keyFile: "dummy.pem", |
| 26 | + signatureAlgorithm: "dummy", |
| 27 | + } as any); |
| 28 | + }); |
| 29 | + t.is(error!.message, "ERR_ZERO_SIGNATURE"); |
| 30 | +}); |
| 31 | + |
| 32 | +test("throws ERR_POTENTIAL_WRAPPING_ATTACK when wrapping element is present", (t) => { |
| 33 | + // Construct XML with a wrapping assertion inside SubjectConfirmationData |
| 34 | + const xml = ` |
| 35 | + <Response> |
| 36 | + <Signature> |
| 37 | + <X509Data> |
| 38 | + <X509Certificate>VALIDCERT</X509Certificate> |
| 39 | + </X509Data> |
| 40 | + </Signature> |
| 41 | + <Assertion ID="ID"> |
| 42 | + <Subject> |
| 43 | + <SubjectConfirmation> |
| 44 | + <SubjectConfirmationData> |
| 45 | + <Assertion></Assertion> |
| 46 | + </SubjectConfirmationData> |
| 47 | + </SubjectConfirmation> |
| 48 | + </Subject> |
| 49 | + </Assertion> |
| 50 | + </Response> |
| 51 | + `; |
| 52 | + const error = t.throws(() => { |
| 53 | + libSaml.verifySignature(xml, { |
| 54 | + keyFile: "dummy.pem", |
| 55 | + signatureAlgorithm: "dummy", |
| 56 | + } as any); |
| 57 | + }); |
| 58 | + t.is(error!.message, "ERR_POTENTIAL_WRAPPING_ATTACK"); |
| 59 | +}); |
| 60 | + |
| 61 | +test("throws INVALID_CERTIFICATE_PROVIDED when metadata returns no certificate", (t) => { |
| 62 | + // Signature element is present and metadata returns null certificate. |
| 63 | + const xml = ` |
| 64 | + <Response> |
| 65 | + <Signature> |
| 66 | + <X509Data> |
| 67 | + <X509Certificate>ANOTHERCERT</X509Certificate> |
| 68 | + </X509Data> |
| 69 | + </Signature> |
| 70 | + <Assertion ID="ID">Content</Assertion> |
| 71 | + </Response> |
| 72 | + `; |
| 73 | + const error = t.throws(() => { |
| 74 | + libSaml.verifySignature(xml, { |
| 75 | + metadata: fakeMetadataInvalid, |
| 76 | + signatureAlgorithm: "dummy", |
| 77 | + } as any); |
| 78 | + }); |
| 79 | + t.is(error!.message, "INVALID_CERTIFICATE_PROVIDED"); |
| 80 | +}); |
| 81 | + |
| 82 | +test("returns valid verification result when signature is valid", (t) => { |
| 83 | + // Override SignedXml methods to simulate valid signature checking |
| 84 | + const origCheckSignature = SignedXml.prototype.checkSignature; |
| 85 | + const origLoadSignature = SignedXml.prototype.loadSignature; |
| 86 | + SignedXml.prototype.checkSignature = () => true; |
| 87 | + SignedXml.prototype.loadSignature = () => {}; |
| 88 | + |
| 89 | + // Create a minimal XML with a Signature element containing a valid certificate node |
| 90 | + const xml = ` |
| 91 | + <Response> |
| 92 | + <Signature> |
| 93 | + <X509Data> |
| 94 | + <X509Certificate>VALIDCERT</X509Certificate> |
| 95 | + </X509Data> |
| 96 | + </Signature> |
| 97 | + <Assertion ID="ID">AssertionContent</Assertion> |
| 98 | + </Response> |
| 99 | + `; |
| 100 | + const result = libSaml.verifySignature(xml, { |
| 101 | + metadata: fakeMetadataValid, |
| 102 | + signatureAlgorithm: "dummy", |
| 103 | + } as any); |
| 104 | + t.true(Array.isArray(result)); |
| 105 | + t.true(result[0] === true); |
| 106 | + t.regex(typeof result[1] === "string" ? result[1] : "", /Assertion/); |
| 107 | + |
| 108 | + // Restore the original methods |
| 109 | + SignedXml.prototype.checkSignature = origCheckSignature; |
| 110 | + SignedXml.prototype.loadSignature = origLoadSignature; |
| 111 | +}); |
0 commit comments