diff --git a/README.md b/README.md index 2dd108c..bee5196 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,12 @@ Once you use external session store, session is strong dependent on your externa Support all configurations in [koa-session](https://github.com/koajs/session). +* logValue + +``` +Support not to print the session value when session event trigger log. Default to be true. +``` + [View the default configurations](https://github.com/eggjs/egg-session/blob/master/config/config.default.js) ## Questions & Suggestions diff --git a/app.js b/app.js index 91da4b6..d40c00a 100644 --- a/app.js +++ b/app.js @@ -11,9 +11,9 @@ module.exports = function(app) { ctx.coreLogger.warn('[session][missed] key(%s)', key); }); app.on('session:expired', ({ ctx, key, value }) => { - ctx.coreLogger.warn('[session][expired] key(%s) value(%j)', key, value); + ctx.coreLogger.warn('[session][expired] key(%s) value(%j)', key, app.config.session.logValue ? value : ''); }); app.on('session:invalid', ({ ctx, key, value }) => { - ctx.coreLogger.warn('[session][invalid] key(%s) value(%j)', key, value); + ctx.coreLogger.warn('[session][invalid] key(%s) value(%j)', key, app.config.session.logValue ? value : ''); }); }; diff --git a/config/config.default.js b/config/config.default.js index fca7223..f445487 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -6,4 +6,5 @@ exports.session = { httpOnly: true, encrypt: true, // sameSite: null, + logValue: true, }; diff --git a/test/app/middleware/session.test.js b/test/app/middleware/session.test.js index 6b49df1..82f8969 100644 --- a/test/app/middleware/session.test.js +++ b/test/app/middleware/session.test.js @@ -92,6 +92,83 @@ describe('test/app/middlewares/session.test.js', () => { }); }); + describe('logValue', () => { + before(() => { + app = mm.app({ baseDir: 'logValue-false-session' }); + return app.ready(); + }); + beforeEach(() => { + agent = request.agent(app.callback()); + app.mockLog(); + }); + after(() => app.close()); + + it('when logValue is true, should log the session value', async () => { + let cookie; + app.mockLog(); + mm(app.config.session, 'logValue', true); + + await agent + .get('/maxAge?maxAge=100') + .expect(200) + .expect(res => { + cookie = res.headers['set-cookie'].join(';'); + }); + + await sleep(200); + + await request(app.callback()) + .get('/get') + .set('cookie', cookie) + .expect(200) + .expect({}); + app.notExpectLog('[session][expired] key(undefined) value("")', 'coreLogger'); + }); + + it('when logValue is false, should not log the session value', async () => { + mm(app.config.session, 'logValue', false); + app.mockLog(); + let cookie; + + await agent + .get('/maxAge?maxAge=100') + .expect(200) + .expect(res => { + cookie = res.headers['set-cookie'].join(';'); + }); + + await sleep(200); + + await request(app.callback()) + .get('/get') + .set('cookie', cookie) + .expect(200) + .expect({}); + + await sleep(1000); + + app.expectLog('[session][expired] key(undefined) value("")', 'coreLogger'); + }); + + it.only('when logValue is false, valid false, should not log the session value', async () => { + mm(app.config.session, 'logValue', false); + mm(app.config.session, 'valid', () => false); + app.mockLog(); + + await agent + .get('/set?foo=bar') + .expect(200) + .expect({ foo: 'bar' }); + + await agent + .get('/get'); + + await sleep(1000); + + app.expectLog('[session][invalid] key(undefined) value("")', 'coreLogger'); + }); + }); + describe('session maxage', () => { before(() => { app = mm.app({ baseDir: 'session-maxage-session' }); diff --git a/test/fixtures/logValue-false-session/app/controller/home.js b/test/fixtures/logValue-false-session/app/controller/home.js new file mode 100644 index 0000000..b122fac --- /dev/null +++ b/test/fixtures/logValue-false-session/app/controller/home.js @@ -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; +}; diff --git a/test/fixtures/logValue-false-session/app/router.js b/test/fixtures/logValue-false-session/app/router.js new file mode 100644 index 0000000..d5938ed --- /dev/null +++ b/test/fixtures/logValue-false-session/app/router.js @@ -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'); +}; diff --git a/test/fixtures/logValue-false-session/config/config.default.js b/test/fixtures/logValue-false-session/config/config.default.js new file mode 100644 index 0000000..7aee4cf --- /dev/null +++ b/test/fixtures/logValue-false-session/config/config.default.js @@ -0,0 +1,6 @@ +'use strict'; + +exports.keys = 'keys'; +exports.session = { + logValue: false, +}; diff --git a/test/fixtures/logValue-false-session/package.json b/test/fixtures/logValue-false-session/package.json new file mode 100644 index 0000000..217cbd6 --- /dev/null +++ b/test/fixtures/logValue-false-session/package.json @@ -0,0 +1,3 @@ +{ + "name": "logvalue-false-session" +}