-
-
Notifications
You must be signed in to change notification settings - Fork 590
Expand file tree
/
Copy pathresponse_test.rb
More file actions
2129 lines (1696 loc) · 106 KB
/
response_test.rb
File metadata and controls
2129 lines (1696 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require_relative 'test_helper'
class RubySamlTest < Minitest::Test
describe 'Response' do
let(:settings) { RubySaml::Settings.new }
let(:response) { RubySaml::Response.new(response_document_without_recipient) }
let(:response_without_recipient) { RubySaml::Response.new(signed_response_document_without_recipient) }
let(:response_without_attributes) { RubySaml::Response.new(response_document_without_attributes) }
let(:response_with_multiple_attribute_statements) { RubySaml::Response.new(fixture(:response_with_multiple_attribute_statements)) }
let(:response_without_reference_uri) { RubySaml::Response.new(response_document_without_reference_uri) }
let(:response_with_signed_assertion) { RubySaml::Response.new(response_document_with_signed_assertion) }
let(:response_with_ds_namespace_at_the_root) { RubySaml::Response.new(response_document_with_ds_namespace_at_the_root) }
let(:response_unsigned) { RubySaml::Response.new(response_document_unsigned) }
let(:response_wrapped) { RubySaml::Response.new(response_document_wrapped) }
let(:response_multiple_attr_values) { RubySaml::Response.new(fixture(:response_with_multiple_attribute_values)) }
let(:response_valid_signed) { RubySaml::Response.new(response_document_valid_signed) }
let(:response_valid_signed_without_recipient) { RubySaml::Response.new(response_document_valid_signed, { skip_recipient_check: true }) }
let(:response_valid_signed_without_x509certificate) { RubySaml::Response.new(response_document_valid_signed_without_x509certificate) }
let(:response_no_id) { RubySaml::Response.new(read_invalid_response('no_id.xml.base64')) }
let(:response_no_version) { RubySaml::Response.new(read_invalid_response('no_saml2.xml.base64')) }
let(:response_multi_assertion) { RubySaml::Response.new(read_invalid_response('multiple_assertions.xml.base64')) }
let(:response_no_conditions) { RubySaml::Response.new(read_invalid_response('no_conditions.xml.base64')) }
let(:response_no_conditions_with_skip) { RubySaml::Response.new(read_invalid_response('no_conditions.xml.base64'), { skip_conditions: true }) }
let(:response_no_authnstatement) { RubySaml::Response.new(read_invalid_response('no_authnstatement.xml.base64')) }
let(:response_no_authnstatement_with_skip) { RubySaml::Response.new(read_invalid_response('no_authnstatement.xml.base64'), { skip_authnstatement: true }) }
let(:response_empty_destination) { RubySaml::Response.new(read_invalid_response('empty_destination.xml.base64')) }
let(:response_empty_destination_with_skip) { RubySaml::Response.new(read_invalid_response('empty_destination.xml.base64'), { skip_destination: true }) }
let(:response_no_status) { RubySaml::Response.new(read_invalid_response('no_status.xml.base64')) }
let(:response_no_statuscode) { RubySaml::Response.new(read_invalid_response('no_status_code.xml.base64')) }
let(:response_statuscode_responder) { RubySaml::Response.new(read_invalid_response('status_code_responder.xml.base64')) }
let(:response_statuscode_responder_and_msg) { RubySaml::Response.new(read_invalid_response('status_code_responder_and_msg.xml.base64')) }
let(:response_double_statuscode) { RubySaml::Response.new(response_document_double_status_code) }
let(:response_encrypted_attrs) { RubySaml::Response.new(response_document_encrypted_attrs) }
let(:response_no_signed_elements) { RubySaml::Response.new(read_invalid_response('no_signature.xml.base64')) }
let(:response_multiple_signed) { RubySaml::Response.new(read_invalid_response('multiple_signed.xml.base64')) }
let(:response_audience_self_closed) { RubySaml::Response.new(read_response('response_audience_self_closed_tag.xml.base64')) }
let(:response_invalid_audience) { RubySaml::Response.new(read_invalid_response('invalid_audience.xml.base64')) }
let(:response_invalid_audience_with_skip) { RubySaml::Response.new(read_invalid_response('invalid_audience.xml.base64'), { skip_audience: true }) }
let(:response_invalid_signed_element) { RubySaml::Response.new(read_invalid_response('response_invalid_signed_element.xml.base64')) }
let(:response_invalid_issuer_assertion) { RubySaml::Response.new(read_invalid_response('invalid_issuer_assertion.xml.base64')) }
let(:response_invalid_issuer_message) { RubySaml::Response.new(read_invalid_response('invalid_issuer_message.xml.base64')) }
let(:response_no_issuer_response) { RubySaml::Response.new(read_invalid_response('no_issuer_response.xml.base64')) }
let(:response_no_issuer_assertion) { RubySaml::Response.new(read_invalid_response('no_issuer_assertion.xml.base64')) }
let(:response_no_nameid) { RubySaml::Response.new(read_invalid_response('no_nameid.xml.base64')) }
let(:response_empty_nameid) { RubySaml::Response.new(read_invalid_response('empty_nameid.xml.base64')) }
let(:response_wrong_spnamequalifier) { RubySaml::Response.new(read_invalid_response('wrong_spnamequalifier.xml.base64')) }
let(:response_duplicated_attributes) { RubySaml::Response.new(read_invalid_response('duplicated_attributes.xml.base64')) }
let(:response_no_subjectconfirmation_data) { RubySaml::Response.new(read_invalid_response('no_subjectconfirmation_data.xml.base64')) }
let(:response_no_subjectconfirmation_method) { RubySaml::Response.new(read_invalid_response('no_subjectconfirmation_method.xml.base64')) }
let(:response_invalid_subjectconfirmation_inresponse) { RubySaml::Response.new(read_invalid_response('invalid_subjectconfirmation_inresponse.xml.base64')) }
let(:response_invalid_subjectconfirmation_recipient) { RubySaml::Response.new(read_invalid_response('invalid_subjectconfirmation_recipient.xml.base64')) }
let(:response_invalid_subjectconfirmation_nb) { RubySaml::Response.new(read_invalid_response('invalid_subjectconfirmation_nb.xml.base64')) }
let(:response_invalid_subjectconfirmation_noa) { RubySaml::Response.new(read_invalid_response('invalid_subjectconfirmation_noa.xml.base64')) }
let(:response_invalid_signature_position) { RubySaml::Response.new(read_invalid_response('invalid_signature_position.xml.base64')) }
let(:response_encrypted_nameid) { RubySaml::Response.new(response_document_encrypted_nameid) }
def generate_audience_error(expected, actual)
s = actual.count > 1 ? 's' : ''
"Invalid Audience#{s}. The audience#{s} #{actual.join(',')}, did not match the expected audience #{expected}"
end
it 'raise an exception when response is initialized with nil' do
assert_raises(ArgumentError) { RubySaml::Response.new(nil) }
end
it 'raise an exception when the settings provided are not a RubySaml::Settings object' do
settings = "invalid settings"
error_msg = "Invalid settings type: expected RubySaml::Settings, got String"
options = { :settings => settings }
assert_raises(RubySaml::ValidationError, error_msg) do
RubySaml::Response.new(response_document_valid_signed, options)
end
end
it 'not filter available options only' do
options = { skip_destination: true, foo: :bar }
response = RubySaml::Response.new(response_document_valid_signed, options)
assert_includes response.options.keys, :skip_destination
assert_includes response.options.keys, :foo
end
it 'be able to parse a document which contains ampersands' do
RubySaml::XML::SignedDocumentValidator.stubs(:digests_match?).returns(true)
RubySaml::Response.any_instance.stubs(:validate_conditions).returns(true)
ampersands_response = RubySaml::Response.new(ampersands_document)
ampersands_response.settings = settings
ampersands_response.settings.idp_cert_fingerprint = 'c51985d947f1be57082025050846eb27f6cab783'
refute ampersands_response.is_valid?
assert_includes ampersands_response.errors, 'SAML Response must contain 1 assertion'
end
it 'Raise ValidationError if XML contains SyntaxError trying to initialize and soft = false' do
settings.soft = false
error_msg = if jruby?
'XML load failed: The element type "ds:X509Certificate" must be terminated by the matching end-tag "</ds:X509Certificate>".'
else
'XML load failed: 53:875: FATAL: Opening and ending tag mismatch: X509Certificate line 53 and SignatureValue'
end
assert_raises(RubySaml::ValidationError, error_msg) do
OneLogin::RubySaml::Response.new(fixture(:response_wrong_syntax), :settings => settings)
end
end
it "Do not raise validation error when XML contains SyntaxError and soft = true, but validation fails" do
settings.soft = true
settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
response = OneLogin::RubySaml::Response.new(fixture(:response_wrong_syntax), :settings => settings)
error_msg = if jruby?
'XML load failed: The element type "ds:X509Certificate" must be terminated by the matching end-tag "</ds:X509Certificate>".'
else
'XML load failed: 53:875: FATAL: Opening and ending tag mismatch: X509Certificate line 53 and SignatureValue'
end
assert_includes response.errors, error_msg
refute response.is_valid?
assert_includes response.errors, 'Blank response'
assert_nil response.nameid
assert_nil response.attributes
end
describe 'Prevent node text with comment attack (VU#475445)' do
before do
@response = RubySaml::Response.new(read_response('response_node_text_attack.xml.base64'))
end
it 'receives the full NameID when there is an injected comment' do
assert_equal 'support@onelogin.com', @response.name_id
end
it 'receives the full AttributeValue when there is an injected comment' do
assert_equal 'smith', @response.attributes['surname']
end
end
describe 'Another test to prevent with comment attack (VU#475445)' do
before do
@response = RubySaml::Response.new(read_response('response_node_text_attack2.xml.base64'), { skip_recipient_check: true })
@response.settings = settings
@response.settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
end
it 'receives the full NameID when there is an injected comment, validates the response' do
assert_equal 'test@onelogin.com', @response.name_id
end
end
describe 'Another test with CDATA injected' do
before do
@response = RubySaml::Response.new(read_response('response_node_text_attack3.xml.base64'), { skip_recipient_check: true })
@response.settings = settings
@response.settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
end
it 'it normalizes CDATA but reject SAMLResponse due signature invalidation' do
assert_equal 'test@onelogin.com.evil.com', @response.name_id
refute @response.is_valid?
assert_includes @response.errors, 'Invalid Signature on SAML Response'
end
end
describe 'Prevent XEE attack' do
before do
@response = RubySaml::Response.new(fixture(:attackxee))
end
it 'false when evil attack vector is present, soft = true' do
@response.soft = true
refute @response.send(:validate_structure)
assert_includes @response.errors, 'Invalid SAML Response. Not match the saml-schema-protocol-2.0.xsd'
end
it 'raise when evil attack vector is present, soft = false ' do
@response.soft = false
error_msg = 'XML load failed: Dangerous XML detected. No Doctype nodes allowed'
assert_raises(RubySaml::ValidationError, error_msg) do
@response.send(:validate_structure)
end
end
end
it 'adapt namespace' do
refute_nil response.nameid
refute_nil response_without_attributes.nameid
refute_nil response_with_signed_assertion.nameid
end
it 'default to raw input when a response is not Base64 encoded' do
decoded = Base64.decode64(response_document_without_attributes)
response_from_raw = RubySaml::Response.new(decoded)
assert response_from_raw.document
end
describe 'Assertion' do
it "only retrieve an assertion with an ID that matches the signature's reference URI" do
response_wrapped.stubs(:conditions).returns(nil)
settings.idp_cert_fingerprint = signature_fingerprint_1
response_wrapped.settings = settings
assert_nil response_wrapped.nameid
end
end
describe '#is_valid?' do
describe 'soft = false' do
before do
response.soft = false
response_valid_signed.soft = false
end
it 'raise when response is initialized with blank data' do
blank_response = RubySaml::Response.new('')
blank_response.soft = false
error_msg = 'Blank response'
assert_raises(RubySaml::ValidationError, error_msg) do
blank_response.is_valid?
end
assert_includes blank_response.errors, error_msg
end
it 'raise when settings have not been set' do
error_msg = 'No settings on response'
assert_raises(RubySaml::ValidationError, error_msg) do
response.is_valid?
end
assert_includes response.errors, error_msg
end
it 'raise when No fingerprint or certificate on settings' do
settings.idp_cert_fingerprint = nil
settings.idp_cert = nil
settings.idp_cert_multi = nil
response.settings = settings
error_msg = 'No fingerprint or certificate on settings'
assert_raises(RubySaml::ValidationError, error_msg) do
response.is_valid?
end
assert_includes response.errors, error_msg
end
it 'raise when signature wrapping attack' do
response_wrapped.stubs(:conditions).returns(nil)
response_wrapped.stubs(:validate_subject_confirmation).returns(true)
settings.idp_cert_fingerprint = signature_fingerprint_1
response_wrapped.settings = settings
refute response_wrapped.is_valid?
end
it 'raise when no signature' do
settings.idp_cert_fingerprint = signature_fingerprint_1
response_no_signed_elements.settings = settings
response_no_signed_elements.soft = false
error_msg = 'Found an unexpected number of Signature Element. SAML Response rejected'
assert_raises(RubySaml::ValidationError, error_msg) do
response_no_signed_elements.is_valid?
end
end
it 'raise when multiple signatures' do
settings.idp_cert_fingerprint = signature_fingerprint_1
response_multiple_signed.settings = settings
response_multiple_signed.soft = false
error_msg = 'Duplicated ID. SAML Response rejected'
assert_raises(RubySaml::ValidationError, error_msg) do
response_multiple_signed.is_valid?
end
end
it 'validate SAML 2.0 XML structure' do
resp_xml = Base64.decode64(response_document_unsigned).gsub('emailAddress', 'test')
response_unsigned_mod = RubySaml::Response.new(Base64.strict_encode64(resp_xml))
response_unsigned_mod.stubs(:conditions).returns(nil)
settings.idp_cert_fingerprint = signature_fingerprint_1
response_unsigned_mod.settings = settings
response_unsigned_mod.soft = false
assert_raises(RubySaml::ValidationError, 'Digest mismatch') do
response_unsigned_mod.is_valid?
end
end
it 'raise when encountering a condition that prevents the document from being valid' do
settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
response_without_recipient.settings = settings
response_without_recipient.soft = false
error_msg = 'Current time is on or after NotOnOrAfter condition'
assert_raises(RubySaml::ValidationError, error_msg) do
response_without_recipient.is_valid?
end
refute_empty response_without_recipient.errors
assert_includes response_without_recipient.errors[0], error_msg
end
it 'raise when encountering a SAML Response with bad formatting' do
settings.idp_cert_fingerprint = signature_fingerprint_1
response_without_attributes.settings = settings
response_without_attributes.soft = false
assert_raises(RubySaml::ValidationError) do
response_without_attributes.is_valid?
end
end
it 'raise when the inResponseTo value does not match the Request ID' do
settings.soft = false
settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
opts = {}
opts[:settings] = settings
opts[:matches_request_id] = 'invalid_request_id'
response_valid_signed = RubySaml::Response.new(response_document_valid_signed, opts)
error_msg = 'The InResponseTo of the Response: _fc4a34b0-7efb-012e-caae-782bcb13bb38, does not match the ID of the AuthNRequest sent by the SP: invalid_request_id'
assert_raises(RubySaml::ValidationError, error_msg) do
response_valid_signed.is_valid?
end
assert_includes response_valid_signed.errors, error_msg
end
it 'raise when there is no valid audience' do
settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
settings.sp_entity_id = 'invalid'
response_valid_signed.settings = settings
response_valid_signed.soft = false
error_msg = generate_audience_error(response_valid_signed.settings.sp_entity_id, ['https://someone.example.com/audience'])
assert_raises(RubySaml::ValidationError, error_msg) do
response_valid_signed.is_valid?
end
assert_includes response_valid_signed.errors, error_msg
end
it 'raise when no ID present in the SAML Response' do
settings.idp_cert_fingerprint = signature_fingerprint_1
response_no_id.settings = settings
response_no_id.soft = false
error_msg = 'Missing ID attribute on SAML Response'
assert_raises(RubySaml::ValidationError, error_msg) do
response_no_id.is_valid?
end
assert_includes response_no_id.errors, error_msg
end
it 'raise when no 2.0 Version present in the SAML Response' do
settings.idp_cert_fingerprint = signature_fingerprint_1
response_no_version.settings = settings
response_no_version.soft = false
error_msg = 'Unsupported SAML version'
assert_raises(RubySaml::ValidationError, error_msg) do
response_no_version.is_valid?
end
assert_includes response_no_version.errors, error_msg
end
end
describe 'soft = true' do
before do
response.soft = true
response_valid_signed.soft = true
end
it 'return true when the response is initialized with valid data' do
response_valid_signed_without_recipient.stubs(:conditions).returns(nil)
response_valid_signed_without_recipient.settings = settings
response_valid_signed_without_recipient.settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
assert_predicate response_valid_signed_without_recipient, :is_valid?
assert_empty response_valid_signed_without_recipient.errors
end
it 'return true when the response is initialized with valid data and using certificate instead of fingerprint' do
response_valid_signed_without_recipient.stubs(:conditions).returns(nil)
response_valid_signed_without_recipient.settings = settings
response_valid_signed_without_recipient.settings.idp_cert = ruby_saml_cert_text
assert_predicate response_valid_signed_without_recipient, :is_valid?
assert_empty response_valid_signed_without_recipient.errors
end
it 'return false when response is initialized with blank data' do
blank_response = RubySaml::Response.new('')
blank_response.soft = true
refute blank_response.is_valid?
assert_includes blank_response.errors, 'Blank response'
end
it 'return false if settings have not been set' do
refute response.is_valid?
assert_includes response.errors, 'No settings on response'
end
it 'return false if fingerprint or certificate not been set on settings' do
response.settings = settings
refute response.is_valid?
assert_includes response.errors, 'No fingerprint or certificate on settings'
end
it 'should be idempotent when the response is initialized with invalid data' do
response_unsigned.stubs(:conditions).returns(nil)
response_unsigned.settings = settings
refute response_unsigned.is_valid?
refute response_unsigned.is_valid?
end
it 'should be idempotent when the response is initialized with valid data' do
response_valid_signed_without_recipient.stubs(:conditions).returns(nil)
response_valid_signed_without_recipient.settings = settings
response_valid_signed_without_recipient.settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
assert_predicate response_valid_signed_without_recipient, :is_valid?
assert_predicate response_valid_signed_without_recipient, :is_valid?
end
it 'not allow signature wrapping attack' do
response_wrapped.stubs(:conditions).returns(nil)
response_wrapped.stubs(:validate_subject_confirmation).returns(true)
settings.idp_cert_fingerprint = signature_fingerprint_1
response_wrapped.settings = settings
refute response_wrapped.is_valid?
end
it 'support dynamic namespace resolution on signature elements' do
no_signature_response = RubySaml::Response.new(fixture('inclusive_namespaces.xml'))
no_signature_response.stubs(:conditions).returns(nil)
no_signature_response.stubs(:validate_subject_confirmation).returns(true)
no_signature_response.settings = settings
no_signature_response.settings.idp_cert_fingerprint = 'E0:89:CF:86:E3:00:C0:C8:B9:BC:04:16:D7:F3:8D:8D:9C:8F:20:B3:FE:7C:EC:64:D5:5D:90:E3:7B:8B:5A:51'
assert_predicate no_signature_response, :is_valid?
end
it 'validate ADFS assertions' do
adfs_response = RubySaml::Response.new(fixture(:adfs_response_sha256))
adfs_response.stubs(:conditions).returns(nil)
adfs_response.stubs(:validate_subject_confirmation).returns(true)
settings.idp_cert_fingerprint = '3D:C5:BC:58:60:5D:19:64:94:E3:BA:C8:3D:49:01:D5:56:34:44:65:C2:85:0A:A8:65:A5:AC:76:7E:65:1F:F7'
adfs_response.settings = settings
adfs_response.soft = true
assert_predicate adfs_response, :is_valid?
end
it 'validate SAML 2.0 XML structure' do
resp_xml = Base64.decode64(response_document_unsigned).gsub('emailAddress', 'test')
response_unsigned_mod = RubySaml::Response.new(Base64.strict_encode64(resp_xml))
response_unsigned_mod.stubs(:conditions).returns(nil)
settings.idp_cert_fingerprint = signature_fingerprint_1
response_unsigned_mod.settings = settings
response_unsigned_mod.soft = true
refute response_unsigned_mod.is_valid?
end
it 'return false when encountering a condition that prevents the document from being valid' do
settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
response_without_recipient.settings = settings
error_msg = 'Current time is on or after NotOnOrAfter condition'
refute response_without_recipient.is_valid?
refute_empty response_without_recipient.errors
assert_includes response_without_recipient.errors[0], error_msg
end
it 'return false when encountering a SAML Response with bad formatted' do
settings.idp_cert_fingerprint = signature_fingerprint_1
response_without_attributes.settings = settings
response_without_attributes.soft = true
error_msg = 'Invalid SAML Response. Not match the saml-schema-protocol-2.0.xsd'
response_without_attributes.is_valid?
assert_includes response_without_attributes.errors, error_msg
end
it 'return false when the inResponseTo value does not match the Request ID' do
settings.soft = true
settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
opts = {}
opts[:settings] = settings
opts[:matches_request_id] = 'invalid_request_id'
response_valid_signed = RubySaml::Response.new(response_document_valid_signed, opts)
response_valid_signed.is_valid?
assert_includes response_valid_signed.errors, 'The InResponseTo of the Response: _fc4a34b0-7efb-012e-caae-782bcb13bb38, does not match the ID of the AuthNRequest sent by the SP: invalid_request_id'
end
it 'return false when there is no valid audience' do
settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
settings.sp_entity_id = 'invalid'
response_valid_signed.settings = settings
response_valid_signed.is_valid?
assert_includes response_valid_signed.errors, generate_audience_error(response_valid_signed.settings.sp_entity_id, ['https://someone.example.com/audience'])
end
it 'return false when no ID present in the SAML Response' do
settings.idp_cert_fingerprint = signature_fingerprint_1
response_no_id.settings = settings
response_no_id.soft = true
response_no_id.is_valid?
assert_includes response_no_id.errors, 'Missing ID attribute on SAML Response'
end
it 'return false when no 2.0 Version present in the SAML Response' do
settings.idp_cert_fingerprint = signature_fingerprint_1
response_no_version.settings = settings
response_no_version.soft = true
error_msg = 'Unsupported SAML version'
response_no_version.is_valid?
assert_includes response_no_version.errors, error_msg
end
it 'return true when a nil URI is given in the ds:Reference' do
settings.idp_cert = ruby_saml_cert_text
settings.assertion_consumer_service_url = 'http://localhost:9001/v1/users/authorize/saml'
response_without_reference_uri.settings = settings
response_without_reference_uri.stubs(:conditions).returns(nil)
response_without_reference_uri.is_valid?
assert_empty response_without_reference_uri.errors
assert 'saml@user.com', response_without_reference_uri.attributes['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress']
end
it 'collect errors when collect_errors=true' do
settings.idp_cert = ruby_saml_cert_text
settings.sp_entity_id = 'invalid'
response_invalid_subjectconfirmation_recipient.settings = settings
collect_errors = true
response_invalid_subjectconfirmation_recipient.is_valid?(collect_errors)
assert_includes response_invalid_subjectconfirmation_recipient.errors, generate_audience_error('invalid', ['http://stuff.com/endpoints/metadata.php'])
assert_includes response_invalid_subjectconfirmation_recipient.errors, 'Invalid Signature on SAML Response'
end
end
end
describe '#validate_audience' do
it 'return true when the audience is valid' do
response.settings = settings
response.settings.sp_entity_id = '{audience}'
assert response.send(:validate_audience)
assert_empty response.errors
end
it 'return true when the audience is self closing and strict audience validation is not enabled' do
response_audience_self_closed.settings = settings
response_audience_self_closed.settings.sp_entity_id = '{audience}'
assert response_audience_self_closed.send(:validate_audience)
assert_empty response_audience_self_closed.errors
end
it 'return false when the audience is self closing and strict audience validation is enabled' do
response_audience_self_closed.settings = settings
response_audience_self_closed.settings.security[:strict_audience_validation] = true
response_audience_self_closed.settings.sp_entity_id = '{audience}'
refute response_audience_self_closed.send(:validate_audience)
assert_includes response_audience_self_closed.errors, 'Invalid Audiences. The <AudienceRestriction> element contained only empty <Audience> elements. Expected audience {audience}.'
end
it 'return false when the audience is invalid' do
response.settings = settings
response.settings.sp_entity_id = 'invalid_audience'
refute response.send(:validate_audience)
assert_includes response.errors, generate_audience_error(response.settings.sp_entity_id, ['{audience}'])
end
end
describe '#validate_destination' do
it 'return true when the destination of the SAML Response matches the assertion consumer service url' do
response.settings = settings
assert response.send(:validate_destination)
assert_empty response.errors
end
it 'return false when the destination of the SAML Response does not match the assertion consumer service url' do
response.settings = settings
response.settings.assertion_consumer_service_url = 'invalid_acs'
refute response.send(:validate_destination)
assert_includes response.errors, "The response was received at #{response.destination} instead of #{response.settings.assertion_consumer_service_url}"
end
it 'return false when the destination of the SAML Response is empty' do
response_empty_destination.settings = settings
refute response_empty_destination.send(:validate_destination)
assert_includes response_empty_destination.errors, 'The response has an empty Destination value'
end
it 'return true when the destination of the SAML Response is empty but skip_destination option is used' do
response_empty_destination_with_skip.settings = settings
assert response_empty_destination_with_skip.send(:validate_destination)
assert_empty response_empty_destination.errors
end
it 'returns true on a case insensitive match on the domain' do
response_valid_signed_without_x509certificate.settings = settings
response_valid_signed_without_x509certificate.settings.assertion_consumer_service_url = 'http://APP.muDa.no/sso/consume'
assert response_valid_signed_without_x509certificate.send(:validate_destination)
assert_empty response_valid_signed_without_x509certificate.errors
end
it 'returns true on a case insensitive match on the scheme' do
response_valid_signed_without_x509certificate.settings = settings
response_valid_signed_without_x509certificate.settings.assertion_consumer_service_url = 'HTTP://app.muda.no/sso/consume'
assert response_valid_signed_without_x509certificate.send(:validate_destination)
assert_empty response_valid_signed_without_x509certificate.errors
end
it 'returns false on a case insensitive match on the path' do
response_valid_signed_without_x509certificate.settings = settings
response_valid_signed_without_x509certificate.settings.assertion_consumer_service_url = 'http://app.muda.no/SSO/consume'
refute response_valid_signed_without_x509certificate.send(:validate_destination)
assert_includes response_valid_signed_without_x509certificate.errors,
"The response was received at #{response_valid_signed_without_x509certificate.destination} instead of #{response_valid_signed_without_x509certificate.settings.assertion_consumer_service_url}"
end
it "returns true if it can't parse out a full URI." do
response_valid_signed_without_x509certificate.settings = settings
response_valid_signed_without_x509certificate.settings.assertion_consumer_service_url = 'presenter'
refute response_valid_signed_without_x509certificate.send(:validate_destination)
assert_includes response_valid_signed_without_x509certificate.errors,
"The response was received at #{response_valid_signed_without_x509certificate.destination} instead of #{response_valid_signed_without_x509certificate.settings.assertion_consumer_service_url}"
end
end
describe '#validate_issuer' do
it 'return true when the issuer of the Message/Assertion matches the IdP entityId' do
response_valid_signed.settings = settings
assert response_valid_signed.send(:validate_issuer)
response_valid_signed.settings.idp_entity_id = 'https://app.onelogin.com/saml2'
assert response_valid_signed.send(:validate_issuer)
end
it 'return false when the issuer of the Message does not match the IdP entityId' do
response_invalid_issuer_message.settings = settings
response_invalid_issuer_message.settings.idp_entity_id = 'http://idp.example.com/'
refute response_invalid_issuer_message.send(:validate_issuer)
assert_includes response_invalid_issuer_message.errors, "Doesn't match the issuer, expected: <#{response_invalid_issuer_message.settings.idp_entity_id}>, but was: <http://invalid.issuer.example.com/>"
end
it 'return false when the issuer of the Assertion does not match the IdP entityId' do
response_invalid_issuer_assertion.settings = settings
response_invalid_issuer_assertion.settings.idp_entity_id = 'http://idp.example.com/'
refute response_invalid_issuer_assertion.send(:validate_issuer)
assert_includes response_invalid_issuer_assertion.errors, "Doesn't match the issuer, expected: <#{response_invalid_issuer_assertion.settings.idp_entity_id}>, but was: <http://invalid.issuer.example.com/>"
end
end
describe '#validate_num_assertion' do
it 'return true when SAML Response contains 1 assertion' do
assert response.send(:validate_num_assertion)
assert_empty response.errors
end
it 'return false when no 2.0 Version present in the SAML Response' do
refute response_multi_assertion.send(:validate_num_assertion)
assert_includes response_multi_assertion.errors, 'SAML Response must contain 1 assertion'
end
end
describe 'validate_success_status' do
it "return true when the status is 'Success'" do
assert response.send(:validate_success_status)
assert_empty response.errors
end
it 'return false when no Status provided' do
refute response_no_status.send(:validate_success_status)
assert_includes response_no_status.errors, 'The status code of the Response was not Success'
end
it 'return false when no StatusCode provided' do
refute response_no_statuscode.send(:validate_success_status)
assert_includes response_no_statuscode.errors, 'The status code of the Response was not Success'
end
it "return false when the status is not 'Success'" do
refute response_statuscode_responder.send(:validate_success_status)
assert_includes response_statuscode_responder.errors, 'The status code of the Response was not Success, was Responder'
end
it "return false when the status is not 'Success', and shows the StatusMessage" do
refute response_statuscode_responder_and_msg.send(:validate_success_status)
assert_includes response_statuscode_responder_and_msg.errors, 'The status code of the Response was not Success, was Responder -> something_is_wrong'
end
it "return false when the status is not 'Success'" do
refute response_double_statuscode.send(:validate_success_status)
assert_includes response_double_statuscode.errors, 'The status code of the Response was not Success, was Requester => UnsupportedBinding'
end
end
describe '#validate_structure' do
it 'return true when encountering a wellformed SAML Response' do
assert response.send(:validate_structure)
assert_empty response.errors
end
it 'return false when encountering a malformed element that prevents the document from being valid' do
response_without_attributes.soft = true
response_without_attributes.send(:validate_structure)
assert_includes response_without_attributes.errors, 'Invalid SAML Response. Not match the saml-schema-protocol-2.0.xsd'
end
it 'raise when encountering a malformed element that prevents the document from being valid' do
response_without_attributes.soft = false
assert_raises(RubySaml::ValidationError) do
response_without_attributes.send(:validate_structure)
end
end
end
describe 'validate_formatted_x509_certificate' do
let(:response_with_formatted_x509certificate) do
RubySaml::Response.new(read_response('valid_response_with_formatted_x509certificate.xml.base64'), {
skip_conditions: true,
skip_subject_confirmation: true
})
end
it 'be able to parse the response without errors' do
response_with_formatted_x509certificate.settings = settings
response_with_formatted_x509certificate.settings.idp_cert = ruby_saml_cert_text
assert_predicate response_with_formatted_x509certificate, :is_valid?
assert_empty response_with_formatted_x509certificate.errors
end
end
describe '#validate_in_response_to' do
it 'return true when the inResponseTo value matches the Request ID' do
response = RubySaml::Response.new(response_document_valid_signed, settings: settings, matches_request_id: '_fc4a34b0-7efb-012e-caae-782bcb13bb38')
assert response.send(:validate_in_response_to)
assert_empty response.errors
end
it 'return true when no Request ID is provided for checking' do
response = RubySaml::Response.new(response_document_valid_signed, settings: settings)
assert response.send(:validate_in_response_to)
assert_empty response.errors
end
it 'return false when the inResponseTo value does not match the Request ID' do
response = RubySaml::Response.new(response_document_valid_signed, settings: settings, matches_request_id: 'invalid_request_id')
refute response.send(:validate_in_response_to)
assert_includes response.errors, 'The InResponseTo of the Response: _fc4a34b0-7efb-012e-caae-782bcb13bb38, does not match the ID of the AuthNRequest sent by the SP: invalid_request_id'
end
end
describe '#validate_audience' do
it 'return true when the audience is valid' do
response_valid_signed.settings = settings
response_valid_signed.settings.sp_entity_id = 'https://someone.example.com/audience'
assert response_valid_signed.send(:validate_audience)
assert_empty response_valid_signed.errors
end
it 'return true when there is not sp_entity_id defined' do
response_valid_signed.settings = settings
response_valid_signed.settings.sp_entity_id = nil
assert response_valid_signed.send(:validate_audience)
assert_empty response_valid_signed.errors
end
it 'return false when there is no valid audience' do
response_invalid_audience.settings = settings
response_invalid_audience.settings.sp_entity_id = 'https://invalid.example.com/audience'
refute response_invalid_audience.send(:validate_audience)
assert_includes response_invalid_audience.errors, generate_audience_error(response_invalid_audience.settings.sp_entity_id, ['http://invalid.audience.com'])
end
it 'return true when there is no valid audience but skip_destination option is used' do
response_invalid_audience_with_skip.settings = settings
response_invalid_audience_with_skip.settings.sp_entity_id = 'https://invalid.example.com/audience'
assert response_invalid_audience_with_skip.send(:validate_audience)
assert_empty response_invalid_audience_with_skip.errors
end
end
describe '#validate_issuer' do
it 'return true when the issuer of the Message/Assertion matches the IdP entityId or it was empty' do
response_valid_signed.settings = settings
assert response_valid_signed.send(:validate_issuer)
assert_empty response_valid_signed.errors
response_valid_signed.settings.idp_entity_id = 'https://app.onelogin.com/saml2'
assert response_valid_signed.send(:validate_issuer)
assert_empty response_valid_signed.errors
end
it 'return false when the issuer of the Message does not match the IdP entityId' do
response_invalid_issuer_message.settings = settings
response_invalid_issuer_message.settings.idp_entity_id = 'http://idp.example.com/'
refute response_invalid_issuer_message.send(:validate_issuer)
assert_includes response_invalid_issuer_message.errors, "Doesn't match the issuer, expected: <#{response_invalid_issuer_message.settings.idp_entity_id}>, but was: <http://invalid.issuer.example.com/>"
end
it 'return false when the issuer of the Assertion does not match the IdP entityId' do
response_invalid_issuer_assertion.settings = settings
response_invalid_issuer_assertion.settings.idp_entity_id = 'http://idp.example.com/'
refute response_invalid_issuer_assertion.send(:validate_issuer)
assert_includes response_invalid_issuer_assertion.errors, "Doesn't match the issuer, expected: <#{response_invalid_issuer_assertion.settings.idp_entity_id}>, but was: <http://invalid.issuer.example.com/>"
end
it 'return false when the no issuer at the Response' do
response_no_issuer_response.settings = settings
response_no_issuer_response.settings.idp_entity_id = 'http://idp.example.com/'
refute response_no_issuer_response.send(:validate_issuer)
assert_includes response_no_issuer_response.errors, 'Issuer of the Response not found or multiple.'
end
it 'return false when the no issuer at the Assertion' do
response_no_issuer_assertion.settings = settings
response_no_issuer_assertion.settings.idp_entity_id = 'http://idp.example.com/'
refute response_no_issuer_assertion.send(:validate_issuer)
assert_includes response_no_issuer_assertion.errors, 'Issuer of the Assertion not found or multiple.'
end
end
describe '#validate_subject_confirmation' do
it 'return true when valid subject confirmation' do
response_valid_signed.settings = settings
response_valid_signed.settings.assertion_consumer_service_url = 'recipient'
assert response_valid_signed.send(:validate_subject_confirmation)
assert_empty response_valid_signed.errors
end
it 'return false when no subject confirmation data' do
response_no_subjectconfirmation_data.settings = settings
refute response_no_subjectconfirmation_data.send(:validate_subject_confirmation)
assert_includes response_no_subjectconfirmation_data.errors, 'A valid SubjectConfirmation was not found on this Response'
end
it 'return false when no valid subject confirmation method' do
response_no_subjectconfirmation_method.settings = settings
refute response_no_subjectconfirmation_method.send(:validate_subject_confirmation)
assert_includes response_no_subjectconfirmation_method.errors, 'A valid SubjectConfirmation was not found on this Response'
end
it 'return false when invalid inresponse' do
response_invalid_subjectconfirmation_inresponse.settings = settings
refute response_invalid_subjectconfirmation_inresponse.send(:validate_subject_confirmation)
assert_includes response_invalid_subjectconfirmation_inresponse.errors, 'A valid SubjectConfirmation was not found on this Response'
end
it 'return false when invalid NotBefore' do
response_invalid_subjectconfirmation_nb.settings = settings
refute response_invalid_subjectconfirmation_nb.send(:validate_subject_confirmation)
assert_includes response_invalid_subjectconfirmation_nb.errors, 'A valid SubjectConfirmation was not found on this Response'
end
it 'return false when invalid NotOnOrAfter' do
response_invalid_subjectconfirmation_noa.settings = settings
refute response_invalid_subjectconfirmation_noa.send(:validate_subject_confirmation)
assert_includes response_invalid_subjectconfirmation_noa.errors, 'A valid SubjectConfirmation was not found on this Response'
end
it 'return true when valid subject confirmation recipient' do
response_valid_signed.settings = settings
response_valid_signed.settings.assertion_consumer_service_url = 'recipient'
assert response_valid_signed.send(:validate_subject_confirmation)
assert_empty response_valid_signed.errors
assert_empty response_valid_signed.errors
end
it 'return false when invalid subject confirmation recipient' do
response_valid_signed.settings = settings
response_valid_signed.settings.assertion_consumer_service_url = 'not-the-recipient'
refute response_valid_signed.send(:validate_subject_confirmation)
assert_includes response_valid_signed.errors, 'A valid SubjectConfirmation was not found on this Response'
end
it 'return false when invalid subject confirmation recipient, but skipping the check(default)' do
response_valid_signed_without_recipient.settings = settings
response_valid_signed_without_recipient.settings.assertion_consumer_service_url = 'not-the-recipient'
assert response_valid_signed_without_recipient.send(:validate_subject_confirmation)
assert_empty response_valid_signed_without_recipient.errors
end
it 'return true when the skip_subject_confirmation option is passed and the subject confirmation is valid' do
opts = {}
opts[:skip_subject_confirmation] = true
response_with_skip = RubySaml::Response.new(response_document_valid_signed, opts)
response_with_skip.settings = settings
response_with_skip.settings.assertion_consumer_service_url = 'recipient'
Time.expects(:now).times(0) # ensures the test isn't run and thus Time.now.utc is never called within the test
assert response_with_skip.send(:validate_subject_confirmation)
assert_empty response_with_skip.errors
end
it 'return true when the skip_subject_confirmation option is passed and the response has an invalid subject confirmation' do
opts = {}
opts[:skip_subject_confirmation] = true
response_with_skip = RubySaml::Response.new(read_invalid_response('invalid_subjectconfirmation_noa.xml.base64'), opts)
response_with_skip.settings = settings
Time.expects(:now).times(0) # ensures the test isn't run and thus Time.now.utc is never called within the test
assert response_with_skip.send(:validate_subject_confirmation)
assert_empty response_with_skip.errors
end
end
describe '#validate_session_expiration' do
it 'return true when the session has not expired' do
response_valid_signed.settings = settings
assert response_valid_signed.send(:validate_session_expiration)
assert_empty response_valid_signed.errors
end
it 'return false when the session has expired' do
response.settings = settings
refute response.send(:validate_session_expiration)
assert_includes response.errors, 'The attributes have expired, based on the SessionNotOnOrAfter of the AuthnStatement of this Response'
end
it 'returns true when the session has expired, but is still within the allowed_clock_drift' do
drift = (Time.now - Time.parse('2010-11-19T21:57:37Z')) * 60 # seconds ago that this assertion expired
drift += 10 # add a buffer of 10 seconds to make sure the test passes
opts = {}
opts[:allowed_clock_drift] = drift
response_with_drift = RubySaml::Response.new(response_document_without_recipient, opts)
response_with_drift.settings = settings
assert response_with_drift.send(:validate_session_expiration)
assert_empty response_with_drift.errors
end
end
describe '#validate_signature' do
it 'return true when the signature is valid' do
settings.idp_cert_fingerprint = ruby_saml_cert_fingerprint
response_valid_signed.settings = settings
assert response_valid_signed.send(:validate_signature)
assert_empty response_valid_signed.errors
end
it 'return true when the signature is valid and ds namespace is at the root' do
settings.idp_cert_fingerprint = '19a4fff2e8fcc7f3ea5046348dbf1d81320654d1f712028cc97933cb1247fc99'
response_with_ds_namespace_at_the_root.settings = settings
assert response_with_ds_namespace_at_the_root.send(:validate_signature)
assert_empty response_with_ds_namespace_at_the_root.errors
end
it 'return true when the signature is valid and fingerprint provided' do