Expect AuthenticationException for invalid issuer instead of IllegalStateException
Describe the bug
Using oauth2 resource server with JWT authentication, if the client send a token with an issuer different from the one expected, the exception thrown is IllegalStateException : https://github.com/spring-projects/spring-security/blob/5fe6d9259fbee532d402a801527b7aed4d937e98/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtDecoderProviderConfigurationUtils.java#L99
The exception is never wrapped to AuthenticationException.
To Reproduce Configure oauth2 resource server and exception handling :
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(matcher -> matcher.anyRequest().authenticated())
.oauth2ResourceServer((oauth2) -> oauth2.jwt(Customizer.withDefaults()))
.exceptionHandling(exceptionHandling -> exceptionHandling
.authenticationEntryPoint(authenticationEntryPoint())
.accessDeniedHandler(accessDeniedHandler()))
.build();
}
@Bean
public AuthenticationEntryPoint authenticationEntryPoint() {
return (HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) -> {
LOGGER.error("Authentication failed", authException);
};
}
@Bean
public AccessDeniedHandler accessDeniedHandler() {
return (HttpServletRequest request, HttpServletResponse response,
org.springframework.security.access.AccessDeniedException accessDeniedException) -> {
LOGGER.error("Access denied", accessDeniedException);
};
}
Configure JWT expected issuer uri :
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://issuer-uri
Send a request with an other issuer-uri : the exception is not logged
Expected behavior
The exception is an AuthenticationException so we can catch it through httpSecurity.exceptionHandling() or Authentication events.
Hi @gulecroc !, I'd like to work on this issue if it's available.
My proposed fix is to catch the IllegalStateException from validateIssuer() and wrap it into InvalidBearerTokenException (which extends AuthenticationException), so that ExceptionTranslationFilter can properly delegate it to the configured AuthenticationEntryPoint.
This needs to be fixed in both the Servlet path (JwtDecoders.withProviderConfiguration()) and the Reactive path (NimbusReactiveJwtDecoder), along with corresponding tests.
Please let me know if this approach looks good!
I’d like to work on an issue. Could you please assign one to me?
Hi @chanani @KartikeyTripathi469, I didn't start anything on this.
Hi @gulecroc !, I'd like to work on this issue if it's available.
My proposed fix is to catch the
IllegalStateExceptionfromvalidateIssuer()and wrap it intoInvalidBearerTokenException(which extendsAuthenticationException), so thatExceptionTranslationFiltercan properly delegate it to the configuredAuthenticationEntryPoint.This needs to be fixed in both the Servlet path (
JwtDecoders.withProviderConfiguration()) and the Reactive path (NimbusReactiveJwtDecoder), along with corresponding tests.Please let me know if this approach looks good!
I think there is multiple IllegalStateException during the validation process, so maybe we need a more global behavior
@gulecroc, thank you for posting this issue. Can you elaborate on how the IllegalStateException is getting exposed? Perhaps you can post the stack trace also.
Since validateIssuer is called at configuration time, I would expect you to see either an IllegalStateException at startup (not at request time) or, if your application is using SupplierJwtDecoder, then at request time I'd expect a JwtDecoderInitializationException (wrapping an IllegalStateException).
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.