I have a front/back applications that needs to be logged in to be used. When I log in (by means of the front-end app sending a request to the back end), what I do is not sending a cookie, but a JSON
with a token in it. The latter will be stored by the front end app in a sessionstorage
and each time it will interact with the back end it will send a request along with the token stored in the sessionstorage
. The back end will verify the validity of the token.
Do you think this solution is CSRF safe? Do you see any other vulnerabilities I'm not considering/ignoring?
1 Answer 1
It's certainly safer than using a cookie when it comes to CSRF, but it is less safe when it comes to XSS, because session storage can be read from javascript, while http-only cookies cannot.
So it depends on how confident you are about your protection against XSS.
If you do go for this approach, I would suggest using the Authorization header with the Bearer scheme.
Authorization: Bearer <token>
Other things to keep in mind:
- Session storage isn't shared between tabs.
- You can't use this method for images that require authentication. For those you could still use a cookie (just make sure you ignore the cookie for all other endpoints and possibly limit the path of the cookie to a path under which only images are found)
-
1Well answered. What about sending an http-only cookie along with a random string that will be stored into the localstorage? The client should send the cookie along with the string and they must match. In case of XSS attack, the token can be stolen, but each five minutes or so I'll send a renew request of my session, where a new random string will be sent. That way any attacker will have a potential slot of 5 minutes to get the string and try to get a CSRF attack with that. What do you think?Bertuz– Bertuz04/15/2016 19:40:07Commented Apr 15, 2016 at 19:40
x-access-token
etc. And securing JS to middle backend it could be CORS and (timestamp + userIp + endpoint & request details) by hashing and you can use https here, and in middle backend application you can use session to write this things to diferantiate request from each other.