Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#172: Correctly expire cookies for nullified sessions #180

Merged
merged 1 commit into from
Aug 23, 2019
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
8 changes: 7 additions & 1 deletion lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const debug = require('debug')('koa-session:context');
const Session = require('./session');
const util = require('./util');

const COOKIE_EXP_DATE = new Date(util.CookieDateEpoch);
const ONE_DAY = 24 * 60 * 60 * 1000;

class ContextSession {
Expand Down Expand Up @@ -273,7 +274,12 @@ class ContextSession {
*/

async remove() {
const opts = this.opts;
// Override the default options so that we can properly expire the session cookies
const opts = Object.assign({}, this.opts, {
expires: COOKIE_EXP_DATE,
maxAge: false,
});

const ctx = this.ctx;
const key = opts.key;
const externalKey = this.externalKey;
Expand Down
2 changes: 2 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ module.exports = {
hash(sess) {
return crc(JSON.stringify(sess));
},

CookieDateEpoch: 'Thu, 01 Jan 1970 00:00:00 GMT',
};
41 changes: 41 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,47 @@ describe('Koa Session Cookie', () => {
});
});

describe('after session set to null with signed cookie', () => {
it('should return expired cookies', done => {
const app = App({
signed: true,
});

app.use(async function(ctx) {
ctx.session.hello = {};
ctx.session = null;
ctx.body = String(ctx.session === null);
});

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=; path=\/; expires=Thu, 01 Jan 1970 00:00:00 GMT/)
.expect('Set-Cookie', /koa:sess.sig=(.*); path=\/; expires=Thu, 01 Jan 1970 00:00:00 GMT/)
.expect('true')
.expect(200, done);
});
});

describe('after session set to null without signed cookie', () => {
it('should return expired cookies', done => {
const app = App({
signed: false,
});

app.use(async function(ctx) {
ctx.session.hello = {};
ctx.session = null;
ctx.body = String(ctx.session === null);
});

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=; path=\/; expires=Thu, 01 Jan 1970 00:00:00 GMT/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have an assert to check if signed cookie is also set to expired?

.expect('true')
.expect(200, done);
});
});

describe('when get session after set to null', () => {
it('should return null', done => {
const app = App();
Expand Down