Skip to content

Commit 4541888

Browse files
Adding work to resolve issue 172: If a user decides to use signed cookies, only one of the 'koa:sess' cookies are correctly destroyed by the browser when the data is set to 'null'. This change fixes that by ensuring that the maxAge is always set when destroying the cookie, and the 'koa:sess.sig' cookies are also destroyed.
Changes: - On destroy, set 'maxAge' to be false - On destroy, set the 'expires' flag to be UNIXTIME epoch, which the 'Cookie' module relies on - Added test case for the cookie time being set
1 parent 10bb122 commit 4541888

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/context.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const debug = require('debug')('koa-session:context');
44
const Session = require('./session');
55
const util = require('./util');
66

7+
const COOKIE_EXP_DATE = 'Thu, 01 Jan 1970 00:00:00 GMT';
78
const ONE_DAY = 24 * 60 * 60 * 1000;
89

910
class ContextSession {
@@ -273,7 +274,12 @@ class ContextSession {
273274
*/
274275

275276
async remove() {
276-
const opts = this.opts;
277+
// Override the default options so that we can properly expire the session cookies
278+
const opts = Object.assign({}, this.opts, {
279+
expires: new Date(COOKIE_EXP_DATE),
280+
maxAge: false,
281+
});
282+
277283
const ctx = this.ctx;
278284
const key = opts.key;
279285
const externalKey = this.externalKey;

test/cookie.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,24 @@ describe('Koa Session Cookie', () => {
249249
});
250250
});
251251

252+
describe('after session set to null', () => {
253+
it('should return expired cookies', done => {
254+
const app = App();
255+
256+
app.use(async function(ctx) {
257+
ctx.session.hello = {};
258+
ctx.session = null;
259+
ctx.body = String(ctx.session === null);
260+
});
261+
262+
request(app.listen())
263+
.get('/')
264+
.expect('Set-Cookie', /koa:sess=; path=\/; expires=Thu, 01 Jan 1970 00:00:00 GMT/)
265+
.expect('true')
266+
.expect(200, done);
267+
});
268+
});
269+
252270
describe('when get session after set to null', () => {
253271
it('should return null', done => {
254272
const app = App();

0 commit comments

Comments
 (0)