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:

  1. Show code examples sourcing secrets from environment variables
  2. 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

Most upvoted comments

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.fetch which will provide a safe fallback if the specified ENV var is unavailable. If you use ENV['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

The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.

Option 1

Throws an exception if SESSION_SECRET does not exist in the current ENV. Use this if you want to fail fast if this critical setting is missing.

set :session_secret, ENV.fetch('SESSION_SECRET')

Option 2

Generate a secure value for SESSION_SECRET if its not set in the current env.

# fail safe to a secure hex value if `SESSION_SECRET` is unset
require 'securerandom'
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) }

Towards Stronger Keys

I would also recommend using the Sysrandom gem 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.

# gem install sysrandom

# Replace the userspace Ruby (OpenSSL) RNG with `/dev/urandom`
require 'sysrandom/securerandom'

# works with the same API, calls `/dev/urandom`
# or an appropriate OS kernel alternative
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) }

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

So what does this mean? It means that when choosing your HMAC keys, you should stay within one block size of bytes- 64 bytes or less for MD5, RIPEMD-128/160, SHA-1, SHA-224, SHA-256, and 128-bytes or less for SHA-384 and SHA-512. If you do this, you’ll be fine.

Bonus Points

Just for kicks, here’s Ruby Demo showing the same thing Aaron showed in Python in that article:

key is 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.

[33] pry(main)> key = SecureRandom.random_bytes(65)
=> "..."
[34] pry(main)> msg = SecureRandom.random_bytes(256)
=> "..."
[35] pry(main)> OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, key, msg)
=> "2875f708782f71e1bcf23cfb088cdd85155f1773"
[36] pry(main)> OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, OpenSSL::Digest::SHA1.new(key).digest, msg)
=> "2875f708782f71e1bcf23cfb088cdd85155f1773"

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