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.

1 Answer 1


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, 2015 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, 2015 at 6:26
  • 1
    Yes @datakey, if the session is crypto secure the HMAC seems unnecessary. May 25, 2015 at 21:21
  • Alternatively, instead of uid2, the built-in crypto module can be used with e.g. crypto.randomBytes(64).toString("base64") to generate crypto random data
    – undefined
    Dec 19, 2021 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .