0
votes

I want to protect my application against csrf. Although I couldn't really understand what the problem is and how my solution works, after some research I came up with a solution, which Angular uses. As far as I got, my solution requires following steps :

--> client request for my spa

--> I send csrf token (not httponly so that js will be able to read it). I also save this csrf token to user session on server.

--> for every post request I want my client to read csrf token and set X-XSRF-TOKEN header to this token.

--> I ll check every request by checking request header and user session csrf token. If matches, I ll also check jwt for authentication if I need.

--> After validating csrf token, I ll make changes to database. Also I ll change csrf token again, send new token to user, change token for the session.

But I am not sure how this helps, If I have a xss vulnerability, any injected javascript code also can do the same. I want to understand the problem and how such a solution helps. Thanks.

FYI. I am also implementing JWT based authentication, using redis for session management, on express server.

1

1 Answers

2
votes

The following link might help, but I'll summarize here.

https://nirajrules.wordpress.com/2010/01/16/cross-site-scripting-xss-vs-cross-site-request-forgery/

CSRF is more about cross site requests. Somebody just finding your form actions and posting to them directly, for example. That is what a CSRF token helps to prevent. Imagine people making a fake website, a phishing site, that actually calls through to your form submit endpoints.

XSS is very different, and you are right that any malicious javascript that is able to be run within your page would be able to access and grab the token. But these are different things, and doesn't diminish the value of CSRF tokens.

Good luck.