Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -1,9 +1,32 @@
import { describe, expect, it } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
const { testWebConfig } = vi.hoisted(() => ({
testWebConfig: {
nodeEnv: "test" as string,
routing: {
appBaseDomain: "example.test",
webAppHost: "app.example.test",
adminAppHost: "admin.example.test",
apiHost: "api.example.test",
protocol: "https" as "http" | "https",
},
},
}));
vi.mock("@hivegrid/config", async () => {
const actual = await vi.importActual<typeof import("@hivegrid/config")>("@hivegrid/config");
return {
...actual,
loadRequiredWebConfig: () => testWebConfig,
};
});
import {
ADMIN_SSO_CHALLENGE_COOKIE,
ADMIN_SSO_CHALLENGE_TTL_MS,
consumeAdminSsoChallenge,
createAdminSsoChallenge,
LOCAL_ADMIN_SSO_CHALLENGE_COOKIE,
serializeAdminSsoChallengeCookie,
} from "./state.server";
@@ -14,12 +37,18 @@ function cookieValue(cookie: string): string {
return head.slice(`${ADMIN_SSO_CHALLENGE_COOKIE}=`.length);
}
function requestWithCookie(value: string): Request {
function requestWithCookie(value: string, name = ADMIN_SSO_CHALLENGE_COOKIE): Request {
return new Request("https://admin.example.test/auth/admin/callback", {
headers: { cookie: `${ADMIN_SSO_CHALLENGE_COOKIE}=${value}` },
headers: { cookie: `${name}=${value}` },
});
}
beforeEach(() => {
testWebConfig.nodeEnv = "test";
testWebConfig.routing.protocol = "https";
testWebConfig.routing.appBaseDomain = "example.test";
});
describe("createAdminSsoChallenge", () => {
it("produces distinct state, nonce and codeVerifier for each invocation", () => {
const a = createAdminSsoChallenge({ secret: SECRET });
@@ -38,11 +67,27 @@ describe("createAdminSsoChallenge", () => {
expect(payload.codeChallenge).not.toBe(payload.codeVerifier);
});
it("emits an HttpOnly, Path-scoped cookie", () => {
it("emits a host-prefixed cookie on HTTPS without a Domain attribute", () => {
const { cookie } = createAdminSsoChallenge({ secret: SECRET });
expect(cookie).toContain(`${ADMIN_SSO_CHALLENGE_COOKIE}=`);
expect(cookie).toMatch(/HttpOnly/);
expect(cookie).toMatch(/Path=\/auth\/admin/);
expect(cookie).toMatch(/Path=\//);
expect(cookie).toMatch(/SameSite=Lax/);
expect(cookie).toMatch(/Secure/);
expect(cookie).not.toMatch(/Domain=/);
});
it("uses a distinct localhost-compatible name on local HTTP", () => {
testWebConfig.nodeEnv = "development";
testWebConfig.routing.protocol = "http";
testWebConfig.routing.appBaseDomain = "localhost:3000";
const { cookie } = createAdminSsoChallenge({ secret: SECRET });
expect(cookie).toContain(`${LOCAL_ADMIN_SSO_CHALLENGE_COOKIE}=`);
expect(cookie).not.toContain(ADMIN_SSO_CHALLENGE_COOKIE);
expect(cookie).not.toContain("Secure");
expect(cookie).not.toContain("Domain=");
});
});
@@ -70,6 +115,26 @@ describe("consumeAdminSsoChallenge", () => {
if (!result.ok) expect(result.reason).toBe("missing");
});
it("ignores a legacy parent-domain cookie on HTTPS", () => {
const { cookie } = createAdminSsoChallenge({ secret: SECRET });
const result = consumeAdminSsoChallenge(requestWithCookie(cookieValue(cookie), LOCAL_ADMIN_SSO_CHALLENGE_COOKIE), { secret: SECRET });
expect(result).toEqual({ ok: false, reason: "missing" });
});
it("does not let a legacy cookie shadow the host-prefixed challenge", () => {
const { payload, cookie } = createAdminSsoChallenge({ secret: SECRET });
const value = cookieValue(cookie);
const request = new Request("https://admin.example.test/auth/admin/callback", {
headers: { cookie: `${LOCAL_ADMIN_SSO_CHALLENGE_COOKIE}=attacker; ${ADMIN_SSO_CHALLENGE_COOKIE}=${value}` },
});
const result = consumeAdminSsoChallenge(request, { secret: SECRET });
expect(result.ok).toBe(true);
if (result.ok) expect(result.payload.state).toBe(payload.state);
});
it("rejects a cookie with a tampered signature", () => {
const { cookie } = createAdminSsoChallenge({ secret: SECRET });
const value = cookieValue(cookie);
@@ -96,6 +161,22 @@ describe("consumeAdminSsoChallenge", () => {
if (!result.ok) expect(result.reason).toBe("expired");
});
it("rejects a challenge dated implausibly far in the future", () => {
const now = Date.now();
const { payload } = createAdminSsoChallenge({ secret: SECRET, now: () => now + 5 * 60 * 1000 });
const cookie = serializeAdminSsoChallengeCookie(payload, SECRET);
const result = consumeAdminSsoChallenge(requestWithCookie(cookieValue(cookie)), { secret: SECRET, now: () => now });
expect(result).toEqual({ ok: false, reason: "malformed" });
});
it("rejects a signed payload whose PKCE verifier and challenge do not match", () => {
const { payload } = createAdminSsoChallenge({ secret: SECRET });
const inconsistent = { ...payload, codeChallenge: "A".repeat(43) };
const cookie = serializeAdminSsoChallengeCookie(inconsistent, SECRET);
const result = consumeAdminSsoChallenge(requestWithCookie(cookieValue(cookie)), { secret: SECRET });
expect(result).toEqual({ ok: false, reason: "malformed" });
});
it("rejects a malformed cookie value", () => {
const result = consumeAdminSsoChallenge(requestWithCookie("garbage"), { secret: SECRET });
expect(result.ok).toBe(false);