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

jwt/login example CSRF ignored because of Bearer Token #116

Closed
simasch opened this issue Jan 17, 2023 · 6 comments
Closed

jwt/login example CSRF ignored because of Bearer Token #116

simasch opened this issue Jan 17, 2023 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@simasch
Copy link

simasch commented Jan 17, 2023

Hi,

I tried the jwt/login example and noticed that the CSRF token is not generated when a Bearer token is present because of BearerTokenRequestMatcher.

The only request without Bearer token would be the auth request with basic authentication, but for this request CSRF is disabled.

Does the example make sense? Or why isn't CSRF disabled at all?

Thank you.

@marcusdacoregio marcusdacoregio self-assigned this Jan 17, 2023
@marcusdacoregio
Copy link
Contributor

Hi @simasch,

CSRF protection is unnecessary in this scenario because we are using stateless authentication. In other words, we are not dealing with Session Cookies, which are added to the request automatically by the browser, this way avoiding the majority of CSRF exploits. When using an Authorization header, we have to add the header manually using JavaScript.

The HTTP Basic authentication mechanism is also stateless, so no need for CSRF protection either.

@marcusdacoregio marcusdacoregio added the question Further information is requested label Jan 17, 2023
@simasch
Copy link
Author

simasch commented Jan 17, 2023

@marcusdacoregio
Copy link
Contributor

Hi @simasch,

What changes specifically do you expect to see in the configuration?

@simasch
Copy link
Author

simasch commented Jan 17, 2023

csrf(AbstractHttpConfigurer::disable)

instead of

csrf((csrf) -> csrf.ignoringRequestMatchers("/token"))

Because it was very confusing that CSRF is configured but not used and now token was generated.

@marcusdacoregio
Copy link
Contributor

It is a little tricky for sure, but I think keeping .csrf((csrf) -> csrf.ignoringAntMatchers("/token")) makes sense. If the user happens to add some authentication mechanism that relies on a session cookie they do not have to remember to change the CSRF configuration to now include those endpoints.

It is a recommended secure posture to "deny" everything unless they are authorized, it's the same train of thought as spring-projects/spring-security#11958

@simasch
Copy link
Author

simasch commented Jan 17, 2023

Makes sense!
Thank you

simasch added a commit to simasch/angular-springboot-demo that referenced this issue Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants