Considering the following facts, using CSRF token
for a restful API (which of course is intrisincly session-less) seems inevitable:
- storing
JWT
inlocal storage
(any where other thanhttp-only cookie
) makes the API vulnerable toXSS
attack. - storing jwt in
http-only cookie
makes the API vulnerable toCSRF
attack
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/csrf
so 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 token
and 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?