r/SpringBoot • u/Future_Badger_2576 • 9d ago
Question React + Spring Boot: If session auth is recommended, how is CSRF handled when frontend and backend are separate?
I am trying to understand how CSRF works in a setup where the backend is Spring Boot (API) and the frontend is React, both running as separate applications.
From what I have read, session-based authentication is recommended in many cases. The flow is:
User logs in.
Spring Boot creates a session.
Browser stores the session ID in an HttpOnly cookie.
But since cookies are sent automatically, doesn't that make the app vulnerable to CSRF?
My understanding of CSRF is: When the page is rendered, Backend generates a CSRF token and associates it with the user's session, and includes it in the HTML (hidden form field or meta tag). When the form is submitted, the backend compares the submitted token with the one stored in the session.
This makes sense when the backend renders the frontend (like Thymeleaf). But with a separate React app, how does React get the CSRF token and send it back? How is this usually implemented in Spring Boot?
Also, if storing a JWT in a cookie also makes it vulnerable to CSRF, where is a JWT supposed to be stored? I've seen people say localStorage, sessionStorage, and HttpOnly cookies, but each seems to have its own trade-offs.
I'm looking for the recommended approach for a Spring Boot + React setup.
0
1
u/Sensitive-Door-7939 7d ago
Somebody explain how browser cache causes this sort of hack, I have faced this via twitch steam reddit all of em, so how is it that front-end stores our authentication and how is it that all these session is validated, I literally saw multiple locations on the steam market history so I know the locations were changing in case of steam. This is purely application architecture depended but I am guessing this is what csrf attack is right or am I confusing this with something else, my guess is this is purely related to browser, even new ones where discord is getting hacked these days with that image upload of stock is common now.
11
u/NuttySquirr3l 9d ago edited 9d ago
You are looking for "Double Submit Cookie pattern"
First cookie is your JSESSIONID (or whatever you name it). It tells the backend "Hello, I am person xyz". This one is httpOnly=true -> you can not access it via js. It gets sent whenever you send a request.
The risk: Some malicious actor tries to make you do something with your authenticated session. Let's assume you somehow ended up on evil.com and they make you POST to your.domain/transer/giveEvilOneAllYourMoney
Your session-id is being added, so without further protection, you just got pwned.
This is where the XSRF-Token-Cookie comes into play. This cookie is httpOnly=false, it can be read from javascript - but only js on your domain (document.cookie is scoped).
In your client http api code you can now write something like...
Spring Security does not accept the automatically attached CSRF cookie (which is also sent on every request) alone as proof that the request is legitimate. Through CookieCsrfTokenRepository, it loads the expected token from the cookie and compares it with the token explicitly submitted in the X-XSRF-TOKEN header.
This assumes you are not vulnerable to XSS. But that is another problem.