Skip to content

Commit

Permalink
fix: RFC6265 default cookie name
Browse files Browse the repository at this point in the history
  • Loading branch information
zacanger committed Apr 23, 2020
1 parent 17b3280 commit d34fc8e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
*.log
package-lock.json
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const app = new Koa();
app.keys = ['some secret hurr'];

const CONFIG = {
key: 'koa:sess', /** (string) cookie key (default is koa:sess) */
key: 'koa.sess', /** (string) cookie key (default is koa.sess) */
/** (number || 'session') maxAge in ms (default is 1 days) */
/** 'session' will result in a cookie that expires when session/browser is closed */
/** Warning: If a session cookie is stolen, this cookie will never expire */
Expand Down Expand Up @@ -84,7 +84,7 @@ console.log('listening on port 3000');
### Options

The cookie name is controlled by the `key` option, which defaults
to "koa:sess". All other options are passed to `ctx.cookies.get()` and
to "koa.sess". All other options are passed to `ctx.cookies.get()` and
`ctx.cookies.set()` allowing you to control security, domain, path,
and signing among other settings.

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const _CONTEXT_SESSION = Symbol('context#_contextSession');
/**
* Initialize session middleware with `opts`:
*
* - `key` session cookie name ["koa:sess"]
* - `key` session cookie name ["koa.sess"]
* - all other options are passed as cookie options
*
* @param {Object} [opts]
Expand Down Expand Up @@ -60,7 +60,7 @@ module.exports = function(opts, app) {
function formatOpts(opts) {
opts = opts || {};
// key
opts.key = opts.key || 'koa:sess';
opts.key = opts.key || 'koa.sess';

// back-compat maxage
if (!('maxAge' in opts)) opts.maxAge = opts.maxage;
Expand Down
40 changes: 20 additions & 20 deletions test/contextstore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(200, (err, res) => {
if (err) return done(err);
cookie = res.header['set-cookie'].join(';');
Expand All @@ -107,7 +107,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(200, (err, res) => {
if (err) return done(err);
cookie = res.header['set-cookie'].join(';');
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('Koa Session External Context Store', () => {
request(app.listen())
.get('/')
.set('Cookie', cookie)
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(200, done);
});
});
Expand All @@ -218,7 +218,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(200, done);
});
});
Expand Down Expand Up @@ -253,7 +253,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(200, done);
});
});
Expand Down Expand Up @@ -285,7 +285,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=.+;/)
.expect('Set-Cookie', /koa\.sess=.+;/)
.expect({ foo: 'bar' })
.expect(200, done);
});
Expand All @@ -302,7 +302,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=.+;/)
.expect('Set-Cookie', /koa\.sess=.+;/)
.expect('1')
.expect(200, done);
});
Expand All @@ -319,7 +319,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=.+;/)
.expect('Set-Cookie', /koa\.sess=.+;/)
.expect('true')
.expect(200, done);
});
Expand All @@ -336,7 +336,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=.+;/)
.expect('Set-Cookie', /koa\.sess=.+;/)
.expect('hello')
.expect(200, done);
});
Expand Down Expand Up @@ -371,7 +371,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(401, done);
});
});
Expand Down Expand Up @@ -415,7 +415,7 @@ describe('Koa Session External Context Store', () => {

request(server)
.post('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.end(err => {
if (err) return done(err);
})
Expand All @@ -441,7 +441,7 @@ describe('Koa Session External Context Store', () => {

request(server)
.post('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.end((err, res) => {
if (err) return done(err);
const cookie = res.headers['set-cookie'].join(';');
Expand All @@ -463,7 +463,7 @@ describe('Koa Session External Context Store', () => {

request(server)
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect('hi 1')
.end((err, res) => {
if (err) return done(err);
Expand All @@ -473,7 +473,7 @@ describe('Koa Session External Context Store', () => {
request(server)
.get('/')
.set('cookie', cookie)
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect('hi 2')
.end((err, res) => {
if (err) return done(err);
Expand All @@ -483,7 +483,7 @@ describe('Koa Session External Context Store', () => {
request(server)
.get('/')
.set('cookie', cookie)
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect('hi 3')
.end((err, res) => {
if (err) return done(err);
Expand All @@ -510,7 +510,7 @@ describe('Koa Session External Context Store', () => {

request(server)
.post('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.end((err, res) => {
if (err) return done(err);
const cookie = res.headers['set-cookie'].join(';');
Expand Down Expand Up @@ -539,7 +539,7 @@ describe('Koa Session External Context Store', () => {

request(server)
.post('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.end((err, res) => {
if (err) return done(err);
const cookie = res.headers['set-cookie'].join(';');
Expand Down Expand Up @@ -570,7 +570,7 @@ describe('Koa Session External Context Store', () => {

request(server)
.post('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.end((err, res) => {
if (err) return done(err);
const cookie = res.headers['set-cookie'].join(';');
Expand Down Expand Up @@ -655,7 +655,7 @@ describe('Koa Session External Context Store', () => {

request(app.listen())
.get('/')
.set('cookie', 'koa:sess=invalid-key')
.set('cookie', 'koa.sess=invalid-key')
.expect('true')
.expect(200, done);
});
Expand Down Expand Up @@ -693,7 +693,7 @@ describe('Koa Session External Context Store', () => {
should.not.exist(err);
const data = res.body;
const cookies = res.headers['set-cookie'].join(';');
cookies.should.containEql('koa:sess=');
cookies.should.containEql('koa.sess=');

request(app.callback())
.get('/')
Expand Down
40 changes: 20 additions & 20 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(200, (err, res) => {
if (err) return done(err);
cookie = res.header['set-cookie'].join(';');
Expand Down Expand Up @@ -245,7 +245,7 @@ describe('Koa Session Cookie', () => {
request(app.listen())
.get('/')
.set('Cookie', cookie)
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(res => {
const cookie = res.headers['set-cookie'];
// samesite is not set
Expand All @@ -270,8 +270,8 @@ describe('Koa Session Cookie', () => {

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('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);
});
Expand All @@ -291,7 +291,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=; path=\/; expires=Thu, 01 Jan 1970 00:00:00 GMT/)
.expect('Set-Cookie', /koa\.sess=; path=\/; expires=Thu, 01 Jan 1970 00:00:00 GMT/)
.expect('true')
.expect(200, done);
});
Expand All @@ -309,7 +309,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=;/)
.expect('Set-Cookie', /koa\.sess=;/)
.expect('true')
.expect(200, done);
});
Expand All @@ -326,7 +326,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.set('cookie', 'koa:sess=invalid-session;')
.set('cookie', 'koa.sess=invalid-session;')
.expect('true')
.expect(200, done);
});
Expand All @@ -347,8 +347,8 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.set('cookie', 'koa:sess=invalid-session;')
.expect('Set-Cookie', /koa:sess=;/)
.set('cookie', 'koa.sess=invalid-session;')
.expect('Set-Cookie', /koa\.sess=;/)
.expect(500, done);
});
});
Expand Down Expand Up @@ -390,7 +390,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=.+;/)
.expect('Set-Cookie', /koa\.sess=.+;/)
.expect({ foo: 'bar' })
.expect(200, done);
});
Expand All @@ -407,7 +407,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=.+;/)
.expect('Set-Cookie', /koa\.sess=.+;/)
.expect('1')
.expect(200, done);
});
Expand All @@ -424,7 +424,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=.+;/)
.expect('Set-Cookie', /koa\.sess=.+;/)
.expect('true')
.expect(200, done);
});
Expand All @@ -441,7 +441,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess=.+;/)
.expect('Set-Cookie', /koa\.sess=.+;/)
.expect('hello')
.expect(200, done);
});
Expand All @@ -460,7 +460,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(200, done);
});
});
Expand Down Expand Up @@ -495,7 +495,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(200, done);
});
});
Expand Down Expand Up @@ -543,7 +543,7 @@ describe('Koa Session Cookie', () => {

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.expect(401, done);
});
});
Expand All @@ -566,7 +566,7 @@ describe('Koa Session Cookie', () => {

request(server)
.post('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.end((err, res) => {
if (err) return done(err);
const cookie = res.headers['set-cookie'].join(';');
Expand Down Expand Up @@ -597,7 +597,7 @@ describe('Koa Session Cookie', () => {

request(server)
.post('/')
.expect('Set-Cookie', /koa:sess/)
.expect('Set-Cookie', /koa\.sess/)
.end((err, res) => {
if (err) return done(err);
const cookie = res.headers['set-cookie'].join(';');
Expand Down Expand Up @@ -691,7 +691,7 @@ describe('Koa Session Cookie', () => {
.expect(200, (err, res) => {
should.not.exist(err);
const cookies = res.headers['set-cookie'].join(';');
cookies.should.containEql('koa:sess=');
cookies.should.containEql('koa.sess=');

request(app.callback())
.get('/')
Expand Down Expand Up @@ -770,7 +770,7 @@ describe('Koa Session Cookie', () => {
should.not.exist(err);
const data = res.body;
const cookies = res.headers['set-cookie'].join(';');
cookies.should.containEql('koa:sess=');
cookies.should.containEql('koa.sess=');

request(app.callback())
.get('/')
Expand Down
Loading

0 comments on commit d34fc8e

Please sign in to comment.