Reputation: 770
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.
Upvotes: 34
Views: 21292
Reputation: 53665
.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):
SESSION_SECRET=This is my funky secret oh my god it has ninja turtles
app.js:
const dotenv = require("dotenv");
dotenv.config();
const express = require("express");
const app = express();
const compression = require("compression");
app.use(compression({ filter: shouldCompress }));
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const cookieParser = require("cookie-parser");
app.use(cookieParser());
const cookieSession = require("cookie-session");
app.use(
cookieSession({
name: "session",
keys: [process.env.SESSION_SECRET],
maxAge: 2678400000, // 31 days
})
);
const helmet = require("helmet");
app.use(helmet());
app.listen(...)
Where helmet
takes care of the kind of security settings that none of us can remember (cors, csp, etc)
Upvotes: 44
Reputation: 1488
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.
Upvotes: 13
Reputation: 770
My confusion was between server-side sessions and client-side sessions. Before today, I had not known about client-side. A clear explanation of the difference is found below.
Why CherryPy session does not require a secret key?
Thinking of the server-side model, I was very confused where encryption would be required in sessions.
Upvotes: 3