I'm quite confused by the importance of a session secret. I'm jumping into web development with Express and Node, and at the moment, I'm trying to implement a simple login. The below code is taken from the sessions example in Express.
// Required by session() middleware // pass the secret for signed cookies // (required by session()) app.use(express.cookieParser('keyboard cat')); // Populates req.session app.use(express.session());
It uses "keyboard cat" as a session secret. Many of the things I've looked around about session secrets recommend me to change this to something custom. I now have 3 specific questions concerning this.
It's used to encrypt the session cookie so that you can be reasonably (but not 100%) sure the cookie isn't a fake one, and the connection should be treated as part of the larger session with express.
The session middleware handles all things for us, i.e., creating the session, setting the session cookie and creating the session object in req object. Whenever we make a request from the same client again, we will have their session information stored with us (given that the server was not restarted).
The secret should be a random string of characters. Ideally you would also change it periodically in case it has been discovered. However, this requires support for secret rotation so you don't immediately invalidate existing sessions. That is, two session secrets should be considered valid simultaneously.
A string of random alnum characters, say 24 to 32 bytes long, should do just fine.
.env
file with https://npmjs.org/package/dotenv, and make sure those files never touch your repository (svn/git exclusion/ignores) so that your secret data stays secret."I didn't think I needed a secret, but some rando on Stackoverflow told me Express needed one so here we are"
.How I use sessions:
.env file (always in my .gitignore file so it never hits my public repos):
SECRET="This is my funky secret oh my god it has ninja turtles"
app.js:
var express = require('express'), env = (function(){ var Habitat = require("habitat"); Habitat.load(); return new Habitat(); }()), app = express(); app.use(express.compress()); // gzip all the things. If possible. app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.cookieSession({ key: "mysite.sid", // seeing this tells you nothing about the actual secret: secret: env.get("SESSION_SECRET"), cookie: { maxAge: 2678400000 // 31 days } })); app.use(express.csrf());
That CSRF bit ensures that page requests are coming from your own site, not cURL requests or embeds in other people's websites. http://expressjs.com/api.html#csrf for more info on that.
I think that the major point is missed in the other answers, which is whether the secret
parameter is making session management more secure. it is discussed nicely in this Security.StackExchange question: Why is it insecure to store the session ID in a cookie directly?
I recommend reading it (not only the top voted answer there is relevant).
Trying to sum it up: it won't reduce segnificantly the chances of a session being guessed and hijacked in case that the session IDs are large random numbers, but it will obviously help greatly if the session IDs are custom like incrementing IDs, which is possible in ExpressJS.
Users can use whatever session IDs they want. Perhaps someone feels like they should use an auto incrementing number from the SQL database, it doesn't matter, because we protect their uninformed decision by signing the value, elongating the key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With