13

I am building a node.js web application with react for the the GUI and graphQL served with Apollo for the back-end connecting to a RDS (MySQL) instance on AWS.

I am authenticating users and then returning JWTs. I have it figured out on how to renew/expire tokens, but now I am being faced with the question where to save it on the client side when a user visits the site...

There are two main concepts with a third being a hybrid model. 1) Store it as localStorage with JavaScript as described on HowToGraphQL 2) Store it in a Cookie with http-only set to true as described in the afore mentioned article as a cationary reference to Randall Degges

There is another alternative to store it in memory only on the client side but then a user would have to login every time the page is refreshed as it would not be persistent anywhere.

Concept 1 is vulnerable to XSS only if there is another XSS vulnerability already exploited. But it is secure to the site only so only scripts running on the site can access it and not scripts on any site. There it a lot of security talk that it should not be stored this way even though it is the common way because a developer cannot trust EVERY JavaScript script they are running on their site and there may be one that reads the localStorage and then sends it offsite.

Concept 2 removes the XSS vulnerable by declaring the http-only to only make it accessible to the server at your site. The problem here lies in that then a separate method has to be created to use the same backend authentication for other uses such as a standard API (for native apps or other sites) where the JWT is sent in the header over https where it is stored securely on another server.

So I researched and found this hybrid method described by Ben Awad 3) use a request token and a refresh token. The request token can then act normally for the standard API but then also on our react app site we can store it only in memory and store a refresh token in a cookie to send back a request token when users refresh or close and reopen browsers.

So theoretically, the best solution is Concept 3 which solves all of the concerns, but it is of course more complicated to setup.

My question: How worried should I be about opening up a JWT to an XSS vulnerability? It is something that down the road I would do the long way when I have more time, but I am pushing for a deadline. My site will be lesser known and not something like Facebook or Sales-Force that hackers would necessarily target. My site is not storing Credit Card data or other highly sensitive data other than a basic CRM and task list. If my site was open to XSS through other code, wouldn't the entire authentication process be vulnerable through keylogging scripts or the likes without even knowing the JWT. I feel like I would be doing a lot of extra work to secure against a possible threat that if occurred, the entire system would be compromised already.

halfer
  • 19,824
  • 17
  • 99
  • 186
amaster
  • 1,915
  • 5
  • 25
  • 51
  • as a related topic, does anyone have anything positive or negative to say against npm packages to figure out the local storage available such as the `store` package? Does it help or hinder in solving the OP? – amaster Apr 06 '20 at 02:02
  • I really liked the ideas presented by Cedomir. What approach did you take at the end? Did you go with the local storage methodology as mention by Cedomir? Or you go with Secure HTTP only cookie as mention by JerryCauser. I am really confused and want to know your point of view. – Lizesh Shakya Aug 22 '20 at 08:20
  • 1
    localstorage is what we are using – amaster Aug 22 '20 at 13:00

6 Answers6

7

If you are comfortable with your site to not work on Internet Explorer and some older versions of the major browsers, you can take advantage of a new cookies property, called Same-Site (to be more precise, the site will work but the cookie will not be secure).

By defining a cookie as HttpOnly, you are immediately secured from XSS attacks, but you leave yourself open to CSRF attacks.

Now by defining the cookie to have the property Same-Site=Strict, the cookie will be only sent through Http calls and only if the domain matches your site's domain. So for example, if someone creates a form in another site and tries to perform a post request to your own site, the cookie will be never sent.

If you want the cookie to be passed only on GET requests, you can set the Same-Site property to Lax but as you mentioned.

You can find more info about this feature in the following link under the SameSite cookies section:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies

You should also check the browser compatibility of the feature by using the following link:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#Browser_compatibility

George Koniaris
  • 324
  • 2
  • 9
  • 1
    Just using a Cookie does not safe you from XSS attack - when he can inject any amount of JavaScript the attacker can certainly just call your backend and the session-cookie (even though unknown to the attacker) will be sent along by the browser. CSRF protection could mitigate this to some degree, but with full access to your client code (common for SPAs) the attacker will certainly find out how you add the CSRF Token (which you did with JS => the attacker can do the same). Real CSRF is another story. With the additional token you are safe from forged request coming from another site. – Mario B Apr 21 '22 at 04:46
5

This is the issue I spent a lot of time on. How to store the authorization token securely. People have different strategies in dealing with this so I will share what works for me. Users of my apps were targeted by different attacks, all of them where unsuccessful in stealing anything so far. None used XSS.

Here is what I do

In the end I opted for storing authorization token in local storage. Applications that I work usually have WebSocket connections on top of HTTP routes and I want the token to be saved in one place and act as a single source of truth. They are all web applications running in the browser. Most of the applications I build use JWT.

Why I do it like that

First why I don't use refresh tokens. If they are saved the same way as the actual authorization token would be saved negates the reason for refresh token to exist since the attacker can use the refresh token to get the authorization one.

Storing the token in cookies gives no benefits over local storage assuming that the app is secured against attackers being able to inject JavaScript into your app, mostly through forms and api on your app. Make sure all user inputs are JS injection safe. On top of that with cookies there are issues when using WebSockets that you must go around.

There is also the point of one of the accounts being hacked and you want to invalidate that token as soon as possible. JWT by default has no mechanism of being revoked. Implementing this feature negates the scalability of JWT because checking the JWT would require a call to the database to know if that user can do the specific action. There are 2 ways you can go about this. One is just check the user data if the user is frozen from the database, it is less scalable because of the call but if you already pull the user data in a middleware it is good enough TM. Other is to pull the the "is the user frozen" data from the database just when making changes to the database or when the call from the client is important.

In summary

I would store the token in local storage. Secure the app from code injections. And make a kill switch for the accounts if they get compromised in any way.

EDIT THANKS TO THE COMMENTS BY @JerryCauser

It is more secure to keep your token in a secure http only cookie. Don't expect a storage mechanism choice to automatically save your users from being hacked. There are ways to hijack sessions and other exploits including users using web extensions and approving their request to read protected data.

For the example of the betting website below, you wouldn't require user to write their password (or approve the request via automated email) every time they place a bet, but you would every time they want to take a withdrawal for example.

I use local storage because even if it happens for the token to be stolen, or another person got to your user's laptop (like a kid for example) you should never let the account do critical tasks without approval.

There is no magic bullet of anti hack protection. Try your best to keep your users safe with common sense.

EDIT AS ANSWER TO THE COMMENT FROM THE ASKER @amaster

If you are making a trip to the database on every call, maybe JWT is not the best solution. Point of JWT is to have signed claims and the id of the user without calling the database. In this case, maybe opt in for sessions instead of JWT.

Community
  • 1
  • 1
Cedomir Rackov
  • 1,032
  • 7
  • 17
  • 1
    Thank you for the answer. I have thought about instead of a "is the user frozen" have a last_valid_date field or something and then in the token I can compare if the date when making the token is before that date and invalidate the token. I am doing a validation of each request against the userdata in order to also do more refined access control through the API at that lower level. – amaster Apr 06 '20 at 02:00
  • LocalStorage data can be stolen by any kind of third party libs and browser extension. JWT should be stored as HTTP only cookie to prevent that. – JerryCauser Apr 07 '20 at 18:46
  • 1
    @JerryCauser HTTP only cookies cannot be read by your code and thus cannot be sent to a websocket. Plus here is a good read for the security https://stackoverflow.com/a/8069697/11291413 This is exactly why changing passwords and other critical features require you to enter your password again, maybe even approve of the action via email. – Cedomir Rackov Apr 07 '20 at 19:15
  • @CedomirRackov JWT should not be read by front-end. It is only for server-side. If you need some data from JWT (for example sanitized user data), then make request to server, server will parse JWT and send safe data to user. And what is the reason to send JWT over WS? If you need auth ws connection, then you can (and should) do it in handshake. Here is example https://github.com/websockets/ws/tree/master/examples/express-session-parse – JerryCauser Apr 07 '20 at 20:15
  • @CedomirRackov And we are not talking about how we should change passwords ands other stuff. We are talking how to store JWT token. If I wrote a something like sports betting site, then if someone stole my JWT token - he can lose all my money (bcs I will not ask user a password for every bet). With LocalStorage it is a simple like 2x2=4 even for bad chrome extensions. With HTTP Only cookie I will not worried about that. It is always a bad practice to keep session key in localstorage – JerryCauser Apr 07 '20 at 20:19
  • @JerryCauser Chrome extensions can read http only cookies https://stackoverflow.com/a/34993849/11291413 – Cedomir Rackov Apr 07 '20 at 22:02
  • @CedomirRackov `Note that this API requires declaring a permission and will not work from content scripts.`. If someone want to be hacked - well, it is his decision. – JerryCauser Apr 08 '20 at 05:46
  • @JerryCauser Thanks for the comments. Please check out the update in th eanswer. – Cedomir Rackov Apr 08 '20 at 08:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211202/discussion-between-cedomir-rackov-and-jerrycauser). – Cedomir Rackov Apr 08 '20 at 09:39
3

Before I proceed with my answer, you may want to check out OWASP for a set of general guidelines regarding XSS and CSRF since you've mentioned cookies.

Cedomir already covered a good deal of the points with storing JWT client side. One thing that's worth mentioning is that if you have Third-Party scripts running in your web app, they also have access to the Storage API. So if a script you had loaded were to be hijacked, they could conceivably steal the token there. As for XSS with inputs, if you make sure to escape every possible user input, then that is largely mitigated as an attack vector. But you only have to screw up once for someone to take advantage of the hole and steal the JWT at that point. (Refer to this blog post for more details)

Now, if you instead store the JWT in a Http-Only, then you largely sidestep the XSS issue as you've already noted. However, now you introduced a new problem, that being Cross Site Request Forgery. Since cookies are sent with every request, a malicious actor could set up a website to make a fraudulent request on behalf of user and execute actions without the user's consent. Now I won't cover the mitigation in detail here as OWASP and other places have done a pretty good job already, but the short of it can be summed up by installing the most popular and well-maintained Anti-CSRF package for your language :-)

As for invalidating the token as Cedomir brought up, having that mechanism can be quite useful. However, to implement it does mean you give up some of the benefits of using JWT gives you. Whether you store the current JWT assigned to user and validate that or a unique key used to sign the JWT for each user, you now have user state to keep track of, eliminating one of the reasons to use JWTs. Depending on your application, you will need to weigh that tradeoff. A much simpler way could be simply to have short-lived tokens so that any token that is stolen potentially won't have a very useful lifetime. However, as you probably recognize a short lifetime would be a potentially a very annoying user experience. You could have your website periodically poll the server for a new token while your user continues to use the website as a way to improve the experience. You can also balance your security concerns with the lifetime of the token, like a 15 minute token lifetime for a e-commerce app vs. a hour or more for a social application.

I would however advise against the use of a refresh token, at least for a Browser-Based Web App. Typically speaking, the browser is just not considered capable of securing sensitive secrets. By using a refresh token, you're just deferring the stealing of credentials to another layer as by the nature of the refresh tokens, they're 1) long-lived and 2) effectively used as credentials to obtain more JWTs. So if the refresh token were to be stolen, an attacker can just get more valid JWTs on behalf of a user. If you have a mobile or desktop app, you have mechanisms you can use to securely store refresh tokens and this advice does not apply.

...Or you could just use sessions ;-)

xinkecf35
  • 328
  • 1
  • 10
  • Agree with the sessions completely. If you have a call the database based on the JWT on every request, the is no reason for the JWT – Cedomir Rackov Apr 08 '20 at 08:14
3
  1. When logging in on server set JWT token and a random csrf token in the httpOnly cookie
  2. Also send this csrf token in body response of login back to client
  3. On every future request from client send this csrf token via some header (eg. X-CSRF-TOKEN)
  4. On the backend verify if the csrf tokens coming through the cookie and x-csrf-token are the same.
  5. Then verify your JWT token and continue with your app logic.

Putting JWT token in httpOnly cookie prevents XSS attacks, validating CSRF token prevents CSRF attacks. Double sending csrf token in both cookie and header avoids storing stuff in the backend database.

  • XSS check
  • CSRF check
  • Stateless auth check

Auth doesn’t have to be over complicated. If you have clients that only want to pass JWT token in some header other than cookie then it’s better to just make a separate api endpoint for those programs.

Kashif Siddiqui
  • 1,476
  • 14
  • 26
  • But if the attacker finds an XSS vulnerability (with httpOnly cookie u just preventing to stole the cookie from the client), they can inject any code into your app and for example they can create a malicious form and all your effort is bypassed. (in your solution the CSRF check works from another site) – IamK Jul 03 '22 at 20:07
  • That's what the double signed cookie is for. If someone sends a malicious form they will only be able to use the cookie but won't pass the csrf via x-csrf-header, which will get blocked by the backend as it expects both. – Kashif Siddiqui Jul 05 '22 at 10:44
  • But as you stated "Also send this csrf token in body response of login back to client" (this is same as in the httpOnly cookie) if the attacker founds an xss and can access the whole document, then he/she can read the token, am i wrong? – IamK Jul 07 '22 at 20:10
  • 1
    Well the idea with sending token in body response is to store it in local session or storage. Both the techniques (cookie and storage) are individually weak but are meant to work together. The idea is to protect attacks by only having access to one or the other at a time but never both, only your app should access to both. But if someone has ability to both send malicious forms to you AND inject code that can read storage then unfortunately you can't do much about it. Because at that point that hacked code is just part of the app itself. – Kashif Siddiqui Jul 08 '22 at 00:51
  • In this approach, can csrf token be skipped, sending only JwtToken in both HttpOnly cookie and login response? – Xiang Wei Huang Jun 13 '23 at 08:20
1

While the question is not actually about OAuth / OpenID Connect I still think you can learn a great deal by checking out this Internet-Draft: OAuth 2.0 for Browser-Based Apps (Best Current Practice)

To sum it up: there simply is no secure way to store an access token on the client. If you develop only the frontend you pretty much have to use and store a token on client side - not because it's great but because you have no other choice. However, if you do have full control over Frontend and Backend you do have that choice and should think about using the same domain for both and use a session cookie as described in the Internet Draft. Basically the React application never even sees the acesss token, because your backend is serving a http page and handling the authentication directly, with the final step being a redirect back to your frontend while setting the session-cookie.

A potential XSS attack is pretty bad as it is and you should be careful not to introduce a vulnerability. The thing is: with the JWT-approach a XSS vulnerability leads pretty much to the worst-case scenario: the attacker is able to steal the user authentication and can impersonate the user - this is basically session hijacking.

The same attack against a regular session-cookie simply does not have the same impact (as long as the cookie uses the HttpOnly Flag which is highly recommended). Even though the vulnerability enables arbitrary JavaScript Code to run on the machine (which is really bad obviously) it's still a lot harder for the attacker to do some damage. He is not able to hijack the session in this case, because he is unable to read the cookie.

Mario B
  • 2,102
  • 2
  • 29
  • 41
-1

Just use HTTP only + SSL only cookies to save your JWT. It will make almost impossible to stole user's jwt via a soft or any type of code injections.

Someone said here, what it is no diff between LocalStorage and Cookies. He is not correct, bcs third party libraries and chrome extensions can easily stole LocalStorage data. But they cannot stole HTTP only cookie.

It will protect against any known and most likely new types of attacks.

JWT itself is completely protected. Just don’t store something there that could compromise your architecture or something like that (do not put a hashed password for example)

Upd: Good article about best practices for JWT strategy: https://ducktypelabs.com/5-mistakes-web-developers-should-avoid-when-using-jwts-for-authentication/

JerryCauser
  • 811
  • 5
  • 17
  • As mentioned in the comments in my answer, cookies can be stolen. Saying they cannot is false. – Cedomir Rackov Apr 08 '20 at 08:13
  • As I said: `Note that this API requires declaring a permission and will not work from content scripts.` If someone want to be hacked - well, it is his decision. – JerryCauser Apr 08 '20 at 14:32