@@ -12,36 +12,45 @@ describe("SAML response tests", function () {
1212 "/*/*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']" ,
1313 doc ,
1414 ) ;
15- const sig = new SignedXml ( ) ;
16- sig . publicCert = fs . readFileSync ( "./test/static/feide_public.pem" ) ;
17- // @ts -expect-error FIXME
18- sig . loadSignature ( signature ) ;
19- const result = sig . checkSignature ( xml ) ;
15+ if ( xpath . isNodeLike ( signature ) ) {
16+ const sig = new SignedXml ( ) ;
17+ sig . publicCert = fs . readFileSync ( "./test/static/feide_public.pem" ) ;
18+ sig . loadSignature ( signature ) ;
19+ const result = sig . checkSignature ( xml ) ;
2020
21- expect ( result ) . to . be . true ;
21+ expect ( result ) . to . be . true ;
22+ } else {
23+ expect ( xpath . isNodeLike ( signature ) ) . to . be . true ;
24+ }
2225 } ) ;
2326
2427 it ( "test validating wrapped assertion signature" , function ( ) {
2528 const xml = fs . readFileSync ( "./test/static/valid_saml_signature_wrapping.xml" , "utf-8" ) ;
2629 const doc = new xmldom . DOMParser ( ) . parseFromString ( xml ) ;
2730 const assertion = xpath . select1 ( "//*[local-name(.)='Assertion']" , doc ) ;
28- const signature = xpath . select1 (
29- "//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']" ,
30- // @ts -expect-error FIXME
31- assertion ,
32- ) ;
33- const sig = new SignedXml ( ) ;
34- sig . publicCert = fs . readFileSync ( "./test/static/feide_public.pem" ) ;
35- // @ts -expect-error FIXME
36- sig . loadSignature ( signature ) ;
37- expect (
38- function ( ) {
39- sig . checkSignature ( xml ) ;
40- } ,
41- "Should not validate a document which contains multiple elements with the " +
42- "same value for the ID / Id / Id attributes, in order to prevent " +
43- "signature wrapping attack." ,
44- ) . to . throw ( ) ;
31+ if ( xpath . isNodeLike ( assertion ) ) {
32+ const signature = xpath . select1 (
33+ "//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']" ,
34+ assertion ,
35+ ) ;
36+ if ( xpath . isNodeLike ( signature ) ) {
37+ const sig = new SignedXml ( ) ;
38+ sig . publicCert = fs . readFileSync ( "./test/static/feide_public.pem" ) ;
39+ sig . loadSignature ( signature ) ;
40+ expect (
41+ function ( ) {
42+ sig . checkSignature ( xml ) ;
43+ } ,
44+ "Should not validate a document which contains multiple elements with the " +
45+ "same value for the ID / Id / Id attributes, in order to prevent " +
46+ "signature wrapping attack." ,
47+ ) . to . throw ( ) ;
48+ } else {
49+ expect ( xpath . isNodeLike ( signature ) ) . to . be . true ;
50+ }
51+ } else {
52+ expect ( xpath . isNodeLike ( assertion ) ) . to . be . true ;
53+ }
4554 } ) ;
4655
4756 it ( "test validating SAML response where a namespace is defined outside the signed element" , function ( ) {
@@ -51,30 +60,39 @@ describe("SAML response tests", function () {
5160 "//*//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']" ,
5261 doc ,
5362 ) ;
54- const sig = new SignedXml ( ) ;
55- sig . publicCert = fs . readFileSync ( "./test/static/saml_external_ns.pem" ) ;
56- // @ts -expect-error FIXME
57- sig . loadSignature ( signature ) ;
58- const result = sig . checkSignature ( xml ) ;
59- expect ( result ) . to . be . true ;
63+ if ( xpath . isNodeLike ( signature ) ) {
64+ const sig = new SignedXml ( ) ;
65+ sig . publicCert = fs . readFileSync ( "./test/static/saml_external_ns.pem" ) ;
66+ sig . loadSignature ( signature ) ;
67+ const result = sig . checkSignature ( xml ) ;
68+ expect ( result ) . to . be . true ;
69+ } else {
70+ expect ( xpath . isNodeLike ( signature ) ) . to . be . true ;
71+ }
6072 } ) ;
6173
6274 it ( "test reference id does not contain quotes" , function ( ) {
6375 const xml = fs . readFileSync ( "./test/static/id_with_quotes.xml" , "utf-8" ) ;
6476 const doc = new xmldom . DOMParser ( ) . parseFromString ( xml ) ;
6577 const assertion = xpath . select1 ( "//*[local-name(.)='Assertion']" , doc ) ;
66- const signature = xpath . select1 (
67- "//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']" ,
68- // @ts -expect-error FIXME
69- assertion ,
70- ) ;
71- const sig = new SignedXml ( ) ;
72- sig . publicCert = fs . readFileSync ( "./test/static/feide_public.pem" ) ;
73- // @ts -expect-error FIXME
74- sig . loadSignature ( signature ) ;
75- expect ( function ( ) {
76- sig . checkSignature ( xml ) ;
77- } , "id should not contain quotes" ) . to . throw ( ) ;
78+ if ( xpath . isNodeLike ( assertion ) ) {
79+ const signature = xpath . select1 (
80+ "//*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']" ,
81+ assertion ,
82+ ) ;
83+ if ( xpath . isNodeLike ( signature ) ) {
84+ const sig = new SignedXml ( ) ;
85+ sig . publicCert = fs . readFileSync ( "./test/static/feide_public.pem" ) ;
86+ sig . loadSignature ( signature ) ;
87+ expect ( function ( ) {
88+ sig . checkSignature ( xml ) ;
89+ } , "id should not contain quotes" ) . to . throw ( ) ;
90+ } else {
91+ expect ( xpath . isNodeLike ( signature ) ) . to . be . true ;
92+ }
93+ } else {
94+ expect ( xpath . isNodeLike ( assertion ) ) . to . be . true ;
95+ }
7896 } ) ;
7997
8098 it ( "test validating SAML response WithComments" , function ( ) {
@@ -84,12 +102,15 @@ describe("SAML response tests", function () {
84102 "/*/*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']" ,
85103 doc ,
86104 ) ;
87- const sig = new SignedXml ( ) ;
88- sig . publicCert = fs . readFileSync ( "./test/static/feide_public.pem" ) ;
89- // @ts -expect-error FIXME
90- sig . loadSignature ( signature ) ;
91- const result = sig . checkSignature ( xml ) ;
92- // This doesn't matter, just want to make sure that we don't fail due to unknown algorithm
93- expect ( result ) . to . be . false ;
105+ if ( xpath . isNodeLike ( signature ) ) {
106+ const sig = new SignedXml ( ) ;
107+ sig . publicCert = fs . readFileSync ( "./test/static/feide_public.pem" ) ;
108+ sig . loadSignature ( signature ) ;
109+ const result = sig . checkSignature ( xml ) ;
110+ // This doesn't matter, just want to make sure that we don't fail due to unknown algorithm
111+ expect ( result ) . to . be . false ;
112+ } else {
113+ expect ( xpath . isNodeLike ( signature ) ) . to . be . true ;
114+ }
94115 } ) ;
95116} ) ;
0 commit comments