Skip to content

Commit 3599fb7

Browse files
committed
Automatically generate a session secret if default is used
The session secret is used to sign and authenticate the session cookie and this way very important for the authentication process. By default the session secret is set to `secret` and never changes. This commit will add a generator for a dynamic session secret if it stays unchanged. It prevents session hijacking this way and will warn the user about the missing secret. This also implies that on a restart without configured session secret will log out all users. While it may seems annoying, it's for the users best. Signed-off-by: Sheogorath <[email protected]>
1 parent 57c47a6 commit 3599fb7

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/config/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module.exports = {
4646
// session
4747
sessionName: 'connect.sid',
4848
sessionSecret: 'secret',
49+
sessionSecretLen: 128,
4950
sessionLife: 14 * 24 * 60 * 60 * 1000, // 14 days
5051
staticCacheTime: 1 * 24 * 60 * 60 * 1000, // 1 day
5152
// socket.io

lib/config/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
'use strict'
33

4+
const crypto = require('crypto')
45
const fs = require('fs')
56
const path = require('path')
67
const {merge} = require('lodash')
@@ -117,6 +118,14 @@ for (let i = keys.length; i--;) {
117118
}
118119
}
119120

121+
// Generate session secret if it stays on default values
122+
if (config.sessionSecret === 'secret') {
123+
logger.warn('Session secret not set. Using random generated one. Please set `sessionSecret` in your config.js file. All users will be logged out.')
124+
config.sessionSecret = crypto.randomBytes(Math.ceil(config.sessionSecretLen / 2)) // generate crypto graphic random number
125+
.toString('hex') // convert to hexadecimal format
126+
.slice(0, config.sessionSecretLen) // return required number of characters
127+
}
128+
120129
// Validate upload upload providers
121130
if (['filesystem', 's3', 'minio', 'imgur'].indexOf(config.imageUploadType) === -1) {
122131
logger.error('"imageuploadtype" is not correctly set. Please use "filesystem", "s3", "minio" or "imgur". Defaulting to "imgur"')

0 commit comments

Comments
 (0)