Skip to content
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

We should cleanup unused cookies when switching between chunked and unchunked #303

Merged
merged 6 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/auth0-session/cookie-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ export default class CookieStore {
cookie: { transient, ...cookieConfig },
name: sessionName
} = this.config.session;
const cookies = getCookies(req);

if (!session) {
debug('clearing all matching session cookies');
for (const cookieName of Object.keys(getCookies(req))) {
for (const cookieName of Object.keys(cookies)) {
if (cookieName.match(`^${sessionName}(?:\\.\\d)?$`)) {
clearCookie(res, cookieName, {
domain: cookieConfig.domain,
Expand Down Expand Up @@ -196,8 +197,22 @@ export default class CookieStore {
const chunkCookieName = `${sessionName}.${i}`;
setCookie(res, chunkCookieName, chunkValue, cookieOptions);
}
if (sessionName in cookies) {
clearCookie(res, sessionName, {
domain: cookieConfig.domain,
path: cookieConfig.path
});
}
} else {
setCookie(res, sessionName, value, cookieOptions);
for (const cookieName of Object.keys(cookies)) {
if (cookieName.match(`^${sessionName}\\.\\d$`)) {
clearCookie(res, cookieName, {
domain: cookieConfig.domain,
path: cookieConfig.path
});
}
}
}
}
}
31 changes: 31 additions & 0 deletions tests/auth0-session/cookie-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,37 @@ describe('CookieStore', () => {
await expect(get(baseURL, '/session', { cookieJar })).rejects.toThrowError('Unauthorized');
});

it('should clean up single cookie when switching to chunked', async () => {
const baseURL = await setup(defaultConfig);
const appSession = encrypted({
big_claim: randomBytes(2000).toString('base64')
});
expect(appSession.length).toBeGreaterThan(4000);
const cookieJar = toCookieJar({ appSession }, baseURL);
const session = await get(baseURL, '/session', { cookieJar });
expect(session.claims).toHaveProperty('big_claim');
const cookies = fromCookieJar(cookieJar, baseURL);
expect(cookies).toHaveProperty(['appSession.0']);
expect(cookies).not.toHaveProperty('appSession');
});

it('should clean up chunked cookies when switching to a single cookie', async () => {
const baseURL = await setup(defaultConfig);
const appSession = encrypted({ sub: 'foo' });
const cookieJar = toCookieJar(
{
'appSession.0': appSession.slice(0, 100),
'appSession.1': appSession.slice(100)
},
baseURL
);
const session = await get(baseURL, '/session', { cookieJar });
expect(session.claims).toHaveProperty('sub');
const cookies = fromCookieJar(cookieJar, baseURL);
expect(cookies).toHaveProperty('appSession');
expect(cookies).not.toHaveProperty(['appSession.0']);
});

it('should set the default cookie options on http', async () => {
const baseURL = await setup(defaultConfig);
const appSession = encrypted();
Expand Down