I make session cookies of form session_id+'|'+hmac_sha256(session_id, static_secret)
where session_id = random_string(16 bytes)
made with a PRNG (not a CSPRNG!) seeded on the server's timestamp at start-up.
The cookies have the Secure
flag so transport security is left to TLS to solve.
The session id is authenticated statelessly (i.e. without the need for a session database) the same way it was created, i.e. with hmac_sha256(session_id, static_secret)
.
Is this breakable? If so, how?
Notes from brynk's answer:
A revocation list would have to be queried as a second authentication step to allow for killing sessions on stolen devices. This will make the session authentication stateful (though the list would likely be short).
The secret has to have 256bit+ of entropy if using HMAC-SHA256. Your cat should be able to handle that in 2-3 walks over the keyboard.
Using a PRNG for session id as opposed to say, an auto-incremented id does not make the cookie more secure. The reason I prefer it is because 1) it's stateless (don't have to keep a
next_id
file around), and 2) it hides the information of knowing how many sessions were generated before yours.
hmac_sha256(auto_increment_number, static_secret)
, which I think it's just as secure, except for the minor information leak of knowing how many sessions were generated before yours. Besides, you can always just hmac the seed with the same secret.sessid = get_secure_random(32)
? Stateless implies the client is holding additional session metadata, or that this is the key to some stored state. Please feel free to edit your answer/ elaborate. From an 'is this breakable' perspective: it apparently rests on the entropy introduced bystatic_secret
. See: auth0.com/docs/tokens and csrc.nist.gov/publications/detail/sp/800-90a/rev-1/final