Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
π Fix admin-setup redirect loop and authorize admin access against the database (#2632)
Luke Vella7a924d9b5ba8
diff --git a/apps/web/src/app/[locale]/admin-setup/actions.ts b/apps/web/src/app/[locale]/admin-setup/actions.tsindex e72e2eb66..0ade9d2df 100644--- a/apps/web/src/app/[locale]/admin-setup/actions.ts+++ b/apps/web/src/app/[locale]/admin-setup/actions.ts@@ -1,8 +1,10 @@"use server";import { prisma } from "@rallly/database";+import { headers } from "next/headers";import { redirect } from "next/navigation";import { isInitialAdmin } from "@/features/setup/utils";+import { authLib } from "@/lib/auth";import { AppError } from "@/lib/errors/app-error";import { authActionClient } from "@/lib/safe-action/server";@@ -25,5 +27,15 @@ export const makeMeAdminAction = authActionClient},});+ // The session cookie cache still holds the old role, which would send+ // /control-panel straight back here. Re-issue it from the database+ // before redirecting.+ await authLib.api.getSession({+ headers: await headers(),+ query: {+ disableCookieCache: true,+ },+ });+redirect("/control-panel");});diff --git a/apps/web/src/app/[locale]/admin-setup/page.tsx b/apps/web/src/app/[locale]/admin-setup/page.tsxindex 288a3c43f..14bcd2497 100644--- a/apps/web/src/app/[locale]/admin-setup/page.tsx+++ b/apps/web/src/app/[locale]/admin-setup/page.tsx@@ -11,14 +11,25 @@ import {EmptyStateTitle,} from "@/components/empty-state";import { isInitialAdmin } from "@/features/setup/utils";+import { getCurrentUser } from "@/features/user/data";import { Trans } from "@/i18n/client";import { getTranslation } from "@/i18n/server";-import { createPrivateSSRHelper } from "@/trpc/server/create-ssr-helper";+import { buildSafeRedirectUrl } from "@/lib/utils/redirect";import { MakeMeAdminButton } from "./make-me-admin-button";export default async function AdminSetupPage() {- const helpers = await createPrivateSSRHelper();- const user = await helpers.user.getAuthed.fetch();+ // Read the role from the database β the session cookie cache can hold+ // a stale role.+ const user = await getCurrentUser();++ if (!user) {+ redirect(+ buildSafeRedirectUrl({+ destination: "/login",+ returnUrl: "/admin-setup",+ }),+ );+ }if (user.role === "admin") {redirect("/control-panel");diff --git a/apps/web/src/trpc/server/create-ssr-helper.ts b/apps/web/src/trpc/server/create-ssr-helper.tsindex 791ab1751..fb7fd600d 100644--- a/apps/web/src/trpc/server/create-ssr-helper.ts+++ b/apps/web/src/trpc/server/create-ssr-helper.ts@@ -3,6 +3,7 @@ import { notFound, redirect } from "next/navigation";import { cache } from "react";import superjson from "superjson";import { isInitialAdmin } from "@/features/setup/utils";+import { getCurrentUser } from "@/features/user/data";import { getSession } from "@/lib/auth";import { InvalidSessionError } from "@/lib/errors/invalid-session-error";import { getPathname } from "@/lib/pathname";@@ -65,9 +66,12 @@ export const createPrivateSSRHelper = cache(async () => {* is not an admin.*/export const createAdminSSRHelper = cache(async () => {- const user = (await getSession())?.user;+ // Admin access must be authorized against the database β the session+ // cookie cache can hold a stale role. getCurrentUser also rejects+ // banned users and returns null for guests.+ const user = await getCurrentUser();- if (!user || user.isGuest) {+ if (!user) {redirect(buildSafeRedirectUrl({destination: "/login",@@ -76,10 +80,6 @@ export const createAdminSSRHelper = cache(async () => {);}- if (user.banned) {- throw new InvalidSessionError();- }-if (user.role !== "admin") {if (isInitialAdmin(user.email)) {redirect("/admin-setup");diff --git a/apps/web/src/trpc/trpc.ts b/apps/web/src/trpc/trpc.tsindex 4323e65ad..222ef3c67 100644--- a/apps/web/src/trpc/trpc.ts+++ b/apps/web/src/trpc/trpc.ts@@ -134,7 +134,14 @@ export const privateProcedure = publicProcedure.use(async ({ ctx, next }) => {});export const adminProcedure = privateProcedure.use(async ({ ctx, next }) => {- if (ctx.user.role !== "admin") {+ // ctx.user comes from the session cookie cache, which can hold a stale+ // role β admin access must be authorized against the database.+ const user = await prisma.user.findUnique({+ where: { id: ctx.user.id },+ select: { role: true },+ });++ if (user?.role !== "admin") {throw new TRPCError({code: "FORBIDDEN",message: "Admin access required",diff --git a/apps/web/tests/admin-setup.spec.ts b/apps/web/tests/admin-setup.spec.tsindex a727ff795..96eadb61f 100644--- a/apps/web/tests/admin-setup.spec.ts+++ b/apps/web/tests/admin-setup.spec.ts@@ -114,6 +114,14 @@ test.describe("Admin Setup Page Access", () => {await page.waitForURL("/control-panel", { timeout: 5000 });}).toPass();+ // The URL check above can pass on the transient redirect even when a+ // stale session cookie bounces us back to /admin-setup, so assert the+ // control panel actually rendered.+ await expect(+ page.getByRole("heading", { name: "Home", level: 1 }),+ ).toBeVisible();+ await expect(page).toHaveURL("/control-panel");+const user = await prisma.user.findUnique({where: { email: INITIAL_ADMIN_TEST_EMAIL },});