authentik.sources.saml.tests.test_response_binding
SAML Source response binding tests
1"""SAML Source response binding tests""" 2 3from base64 import b64encode 4 5from django.core.exceptions import SuspiciousOperation 6from django.test import TestCase 7from freezegun import freeze_time 8 9from authentik.core.tests.utils import RequestFactory, create_test_flow 10from authentik.lib.generators import generate_id 11from authentik.lib.tests.utils import load_fixture 12from authentik.sources.saml.exceptions import ( 13 MismatchedBinding, 14 MismatchedRequestID, 15 SAMLException, 16) 17from authentik.sources.saml.models import SAMLSource 18from authentik.sources.saml.processors.request import SESSION_KEY_REQUEST_ID 19from authentik.sources.saml.processors.response import ResponseProcessor 20 21# This source. 22SOURCE_SLUG = "another-idp" 23ENTITY_ID = "https://authentik.company/source/saml/another-idp/metadata/" 24ACS_URL = "http://testserver/source/saml/another-idp/acs/" 25# The ID of the AuthNRequest that this source sent to the IdP. 26REQUEST_ID = "_our_request_id" 27# The end of the validity of the assertion. The tests run at 14:15:00. 28VALID_UNTIL = "2022-10-14T14:16:49Z" 29# A second service provider that uses the same IdP as this source. 30OTHER_ACS_URL = "https://other-app.example/acs" 31OTHER_REQUEST_ID = "_other_app_request_id" 32 33 34@freeze_time("2022-10-14T14:15:00Z") 35class TestResponseBinding(TestCase): 36 """Test that a Response must be addressed to this source""" 37 38 def setUp(self): 39 self.factory = RequestFactory() 40 self.source = SAMLSource.objects.create( 41 name=generate_id(), 42 slug=SOURCE_SLUG, 43 issuer_override=ENTITY_ID, 44 pre_authentication_flow=create_test_flow(), 45 ) 46 47 def test_addressed_to_this_source(self): 48 """A response that names this source is accepted""" 49 response = load_fixture( 50 "fixtures/response_binding.xml", 51 destination=ACS_URL, 52 response_in_response_to=REQUEST_ID, 53 recipient=ACS_URL, 54 assertion_in_response_to=REQUEST_ID, 55 subject_not_on_or_after=VALID_UNTIL, 56 ) 57 request = self.factory.post( 58 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 59 ) 60 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 61 request.session.save() 62 63 ResponseProcessor(self.source, request).parse() 64 65 def test_minimal_response(self): 66 """A response that carries none of the optional values is accepted""" 67 response = load_fixture( 68 "fixtures/response_binding_minimal.xml", 69 response_in_response_to=REQUEST_ID, 70 ) 71 request = self.factory.post( 72 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 73 ) 74 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 75 request.session.save() 76 77 ResponseProcessor(self.source, request).parse() 78 79 def test_no_confirmation(self): 80 """An assertion that carries no confirmation of the subject is refused""" 81 response = load_fixture( 82 "fixtures/response_binding_no_confirmation.xml", 83 destination=ACS_URL, 84 response_in_response_to=REQUEST_ID, 85 ) 86 request = self.factory.post( 87 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 88 ) 89 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 90 request.session.save() 91 92 with self.assertRaises(MismatchedBinding): 93 ResponseProcessor(self.source, request).parse() 94 95 def test_confirmation_without_data(self): 96 """A confirmation of the subject that carries no data is refused""" 97 response = load_fixture( 98 "fixtures/response_binding_confirmation_without_data.xml", 99 destination=ACS_URL, 100 response_in_response_to=REQUEST_ID, 101 ) 102 request = self.factory.post( 103 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 104 ) 105 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 106 request.session.save() 107 108 with self.assertRaises(MismatchedBinding): 109 ResponseProcessor(self.source, request).parse() 110 111 def test_recipient_other_sp(self): 112 """The Recipient must name the ACS URL of this source""" 113 response = load_fixture( 114 "fixtures/response_binding.xml", 115 destination=ACS_URL, 116 response_in_response_to=REQUEST_ID, 117 recipient=OTHER_ACS_URL, 118 assertion_in_response_to=REQUEST_ID, 119 subject_not_on_or_after=VALID_UNTIL, 120 ) 121 request = self.factory.post( 122 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 123 ) 124 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 125 request.session.save() 126 127 with self.assertRaises(MismatchedBinding): 128 ResponseProcessor(self.source, request).parse() 129 130 def test_recipient_other_case(self): 131 """Only the case of the Recipient can differ from the ACS URL""" 132 response = load_fixture( 133 "fixtures/response_binding.xml", 134 destination=ACS_URL, 135 response_in_response_to=REQUEST_ID, 136 recipient=ACS_URL.upper(), 137 assertion_in_response_to=REQUEST_ID, 138 subject_not_on_or_after=VALID_UNTIL, 139 ) 140 request = self.factory.post( 141 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 142 ) 143 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 144 request.session.save() 145 146 ResponseProcessor(self.source, request).parse() 147 148 def test_other_confirmation_method_first(self): 149 """An assertion can carry more than one confirmation of the subject. Satisfying the 150 one that names this source is enough.""" 151 response = load_fixture( 152 "fixtures/response_binding_two_confirmations.xml", 153 destination=ACS_URL, 154 response_in_response_to=REQUEST_ID, 155 other_recipient=OTHER_ACS_URL, 156 other_in_response_to=OTHER_REQUEST_ID, 157 recipient=ACS_URL, 158 assertion_in_response_to=REQUEST_ID, 159 subject_not_on_or_after=VALID_UNTIL, 160 ) 161 request = self.factory.post( 162 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 163 ) 164 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 165 request.session.save() 166 167 ResponseProcessor(self.source, request).parse() 168 169 def test_confirmation_other_method(self): 170 """A confirmation that names another confirmation method is not read, even when it 171 names this source, so an assertion that carries only that one is refused""" 172 response = load_fixture( 173 "fixtures/response_binding_other_method.xml", 174 destination=ACS_URL, 175 response_in_response_to=REQUEST_ID, 176 recipient=ACS_URL, 177 assertion_in_response_to=REQUEST_ID, 178 subject_not_on_or_after=VALID_UNTIL, 179 ) 180 request = self.factory.post( 181 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 182 ) 183 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 184 request.session.save() 185 186 with self.assertRaises(MismatchedBinding): 187 ResponseProcessor(self.source, request).parse() 188 189 def test_other_method_confirmation_constrains_nothing(self): 190 """A confirmation that names another confirmation method does not confirm the 191 subject for this source, not even when it constrains nothing""" 192 response = load_fixture( 193 "fixtures/response_binding_other_method_and_bearer.xml", 194 destination=ACS_URL, 195 response_in_response_to=REQUEST_ID, 196 recipient=OTHER_ACS_URL, 197 assertion_in_response_to=OTHER_REQUEST_ID, 198 subject_not_on_or_after=VALID_UNTIL, 199 ) 200 request = self.factory.post( 201 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 202 ) 203 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 204 request.session.save() 205 206 with self.assertRaises(MismatchedBinding): 207 ResponseProcessor(self.source, request).parse() 208 209 def test_confirmation_without_method(self): 210 """A confirmation of the subject that names no method is read""" 211 response = load_fixture( 212 "fixtures/response_binding_no_method.xml", 213 destination=ACS_URL, 214 response_in_response_to=REQUEST_ID, 215 recipient=ACS_URL, 216 assertion_in_response_to=REQUEST_ID, 217 subject_not_on_or_after=VALID_UNTIL, 218 ) 219 request = self.factory.post( 220 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 221 ) 222 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 223 request.session.save() 224 225 ResponseProcessor(self.source, request).parse() 226 227 def test_no_confirmation_names_this_source(self): 228 """One confirmation of the subject must name this source""" 229 response = load_fixture( 230 "fixtures/response_binding_two_confirmations.xml", 231 destination=ACS_URL, 232 response_in_response_to=REQUEST_ID, 233 other_recipient=OTHER_ACS_URL, 234 other_in_response_to=OTHER_REQUEST_ID, 235 recipient=OTHER_ACS_URL, 236 assertion_in_response_to=REQUEST_ID, 237 subject_not_on_or_after=VALID_UNTIL, 238 ) 239 request = self.factory.post( 240 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 241 ) 242 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 243 request.session.save() 244 245 with self.assertRaises(MismatchedBinding): 246 ResponseProcessor(self.source, request).parse() 247 248 def test_recipient_other_scheme(self): 249 """A Recipient that names another scheme does not match. A deployment whose reverse 250 proxy does not send X-Forwarded-Proto builds the ACS URL with the wrong scheme.""" 251 response = load_fixture( 252 "fixtures/response_binding.xml", 253 destination=ACS_URL, 254 response_in_response_to=REQUEST_ID, 255 recipient="https://testserver/source/saml/another-idp/acs/", 256 assertion_in_response_to=REQUEST_ID, 257 subject_not_on_or_after=VALID_UNTIL, 258 ) 259 request = self.factory.post( 260 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 261 ) 262 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 263 request.session.save() 264 265 with self.assertRaises(MismatchedBinding): 266 ResponseProcessor(self.source, request).parse() 267 268 def test_recipient_other_port(self): 269 """A Recipient that names another port does not match""" 270 response = load_fixture( 271 "fixtures/response_binding.xml", 272 destination=ACS_URL, 273 response_in_response_to=REQUEST_ID, 274 recipient="http://testserver:80/source/saml/another-idp/acs/", 275 assertion_in_response_to=REQUEST_ID, 276 subject_not_on_or_after=VALID_UNTIL, 277 ) 278 request = self.factory.post( 279 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 280 ) 281 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 282 request.session.save() 283 284 with self.assertRaises(MismatchedBinding): 285 ResponseProcessor(self.source, request).parse() 286 287 def test_destination_other_sp(self): 288 """The Destination must name the ACS URL of this source""" 289 response = load_fixture( 290 "fixtures/response_binding.xml", 291 destination=OTHER_ACS_URL, 292 response_in_response_to=REQUEST_ID, 293 recipient=ACS_URL, 294 assertion_in_response_to=REQUEST_ID, 295 subject_not_on_or_after=VALID_UNTIL, 296 ) 297 request = self.factory.post( 298 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 299 ) 300 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 301 request.session.save() 302 303 with self.assertRaises(MismatchedBinding): 304 ResponseProcessor(self.source, request).parse() 305 306 def test_in_response_to_of_assertion(self): 307 """The InResponseTo of the assertion must be the ID of our own request""" 308 response = load_fixture( 309 "fixtures/response_binding.xml", 310 destination=ACS_URL, 311 response_in_response_to=REQUEST_ID, 312 recipient=ACS_URL, 313 assertion_in_response_to=OTHER_REQUEST_ID, 314 subject_not_on_or_after=VALID_UNTIL, 315 ) 316 request = self.factory.post( 317 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 318 ) 319 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 320 request.session.save() 321 322 with self.assertRaises(MismatchedRequestID): 323 ResponseProcessor(self.source, request).parse() 324 325 def test_in_response_to_of_response(self): 326 """The InResponseTo of the response must be the ID of our own request""" 327 response = load_fixture( 328 "fixtures/response_binding.xml", 329 destination=ACS_URL, 330 response_in_response_to=OTHER_REQUEST_ID, 331 recipient=ACS_URL, 332 assertion_in_response_to=REQUEST_ID, 333 subject_not_on_or_after=VALID_UNTIL, 334 ) 335 request = self.factory.post( 336 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 337 ) 338 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 339 request.session.save() 340 341 with self.assertRaises(MismatchedRequestID): 342 ResponseProcessor(self.source, request).parse() 343 344 def test_subject_confirmation_expired(self): 345 """The NotOnOrAfter of the SubjectConfirmationData is checked""" 346 response = load_fixture( 347 "fixtures/response_binding.xml", 348 destination=ACS_URL, 349 response_in_response_to=REQUEST_ID, 350 recipient=ACS_URL, 351 assertion_in_response_to=REQUEST_ID, 352 subject_not_on_or_after="2022-10-14T14:14:00Z", 353 ) 354 request = self.factory.post( 355 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 356 ) 357 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 358 request.session.save() 359 360 with self.assertRaises(SAMLException): 361 ResponseProcessor(self.source, request).parse() 362 363 def test_assertion_used_two_times(self): 364 """One assertion is accepted one time only""" 365 response = load_fixture( 366 "fixtures/response_binding.xml", 367 destination=ACS_URL, 368 response_in_response_to=REQUEST_ID, 369 recipient=ACS_URL, 370 assertion_in_response_to=REQUEST_ID, 371 subject_not_on_or_after=VALID_UNTIL, 372 ) 373 first_request = self.factory.post( 374 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 375 ) 376 first_request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 377 first_request.session.save() 378 second_request = self.factory.post( 379 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 380 ) 381 second_request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 382 second_request.session.save() 383 384 ResponseProcessor(self.source, first_request).parse() 385 with self.assertRaises(SuspiciousOperation): 386 ResponseProcessor(self.source, second_request).parse() 387 388 def test_assertion_used_two_times_idp_initiated(self): 389 """A new ID on the response does not make a used assertion valid again""" 390 self.source.allow_idp_initiated = True 391 self.source.save() 392 first_response = load_fixture( 393 "fixtures/response_binding.xml", 394 destination=ACS_URL, 395 response_in_response_to=REQUEST_ID, 396 recipient=ACS_URL, 397 assertion_in_response_to=REQUEST_ID, 398 subject_not_on_or_after=VALID_UNTIL, 399 ) 400 second_response = load_fixture( 401 "fixtures/response_binding_other_response_id.xml", 402 destination=ACS_URL, 403 response_in_response_to=REQUEST_ID, 404 recipient=ACS_URL, 405 assertion_in_response_to=REQUEST_ID, 406 subject_not_on_or_after=VALID_UNTIL, 407 ) 408 first_request = self.factory.post( 409 "/", data={"SAMLResponse": b64encode(first_response.encode()).decode()} 410 ) 411 second_request = self.factory.post( 412 "/", data={"SAMLResponse": b64encode(second_response.encode()).decode()} 413 ) 414 415 ResponseProcessor(self.source, first_request).parse() 416 with self.assertRaises(SuspiciousOperation): 417 ResponseProcessor(self.source, second_request).parse()
35@freeze_time("2022-10-14T14:15:00Z") 36class TestResponseBinding(TestCase): 37 """Test that a Response must be addressed to this source""" 38 39 def setUp(self): 40 self.factory = RequestFactory() 41 self.source = SAMLSource.objects.create( 42 name=generate_id(), 43 slug=SOURCE_SLUG, 44 issuer_override=ENTITY_ID, 45 pre_authentication_flow=create_test_flow(), 46 ) 47 48 def test_addressed_to_this_source(self): 49 """A response that names this source is accepted""" 50 response = load_fixture( 51 "fixtures/response_binding.xml", 52 destination=ACS_URL, 53 response_in_response_to=REQUEST_ID, 54 recipient=ACS_URL, 55 assertion_in_response_to=REQUEST_ID, 56 subject_not_on_or_after=VALID_UNTIL, 57 ) 58 request = self.factory.post( 59 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 60 ) 61 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 62 request.session.save() 63 64 ResponseProcessor(self.source, request).parse() 65 66 def test_minimal_response(self): 67 """A response that carries none of the optional values is accepted""" 68 response = load_fixture( 69 "fixtures/response_binding_minimal.xml", 70 response_in_response_to=REQUEST_ID, 71 ) 72 request = self.factory.post( 73 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 74 ) 75 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 76 request.session.save() 77 78 ResponseProcessor(self.source, request).parse() 79 80 def test_no_confirmation(self): 81 """An assertion that carries no confirmation of the subject is refused""" 82 response = load_fixture( 83 "fixtures/response_binding_no_confirmation.xml", 84 destination=ACS_URL, 85 response_in_response_to=REQUEST_ID, 86 ) 87 request = self.factory.post( 88 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 89 ) 90 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 91 request.session.save() 92 93 with self.assertRaises(MismatchedBinding): 94 ResponseProcessor(self.source, request).parse() 95 96 def test_confirmation_without_data(self): 97 """A confirmation of the subject that carries no data is refused""" 98 response = load_fixture( 99 "fixtures/response_binding_confirmation_without_data.xml", 100 destination=ACS_URL, 101 response_in_response_to=REQUEST_ID, 102 ) 103 request = self.factory.post( 104 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 105 ) 106 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 107 request.session.save() 108 109 with self.assertRaises(MismatchedBinding): 110 ResponseProcessor(self.source, request).parse() 111 112 def test_recipient_other_sp(self): 113 """The Recipient must name the ACS URL of this source""" 114 response = load_fixture( 115 "fixtures/response_binding.xml", 116 destination=ACS_URL, 117 response_in_response_to=REQUEST_ID, 118 recipient=OTHER_ACS_URL, 119 assertion_in_response_to=REQUEST_ID, 120 subject_not_on_or_after=VALID_UNTIL, 121 ) 122 request = self.factory.post( 123 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 124 ) 125 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 126 request.session.save() 127 128 with self.assertRaises(MismatchedBinding): 129 ResponseProcessor(self.source, request).parse() 130 131 def test_recipient_other_case(self): 132 """Only the case of the Recipient can differ from the ACS URL""" 133 response = load_fixture( 134 "fixtures/response_binding.xml", 135 destination=ACS_URL, 136 response_in_response_to=REQUEST_ID, 137 recipient=ACS_URL.upper(), 138 assertion_in_response_to=REQUEST_ID, 139 subject_not_on_or_after=VALID_UNTIL, 140 ) 141 request = self.factory.post( 142 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 143 ) 144 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 145 request.session.save() 146 147 ResponseProcessor(self.source, request).parse() 148 149 def test_other_confirmation_method_first(self): 150 """An assertion can carry more than one confirmation of the subject. Satisfying the 151 one that names this source is enough.""" 152 response = load_fixture( 153 "fixtures/response_binding_two_confirmations.xml", 154 destination=ACS_URL, 155 response_in_response_to=REQUEST_ID, 156 other_recipient=OTHER_ACS_URL, 157 other_in_response_to=OTHER_REQUEST_ID, 158 recipient=ACS_URL, 159 assertion_in_response_to=REQUEST_ID, 160 subject_not_on_or_after=VALID_UNTIL, 161 ) 162 request = self.factory.post( 163 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 164 ) 165 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 166 request.session.save() 167 168 ResponseProcessor(self.source, request).parse() 169 170 def test_confirmation_other_method(self): 171 """A confirmation that names another confirmation method is not read, even when it 172 names this source, so an assertion that carries only that one is refused""" 173 response = load_fixture( 174 "fixtures/response_binding_other_method.xml", 175 destination=ACS_URL, 176 response_in_response_to=REQUEST_ID, 177 recipient=ACS_URL, 178 assertion_in_response_to=REQUEST_ID, 179 subject_not_on_or_after=VALID_UNTIL, 180 ) 181 request = self.factory.post( 182 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 183 ) 184 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 185 request.session.save() 186 187 with self.assertRaises(MismatchedBinding): 188 ResponseProcessor(self.source, request).parse() 189 190 def test_other_method_confirmation_constrains_nothing(self): 191 """A confirmation that names another confirmation method does not confirm the 192 subject for this source, not even when it constrains nothing""" 193 response = load_fixture( 194 "fixtures/response_binding_other_method_and_bearer.xml", 195 destination=ACS_URL, 196 response_in_response_to=REQUEST_ID, 197 recipient=OTHER_ACS_URL, 198 assertion_in_response_to=OTHER_REQUEST_ID, 199 subject_not_on_or_after=VALID_UNTIL, 200 ) 201 request = self.factory.post( 202 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 203 ) 204 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 205 request.session.save() 206 207 with self.assertRaises(MismatchedBinding): 208 ResponseProcessor(self.source, request).parse() 209 210 def test_confirmation_without_method(self): 211 """A confirmation of the subject that names no method is read""" 212 response = load_fixture( 213 "fixtures/response_binding_no_method.xml", 214 destination=ACS_URL, 215 response_in_response_to=REQUEST_ID, 216 recipient=ACS_URL, 217 assertion_in_response_to=REQUEST_ID, 218 subject_not_on_or_after=VALID_UNTIL, 219 ) 220 request = self.factory.post( 221 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 222 ) 223 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 224 request.session.save() 225 226 ResponseProcessor(self.source, request).parse() 227 228 def test_no_confirmation_names_this_source(self): 229 """One confirmation of the subject must name this source""" 230 response = load_fixture( 231 "fixtures/response_binding_two_confirmations.xml", 232 destination=ACS_URL, 233 response_in_response_to=REQUEST_ID, 234 other_recipient=OTHER_ACS_URL, 235 other_in_response_to=OTHER_REQUEST_ID, 236 recipient=OTHER_ACS_URL, 237 assertion_in_response_to=REQUEST_ID, 238 subject_not_on_or_after=VALID_UNTIL, 239 ) 240 request = self.factory.post( 241 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 242 ) 243 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 244 request.session.save() 245 246 with self.assertRaises(MismatchedBinding): 247 ResponseProcessor(self.source, request).parse() 248 249 def test_recipient_other_scheme(self): 250 """A Recipient that names another scheme does not match. A deployment whose reverse 251 proxy does not send X-Forwarded-Proto builds the ACS URL with the wrong scheme.""" 252 response = load_fixture( 253 "fixtures/response_binding.xml", 254 destination=ACS_URL, 255 response_in_response_to=REQUEST_ID, 256 recipient="https://testserver/source/saml/another-idp/acs/", 257 assertion_in_response_to=REQUEST_ID, 258 subject_not_on_or_after=VALID_UNTIL, 259 ) 260 request = self.factory.post( 261 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 262 ) 263 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 264 request.session.save() 265 266 with self.assertRaises(MismatchedBinding): 267 ResponseProcessor(self.source, request).parse() 268 269 def test_recipient_other_port(self): 270 """A Recipient that names another port does not match""" 271 response = load_fixture( 272 "fixtures/response_binding.xml", 273 destination=ACS_URL, 274 response_in_response_to=REQUEST_ID, 275 recipient="http://testserver:80/source/saml/another-idp/acs/", 276 assertion_in_response_to=REQUEST_ID, 277 subject_not_on_or_after=VALID_UNTIL, 278 ) 279 request = self.factory.post( 280 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 281 ) 282 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 283 request.session.save() 284 285 with self.assertRaises(MismatchedBinding): 286 ResponseProcessor(self.source, request).parse() 287 288 def test_destination_other_sp(self): 289 """The Destination must name the ACS URL of this source""" 290 response = load_fixture( 291 "fixtures/response_binding.xml", 292 destination=OTHER_ACS_URL, 293 response_in_response_to=REQUEST_ID, 294 recipient=ACS_URL, 295 assertion_in_response_to=REQUEST_ID, 296 subject_not_on_or_after=VALID_UNTIL, 297 ) 298 request = self.factory.post( 299 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 300 ) 301 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 302 request.session.save() 303 304 with self.assertRaises(MismatchedBinding): 305 ResponseProcessor(self.source, request).parse() 306 307 def test_in_response_to_of_assertion(self): 308 """The InResponseTo of the assertion must be the ID of our own request""" 309 response = load_fixture( 310 "fixtures/response_binding.xml", 311 destination=ACS_URL, 312 response_in_response_to=REQUEST_ID, 313 recipient=ACS_URL, 314 assertion_in_response_to=OTHER_REQUEST_ID, 315 subject_not_on_or_after=VALID_UNTIL, 316 ) 317 request = self.factory.post( 318 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 319 ) 320 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 321 request.session.save() 322 323 with self.assertRaises(MismatchedRequestID): 324 ResponseProcessor(self.source, request).parse() 325 326 def test_in_response_to_of_response(self): 327 """The InResponseTo of the response must be the ID of our own request""" 328 response = load_fixture( 329 "fixtures/response_binding.xml", 330 destination=ACS_URL, 331 response_in_response_to=OTHER_REQUEST_ID, 332 recipient=ACS_URL, 333 assertion_in_response_to=REQUEST_ID, 334 subject_not_on_or_after=VALID_UNTIL, 335 ) 336 request = self.factory.post( 337 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 338 ) 339 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 340 request.session.save() 341 342 with self.assertRaises(MismatchedRequestID): 343 ResponseProcessor(self.source, request).parse() 344 345 def test_subject_confirmation_expired(self): 346 """The NotOnOrAfter of the SubjectConfirmationData is checked""" 347 response = load_fixture( 348 "fixtures/response_binding.xml", 349 destination=ACS_URL, 350 response_in_response_to=REQUEST_ID, 351 recipient=ACS_URL, 352 assertion_in_response_to=REQUEST_ID, 353 subject_not_on_or_after="2022-10-14T14:14:00Z", 354 ) 355 request = self.factory.post( 356 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 357 ) 358 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 359 request.session.save() 360 361 with self.assertRaises(SAMLException): 362 ResponseProcessor(self.source, request).parse() 363 364 def test_assertion_used_two_times(self): 365 """One assertion is accepted one time only""" 366 response = load_fixture( 367 "fixtures/response_binding.xml", 368 destination=ACS_URL, 369 response_in_response_to=REQUEST_ID, 370 recipient=ACS_URL, 371 assertion_in_response_to=REQUEST_ID, 372 subject_not_on_or_after=VALID_UNTIL, 373 ) 374 first_request = self.factory.post( 375 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 376 ) 377 first_request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 378 first_request.session.save() 379 second_request = self.factory.post( 380 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 381 ) 382 second_request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 383 second_request.session.save() 384 385 ResponseProcessor(self.source, first_request).parse() 386 with self.assertRaises(SuspiciousOperation): 387 ResponseProcessor(self.source, second_request).parse() 388 389 def test_assertion_used_two_times_idp_initiated(self): 390 """A new ID on the response does not make a used assertion valid again""" 391 self.source.allow_idp_initiated = True 392 self.source.save() 393 first_response = load_fixture( 394 "fixtures/response_binding.xml", 395 destination=ACS_URL, 396 response_in_response_to=REQUEST_ID, 397 recipient=ACS_URL, 398 assertion_in_response_to=REQUEST_ID, 399 subject_not_on_or_after=VALID_UNTIL, 400 ) 401 second_response = load_fixture( 402 "fixtures/response_binding_other_response_id.xml", 403 destination=ACS_URL, 404 response_in_response_to=REQUEST_ID, 405 recipient=ACS_URL, 406 assertion_in_response_to=REQUEST_ID, 407 subject_not_on_or_after=VALID_UNTIL, 408 ) 409 first_request = self.factory.post( 410 "/", data={"SAMLResponse": b64encode(first_response.encode()).decode()} 411 ) 412 second_request = self.factory.post( 413 "/", data={"SAMLResponse": b64encode(second_response.encode()).decode()} 414 ) 415 416 ResponseProcessor(self.source, first_request).parse() 417 with self.assertRaises(SuspiciousOperation): 418 ResponseProcessor(self.source, second_request).parse()
Test that a Response must be addressed to this source
692 def setUp(*args: Any, **kwargs: Any) -> None: 693 self.start() 694 if orig_setUp is not None: 695 orig_setUp(*args, **kwargs)
The type of the None singleton.
48 def test_addressed_to_this_source(self): 49 """A response that names this source is accepted""" 50 response = load_fixture( 51 "fixtures/response_binding.xml", 52 destination=ACS_URL, 53 response_in_response_to=REQUEST_ID, 54 recipient=ACS_URL, 55 assertion_in_response_to=REQUEST_ID, 56 subject_not_on_or_after=VALID_UNTIL, 57 ) 58 request = self.factory.post( 59 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 60 ) 61 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 62 request.session.save() 63 64 ResponseProcessor(self.source, request).parse()
A response that names this source is accepted
66 def test_minimal_response(self): 67 """A response that carries none of the optional values is accepted""" 68 response = load_fixture( 69 "fixtures/response_binding_minimal.xml", 70 response_in_response_to=REQUEST_ID, 71 ) 72 request = self.factory.post( 73 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 74 ) 75 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 76 request.session.save() 77 78 ResponseProcessor(self.source, request).parse()
A response that carries none of the optional values is accepted
80 def test_no_confirmation(self): 81 """An assertion that carries no confirmation of the subject is refused""" 82 response = load_fixture( 83 "fixtures/response_binding_no_confirmation.xml", 84 destination=ACS_URL, 85 response_in_response_to=REQUEST_ID, 86 ) 87 request = self.factory.post( 88 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 89 ) 90 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 91 request.session.save() 92 93 with self.assertRaises(MismatchedBinding): 94 ResponseProcessor(self.source, request).parse()
An assertion that carries no confirmation of the subject is refused
96 def test_confirmation_without_data(self): 97 """A confirmation of the subject that carries no data is refused""" 98 response = load_fixture( 99 "fixtures/response_binding_confirmation_without_data.xml", 100 destination=ACS_URL, 101 response_in_response_to=REQUEST_ID, 102 ) 103 request = self.factory.post( 104 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 105 ) 106 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 107 request.session.save() 108 109 with self.assertRaises(MismatchedBinding): 110 ResponseProcessor(self.source, request).parse()
A confirmation of the subject that carries no data is refused
112 def test_recipient_other_sp(self): 113 """The Recipient must name the ACS URL of this source""" 114 response = load_fixture( 115 "fixtures/response_binding.xml", 116 destination=ACS_URL, 117 response_in_response_to=REQUEST_ID, 118 recipient=OTHER_ACS_URL, 119 assertion_in_response_to=REQUEST_ID, 120 subject_not_on_or_after=VALID_UNTIL, 121 ) 122 request = self.factory.post( 123 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 124 ) 125 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 126 request.session.save() 127 128 with self.assertRaises(MismatchedBinding): 129 ResponseProcessor(self.source, request).parse()
The Recipient must name the ACS URL of this source
131 def test_recipient_other_case(self): 132 """Only the case of the Recipient can differ from the ACS URL""" 133 response = load_fixture( 134 "fixtures/response_binding.xml", 135 destination=ACS_URL, 136 response_in_response_to=REQUEST_ID, 137 recipient=ACS_URL.upper(), 138 assertion_in_response_to=REQUEST_ID, 139 subject_not_on_or_after=VALID_UNTIL, 140 ) 141 request = self.factory.post( 142 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 143 ) 144 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 145 request.session.save() 146 147 ResponseProcessor(self.source, request).parse()
Only the case of the Recipient can differ from the ACS URL
149 def test_other_confirmation_method_first(self): 150 """An assertion can carry more than one confirmation of the subject. Satisfying the 151 one that names this source is enough.""" 152 response = load_fixture( 153 "fixtures/response_binding_two_confirmations.xml", 154 destination=ACS_URL, 155 response_in_response_to=REQUEST_ID, 156 other_recipient=OTHER_ACS_URL, 157 other_in_response_to=OTHER_REQUEST_ID, 158 recipient=ACS_URL, 159 assertion_in_response_to=REQUEST_ID, 160 subject_not_on_or_after=VALID_UNTIL, 161 ) 162 request = self.factory.post( 163 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 164 ) 165 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 166 request.session.save() 167 168 ResponseProcessor(self.source, request).parse()
An assertion can carry more than one confirmation of the subject. Satisfying the one that names this source is enough.
170 def test_confirmation_other_method(self): 171 """A confirmation that names another confirmation method is not read, even when it 172 names this source, so an assertion that carries only that one is refused""" 173 response = load_fixture( 174 "fixtures/response_binding_other_method.xml", 175 destination=ACS_URL, 176 response_in_response_to=REQUEST_ID, 177 recipient=ACS_URL, 178 assertion_in_response_to=REQUEST_ID, 179 subject_not_on_or_after=VALID_UNTIL, 180 ) 181 request = self.factory.post( 182 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 183 ) 184 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 185 request.session.save() 186 187 with self.assertRaises(MismatchedBinding): 188 ResponseProcessor(self.source, request).parse()
A confirmation that names another confirmation method is not read, even when it names this source, so an assertion that carries only that one is refused
190 def test_other_method_confirmation_constrains_nothing(self): 191 """A confirmation that names another confirmation method does not confirm the 192 subject for this source, not even when it constrains nothing""" 193 response = load_fixture( 194 "fixtures/response_binding_other_method_and_bearer.xml", 195 destination=ACS_URL, 196 response_in_response_to=REQUEST_ID, 197 recipient=OTHER_ACS_URL, 198 assertion_in_response_to=OTHER_REQUEST_ID, 199 subject_not_on_or_after=VALID_UNTIL, 200 ) 201 request = self.factory.post( 202 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 203 ) 204 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 205 request.session.save() 206 207 with self.assertRaises(MismatchedBinding): 208 ResponseProcessor(self.source, request).parse()
A confirmation that names another confirmation method does not confirm the subject for this source, not even when it constrains nothing
210 def test_confirmation_without_method(self): 211 """A confirmation of the subject that names no method is read""" 212 response = load_fixture( 213 "fixtures/response_binding_no_method.xml", 214 destination=ACS_URL, 215 response_in_response_to=REQUEST_ID, 216 recipient=ACS_URL, 217 assertion_in_response_to=REQUEST_ID, 218 subject_not_on_or_after=VALID_UNTIL, 219 ) 220 request = self.factory.post( 221 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 222 ) 223 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 224 request.session.save() 225 226 ResponseProcessor(self.source, request).parse()
A confirmation of the subject that names no method is read
228 def test_no_confirmation_names_this_source(self): 229 """One confirmation of the subject must name this source""" 230 response = load_fixture( 231 "fixtures/response_binding_two_confirmations.xml", 232 destination=ACS_URL, 233 response_in_response_to=REQUEST_ID, 234 other_recipient=OTHER_ACS_URL, 235 other_in_response_to=OTHER_REQUEST_ID, 236 recipient=OTHER_ACS_URL, 237 assertion_in_response_to=REQUEST_ID, 238 subject_not_on_or_after=VALID_UNTIL, 239 ) 240 request = self.factory.post( 241 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 242 ) 243 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 244 request.session.save() 245 246 with self.assertRaises(MismatchedBinding): 247 ResponseProcessor(self.source, request).parse()
One confirmation of the subject must name this source
249 def test_recipient_other_scheme(self): 250 """A Recipient that names another scheme does not match. A deployment whose reverse 251 proxy does not send X-Forwarded-Proto builds the ACS URL with the wrong scheme.""" 252 response = load_fixture( 253 "fixtures/response_binding.xml", 254 destination=ACS_URL, 255 response_in_response_to=REQUEST_ID, 256 recipient="https://testserver/source/saml/another-idp/acs/", 257 assertion_in_response_to=REQUEST_ID, 258 subject_not_on_or_after=VALID_UNTIL, 259 ) 260 request = self.factory.post( 261 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 262 ) 263 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 264 request.session.save() 265 266 with self.assertRaises(MismatchedBinding): 267 ResponseProcessor(self.source, request).parse()
A Recipient that names another scheme does not match. A deployment whose reverse proxy does not send X-Forwarded-Proto builds the ACS URL with the wrong scheme.
269 def test_recipient_other_port(self): 270 """A Recipient that names another port does not match""" 271 response = load_fixture( 272 "fixtures/response_binding.xml", 273 destination=ACS_URL, 274 response_in_response_to=REQUEST_ID, 275 recipient="http://testserver:80/source/saml/another-idp/acs/", 276 assertion_in_response_to=REQUEST_ID, 277 subject_not_on_or_after=VALID_UNTIL, 278 ) 279 request = self.factory.post( 280 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 281 ) 282 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 283 request.session.save() 284 285 with self.assertRaises(MismatchedBinding): 286 ResponseProcessor(self.source, request).parse()
A Recipient that names another port does not match
288 def test_destination_other_sp(self): 289 """The Destination must name the ACS URL of this source""" 290 response = load_fixture( 291 "fixtures/response_binding.xml", 292 destination=OTHER_ACS_URL, 293 response_in_response_to=REQUEST_ID, 294 recipient=ACS_URL, 295 assertion_in_response_to=REQUEST_ID, 296 subject_not_on_or_after=VALID_UNTIL, 297 ) 298 request = self.factory.post( 299 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 300 ) 301 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 302 request.session.save() 303 304 with self.assertRaises(MismatchedBinding): 305 ResponseProcessor(self.source, request).parse()
The Destination must name the ACS URL of this source
307 def test_in_response_to_of_assertion(self): 308 """The InResponseTo of the assertion must be the ID of our own request""" 309 response = load_fixture( 310 "fixtures/response_binding.xml", 311 destination=ACS_URL, 312 response_in_response_to=REQUEST_ID, 313 recipient=ACS_URL, 314 assertion_in_response_to=OTHER_REQUEST_ID, 315 subject_not_on_or_after=VALID_UNTIL, 316 ) 317 request = self.factory.post( 318 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 319 ) 320 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 321 request.session.save() 322 323 with self.assertRaises(MismatchedRequestID): 324 ResponseProcessor(self.source, request).parse()
The InResponseTo of the assertion must be the ID of our own request
326 def test_in_response_to_of_response(self): 327 """The InResponseTo of the response must be the ID of our own request""" 328 response = load_fixture( 329 "fixtures/response_binding.xml", 330 destination=ACS_URL, 331 response_in_response_to=OTHER_REQUEST_ID, 332 recipient=ACS_URL, 333 assertion_in_response_to=REQUEST_ID, 334 subject_not_on_or_after=VALID_UNTIL, 335 ) 336 request = self.factory.post( 337 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 338 ) 339 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 340 request.session.save() 341 342 with self.assertRaises(MismatchedRequestID): 343 ResponseProcessor(self.source, request).parse()
The InResponseTo of the response must be the ID of our own request
345 def test_subject_confirmation_expired(self): 346 """The NotOnOrAfter of the SubjectConfirmationData is checked""" 347 response = load_fixture( 348 "fixtures/response_binding.xml", 349 destination=ACS_URL, 350 response_in_response_to=REQUEST_ID, 351 recipient=ACS_URL, 352 assertion_in_response_to=REQUEST_ID, 353 subject_not_on_or_after="2022-10-14T14:14:00Z", 354 ) 355 request = self.factory.post( 356 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 357 ) 358 request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 359 request.session.save() 360 361 with self.assertRaises(SAMLException): 362 ResponseProcessor(self.source, request).parse()
The NotOnOrAfter of the SubjectConfirmationData is checked
364 def test_assertion_used_two_times(self): 365 """One assertion is accepted one time only""" 366 response = load_fixture( 367 "fixtures/response_binding.xml", 368 destination=ACS_URL, 369 response_in_response_to=REQUEST_ID, 370 recipient=ACS_URL, 371 assertion_in_response_to=REQUEST_ID, 372 subject_not_on_or_after=VALID_UNTIL, 373 ) 374 first_request = self.factory.post( 375 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 376 ) 377 first_request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 378 first_request.session.save() 379 second_request = self.factory.post( 380 "/", data={"SAMLResponse": b64encode(response.encode()).decode()} 381 ) 382 second_request.session[SESSION_KEY_REQUEST_ID] = REQUEST_ID 383 second_request.session.save() 384 385 ResponseProcessor(self.source, first_request).parse() 386 with self.assertRaises(SuspiciousOperation): 387 ResponseProcessor(self.source, second_request).parse()
One assertion is accepted one time only
389 def test_assertion_used_two_times_idp_initiated(self): 390 """A new ID on the response does not make a used assertion valid again""" 391 self.source.allow_idp_initiated = True 392 self.source.save() 393 first_response = load_fixture( 394 "fixtures/response_binding.xml", 395 destination=ACS_URL, 396 response_in_response_to=REQUEST_ID, 397 recipient=ACS_URL, 398 assertion_in_response_to=REQUEST_ID, 399 subject_not_on_or_after=VALID_UNTIL, 400 ) 401 second_response = load_fixture( 402 "fixtures/response_binding_other_response_id.xml", 403 destination=ACS_URL, 404 response_in_response_to=REQUEST_ID, 405 recipient=ACS_URL, 406 assertion_in_response_to=REQUEST_ID, 407 subject_not_on_or_after=VALID_UNTIL, 408 ) 409 first_request = self.factory.post( 410 "/", data={"SAMLResponse": b64encode(first_response.encode()).decode()} 411 ) 412 second_request = self.factory.post( 413 "/", data={"SAMLResponse": b64encode(second_response.encode()).decode()} 414 ) 415 416 ResponseProcessor(self.source, first_request).parse() 417 with self.assertRaises(SuspiciousOperation): 418 ResponseProcessor(self.source, second_request).parse()
A new ID on the response does not make a used assertion valid again
671 @classmethod # type: ignore 672 def setUpClass(cls: type) -> None: 673 self.start() 674 if orig_setUpClass is not None: 675 orig_setUpClass() 676 self.stop()
The type of the None singleton.