Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+230
-4
@@ -19,8 +19,8 @@ const testWebConfig: TestWebConfig = {
|
||||
},
|
||||
};
|
||||
|
||||
vi.doMock("@toolctrl/config", async () => {
|
||||
const actual = await vi.importActual<typeof import("@toolctrl/config")>("@toolctrl/config");
|
||||
vi.doMock("@hivegrid/config", async () => {
|
||||
const actual = await vi.importActual<typeof import("@hivegrid/config")>("@hivegrid/config");
|
||||
return {
|
||||
...actual,
|
||||
loadRequiredWebConfig: () => testWebConfig,
|
||||
@@ -165,7 +165,7 @@ describe("session cookie policy", () => {
|
||||
|
||||
const header = await storage.commitSession(session);
|
||||
|
||||
expect(header).toContain("RJ_session=");
|
||||
expect(header).toContain("hivegrid_session=");
|
||||
expect(header).toContain("Domain=example.com");
|
||||
expect(header).toContain("SameSite=Lax");
|
||||
expect(header).toContain("Secure");
|
||||
@@ -185,10 +185,35 @@ describe("session cookie policy", () => {
|
||||
});
|
||||
const header = await storage.commitSession(await storage.getSession());
|
||||
|
||||
expect(header).toContain("RJ_session=");
|
||||
expect(header).toContain("hivegrid_session=");
|
||||
expect(header).not.toContain("Domain=");
|
||||
expect(header).not.toContain("Secure");
|
||||
});
|
||||
|
||||
it("builds the cookie storage with an empty secret list when no session secret is configured", async () => {
|
||||
// Drives the falsy side of `webConfig.sessionSecret ? [webConfig.sessionSecret] : []`.
|
||||
const { storage } = await importSessionServerWithConfig({
|
||||
nodeEnv: "test",
|
||||
sessionSecret: "",
|
||||
routing: {
|
||||
appBaseDomain: "localhost",
|
||||
protocol: "http",
|
||||
},
|
||||
});
|
||||
|
||||
const header = await storage.commitSession(await storage.getSession());
|
||||
expect(header).toContain("hivegrid_session=");
|
||||
|
||||
// Restore the canonical test config so subsequent suites see a signed cookie.
|
||||
await importSessionServerWithConfig({
|
||||
nodeEnv: "test",
|
||||
sessionSecret: "dGVzdC1zZXNzaW9uLXNlY3JldC13aXRoLWVub3VnaC1lbnRyb3B5LTEyMzQ1Njc4OTA=",
|
||||
routing: {
|
||||
appBaseDomain: "localhost",
|
||||
protocol: "http",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("session CSRF helpers", () => {
|
||||
@@ -495,6 +520,207 @@ describe("requireUserId server-session revocation (R-P2a S4b)", () => {
|
||||
authStoreMock.getActiveSessionByToken.mockRejectedValueOnce(new Error("db down"));
|
||||
await expect(requireUserId(await requestWithToken("tok-error"))).resolves.toBe("user-1");
|
||||
});
|
||||
|
||||
it("fails open and labels the error generically when the revoke check rejects with a non-Error", async () => {
|
||||
// Non-Error rejection drives the `error instanceof Error ? error.name : "Error"` fallback.
|
||||
authStoreMock.getActiveSessionByToken.mockRejectedValueOnce("boom not an error");
|
||||
await expect(requireUserId(await requestWithToken("tok-string-error"))).resolves.toBe("user-1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUserInfo session token exposure", () => {
|
||||
it("returns a string sessionToken when the cookie carries one", async () => {
|
||||
const session = await storage.getSession();
|
||||
session.set("userId", "user-1");
|
||||
session.set("sessionToken", "tok-active");
|
||||
const request = new Request("https://example.test/app", {
|
||||
headers: { Cookie: (await storage.commitSession(session)).split(";")[0] },
|
||||
});
|
||||
|
||||
await expect(getUserInfo(request)).resolves.toMatchObject({ userId: "user-1", sessionToken: "tok-active" });
|
||||
});
|
||||
|
||||
it("omits a non-string sessionToken from the session info", async () => {
|
||||
const session = await storage.getSession();
|
||||
session.set("userId", "user-1");
|
||||
session.set("sessionToken", 12345);
|
||||
const request = new Request("https://example.test/app", {
|
||||
headers: { Cookie: (await storage.commitSession(session)).split(";")[0] },
|
||||
});
|
||||
|
||||
await expect(getUserInfo(request)).resolves.toMatchObject({ userId: "user-1", sessionToken: undefined });
|
||||
});
|
||||
});
|
||||
|
||||
describe("logout server-session revocation (R-P2a S4b)", () => {
|
||||
async function requestWithLogoutToken(sessionToken?: string) {
|
||||
const session = await storage.getSession();
|
||||
session.set("userId", "user-1");
|
||||
if (sessionToken !== undefined) session.set("sessionToken", sessionToken);
|
||||
return new Request("https://example.test/app/x", {
|
||||
headers: { Cookie: (await storage.commitSession(session)).split(";")[0] },
|
||||
});
|
||||
}
|
||||
|
||||
it("ends the server session row and destroys the cookie when a session token is present", async () => {
|
||||
const { logout } = await import("./session.server");
|
||||
const lifecycle = await import("~/services/auth/sessionLifecycle.server");
|
||||
const endServerSessionMock = vi.mocked(lifecycle.endServerSession);
|
||||
endServerSessionMock.mockReset();
|
||||
endServerSessionMock.mockResolvedValueOnce(undefined as never);
|
||||
|
||||
const response = await logout(await requestWithLogoutToken("tok-logout"));
|
||||
|
||||
expect(endServerSessionMock).toHaveBeenCalledTimes(1);
|
||||
expect(endServerSessionMock).toHaveBeenCalledWith("tok-logout");
|
||||
expect(response.status).toBe(302);
|
||||
expect(response.headers.get("Location")).toBe("/login");
|
||||
expect(response.headers.get("Set-Cookie")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("still destroys the cookie when ending the server session row rejects with an Error (best-effort)", async () => {
|
||||
const { logout } = await import("./session.server");
|
||||
const lifecycle = await import("~/services/auth/sessionLifecycle.server");
|
||||
const endServerSessionMock = vi.mocked(lifecycle.endServerSession);
|
||||
endServerSessionMock.mockReset();
|
||||
endServerSessionMock.mockRejectedValueOnce(new Error("revoke store offline"));
|
||||
|
||||
// logout must NOT throw even though the revoke failed — the cookie is the
|
||||
// source of truth for the device and is destroyed regardless.
|
||||
const response = await logout(await requestWithLogoutToken("tok-error"));
|
||||
|
||||
expect(endServerSessionMock).toHaveBeenCalledWith("tok-error");
|
||||
expect(response.status).toBe(302);
|
||||
expect(response.headers.get("Location")).toBe("/login");
|
||||
expect(response.headers.get("Set-Cookie")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("swallows non-Error rejections from the revoke call and still logs out", async () => {
|
||||
const { logout } = await import("./session.server");
|
||||
const lifecycle = await import("~/services/auth/sessionLifecycle.server");
|
||||
const endServerSessionMock = vi.mocked(lifecycle.endServerSession);
|
||||
endServerSessionMock.mockReset();
|
||||
// A non-Error rejection drives the `error instanceof Error ? … : "Error"` fallback.
|
||||
endServerSessionMock.mockRejectedValueOnce("string failure" as never);
|
||||
|
||||
const response = await logout(await requestWithLogoutToken("tok-string-error"));
|
||||
|
||||
expect(endServerSessionMock).toHaveBeenCalledWith("tok-string-error");
|
||||
expect(response.status).toBe(302);
|
||||
expect(response.headers.get("Location")).toBe("/login");
|
||||
});
|
||||
|
||||
it("does not touch the server-session store when the cookie carries no token", async () => {
|
||||
const { logout } = await import("./session.server");
|
||||
const lifecycle = await import("~/services/auth/sessionLifecycle.server");
|
||||
const endServerSessionMock = vi.mocked(lifecycle.endServerSession);
|
||||
endServerSessionMock.mockReset();
|
||||
|
||||
const response = await logout(await requestWithLogoutToken());
|
||||
|
||||
expect(endServerSessionMock).not.toHaveBeenCalled();
|
||||
expect(response.status).toBe(302);
|
||||
expect(response.headers.get("Location")).toBe("/login");
|
||||
expect(response.headers.get("Set-Cookie")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("does not touch the server-session store when the token is an empty string", async () => {
|
||||
const { logout } = await import("./session.server");
|
||||
const lifecycle = await import("~/services/auth/sessionLifecycle.server");
|
||||
const endServerSessionMock = vi.mocked(lifecycle.endServerSession);
|
||||
endServerSessionMock.mockReset();
|
||||
|
||||
// Empty-string token: `typeof === "string"` is true but `length > 0` is false,
|
||||
// exercising the right-hand side of the `&&` guard.
|
||||
const response = await logout(await requestWithLogoutToken(""));
|
||||
|
||||
expect(endServerSessionMock).not.toHaveBeenCalled();
|
||||
expect(response.status).toBe(302);
|
||||
expect(response.headers.get("Location")).toBe("/login");
|
||||
});
|
||||
});
|
||||
|
||||
describe("commitPendingMfaLoginCookie (D1 — 2FA login gate)", () => {
|
||||
async function requestWithAuthenticatedSession() {
|
||||
const session = await storage.getSession();
|
||||
session.set("userId", "user-1");
|
||||
session.set("sessionToken", "tok-pre-mfa");
|
||||
session.set("csrf", "csrf-keep");
|
||||
return new Request("https://example.test/login/verify", {
|
||||
headers: { Cookie: (await storage.commitSession(session)).split(";")[0] },
|
||||
});
|
||||
}
|
||||
|
||||
async function readBack(setCookie: string) {
|
||||
const request = new Request("https://example.test/login/verify", {
|
||||
headers: { Cookie: setCookie.split(";")[0] },
|
||||
});
|
||||
return getUserInfo(request);
|
||||
}
|
||||
|
||||
it("writes the pending-MFA state, keeps the csrf token, and clears userId/sessionToken", async () => {
|
||||
const { commitPendingMfaLoginCookie } = await import("./session.server");
|
||||
const setCookie = await commitPendingMfaLoginCookie(await requestWithAuthenticatedSession(), {
|
||||
userId: "user-99",
|
||||
redirectTo: "/app/dashboard",
|
||||
});
|
||||
|
||||
expect(setCookie).toContain("hivegrid_session=");
|
||||
|
||||
const info = await readBack(setCookie);
|
||||
expect(info.pendingMfaUserId).toBe("user-99");
|
||||
expect(info.pendingMfaRedirectTo).toBe("/app/dashboard");
|
||||
// userId and sessionToken must be gone — the pending state must NOT grant access.
|
||||
expect(info.userId).toBeNull();
|
||||
expect(info.sessionToken).toBeUndefined();
|
||||
// csrf survives so the verify route still passes the CSRF check.
|
||||
expect(info.csrf).toBe("csrf-keep");
|
||||
});
|
||||
|
||||
it("requireUserId still denies access while only the pending-MFA state is set", async () => {
|
||||
const { commitPendingMfaLoginCookie } = await import("./session.server");
|
||||
const setCookie = await commitPendingMfaLoginCookie(await requestWithAuthenticatedSession(), {
|
||||
userId: "user-99",
|
||||
redirectTo: "/app/dashboard",
|
||||
});
|
||||
|
||||
const request = new Request("https://example.test/app/secret", {
|
||||
headers: { Cookie: setCookie.split(";")[0] },
|
||||
});
|
||||
|
||||
let response: unknown;
|
||||
try {
|
||||
await requireUserId(request);
|
||||
} catch (error) {
|
||||
response = error;
|
||||
}
|
||||
expect(response).toBeInstanceOf(Response);
|
||||
expect((response as Response).status).toBe(302);
|
||||
expect((response as Response).headers.get("Location")).toContain("/login");
|
||||
});
|
||||
|
||||
it("unsets the redirect key when redirectTo is omitted (typeof guard is false)", async () => {
|
||||
const { commitPendingMfaLoginCookie } = await import("./session.server");
|
||||
const setCookie = await commitPendingMfaLoginCookie(await requestWithAuthenticatedSession(), {
|
||||
userId: "user-99",
|
||||
});
|
||||
|
||||
const info = await readBack(setCookie);
|
||||
expect(info.pendingMfaUserId).toBe("user-99");
|
||||
expect(info.pendingMfaRedirectTo).toBeUndefined();
|
||||
});
|
||||
|
||||
it("unsets the redirect key when redirectTo is an empty string (length guard is false)", async () => {
|
||||
const { commitPendingMfaLoginCookie } = await import("./session.server");
|
||||
const setCookie = await commitPendingMfaLoginCookie(await requestWithAuthenticatedSession(), {
|
||||
userId: "user-99",
|
||||
redirectTo: "",
|
||||
});
|
||||
|
||||
const info = await readBack(setCookie);
|
||||
expect(info.pendingMfaUserId).toBe("user-99");
|
||||
expect(info.pendingMfaRedirectTo).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("createUserSession", () => {
|
||||
|
||||
Reference in new issue
Block a user