Related to this other question about sharing the secret to sign the cookies, my question is more trivial: how really secure is to sign a simple cookie identifier?

In example, expressjs#session library requires a secret, but also says at the begining of the docs:

Session data is not saved in the cookie itself, just the session ID. Session data is stored server-side.

But I don't see any benefit to create an HMAC of an identifier, because this value will be anyway a bearer token for server state.


I think that an HMAC provides little added security if the session ID is something that is hard to guess. For example, a long secure PRNG. But if the session ID is simple to guess then there is the risk of session hijacking.

As the expressjs framework allows you to provide a session ID creation function that returns IDs with unknown security properties, an HMAC seems like a good strategy.

The default uses the uid2 library which looks like (I'm not a NodeJS user so I'm not certain) a secure PRNG (see source code). An HMAC seems of less value in this case.

  • 1
    Just to give an example, suppose the server's token generator is just a counter. If you have token 17, you can be pretty sure 16 and 18 are valid tokens, too. If an attacker needs a MAC of that token, though, then knowing that 16 is a valid token doesn't help (because the token is really 16 and HMAC(16, secret_key)).
    – apsillers
    May 18 '15 at 18:30
  • Then, the only reason, is the "guessing" possibility, and if the session ID is hard enough, signing is useless. I'm right?
    – lucasvc
    May 25 '15 at 6:26
  • 1
    Yes @datakey, if the session is crypto secure the HMAC seems unnecessary. May 25 '15 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.