spring-projects / spring-projects/spring-security

OidcBackChannelLogoutWebFilter returns an error for unauthenticated ajax requests

Open
#16,073 1 comment 0 reactions 1 assignee View on GitHub

@jzheaux is already working on this.

Since Nov 14, 2024.

in: oauth2 status: waiting-for-triage type: bug
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

  1. We have a Spring configuration where all our requests, except logout require authentication:
.authorizeExchange(exchanges -> exchanges
   .pathMatchers("/logout").permitAll()
   .anyExchange().authenticated()
)
  1. If you make an unauthenticated request, it triggers an AccessDeniedException, which then goes to ExceptionTranslationWebFilter. When the filter sees that the exchange principal is not authenticated, it calls the registered ServerAuthenticationEntryPoint with a new AuthenticationCredentialsNotFoundException as an argument.

  2. 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 ErrorWebExceptionHandler can 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))
                ...
}
  1. The OidcBackChannelLogoutWebFilter registers 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 the AuthenticationCredentialsNotFoundException and handles it and our exception handler is never reached.

  2. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.