Skip to content

Commit 3607d4c

Browse files
authored
Adds ability to set the cookie session secret (#774)
* Adds ability to set the cookie session secret * nits
1 parent 1ede97c commit 3607d4c

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Parse-Dashboard/Authentication.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function Authentication(validUsers, useEncryptedPasswords, mountPath) {
1717
this.mountPath = mountPath;
1818
}
1919

20-
function initialize(app) {
20+
function initialize(app, options) {
21+
options = options || {};
2122
var self = this;
2223
passport.use('local', new LocalStrategy(
2324
function(username, password, cb) {
@@ -43,11 +44,12 @@ function initialize(app) {
4344
cb(null, user);
4445
});
4546

47+
var cookieSessionSecret = options.cookieSessionSecret || require('crypto').randomBytes(64).toString('hex');
4648
app.use(require('connect-flash')());
4749
app.use(require('body-parser').urlencoded({ extended: true }));
4850
app.use(require('cookie-session')({
4951
key : 'parse_dash',
50-
secret : 'magic',
52+
secret : cookieSessionSecret,
5153
cookie : {
5254
maxAge: (2 * 7 * 24 * 60 * 60 * 1000) // 2 weeks
5355
}

Parse-Dashboard/app.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function checkIfIconsExistForApps(apps, iconsFolder) {
4646
}
4747
}
4848

49-
module.exports = function(config, allowInsecureHTTP) {
49+
module.exports = function(config, options) {
50+
options = options || {};
5051
var app = express();
5152
// Serve public files.
5253
app.use(express.static(path.join(__dirname,'public')));
@@ -62,7 +63,7 @@ module.exports = function(config, allowInsecureHTTP) {
6263
const users = config.users;
6364
const useEncryptedPasswords = config.useEncryptedPasswords ? true : false;
6465
const authInstance = new Authentication(users, useEncryptedPasswords, mountPath);
65-
authInstance.initialize(app);
66+
authInstance.initialize(app, { cookieSessionSecret: options.cookieSessionSecret });
6667

6768
// CSRF error handler
6869
app.use(function (err, req, res, next) {
@@ -86,7 +87,7 @@ module.exports = function(config, allowInsecureHTTP) {
8687
req.connection.remoteAddress === '127.0.0.1' ||
8788
req.connection.remoteAddress === '::ffff:127.0.0.1' ||
8889
req.connection.remoteAddress === '::1';
89-
if (!requestIsLocal && !req.secure && !allowInsecureHTTP) {
90+
if (!requestIsLocal && !req.secure && !options.allowInsecureHTTP) {
9091
//Disallow HTTP requests except on localhost, to prevent the master key from being transmitted in cleartext
9192
return res.send({ success: false, error: 'Parse Dashboard can only be remotely accessed via HTTPS' });
9293
}

Parse-Dashboard/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ program.option('--allowInsecureHTTP [allowInsecureHTTP]', 'set this flag when yo
2525
program.option('--sslKey [sslKey]', 'the path to the SSL private key.');
2626
program.option('--sslCert [sslCert]', 'the path to the SSL certificate.');
2727
program.option('--trustProxy [trustProxy]', 'set this flag when you are behind a front-facing proxy, such as when hosting on Heroku. Uses X-Forwarded-* headers to determine the client\'s connection and IP address.');
28+
program.option('--cookieSessionSecret [cookieSessionSecret]', 'set the cookie session secret, defaults to a random string. You should set that value if you want sessions to work across multiple server, or across restarts');
2829

2930
program.parse(process.argv);
3031

3132
const host = program.host || process.env.HOST || '0.0.0.0';
3233
const port = program.port || process.env.PORT || 4040;
3334
const mountPath = program.mountPath || process.env.MOUNT_PATH || '/';
3435
const allowInsecureHTTP = program.allowInsecureHTTP || process.env.PARSE_DASHBOARD_ALLOW_INSECURE_HTTP;
36+
const cookieSessionSecret = program.cookieSessionSecret || process.env.PARSE_DASHBOARD_COOKIE_SESSION_SECRET;
3537
const trustProxy = program.trustProxy || process.env.PARSE_DASHBOARD_TRUST_PROXY;
3638

3739
if (trustProxy && allowInsecureHTTP) {
@@ -115,7 +117,8 @@ p.then(config => {
115117
if (allowInsecureHTTP || trustProxy) app.enable('trust proxy');
116118

117119
config.data.trustProxy = trustProxy;
118-
app.use(mountPath, parseDashboard(config.data, allowInsecureHTTP));
120+
let dashboardOptions = { allowInsecureHTTP: allowInsecureHTTP, cookieSessionSecret: cookieSessionSecret };
121+
app.use(mountPath, parseDashboard(config.data, dashboardOptions));
119122
if(!configSSLKey || !configSSLCert){
120123
// Start the server.
121124
const server = app.listen(port, host, function () {

0 commit comments

Comments
 (0)