Skip to content

Commit

Permalink
feat: set default sameSite cookie values, short: lax, long: none
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed May 10, 2019
1 parent a056bfd commit cfb1a70
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
6 changes: 4 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,8 @@ _**default value**_:
{
httpOnly: true,
maxAge: 1209600000,
overwrite: true
overwrite: true,
sameSite: 'none'
}
```

Expand Down Expand Up @@ -1708,7 +1709,8 @@ _**default value**_:
{
httpOnly: true,
maxAge: 600000,
overwrite: true
overwrite: true,
sameSite: 'lax'
}
```

Expand Down
13 changes: 13 additions & 0 deletions lib/helpers/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ module.exports = class Configuration {
this.ensureMaps();
this.ensureSets();

// TODO: revert this back to its original state once
// https://github.com/pillarjs/cookies/issues/109 lands
this.removeCookiesSameSiteNone();

// cookies
// featuresValidations
// routesValidations
Expand Down Expand Up @@ -417,6 +421,15 @@ module.exports = class Configuration {
}
}

removeCookiesSameSiteNone() {
if (/^none$/g.test(this.cookies.short.sameSite)) {
this.cookies.short.sameSite = undefined;
}
if (/^none$/g.test(this.cookies.long.sameSite)) {
this.cookies.long.sameSite = undefined;
}
}

logDraftNotice() {
const ENABLED_DRAFTS = new Set();
let throwDraft = false;
Expand Down
2 changes: 2 additions & 0 deletions lib/helpers/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ const DEFAULTS = {
httpOnly: true, // cookies are not readable by client-side javascript
maxAge: (14 * 24 * 60 * 60) * 1000, // 14 days in ms
overwrite: true,
sameSite: 'none',
},

/*
Expand All @@ -202,6 +203,7 @@ const DEFAULTS = {
httpOnly: true, // cookies are not readable by client-side javascript
maxAge: (10 * 60) * 1000, // 10 minutes in ms
overwrite: true,
sameSite: 'lax',
},

/*
Expand Down
16 changes: 11 additions & 5 deletions lib/shared/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,25 @@ module.exports = async function sessionHandler(ctx, next) {
await next();
} finally {
const sessionCookieName = ctx.oidc.provider.cookieName('session');
const stateCookieName = ctx.oidc.provider.cookieName('state');
const longRegexp = new RegExp(`^(${sessionCookieName}|${stateCookieName}\\.[^=]+)(?:\\.sig)?=`);

// refresh the session duration
if ((!ctx.oidc.session.new || ctx.oidc.session.touched) && !ctx.oidc.session.destroyed) {
ctx.cookies.set(sessionCookieName, ctx.oidc.session.id, instance(ctx.oidc.provider).configuration('cookies.long'));
await ctx.oidc.session.save();
}

if (ctx.oidc.session.transient) {
const stateCookieName = ctx.oidc.provider.cookieName('state');

// TODO: revert this back to its original state once
// https://github.com/pillarjs/cookies/issues/109 lands
if (ctx.response.get('set-cookie')) {
ctx.response.get('set-cookie').forEach((cookie, index, ary) => {
const isLong = cookie.startsWith(sessionCookieName) || cookie.startsWith(stateCookieName);
if (isLong && !cookie.includes('expires=Thu, 01 Jan 1970')) {
if (!cookie.includes('samesite=')) {
ary[index] = cookie = `${cookie}; samesite=none`; // eslint-disable-line no-param-reassign, no-multi-assign
}

const isLong = cookie.match(longRegexp);
if (isLong && ctx.oidc.session.transient && cookie.includes('expires=') && !cookie.includes('expires=Thu, 01 Jan 1970')) {

This comment has been minimized.

Copy link
@panva

panva May 11, 2019

Author Owner

TODO put the match as .test directly in the if condition

ary[index] = cookie.replace(/(; ?expires=([\w\d:, ]+))/, ''); // eslint-disable-line no-param-reassign
}
});
Expand Down
8 changes: 4 additions & 4 deletions test/session_management/end_session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('[session_management]', () => {
.type('form')
.expect(302)
.expect((response) => {
expect(response.headers['set-cookie']).to.contain('_state.client=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly');
expect(response.headers['set-cookie']).to.contain('_state.client=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly; samesite=none');
expect(adapter.destroy.called).to.be.true;
expect(adapter.upsert.called).not.to.be.true;
expect(adapter.destroy.withArgs(sessionId).calledOnce).to.be.true;
Expand All @@ -71,7 +71,7 @@ describe('[session_management]', () => {
.expect(302)
.expect((response) => {
session = this.getSession();
expect(response.headers['set-cookie']).to.contain('_state.client=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly');
expect(response.headers['set-cookie']).to.contain('_state.client=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly; samesite=none');
expect(session.authorizations.client).to.be.undefined;
expect(session.state).to.be.undefined;
expect(this.getSessionId()).not.to.eql(oldId);
Expand All @@ -92,7 +92,7 @@ describe('[session_management]', () => {
})
.expect(302)
.expect((response) => {
expect(response.headers['set-cookie']).to.contain('_state.client=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=.oidc.dev; httponly');
expect(response.headers['set-cookie']).to.contain('_state.client=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=.oidc.dev; httponly; samesite=none');
});
});

Expand All @@ -112,7 +112,7 @@ describe('[session_management]', () => {
.expect(302)
.expect('location', '/?state=foobar')
.expect((response) => {
expect(response.headers['set-cookie']).to.contain('_state.client=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=.oidc.dev; httponly');
expect(response.headers['set-cookie']).to.contain('_state.client=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=.oidc.dev; httponly; samesite=none');
});
});

Expand Down

0 comments on commit cfb1a70

Please sign in to comment.