Skip to content

Commit

Permalink
feat: support session cookie sameSite options (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 1, 2020
1 parent f81d713 commit cb09a09
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const CONFIG = {
signed: true, /** (boolean) signed or not (default true) */
rolling: false, /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */
renew: false, /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/
sameSite: '', /** (string) session cookie sameSite options (default '', don't set it) */
};

app.use(session(CONFIG, app));
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function formatOpts(opts) {
// defaults
if (opts.overwrite == null) opts.overwrite = true;
if (opts.httpOnly == null) opts.httpOnly = true;
if (opts.sameSite == null) opts.sameSite = '';
if (opts.signed == null) opts.signed = true;
if (opts.autoCommit == null) opts.autoCommit = true;

Expand Down
45 changes: 45 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ describe('Koa Session Cookie', () => {
.expect(204, (err, res) => {
if (err) return done(err);
const cookie = res.headers['set-cookie'];
// samesite is not set
assert(!cookie.join(';').includes('samesite'));
request(server)
.get('/')
.set('Cookie', cookie.join(';'))
Expand Down Expand Up @@ -244,6 +246,11 @@ describe('Koa Session Cookie', () => {
.get('/')
.set('Cookie', cookie)
.expect('Set-Cookie', /koa:sess/)
.expect(res => {
const cookie = res.headers['set-cookie'];
// samesite is not set
assert(!cookie.join(';').includes('samesite'));
})
.expect(200, done);
});
});
Expand Down Expand Up @@ -694,6 +701,44 @@ describe('Koa Session Cookie', () => {
});
});

describe('options.sameSite', () => {
it('should return opt.sameSite=none', done => {
const app = App({ sameSite: 'none' });

app.use(async function(ctx) {
ctx.session = { foo: 'bar' };
ctx.body = ctx.session.foo;
});

request(app.listen())
.get('/')
.expect(res => {
const cookie = res.headers['set-cookie'].join('|');
assert(cookie.includes('path=/; samesite=none; httponly'));
})
.expect('bar')
.expect(200, done);
});

it('should return opt.sameSite=lax', done => {
const app = App({ sameSite: 'lax' });

app.use(async function(ctx) {
ctx.session = { foo: 'bar' };
ctx.body = ctx.session.foo;
});

request(app.listen())
.get('/')
.expect(res => {
const cookie = res.headers['set-cookie'].join('|');
assert(cookie.includes('path=/; samesite=lax; httponly'));
})
.expect('bar')
.expect(200, done);
});
});

describe('when valid and beforeSave set', () => {
it('should ignore session when uid changed', done => {
const app = new Koa();
Expand Down

0 comments on commit cb09a09

Please sign in to comment.