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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up erroneous cookies when chunk size decreases #1300

Merged
merged 2 commits into from
Jul 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/auth0-session/session/stateless-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,27 @@ export class StatelessSession<
const value = await this.encrypt(session, { iat, uat, exp });

const chunkCount = Math.ceil(value.length / this.chunkSize);

const existingCookies = new Set(
Object.keys(cookies).filter((cookie) => cookie.match(`^${sessionName}(?:\\.\\d)?$`))
);

if (chunkCount > 1) {
debug('cookie size greater than %d, chunking', this.chunkSize);
for (let i = 0; i < chunkCount; i++) {
const chunkValue = value.slice(i * this.chunkSize, (i + 1) * this.chunkSize);
const chunkCookieName = `${sessionName}.${i}`;
cookieSetter.set(chunkCookieName, chunkValue, cookieOptions);
}
if (sessionName in cookies) {
cookieSetter.clear(sessionName, cookieOptions);
existingCookies.delete(chunkCookieName);
}
} else {
cookieSetter.set(sessionName, value, cookieOptions);
for (const cookieName of Object.keys(cookies)) {
if (cookieName.match(`^${sessionName}\\.\\d$`)) {
cookieSetter.clear(cookieName, cookieOptions);
}
}
existingCookies.delete(sessionName);
}

// When the number of chunks changes due to the cookie size changing,
// you need to delete any obsolete cookies.
existingCookies.forEach((cookie) => cookieSetter.clear(cookie, cookieOptions));
cookieSetter.commit(res, this.config.session.name);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/auth0-session/session/stateless-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ describe('StatelessSession', () => {
expect(cookies).not.toHaveProperty(['appSession.0']);
});

it('should clean up chunked cookies when chunk size reduces', async () => {
const baseURL = await setup(defaultConfig);
const appSession = await encrypted({
big_claim: randomBytes(2000).toString('base64')
});
expect(appSession.length).toBeGreaterThan(4000);
const cookieJar = toCookieJar(
{
'appSession.0': appSession.slice(0, 100),
'appSession.1': appSession.slice(100, 200),
'appSession.2': appSession.slice(200)
},
baseURL
);
const { data: session, res } = await get(baseURL, '/session', { cookieJar, fullResponse: true });
expect(res.headers['set-cookie']).toHaveLength(3);
expect(session.claims).toHaveProperty('big_claim');
const cookies = fromCookieJar(cookieJar, baseURL);
expect(cookies).toHaveProperty(['appSession.0']);
expect(cookies).toHaveProperty(['appSession.1']);
expect(cookies).not.toHaveProperty(['appSession.2']);
});

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