Skip to content

Commit

Permalink
fix(cookie): Throw on max cookie length (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvo committed May 26, 2020
1 parent 3772b73 commit 0e43729
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ export async function applySession(
async save() {
const seal = await store.seal();
const cookieValue = cookie.serialize(cookieName, seal, cookieOptions);
if (cookieValue.length > 4096) {
throw new Error(
`next-iron-session: Cookie length is too big ${cookieValue.length}, browsers will refuse it`,
);
}
res.setHeader("set-cookie", [cookieValue]);
return cookieValue;
},
destroy() {
store.clear();
Expand Down
23 changes: 23 additions & 0 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,26 @@ test("ironSession({password})", () => {
`"next-iron-session: Missing parameter \`cookieName\`"`,
);
});

test("it throws when cookie length is too big", () => {
return new Promise((done) => {
const handler = async (req) => {
req.session.set("user", "somevalue".repeat(500));
await expect(async function () {
await req.session.save();
}).rejects.toThrowErrorMatchingInlineSnapshot(
`"next-iron-session: Cookie length is too big 6341, browsers will refuse it"`,
);
done();
};
const wrappedHandler = withIronSession(handler, { password, cookieName });
wrappedHandler(
{
headers: { cookie: "" },
},
{
setHeader: jest.fn(),
},
);
});
});

0 comments on commit 0e43729

Please sign in to comment.