Skip to content
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
5 changes: 2 additions & 3 deletions src/server/cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ describe("encrypt/decrypt", async () => {
const maxAge = 60 * 60; // 1 hour in seconds
const expiration = Math.floor(Date.now() / 1000 + maxAge);
const encrypted = await encrypt(payload, secret, expiration);
await expect(() =>
decrypt(encrypted, incorrectSecret)
).rejects.toThrowError();
const decrypted = await decrypt(encrypted, incorrectSecret);
expect(decrypted).toBeNull();
});

it("should fail to decrypt when expired", async () => {
Expand Down
6 changes: 5 additions & 1 deletion src/server/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export async function decrypt<T>(

return cookie;
} catch (e: any) {
if (e.code === "ERR_JWT_EXPIRED") {
// When the JWE can not be decrypted or has expired, return null to indicate an invalid cookie and treat it as non-existent.
if (
e.code === "ERR_JWE_DECRYPTION_FAILED" ||
e.code === "ERR_JWT_EXPIRED"
) {
return null;
}
throw e;
Expand Down