Considering the following facts, using CSRF token for a restful API (which of course is intrisincly session-less) seems inevitable:
- storing
JWTinlocal storage(any where other thanhttp-only cookie) makes the API vulnerable toXSSattack. - storing jwt in
http-only cookiemakes the API vulnerable toCSRFattack
So in order to make the API secure against, both XSS and CSRF attacks, CSRF token must be implemented in the system. My question is how?
- Should I define a route like
/api/csrfso that the client is forced to send a request (containing theirhttp-only cookie, which holds their JWT) to that endpoint, before each POST/PATCH request, to get aCSRF tokenand attach the token to the aforementioned POST/PATCH requests? - Considering that there are no sessions, In server-side, Should I store the mapping to CSRF and user id(JWT) in database/redis?
Are the abovementioned solutions valid and secure? Are there any standard way to deal with this problem?