Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSRF in token based authentication

We have a token based OAuth authentication mechanism for our angularjs application. The acunetix tool indicated that XSRF threat is there.

Is CSRF an issue for token based authentication (Because we are not using any cookies for user identification / authentication / sessions)?

If CSRF is an issue for token based authentication, is there be any way to implement prevention without using cookies?

like image 904
rahulmr Avatar asked Jun 08 '16 07:06

rahulmr


People also ask

Can CSRF be used for authentication?

A CSRF attack works because browser requests automatically include all cookies including session cookies. Therefore, if the user is authenticated to the site, the site cannot distinguish between legitimate authorized requests and forged authenticated requests.

What is token CSRF?

A CSRF token is a secure random token (e.g., synchronizer token or challenge token) that is used to prevent CSRF attacks. The token needs to be unique per user session and should be of large random value to make it difficult to guess. A CSRF secure application assigns a unique CSRF token for every user session.

Do we need CSRF with JWT?

If our stateless API uses token-based authentication, such as JWT, we don't need CSRF protection, and we must disable it as we saw earlier. However, if our stateless API uses a session cookie authentication, we need to enable CSRF protection as we'll see next.

How does CSRF protect token?

The most effective method of protecting against CSRF is by using anti-CSRF tokens. The developer should add such tokens to all forms that allow users to perform any state-changing operations. When an operation is submitted, the web application should then check for the presence of the correct token.


1 Answers

As far as I know token based authentication is in no way affected by CSRF. E.g. if you use cookies, and bad guys lure users into their site where they can create a special button that will do a post to your site -> here is CSRF where you can execute some requests on behalf of the users.

Now if you use tokens that are stored in session/local storage e.g., they are never automatically passed with the request. You probably use something like angular interceptor or similar technology to pass it along with every XHR request. This never happens automatically.

You can read a bit more on token auth in this very good post. In point number 6 there is a little section about XSRF/CSRF, XSS.

In my modest experience these big security tools can often tell you something that is not true just to make themselves more "significant". But it would be interesting to know exactly how it plans to execute CSRF and what exactly made it think it is possible? E.g. you might have a cookie that you missed?

P.S. XSS attack (to steal token) gets more possible with tokens, since you can put HTTP-only like for cookies. So any successful XSS will be able to read your token, so you need to make sure that you have a good protection against that. But it's usually covered well by frameworks.

like image 94
Ilya Chernomordik Avatar answered Oct 14 '22 03:10

Ilya Chernomordik