Skip to content
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

Is it safe to use System.Random to generate session tokens? #1725

Closed
SmartHypercube opened this issue May 9, 2021 · 4 comments
Closed

Is it safe to use System.Random to generate session tokens? #1725

SmartHypercube opened this issue May 9, 2021 · 4 comments

Comments

@SmartHypercube
Copy link
Contributor

defaultGen = Random.getStdRandom Random.next

Nothing -> Right $ fmap Just . randomString 40

Yesod currently uses System.Random as the default random number generator, and uses this to generate session tokens. This looks very dangerous to me, because System.Random is only a pseudo-random number generator and has not been designed with security in mind. An attacker may easily collect enough information about the internal state of the generator and predict all future outputs.

I suggest using System.Entropy.

@snoyberg
Copy link
Member

snoyberg commented May 9, 2021

That's probably a good call. Are you interested in sending such a pull request?

@SmartHypercube
Copy link
Contributor Author

I'd like to. However, I still have some confusion about session logic:
Is this token (reqToken) directly exposed to end user? Or is it encrypted and signed using server's key before sending to users? Is the answer depends on session backend?

If it is exposed to end user and used for authentication, it should be generated using a secure entropy source and has at least 128 bits (I would suggest using 256 bits for all kinds of secrets).
If it is encrypted and signed before sending to users, we do not need a token here. Simply an incrementing integer will do. In this case I suggest explaining the security considerations in the comment.

@SmartHypercube
Copy link
Contributor Author

Oh, I just realized this token is not session token. Yesod core does not have the "session token" concept. This is CSRF token and only used by defaultCsrfMiddleware...
Given that this middleware is optional, isn't it strange to include the CSRF token in YesodRequest? A better design may be storing CSRF tokens in session data. The token generation logic can also be moved into setCsrfCookieWithCookie where it belongs.
CSRF tokens should still be securely generated though. It is user-facing and not protected by encryption.

@snoyberg
Copy link
Member

I don't really remember the original design around CSRF tokens, and I'm not particularly interested in changing the way it works right now unless there's a concrete reason. Improving how they're generated for proper protection does still seem worthwhile though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants