Skip to content

Commit 5678490

Browse files
committed
Add relying party registration not found exception
Fixes: gh-7865
1 parent 3e07b35 commit 5678490

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/authentication/Saml2ErrorCodes.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -93,4 +93,9 @@ public interface Saml2ErrorCodes {
9393
* authentication process.
9494
*/
9595
String INTERNAL_VALIDATION_ERROR = "internal_validation_error";
96+
/**
97+
* The relying party registration was not found.
98+
* The registration ID did not correspond to any relying party registration.
99+
*/
100+
String RELYING_PARTY_REGISTRATION_NOT_FOUND = "relying_party_registration_not_found";
96101
}

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/servlet/filter/Saml2WebSsoAuthenticationFilter.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,9 @@
1919
import org.springframework.http.HttpMethod;
2020
import org.springframework.security.core.Authentication;
2121
import org.springframework.security.core.AuthenticationException;
22+
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
2223
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationToken;
24+
import org.springframework.security.saml2.provider.service.authentication.Saml2Error;
2325
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
2426
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
2527
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
@@ -32,6 +34,7 @@
3234
import javax.servlet.http.HttpServletResponse;
3335

3436
import static java.nio.charset.StandardCharsets.UTF_8;
37+
import static org.springframework.security.saml2.provider.service.authentication.Saml2ErrorCodes.RELYING_PARTY_REGISTRATION_NOT_FOUND;
3538
import static org.springframework.util.StringUtils.hasText;
3639

3740
/**
@@ -86,8 +89,14 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
8689
byte[] b = Saml2Utils.decode(saml2Response);
8790

8891
String responseXml = inflateIfRequired(request, b);
92+
String registrationId = this.matcher.matcher(request).getVariables().get("registrationId");
8993
RelyingPartyRegistration rp =
90-
this.relyingPartyRegistrationRepository.findByRegistrationId(this.matcher.matcher(request).getVariables().get("registrationId"));
94+
this.relyingPartyRegistrationRepository.findByRegistrationId(registrationId);
95+
if (rp == null) {
96+
Saml2Error saml2Error = new Saml2Error(RELYING_PARTY_REGISTRATION_NOT_FOUND,
97+
"Relying Party Registration not found with ID: " + registrationId);
98+
throw new Saml2AuthenticationException(saml2Error);
99+
}
91100
String localSpEntityId = Saml2Utils.getServiceProviderEntityId(rp, request);
92101
final Saml2AuthenticationToken authentication = new Saml2AuthenticationToken(
93102
responseXml,

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/servlet/filter/Saml2WebSsoAuthenticationFilterTests.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,11 +23,15 @@
2323
import org.junit.rules.ExpectedException;
2424
import org.springframework.mock.web.MockHttpServletRequest;
2525
import org.springframework.mock.web.MockHttpServletResponse;
26+
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
2627
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
2728

2829
import javax.servlet.http.HttpServletResponse;
2930

31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
3033
import static org.mockito.Mockito.mock;
34+
import static org.mockito.Mockito.when;
3135

3236
public class Saml2WebSsoAuthenticationFilterTests {
3337

@@ -71,5 +75,21 @@ public void requiresAuthenticationWhenCustomProcessingUrlThenReturnsTrue() {
7175
Assert.assertTrue(filter.requiresAuthentication(request, response));
7276
}
7377

78+
@Test
79+
public void attemptAuthenticationWhenRegistrationIdDoesNotExistThenThrowsException() {
80+
when(repository.findByRegistrationId("non-existent-id")).thenReturn(null);
81+
82+
filter = new Saml2WebSsoAuthenticationFilter(repository, "/some/other/path/{registrationId}");
7483

84+
request.setPathInfo("/some/other/path/non-existent-id");
85+
request.setParameter("SAMLResponse", "response");
86+
87+
try {
88+
filter.attemptAuthentication(request, response);
89+
failBecauseExceptionWasNotThrown(Saml2AuthenticationException.class);
90+
} catch (Exception e) {
91+
assertThat(e).isInstanceOf(Saml2AuthenticationException.class);
92+
assertThat(e.getMessage()).isEqualTo("Relying Party Registration not found with ID: non-existent-id");
93+
}
94+
}
7595
}

0 commit comments

Comments
 (0)