-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chunked cookies should not exceed browser max #237
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ const { encryption: deriveKey } = require('./hkdf'); | |
const debug = require('./debug')('appSession'); | ||
|
||
const epoch = () => (Date.now() / 1000) | 0; | ||
const CHUNK_BYTE_SIZE = 4000; | ||
const MAX_COOKIE_SIZE = 4096; | ||
|
||
function attachSessionObject(req, sessionName, value) { | ||
Object.defineProperty(req, sessionName, { | ||
|
@@ -49,6 +49,12 @@ module.exports = (config) => { | |
rollingDuration, | ||
} = config.session; | ||
|
||
const { transient: emptyTransient , ...emptyCookieOptions } = cookieConfig; | ||
emptyCookieOptions.expires = emptyTransient ? 0 : new Date(); | ||
|
||
const emptyCookie = cookie.serialize(`${sessionName}.0`, '', emptyCookieOptions); | ||
const cookieChunkSize = MAX_COOKIE_SIZE - emptyCookie.length; | ||
|
||
let keystore = new JWKS.KeyStore(); | ||
|
||
secrets.forEach((secretString, i) => { | ||
|
@@ -90,19 +96,17 @@ module.exports = (config) => { | |
res, | ||
{ uat = epoch(), iat = uat, exp = calculateExp(iat, uat) } | ||
) { | ||
const cookieOptions = { | ||
...cookieConfig, | ||
expires: cookieConfig.transient ? 0 : new Date(exp * 1000), | ||
}; | ||
delete cookieOptions.transient; | ||
const cookies = req[COOKIES]; | ||
const { transient: cookieTransient , ...cookieOptions } = cookieConfig; | ||
cookieOptions.expires = cookieTransient ? 0 : new Date(exp * 1000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this was the existing logic, but I noticed it's not correct. If you want a session cookie
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes See nextjs-auth0 for reference https://github.com/auth0/nextjs-auth0/blob/main/src/auth0-session/cookie-store.ts#L44-L46 and https://github.com/auth0/nextjs-auth0/blob/main/tests/auth0-session/cookie-store.test.ts#L262-L271 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davidpatrick I know this was the existing logic, but I noticed it's not correct. If you want a session cookie
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes See nextjs-auth0 for reference https://github.com/auth0/nextjs-auth0/blob/main/src/auth0-session/cookie-store.ts#L44-L46 and https://github.com/auth0/nextjs-auth0/blob/main/tests/auth0-session/cookie-store.test.ts#L262-L271 Would you mind fixing it and adding a regression test? Either in this PR or another one before you do the release There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davidpatrick - actually, aplogies, ignore me - I've just tested it and I get a sessoion cookie fine - so don't worry |
||
|
||
// session was deleted or is empty, this matches all session cookies (chunked or unchunked) | ||
// and clears them, essentially cleaning up what we've set in the past that is now trash | ||
if (!req[sessionName] || !Object.keys(req[sessionName]).length) { | ||
debug( | ||
'session was deleted or is empty, clearing all matching session cookies' | ||
); | ||
for (const cookieName of Object.keys(req[COOKIES])) { | ||
for (const cookieName of Object.keys(cookies)) { | ||
if (cookieName.match(`^${sessionName}(?:\\.\\d)?$`)) { | ||
res.clearCookie(cookieName, { | ||
domain: cookieOptions.domain, | ||
|
@@ -115,25 +119,44 @@ module.exports = (config) => { | |
'found session, creating signed session cookie(s) with name %o(.i)', | ||
sessionName | ||
); | ||
|
||
const value = encrypt(JSON.stringify(req[sessionName]), { | ||
iat, | ||
uat, | ||
exp, | ||
}); | ||
|
||
const chunkCount = Math.ceil(value.length / CHUNK_BYTE_SIZE); | ||
const chunkCount = Math.ceil(value.length / cookieChunkSize); | ||
|
||
if (chunkCount > 1) { | ||
debug('cookie size greater than %d, chunking', CHUNK_BYTE_SIZE); | ||
debug('cookie size greater than %d, chunking', cookieChunkSize); | ||
for (let i = 0; i < chunkCount; i++) { | ||
const chunkValue = value.slice( | ||
i * CHUNK_BYTE_SIZE, | ||
(i + 1) * CHUNK_BYTE_SIZE | ||
i * cookieChunkSize, | ||
(i + 1) * cookieChunkSize | ||
); | ||
|
||
const chunkCookieName = `${sessionName}.${i}`; | ||
res.cookie(chunkCookieName, chunkValue, cookieOptions); | ||
} | ||
if (sessionName in cookies) { | ||
debug('replacing non chunked cookie with chunked cookies'); | ||
res.clearCookie(sessionName, { | ||
domain: cookieConfig.domain, | ||
path: cookieConfig.path | ||
}); | ||
} | ||
} else { | ||
res.cookie(sessionName, value, cookieOptions); | ||
for (const cookieName of Object.keys(cookies)) { | ||
debug('replacing chunked cookies with non chunked cookies'); | ||
if (cookieName.match(`^${sessionName}\\.\\d$`)) { | ||
res.clearCookie(cookieName, { | ||
domain: cookieConfig.domain, | ||
path: cookieConfig.path | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this was the existing logic, but I noticed it's not correct.
If you want a session cookie
expires
should not be defined (it shouldn't be set to 0)https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes
See nextjs-auth0 for reference https://github.com/auth0/nextjs-auth0/blob/main/src/auth0-session/cookie-store.ts#L44-L46 and https://github.com/auth0/nextjs-auth0/blob/main/tests/auth0-session/cookie-store.test.ts#L262-L271