sinatra: Session secret documentation does not encourage good secret management strategies
In the readme and intro documentation, we describe how to set a session secret manually with this code block:
set :session_secret, 'super secret'
We don’t mention anywhere that the value should really be pulled in from an environment variable (or possibly config file not checked into the codebase, secret store, etc).
We also don’t mention how to securely generate the value.
While ideally all developers would know that this secret shouldn’t be checked into code, and should be generated with a CSPRNG, frameworks have an opportunity to promote good security practices. This will simultaneously educate developers and prevent vulnerabilities.
I’d suggest that we:
- Show code examples sourcing secrets from environment variables
- Add an example of generating a secure random secret
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 20
- Comments: 15 (9 by maintainers)
Commits related to this issue
- encourage sourcing session_secret from an env variable See #1187 — committed to jacksingleton/sinatra by jacksingleton 10 years ago
- Merge pull request #1218 from grempe/1187_session_key_docs Fixes #1187 Improve Session Secret documentation to encourage better security — committed to sinatra/sinatra by deleted user 10 years ago
- Added fallback for session secret This fallback is added to ensure a session secret for the app is always set/available in case the proper ENV variable is missing https://github.com/sinatra/sinatra/... — committed to aitbw/af_maracay by aitbw 10 years ago
- Modify 'Using Sessions' section in README.ja.md to reflect original README.md one of #1187. — committed to shu-i-chi/sinatra by shu-i-chi 5 years ago
- Merge pull request #1731 from shu-i-chi/modify-readme_ja_md/using-sessions-section Modify 'Using Sessions' section in README.ja.md to follow #1187 — committed to sinatra/sinatra by jkowens 5 years ago
I would also recommend setting the session secret in an environment variable. You can retrieve it within your code using the block form of
ENV.fetchwhich will provide a safe fallback if the specified ENV var is unavailable. If you useENV['I_DONT_EXIST']it will return a nil value for a var which is not set which is probably not what you want to set your session keys to.Storing config like this in env vars is also a key tenet of 12 Factor Apps
Option 1
Throws an exception if
SESSION_SECRETdoes not exist in the current ENV. Use this if you want to fail fast if this critical setting is missing.Option 2
Generate a secure value for
SESSION_SECRETif its not set in the current env.Towards Stronger Keys
I would also recommend using the
Sysrandomgem to generate all keys that need to be cryptographically secure.https://github.com/cryptosphere/sysrandom
Example:
Its exactly the same, except for installing the gem and a different
require.Choosing a Key Length
I think it is not desired as @jacksingleton said to reduce the random key length to 20 bytes (40 hex chars). I think you should use a secure key that is the same block size as the underlying HMAC hash function (64 bytes for SHA1) which is what is used by
Rack::Session::Cookie. You should likely utilize the full 64 bytes (512 bits, 128 hex chars) that HMAC-SHA1 wants.https://github.com/rack/rack/blob/master/lib/rack/session/cookie.rb#L108 https://github.com/rack/rack/blob/master/lib/rack/session/cookie.rb#L185
I think Sinatra is also generating a 64 byte key if one isn’t provided if this is the correct spot in the code where that is happening:
https://github.com/sinatra/sinatra/blob/1b0edc0aeaaf4839cadfcec1b21da86e6af1d4c0/lib/sinatra/base.rb#L1797
Here is some good reading on this topic courtesy of @atoponce
https://pthree.org/2016/07/29/breaking-hmac/
tl;dr
Bonus Points
Just for kicks, here’s Ruby Demo showing the same thing Aaron showed in Python in that article:
keyis 65 bytes, one byte larger than the block size of SHA1. If you pass in either the 65 byte key, or the SHA1 hash of that key, you get the same HMAC result.looks good to me! thanks for putting that together @grempe
@zzak nice to see this in the plan for 2.0, thanks for taking it seriously