-
|
Reviewing the frontend code, I saw that it appears the Vuex store saves the JWT tokens in localStorage, against common security recommendations. Although this is just an example project, it's never a good idea to promote bad security practices that find their way into production applications, especially when teaching new developers the "right" way to build Single Page Applications. Would it be possible instead to re-architect this application to use e.g. HttpOnly cookies? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
|
so I'm kinda curious @danjac how would you do this then? Most articles and even code you look at use this same type of setup with vuex typically storing the token. I thought if you had enough form validation in your frontend a XSS attempt would be pretty low or no? I'm following this as I'm setting up a production level app atm sorta based off this and I'm kinda curious the impact of using the vuex store with proper form validation on any inputs. |
Beta Was this translation helpful? Give feedback.
-
|
For a browser frontend I would stick to server HttpOnly cookies that are not accessible to your JS code at all, combined with some form of CSRF protection for POST and other write requests. The index page that loads your application is rendered by your server, which includes a JSON script tag containing all the data you need to hydrate your Vuex store - for example the authenticated user. I think the reason so many articles sidestep this issue is either just pure ignorance - every other article does it, so why not me? Assumption that Vue/React/etc will automatically handle XSS (they do to some extent, but can you ensure some obscure dependency from npm isn't doing something evil? And what if you want to render some user data as html, for example with some markdown editor?) and just plain convenience/laziness - it's easy to just throw a token into localStorage without further explanation of the tradeoffs that might confuse readers. |
Beta Was this translation helpful? Give feedback.
-
|
I found this: https://github.com/cthwaite/fastapi-jwt-cookies |
Beta Was this translation helpful? Give feedback.
-
|
I think a better option is to save the token in a samtesite=Strict cookie
afaik this will prevent csrf and xss attacks |
Beta Was this translation helpful? Give feedback.
-
HttpOnly is also ineffective if vulnerable to XSS attacks. |
Beta Was this translation helpful? Give feedback.
-
|
Using Cookie-based authentication also has a lot of caveats and it would mean tightly coupling the authentication from the backend with browsers, etc. For now I would not want to do all that. |
Beta Was this translation helpful? Give feedback.
-
|
Cookie based authentication has HttpOnly flag, which can not be accessed by third party scripts or browser extensions. If you don’t want to increase the complexity, it is super easy to use signed cookie for the session. And it is much easier than the access token and refresh token mechanism. |
Beta Was this translation helpful? Give feedback.
-
|
Since this template is intended for browser-based use and has so many stars, it’s likely to be used by a large number of people. Storing access tokens in localStorage is unsafe, and widespread use of this template would expose many users to the same vulnerability. Given that this is a browser-focused template, I think it’s best to provide a secure implementation by default. |
Beta Was this translation helpful? Give feedback.
Using Cookie-based authentication also has a lot of caveats and it would mean tightly coupling the authentication from the backend with browsers, etc. For now I would not want to do all that.