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 check on websockets not skipped if there is a Bearer token present. #15312

Closed
ionutbarau opened this issue Jun 27, 2024 · 1 comment
Closed
Assignees
Labels
for: stackoverflow A question that's better suited to stackoverflow.com

Comments

@ionutbarau
Copy link

ionutbarau commented Jun 27, 2024

I have a project composed of spring-cloud-gateway in front of a couple of spring boot microservices that use websockets. For authentication/authorization I use spring-authorization-server (OIDC/OAuth 2). I am trying to implement security in the gateway, including CSRF, however for websockets there seems to be an issue on how CSRF is handled.

Since the microservices are resource servers, when http request are sent from gateway to downstream services, CSRF check is skipped, as it finds the Bearer token, essentially trusting the gateway, as there is no cookie involved. See this for a good explanation. However, this does not happen for websocket endpoints.
When using @EnableWebSocketSecurity, the CSRF token coming from the gateway is checked against the microservice token repo, which, of course will throw Invalid token exception.

Shouldn't websockets CSRF check be skipped if there is a Bearer token, just like regular http calls?

Is this intended behavior?

I have also posted here on SO about this topic.

As a workaround I have disabled CSRF by not using @EnableWebSocketSecurity. I have implemented authorization at gateway level so only authenticated users with specific oauth scope can call the connect endpoint. This could leave the service vulnerable if called directly. I have also used allowedOrigins to allow only specific clients to call the connect endpoint. I have posted here why I think it is enough and CSRF token might not be needed if we check the origin on the server.

@ionutbarau ionutbarau added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Jun 27, 2024
@sjohnr
Copy link
Member

sjohnr commented Jun 27, 2024

@ionutbarau thanks for reaching out. However, it is not necessary to cross-post questions here as the team regularly reviews Stack Overflow.

Configuring a downstream application (e.g. a resource server) using http.oauth2ResourceServer() is only configuring authentication for HTTP requests and does not affect websockets. If you have an area of the documentation that you feel is misleading or missing, please let me know and we can open an issue to address that.

I have implemented authorization at gateway level so only authenticated users with specific oauth scope can call the connect endpoint. This could leave the service vulnerable if called directly.

I'm not clear what you mean by this? Note that CSRF protection protects users (not applications) when the browser can automatically provide credentials (e.g. session cookie, basic auth, etc.) to the server, which doesn't seem to be the case for your downstream service. I would recommend clarifying on the stackoverflow question what your concern is here.

Regardless, I'm going to close this question in favor of the stackoverflow question.

@sjohnr sjohnr closed this as completed Jun 27, 2024
@sjohnr sjohnr added for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Jun 27, 2024
@sjohnr sjohnr self-assigned this Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

2 participants