Files
HealthLog/src/__tests__/api/auth/registration-status/route.test.ts
T
Marc-André Bombeck b100c73a62 fix(auth): refuse a sign-in over plain http before it spends a factor (#1097)
A server that issues Secure session cookies cannot sign anyone in on a
plain http:// page: the browser drops the cookie. The login request was
still verified, the MFA ticket minted and then claimed, and the next
attempt answered "Invalid or expired challenge".

The web client now sends an optional clientTransport on the browser
sign-in requests (password login, passkey start, both MFA completions).
When the server issues Secure cookies and the page is plain http on a
non-loopback host, the request is refused with 409
auth.session.insecure_transport before the password is checked, a
ticket is minted or claimed, a code is spent, or a passkey challenge is
issued. A request without the field is never refused. The SSO start
takes client_proto/client_host and sends such a page back to the login
page instead of to the provider.

GET /api/auth/registration-status publishes sessionCookieSecure, and the
login page shows the reason with both fixes and disables the sign-in
controls before anything is sent; on localhost it shows a note only.
SESSION_COOKIE_SECURE keeps its meaning; the header heuristic log line
stays.
2026-10-03 18:21:19 +02:00

144 lines
4.8 KiB
TypeScript

/**
* v1.4.40 W-INSIGHTS SB-7 — pin the four discovery branches of
* `GET /api/auth/registration-status`.
*
* The route is read-only; the iOS sign-in flow + the web onboarding
* dispatcher both depend on its envelope shape (`registrationEnabled`
* boolean) staying byte-stable. A regression that flipped the
* fail-closed branch to fail-open would silently allow registration on
* a self-hosted deployment whose admin set `registrationEnabled: false`
* and an upstream DB blip lost the row.
*
* Four branches walked:
*
* 1. **Singleton row exists with `registrationEnabled: true`** →
* envelope reports `true`.
* 2. **Singleton row exists with `registrationEnabled: false`** →
* envelope reports `false`. (Admin-disabled tenant — invite-only.)
* 3. **Singleton row missing (`findUnique` returns `null`)** →
* envelope reports the schema default `true` (open registration —
* the no-config baseline for a fresh self-hosted instance).
* 4. **`findUnique` throws** (DB outage / connection drop) → envelope
* fails closed with `false`. Critical: a DB blip on a tenant
* that has disabled registration must NOT unlock the sign-up
* flow.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/lib/db", () => ({
prisma: {
appSettings: {
findUnique: vi.fn(),
},
},
}));
vi.mock("@/lib/auth/audit", () => ({
auditLog: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("@/lib/db-compat", () => ({
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("@/lib/logging/transports", () => ({ emitIfSampled: vi.fn() }));
vi.mock("next/headers", () => ({
headers: vi.fn(async () => ({ get: () => null })),
cookies: vi.fn(async () => ({
get: () => undefined,
set: () => {},
delete: () => {},
})),
}));
import { GET } from "@/app/api/auth/registration-status/route";
import { prisma } from "@/lib/db";
const callGet = GET as unknown as (...args: never[]) => Promise<Response>;
interface RegistrationStatusBody {
data: { registrationEnabled: boolean };
}
beforeEach(() => {
vi.resetAllMocks();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe("GET /api/auth/registration-status", () => {
it("reports `true` when the singleton row carries registrationEnabled: true", async () => {
vi.mocked(prisma.appSettings.findUnique).mockResolvedValue({
id: "singleton",
registrationEnabled: true,
} as never);
const res = await callGet();
expect(res.status).toBe(200);
const body = (await res.json()) as RegistrationStatusBody;
expect(body.data.registrationEnabled).toBe(true);
});
it("reports `false` when the singleton row carries registrationEnabled: false (admin-disabled tenant)", async () => {
vi.mocked(prisma.appSettings.findUnique).mockResolvedValue({
id: "singleton",
registrationEnabled: false,
} as never);
const res = await callGet();
expect(res.status).toBe(200);
const body = (await res.json()) as RegistrationStatusBody;
expect(body.data.registrationEnabled).toBe(false);
});
it("falls back to the schema default `true` when the singleton row is missing (fresh self-hosted instance)", async () => {
vi.mocked(prisma.appSettings.findUnique).mockResolvedValue(null as never);
const res = await callGet();
expect(res.status).toBe(200);
const body = (await res.json()) as RegistrationStatusBody;
expect(body.data.registrationEnabled).toBe(true);
});
it("fails closed with `false` when the DB lookup throws (defense-in-depth against silent unlock)", async () => {
vi.mocked(prisma.appSettings.findUnique).mockRejectedValue(
new Error("connection terminated"),
);
const res = await callGet();
expect(res.status).toBe(200);
const body = (await res.json()) as RegistrationStatusBody;
expect(body.data.registrationEnabled).toBe(false);
});
});
describe("GET /api/auth/registration-status — sessionCookieSecure (#1097)", () => {
it("publishes whether the server issues Secure session cookies, on every branch", async () => {
vi.mocked(prisma.appSettings.findUnique).mockResolvedValue({
id: "singleton",
registrationEnabled: true,
} as never);
vi.stubEnv("SESSION_COOKIE_SECURE", "true");
let body = await (await callGet()).json();
expect(body.data.sessionCookieSecure).toBe(true);
vi.stubEnv("SESSION_COOKIE_SECURE", "false");
body = await (await callGet()).json();
expect(body.data.sessionCookieSecure).toBe(false);
vi.mocked(prisma.appSettings.findUnique).mockRejectedValue(
new Error("db down"),
);
vi.stubEnv("SESSION_COOKIE_SECURE", "true");
body = await (await callGet()).json();
expect(body.data).toEqual({
registrationEnabled: false,
sessionCookieSecure: true,
});
vi.unstubAllEnvs();
});
});