-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/hmac token signing #61
base: main
Are you sure you want to change the base?
Conversation
a172260
to
76efeff
Compare
76efeff
to
ff35b47
Compare
ff35b47
to
525eb68
Compare
BREAKING CHANGE: This change replaces the createHash method with createHmac for hashing tokens. It introduces the getSessionIdentifier configuration option which by default will return req.session.id. The purpose of this function is to return the id of the session associated with the incoming request. The session id will be included in the hmac signature, forcefully tying the generated csrf token with that session. This means by default generated tokens can only be used by the session which they were originally generated for. If you have any kind of session rotation (to mitigate session hijacking), which you should be doing during privilege escaltions and de-escalations (e.g. sign in, sign out) then you will need to generate a new csrf token at the same time. Additionally this change exposes the delimiter and hmacAlgorithm options.
525eb68
to
bb9bc42
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm.
I am assuming if one does not want to use session identifiers they can just do getSessionIdentifier = () => "", right?
Is it the best option to use express session as the default though?
I think the direction I'd like to go is to not explicitly support cases where the session id is not tied to the token. The recommended practice is that generated tokens should only be valid for the session that requested it, and this acheives that in a stateless way. In the case where
Not sure, are you suggesting we don't provide a default, thus removing the dev dependency on express-session? |
Hmmm isn't express-session stateful actually (afaik it stores them in memory by default)? I get the appeal, but the main point of double submit pattern is for it to be completely stateless, right? Hmmm isn't express-session stateful actually (afaik it stores them in memory by default)? I get the appeal, but the main point of double submit pattern is for it to be completely stateless, right? Also, I can't really think of a stateless way of maintaining a session in that sense. |
Right, but in a case where, let's say, a user is using JWT is a httpOnly cookie, then If they aren't using a I understand that the double submit pattern can be used from only the client side, without the backend being involved in the token generation at all - but all of the best practices recommend a backend using a secret so that you can guarantee where the token was generated from, and additionally, it's also recommended to include some piece of information when hashing that ties the token directly to the person who requested it. Maybe I should rename it to Edit: I think I accidentally deleted a message, no idea how. For express-session using memory by default, yes, it does, but express-session also says NOT to use the default in production and has a list of recommended DB or cache based stores. But yeah, it's stateful. Though providing it as a default doesn't require express-session if you're overriding it. Nor does the default even require express-session, it just requires a |
I think that's fine yeah. I saw the 'session' in the name and did not think of that haha |
The eventual intention before the next major release is to completely revamp the documentation and the testing suite as well, as well as provide more and better detailed examples. For testing, instead of repeating the same suite for all of the different configurations, I'd like to start splitting out the tests and only run the minimal amount needed, as well as generate a test coverage report to actually capture what has and hasn't been tested.
These changes are implicitly dependent on #60
The documentation and test changes aren't the best and there are some new edge cases not covered in the tests, such as one session trying to make a request with a CSRF token generated for another session. However, I plan on overhauling both the documentation and the tests before the next release, hopefully generating some sort of documentation from TSDoc, and also including a test coverage report.