Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 2eb0b09

Browse files
committed
adding ability to configure session.secret in local env config
1 parent 7a9ee53 commit 2eb0b09

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

config/config.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@ var validateSecureMode = function (config) {
8787
}
8888
};
8989

90+
/**
91+
* Validate Session Secret parameter is not set to default in production
92+
*/
93+
var validateSessionSecret = function (config, testing) {
94+
95+
if (process.env.NODE_ENV !== 'production') {
96+
return true;
97+
}
98+
99+
if (config.sessionSecret === 'MEAN') {
100+
if (!testing) {
101+
console.log(chalk.red('+ WARNING: It is strongly recommended that you change sessionSecret config while running in production!'));
102+
console.log(chalk.red(' Please add `sessionSecret: process.env.SESSION_SECRET || \'super amazing secret\'` to '));
103+
console.log(chalk.red(' `config/env/production.js` or `config/env/local.js`'));
104+
console.log();
105+
}
106+
return false;
107+
} else {
108+
return true;
109+
}
110+
};
111+
90112
/**
91113
* Initialize global configuration files
92114
*/
@@ -169,7 +191,7 @@ var initGlobalConfig = function () {
169191
// production or development environment. If test environment is used we don't merge it with local.js
170192
// to avoid running test suites on a prod/dev environment (which delete records and make modifications)
171193
if (process.env.NODE_ENV !== 'test') {
172-
config = _.merge(config, (fs.existsSync(path.join(process.cwd(), 'config/env/local.js')) && require(path.join(process.cwd(), 'config/env/local.js'))) || {});
194+
config = _.merge(config, (fs.existsSync(path.join(process.cwd(), 'config/env/local.js')) && require(path.join(process.cwd(), 'config/env/local.js'))) || {});
173195
}
174196

175197
// Initialize global globbed files
@@ -181,9 +203,13 @@ var initGlobalConfig = function () {
181203
// Validate Secure SSL mode can be used
182204
validateSecureMode(config);
183205

206+
// Validate session secret
207+
validateSessionSecret(config);
208+
184209
// Expose configuration utilities
185210
config.utils = {
186-
getGlobbedPaths: getGlobbedPaths
211+
getGlobbedPaths: getGlobbedPaths,
212+
validateSessionSecret: validateSessionSecret
187213
};
188214

189215
return config;

config/env/default.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ module.exports = {
1414
// session expiration is set by default to 24 hours
1515
maxAge: 24 * (60 * 60 * 1000),
1616
// httpOnly flag makes sure the cookie is only accessed
17-
// through the HTTP protocol and not JS/browser
17+
// through the HTTP protocol and not JS/browser
1818
httpOnly: true,
1919
// secure cookie should be turned to true to provide additional
2020
// layer of security so that the cookie is set only when working
2121
// in HTTPS mode.
2222
secure: false
2323
},
2424
// sessionSecret should be changed for security measures and concerns
25-
sessionSecret: 'MEAN',
25+
sessionSecret: process.env.SESSION_SECRET || 'MEAN',
2626
// sessionKey is set to the generic sessionId key used by PHP applications
2727
// for obsecurity reasons
2828
sessionKey: 'sessionId',

config/env/local.example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
pass: ''
1515
}
1616
},
17+
sessionSecret: process.env.SESSION_SECRET || 'youshouldchangethistosomethingsecret',
1718
facebook: {
1819
clientID: process.env.FACEBOOK_ID || 'APP_ID',
1920
clientSecret: process.env.FACEBOOK_SECRET || 'APP_SECRET',

modules/core/tests/server/core.server.config.tests.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ var should = require('should'),
1010
config = require(path.resolve('./config/config')),
1111
seed = require(path.resolve('./config/lib/seed'));
1212

13-
describe('Configuration tests', function () {
13+
describe('Configuration Tests:', function () {
1414
this.timeout(10000);
1515

16-
describe('Testing default seedDB:', function () {
16+
describe('Testing default seedDB', function () {
1717
before(function(done) {
1818
User.remove(function(err) {
1919
should.not.exist(err);
@@ -118,7 +118,43 @@ describe('Configuration tests', function () {
118118
});
119119
});
120120
});
121-
122121
});
123122

123+
describe('Testing Session Secret Configuration', function () {
124+
it('should warn if using default session secret when running in production', function (done) {
125+
var conf = { sessionSecret: 'MEAN' };
126+
// set env to production for this test
127+
process.env.NODE_ENV = 'production';
128+
config.utils.validateSessionSecret(conf, true).should.equal(false);
129+
// set env back to test
130+
process.env.NODE_ENV = 'test';
131+
done();
132+
});
133+
134+
it('should accept non-default session secret when running in production', function (done) {
135+
var conf = { sessionSecret: 'super amazing secret' };
136+
// set env to production for this test
137+
process.env.NODE_ENV = 'production';
138+
config.utils.validateSessionSecret(conf, true).should.equal(true);
139+
// set env back to test
140+
process.env.NODE_ENV = 'test';
141+
done();
142+
});
143+
144+
it('should accept default session secret when running in development', function (done) {
145+
var conf = { sessionSecret: 'MEAN' };
146+
// set env to development for this test
147+
process.env.NODE_ENV = 'development';
148+
config.utils.validateSessionSecret(conf, true).should.equal(true);
149+
// set env back to test
150+
process.env.NODE_ENV = 'test';
151+
done();
152+
});
153+
154+
it('should accept default session secret when running in test', function (done) {
155+
var conf = { sessionSecret: 'MEAN' };
156+
config.utils.validateSessionSecret(conf, true).should.equal(true);
157+
done();
158+
});
159+
});
124160
});

0 commit comments

Comments
 (0)