From fcaf2fdbb00e8346ef8ea72b8fcc61522b04614c Mon Sep 17 00:00:00 2001 From: Naveed Fida Date: Tue, 13 Jun 2023 23:30:57 +0500 Subject: [PATCH 1/4] add tests for issueBug in `createSessionStorage` #6594 --- .../remix-node/__tests__/sessions-test.ts | 35 ++++++++++++++++++- packages/remix-node/sessions/fileStorage.ts | 2 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/remix-node/__tests__/sessions-test.ts b/packages/remix-node/__tests__/sessions-test.ts index 075c90160c8..1ceccd4072e 100644 --- a/packages/remix-node/__tests__/sessions-test.ts +++ b/packages/remix-node/__tests__/sessions-test.ts @@ -2,7 +2,7 @@ import path from "path"; import { promises as fsp } from "fs"; import os from "os"; -import { createFileSessionStorage } from "../sessions/fileStorage"; +import { createFileSessionStorage, getFile } from "../sessions/fileStorage"; function getCookieFromSetCookie(setCookie: string): string { return setCookie.split(/;\s*/)[0]; @@ -62,6 +62,39 @@ describe("File session storage", () => { await expect(destroySession(session)).resolves.not.toThrowError(); }); + it("saves expires to file if expires provided to commitSession when creating new cookie", async () => { + let { getSession, commitSession } = createFileSessionStorage({ + dir, + cookie: { secrets: ["secret1"] }, + }); + let session = await getSession(); + session.set("user", "mjackson"); + let date = new Date(Date.now() + 1000 * 60); + let cookieHeader = await commitSession(session, { expires: date }); + let createdSession = await getSession(cookieHeader); + + let { id } = createdSession; + let fileContents = await fsp.readFile(getFile(dir, id), "utf8"); + let fileData = JSON.parse(fileContents); + expect(fileData.expires).toEqual(date.toISOString()); + }); + + it("saves expires to file if maxAge provided to commitSession when creating new cookie", async () => { + let { getSession, commitSession } = createFileSessionStorage({ + dir, + cookie: { secrets: ["secret1"] }, + }); + let session = await getSession(); + session.set("user", "mjackson"); + let cookieHeader = await commitSession(session, { maxAge: 60 }); + let createdSession = await getSession(cookieHeader); + + let { id } = createdSession; + let fileContents = await fsp.readFile(getFile(dir, id), "utf8"); + let fileData = JSON.parse(fileContents); + expect(typeof fileData.expires).toBe("string"); + }); + describe("when a new secret shows up in the rotation", () => { it("unsigns old session cookies using the old secret and encodes new cookies using the new secret", async () => { let { getSession, commitSession } = createFileSessionStorage({ diff --git a/packages/remix-node/sessions/fileStorage.ts b/packages/remix-node/sessions/fileStorage.ts index 9f690801058..a8f0eeb00dd 100644 --- a/packages/remix-node/sessions/fileStorage.ts +++ b/packages/remix-node/sessions/fileStorage.ts @@ -103,7 +103,7 @@ export function createFileSessionStorage({ }); } -function getFile(dir: string, id: string): string { +export function getFile(dir: string, id: string): string { // Divide the session id up into a directory (first 2 bytes) and filename // (remaining 6 bytes) to reduce the chance of having very large directories, // which should speed up file access. This is a maximum of 2^16 directories, From 73367c99a051efd1d85e5446a87dfc3dde0fc606 Mon Sep 17 00:00:00 2001 From: Naveed Fida Date: Tue, 13 Jun 2023 23:34:05 +0500 Subject: [PATCH 2/4] add fix for issue: Bug in `createSessionStorage` #6594 --- packages/remix-server-runtime/sessions.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/remix-server-runtime/sessions.ts b/packages/remix-server-runtime/sessions.ts index bf809b65414..1b25dba0504 100644 --- a/packages/remix-server-runtime/sessions.ts +++ b/packages/remix-server-runtime/sessions.ts @@ -277,11 +277,20 @@ export const createSessionStorageFactory = }, async commitSession(session, options) { let { id, data } = session; + let expires = cookie.expires; + + if (options?.expires) { + expires = options.expires; + } + + if (options?.maxAge) { + expires = new Date(Date.now() + options.maxAge * 1000); + } if (id) { - await updateData(id, data, cookie.expires); + await updateData(id, data, expires); } else { - id = await createData(data, cookie.expires); + id = await createData(data, expires); } return cookie.serialize(id, options); From 739ca8307adac7f807638d2c4e9d7741cc6f0279 Mon Sep 17 00:00:00 2001 From: Naveed Fida Date: Tue, 13 Jun 2023 23:45:07 +0500 Subject: [PATCH 3/4] add naveed-fida to contributors.yml --- contributors.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/contributors.yml b/contributors.yml index e63369fb96f..738811f08e0 100644 --- a/contributors.yml +++ b/contributors.yml @@ -550,3 +550,4 @@ - amir-ziaei - mrkhosravian - tanerijun +- naveed-fida From 831284335b8d709856d84288c35faf079589a9a4 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Fri, 25 Aug 2023 12:46:23 -0400 Subject: [PATCH 4/4] Move highest precedence option first via ternary --- packages/remix-server-runtime/sessions.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/remix-server-runtime/sessions.ts b/packages/remix-server-runtime/sessions.ts index 1b25dba0504..2d4a6c90806 100644 --- a/packages/remix-server-runtime/sessions.ts +++ b/packages/remix-server-runtime/sessions.ts @@ -277,15 +277,12 @@ export const createSessionStorageFactory = }, async commitSession(session, options) { let { id, data } = session; - let expires = cookie.expires; - - if (options?.expires) { - expires = options.expires; - } - - if (options?.maxAge) { - expires = new Date(Date.now() + options.maxAge * 1000); - } + let expires = + options?.maxAge != null + ? new Date(Date.now() + options.maxAge * 1000) + : options?.expires != null + ? options.expires + : cookie.expires; if (id) { await updateData(id, data, expires);