Skip to content

Commit

Permalink
Merge pull request #129 from kinde-oss/fix/cookie-security
Browse files Browse the repository at this point in the history
fix: Cookie settings and code clean
  • Loading branch information
DaveOrDead authored Feb 22, 2024
2 parents a0a36ea + 5f6ef62 commit 998ab75
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
75 changes: 37 additions & 38 deletions src/session/sessionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ import {config} from '../config/index';

var cookie = require('cookie');

export const GLOBAL_COOKIE_OPTIONS = {
sameSite: 'lax',
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
path: '/',
}

const COOKIE_LIST = [
'id_token_payload',
'id_token',
'access_token_payload',
'access_token',
'user',
'refresh_token',
'post_login_redirect_url'
]

/**
*
* @param {import('next').NextApiRequest} [req]
Expand Down Expand Up @@ -54,7 +71,10 @@ export const appRouterSessionManager = (cookieStore) => ({
cookieStore.set(
itemKey,
typeof itemValue === 'object' ? JSON.stringify(itemValue) : itemValue,
{domain: config.cookieDomain ? config.cookieDomain : undefined}
{
domain: config.cookieDomain ? config.cookieDomain : undefined,
...GLOBAL_COOKIE_OPTIONS
}
);
}
},
Expand All @@ -66,25 +86,19 @@ export const appRouterSessionManager = (cookieStore) => ({
removeSessionItem: (itemKey) => {
cookieStore.set(itemKey, '', {
domain: config.cookieDomain ? config.cookieDomain : undefined,
maxAge: 0
maxAge: 0,
...GLOBAL_COOKIE_OPTIONS
});
},
/**
* @returns {Promise<void>}
*/
destroySession: () => {
[
'id_token_payload',
'id_token',
'access_token_payload',
'access_token',
'user',
'refresh_token',
'post_login_redirect_url'
].forEach((name) =>
COOKIE_LIST.forEach((name) =>
cookieStore.set(name, '', {
domain: config.cookieDomain ? config.cookieDomain : undefined,
maxAge: 0
maxAge: 0,
...GLOBAL_COOKIE_OPTIONS
})
);
}
Expand Down Expand Up @@ -137,7 +151,7 @@ export const pageRouterSessionManager = (req, res) => {
typeof itemValue === 'object' ? JSON.stringify(itemValue) : itemValue,
{
domain: config.cookieDomain ? config.cookieDomain : undefined,
path: '/'
...GLOBAL_COOKIE_OPTIONS
}
)
]);
Expand All @@ -151,52 +165,37 @@ export const pageRouterSessionManager = (req, res) => {
res?.setHeader('Set-Cookie', [
cookie.serialize(itemKey, '', {
domain: config.cookieDomain ? config.cookieDomain : undefined,
path: '/',
maxAge: -1
maxAge: -1,
...GLOBAL_COOKIE_OPTIONS
})
]);

// remove cookies from the root domain
res?.setHeader('Set-Cookie', [
cookie.serialize(itemKey, '', {
path: '/',
maxAge: -1
maxAge: -1,
...GLOBAL_COOKIE_OPTIONS
})
]);
},
destroySession: () => {
res?.setHeader('Set-Cookie', [
...[
'id_token_payload',
'id_token',
'access_token_payload',
'access_token',
'user',
'refresh_token',
'post_login_redirect_url'
].map((name) =>
...COOKIE_LIST.map((name) =>
cookie.serialize(name, '', {
domain: config.cookieDomain ? config.cookieDomain : undefined,
path: '/',
maxAge: -1
maxAge: -1,
...GLOBAL_COOKIE_OPTIONS
})
)
]);

// remove cookies from the root domain
res?.setHeader('Set-Cookie', [
...[
'id_token_payload',
'id_token',
'access_token_payload',
'access_token',
'user',
'refresh_token',
'post_login_redirect_url'
].map((name) =>
...COOKIE_LIST.map((name) =>
cookie.serialize(name, '', {
path: '/',
maxAge: -1
maxAge: -1,
...GLOBAL_COOKIE_OPTIONS
})
)
]);
Expand Down
6 changes: 3 additions & 3 deletions src/utils/appRouter/setVerifierCookie.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {config} from '../../config/index';
import {cookies} from 'next/headers';
import { GLOBAL_COOKIE_OPTIONS } from '../../session/sessionManager';

export const setVerifierCookie = (state, code_verifier, options) => {
cookies().set({
name: `${config.SESSION_PREFIX}-${state}`,
value: JSON.stringify({code_verifier, options}),
httpOnly: true,
path: '/',
maxAge: 60 * 15
maxAge: 60 * 15,
...GLOBAL_COOKIE_OPTIONS
});
};
6 changes: 4 additions & 2 deletions src/utils/pageRouter/setVerifierCookie.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {config} from '../../config/index';
import { GLOBAL_COOKIE_OPTIONS } from '../../session/sessionManager';

const cookie = require('cookie');

export const setVerifierCookie = (state, code_verifier, res, options) => {
Expand All @@ -10,8 +12,8 @@ export const setVerifierCookie = (state, code_verifier, res, options) => {
res.setHeader(
'Set-Cookie',
cookie.serialize(`${config.SESSION_PREFIX}-${state}`, jsonCookieValue, {
httpOnly: true,
maxAge: 60 * 15
maxAge: 60 * 15,
...GLOBAL_COOKIE_OPTIONS
})
);
return state;
Expand Down

0 comments on commit 998ab75

Please sign in to comment.