spring-projects / spring-projects/spring-security
OidcBackChannelLogoutWebFilter returns an error for unauthenticated ajax requests
@jzheaux is already working on this.
Since Nov 14, 2024.
- Dominant language
- Java
- Stars
- 9.6k
- Forks
- 6.3k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 52
Description
Describe the bug
Unauthenticated AJAX requests result in a 400 error from the OidcBackChannelLogoutWebFilter, instead of an error from a ErrorWebExceptionHandler.
To Reproduce
- We have a Spring configuration where all our requests, except logout require authentication:
.authorizeExchange(exchanges -> exchanges
.pathMatchers("/logout").permitAll()
.anyExchange().authenticated()
)
-
If you make an unauthenticated request, it triggers an
AccessDeniedException, which then goes toExceptionTranslationWebFilter. When the filter sees that the exchange principal is not authenticated, it calls the registeredServerAuthenticationEntryPointwith a new AuthenticationCredentialsNotFoundException as an argument. -
In our case we use a custom entry point that only handles basic HTML requests. In case of Ajax requests, we just re-throw the exception from the entry point, so that our generic
ErrorWebExceptionHandlercan take care of this and produce the expected response:
public Mono<Void> commence(final ServerWebExchange exchange, final AuthenticationException e) {
return Mono.just(isHtmlRequest(exchange.getRequest()))
.filter(isHtml -> isHtml)
.flatMap(h -> requiresCsrfProtection(exchange))
.filter(needsCsrf -> !needsCsrf.isMatch())
.switchIfEmpty(Mono.error(e))
...
}
-
The
OidcBackChannelLogoutWebFilterregisters itself very early in the filter chain and this part:
https://github.com/spring-projects/spring-security/blob/1423641c5692279191f864252c6c57bd62594a97/config/src/main/java/org/springframework/security/config/web/server/OidcBackChannelLogoutWebFilter.java#L92-L97
catches theAuthenticationCredentialsNotFoundExceptionand handles it and our exception handler is never reached. -
It seems as if the
https://github.com/spring-projects/spring-security/blob/1423641c5692279191f864252c6c57bd62594a97/config/src/main/java/org/springframework/security/config/web/server/OidcBackChannelLogoutWebFilter.java#L90
is executed too early in that filter or the onErrorResume should be added within the first flatMap. The onErrorResume should not be applied to the result of the chain.filter(exchange) call.
Expected behavior
The unauthenticated request should be handled by a ErrorWebExceptionHandler, instead of the OidcBackChannelLogoutWebFilter
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.