Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

module.exports = function(app) {
if (!app.config.session.httpOnly) {
app.logger.warn('[egg-session]: please set `config.session.httpOnly` to true. It is very dangerous if session can read by client JavaScript.');
}
app.config.coreMiddleware.push('session');

// listen on session's events
Expand Down
9 changes: 9 additions & 0 deletions test/app/middleware/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ describe('test/app/middlewares/session.test.js', () => {
});
});

describe('httpOnly', () => {
it('should warn when httponly false', function* () {
app = mm.app({ baseDir: 'httponly-false-session' });
yield app.ready();
app.expectLog('[egg-session]: please set `config.session.httpOnly` to true. It is very dangerous if session can read by client JavaScript.');
yield app.close();
});
});

[
'cookie-session',
'memory-session',
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/httponly-false-session/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

exports.get = function* (ctx) {
ctx.body = ctx.session;
};

exports.set = function* (ctx) {
ctx.session = ctx.query;
ctx.body = ctx.session;
};

exports.setKey = function* (ctx) {
ctx.session.key = ctx.query.key;
ctx.body = ctx.session;
};

exports.remove = function* (ctx) {
ctx.session = null;
ctx.body = ctx.session;
};

exports.maxAge = function* (ctx) {
ctx.session.maxAge = Number(this.query.maxAge);
ctx.body = ctx.session;
};
9 changes: 9 additions & 0 deletions test/fixtures/httponly-false-session/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function(app) {
app.get('/get', 'home.get');
app.get('/set', 'home.set');
app.get('/setKey', 'home.setKey');
app.get('/remove', 'home.remove');
app.get('/maxAge', 'home.maxAge');
};
6 changes: 6 additions & 0 deletions test/fixtures/httponly-false-session/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

exports.keys = 'keys';
exports.session = {
httpOnly: false,
};
3 changes: 3 additions & 0 deletions test/fixtures/httponly-false-session/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "httponly-false-session"
}