Skip to content

Commit

Permalink
res.cookie adds a default path so we should too when measuring (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath authored Aug 25, 2021
1 parent 05f422f commit 9bcc677
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/appSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = (config) => {

const { transient: emptyTransient, ...emptyCookieOptions } = cookieConfig;
emptyCookieOptions.expires = emptyTransient ? 0 : new Date();
emptyCookieOptions.path = emptyCookieOptions.path || '/';

const emptyCookie = cookie.serialize(
`${sessionName}.0`,
Expand Down
27 changes: 27 additions & 0 deletions test/appSession.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@ describe('appSession', () => {
});

it('should limit total cookie size to 4096 Bytes', async () => {
server = await createServer(appSession(getConfig(defaultConfig)));
const jar = request.jar();

await request.post('session', {
baseUrl,
jar,
json: {
sub: '__test_sub__',
random: crypto.randomBytes(8000).toString('base64'),
},
});

const cookies = jar
.getCookies(baseUrl)
.reduce(
(obj, value) => Object.assign(obj, { [value.key]: value + '' }),
{}
);

assert.exists(cookies);
assert.equal(cookies['appSession.0'].length, 4096);
assert.equal(cookies['appSession.1'].length, 4096);
assert.equal(cookies['appSession.2'].length, 4096);
assert.isTrue(cookies['appSession.3'].length <= 4096);
});

it('should limit total cookie size to 4096 Bytes with custom path', async () => {
const path =
'/some-really-really-really-really-really-really-really-really-really-really-really-really-really-long-path';
server = await createServer(
Expand Down

0 comments on commit 9bcc677

Please sign in to comment.