Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSRF disable not working - getting error "Could not verify the provided CSRF token because no token was found to compare" #15752

Closed
rajcreddy opened this issue Sep 6, 2024 · 6 comments
Assignees
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: invalid An issue that we don't feel is valid

Comments

@rajcreddy
Copy link

Despite disabling csrf, spring is throwing error "Could not verify the provided CSRF token because no token was found to compare" when APIs are being invoked with the below security filter chain. This is the only filter chain in my application. The csrf error goes away if I remove the oauth2ResourceServer block. I have pasted the stack trace below. The error should not be returned since csrf is being disabled.

@bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
requestCache.setMatchingRequestParameterName(null);

http
  .requestCache(cache -> cache.requestCache(requestCache))
  .csrf(csrf -> csrf.disable())
  .authorizeHttpRequests(requests -> requests
        .requestMatchers(AUTH_WHITELIST).permitAll()
        .requestMatchers("/v1/**")
            .authenticated()
    )
    .oauth2ResourceServer(resourceServer -> resourceServer
        .opaqueToken(token -> token
            .introspectionClientCredentials(clientId,clientSecret)
            .introspectionUri(checkTokenEndpointUrl)
        )
    );
return http.build();

}

Caused by: org.springframework.web.client.HttpClientErrorException$Forbidden: 403 : "{"error":"Could not verify the provided CSRF token because no token was found to compare."}"
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:109) ~[spring-web-6.1.4.jar!/:6.1.4]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:183) ~[spring-web-6.1.4.jar!/:6.1.4]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:137) ~[spring-web-6.1.4.jar!/:6.1.4]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-6.1.4.jar!/:6.1.4]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:942) ~[spring-web-6.1.4.jar!/:6.1.4]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:891) ~[spring-web-6.1.4.jar!/:6.1.4]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:740) ~[spring-web-6.1.4.jar!/:6.1.4]
at org.springframework.security.oauth2.server.resource.introspection.SpringOpaqueTokenIntrospector.makeRequest(SpringOpaqueTokenIntrospector.java:146) ~[spring-security-oauth2-resource-server-6.2.2.jar!/:6.2.2]
at org.springframework.security.oauth2.server.resource.introspection.SpringOpaqueTokenIntrospector.introspect(SpringOpaqueTokenIntrospector.java:128) ~[spring-security-oauth2-resource-server-6.2.2.jar!/:6.2.2]
at org.springframework.security.oauth2.server.resource.authentication.OpaqueTokenAuthenticationProvider.getOAuth2AuthenticatedPrincipal(OpaqueTokenAuthenticationProvider.java:124) ~[spring-security-oauth2-resource-server-6.2.2.jar!/:6.2.2]
... 86 more

@rajcreddy rajcreddy added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Sep 6, 2024
@phosphataz
Copy link

Hello @rajcreddy , can you try this :
.csrf(AbstractHttpConfigurer::disable)

@rajcreddy
Copy link
Author

rajcreddy commented Sep 9, 2024

@phosphataz I tried this already and it did not work. I get the same error.

@rajcreddy
Copy link
Author

rajcreddy commented Sep 9, 2024

@marcusdacoregio for some reason the opaquetoken call is resulting in csrf error eventhough csrf is disabled. Is this an issue in the opaquetoken REST call that I see in the stack trace. I am using spring boot 3.2.3

@jzheaux
Copy link
Contributor

jzheaux commented Sep 10, 2024

Based on the stack trace, it appears that the CSRF error is coming from the authorization server receiving the opaque token request. Are you able to still reproduce this when you use a mock backend (for example, using MockWebServer) for the opaque token endpoint?

If so, please provide a minimal sample, and I can take a look.

@jzheaux jzheaux added in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Sep 10, 2024
@jzheaux jzheaux self-assigned this Sep 10, 2024
@rajcreddy
Copy link
Author

@jzheaux thank you! there was a typo in the auth server url which was causing the issue. The auth server url did not exist so the error was misleading

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 10, 2024
@jzheaux
Copy link
Contributor

jzheaux commented Sep 10, 2024

Awesome, @rajcreddy! Glad you found it. I'll close this issue at this point.

@jzheaux jzheaux closed this as completed Sep 10, 2024
@jzheaux jzheaux added status: invalid An issue that we don't feel is valid and removed status: feedback-provided Feedback has been provided labels Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants