fix: authorize super admins by dynamic highest rank
Remote Build and Deploy / deploy (push) Successful in 42s

This commit is contained in:
Simo committed 2026-07-11 22:35:40 +02:00
1 parent bb847176ad
commit f08e56cf53
10 files changed
+173 -65

No files matched your search

@@ -0,0 +1,37 @@
-- Normalize ACL discriminator casing and ensure every emulator rank has a CMS role.
UPDATE `acl_model_permissions` SET `model_type` = 'Role' WHERE LOWER(`model_type`) = 'role';
UPDATE `acl_model_roles` SET `model_type` = 'User' WHERE LOWER(`model_type`) = 'user';
INSERT INTO `acl_roles` (`slug`, `title`, `description`)
SELECT CONCAT('rank_', pr.id), COALESCE(NULLIF(pr.rank_name, ''), CONCAT('Rank ', pr.id)), 'CMS role synchronized from permission_ranks'
FROM `permission_ranks` pr
WHERE NOT EXISTS (SELECT 1 FROM `acl_roles` ar WHERE ar.slug = CONCAT('rank_', pr.id));
-- Preserve the former moderator fallback as explicit ACL data.
INSERT INTO `acl_model_permissions` (`model_type`, `model_id`, `permission_id`)
SELECT 'Role', ar.id, ap.id
FROM `permission_ranks` pr
JOIN `acl_roles` ar ON ar.slug = CONCAT('rank_', pr.id)
JOIN `acl_permissions` ap ON (
pr.id >= 3 AND (
ap.slug = 'mod.dashboard' OR ap.slug = 'mod.actions' OR
(ap.slug LIKE 'mod.%' AND ap.slug LIKE '%.view')
)
)
WHERE NOT EXISTS (
SELECT 1 FROM `acl_model_permissions` amp
WHERE amp.model_type = 'Role' AND amp.model_id = ar.id AND amp.permission_id = ap.id
);
-- Preserve the former administrator read-only fallback as explicit ACL data.
INSERT INTO `acl_model_permissions` (`model_type`, `model_id`, `permission_id`)
SELECT 'Role', ar.id, ap.id
FROM `permission_ranks` pr
JOIN `acl_roles` ar ON ar.slug = CONCAT('rank_', pr.id)
JOIN `acl_permissions` ap ON (
pr.id >= 6 AND (ap.slug = 'admin.dashboard' OR (ap.slug LIKE 'admin.%' AND ap.slug LIKE '%.view'))
)
WHERE NOT EXISTS (
SELECT 1 FROM `acl_model_permissions` amp
WHERE amp.model_type = 'Role' AND amp.model_id = ar.id AND amp.permission_id = ap.id
);
@@ -0,0 +1,11 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
describe("authorization source contract", () => {
it("contains no fixed numeric rank threshold in central authorization files", () => {
for (const file of ["src/lib/permissions.ts", "src/lib/proxy-access.ts", "src/lib/admin/guard.ts"]) {
const source = readFileSync(file, "utf8");
expect(source, file).not.toMatch(/rank\s*[<>]=?\s*\d+/i);
}
});
});
@@ -0,0 +1,18 @@
import { describe, expect, it } from "vitest";
import { decideAuthorization, isDynamicSuperAdmin } from "@/lib/admin/authorization-policy";
describe("isDynamicSuperAdmin", () => {
it.each([[7, 7], [11, 11], [2000, 2000]])("accepts highest rank %i", (rank, highest) => {
expect(isDynamicSuperAdmin(rank, highest)).toBe(true);
});
it("demotes the previous highest rank", () => expect(isDynamicSuperAdmin(2000, 2001)).toBe(false));
it("fails closed without ranks", () => expect(isDynamicSuperAdmin(1, null)).toBe(false));
});
describe("decideAuthorization", () => {
const actor = { id: 1, username: "admin", rank: 11 };
it("allows the dynamically highest rank", () => expect(decideAuthorization({ actor, highestRank: 11, permission: "admin.any", hasPermission: false }).allowed).toBe(true));
it("allows explicit ACL permission below highest", () => expect(decideAuthorization({ actor, highestRank: 12, permission: "admin.news.view", hasPermission: true }).allowed).toBe(true));
it("denies invalid ranks", () => expect(decideAuthorization({ actor: { ...actor, rank: 0 }, highestRank: 11, permission: "admin.any", hasPermission: true })).toMatchObject({ allowed: false, reason: "invalid_rank" }));
it("denies missing permission", () => expect(decideAuthorization({ actor, highestRank: 12, permission: "admin.any", hasPermission: false })).toMatchObject({ allowed: false, reason: "permission_denied" }));
});
+22
View File
@@ -0,0 +1,22 @@
export interface AuthorizationActor { id: number; username: string; rank: number }
export type AuthorizationDenialReason = "invalid_rank" | "permission_denied" | "no_ranks";
export type AuthorizationDecision =
| { allowed: true; superAdmin: boolean }
| { allowed: false; reason: AuthorizationDenialReason };
export function isDynamicSuperAdmin(rank: number, highestRank: number | null): boolean {
return Number.isInteger(rank) && rank > 0 && highestRank !== null && rank === highestRank;
}
export function decideAuthorization(input: {
actor: AuthorizationActor;
highestRank: number | null;
permission?: string;
hasPermission: boolean;
}): AuthorizationDecision {
if (!Number.isInteger(input.actor.rank) || input.actor.rank <= 0) return { allowed: false, reason: "invalid_rank" };
if (input.highestRank === null) return { allowed: false, reason: "no_ranks" };
if (isDynamicSuperAdmin(input.actor.rank, input.highestRank)) return { allowed: true, superAdmin: true };
if (!input.permission || input.hasPermission) return { allowed: true, superAdmin: false };
return { allowed: false, reason: "permission_denied" };
}
+4 -20
View File
@@ -1,18 +1,10 @@
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { isStaff } from "@/lib/admin/is-staff"; import { isStaff } from "@/lib/admin/is-staff";
import { resolveStaffUser } from "@/lib/admin/staff-user"; import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
import { auth } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { siteSettings } from "@/lib/services/site-settings";
import { clientIp, rateLimit } from "@/lib/rate-limit"; import { clientIp, rateLimit } from "@/lib/rate-limit";
export { isStaff }; export { isStaff };
export async function getMinStaffRank(): Promise<number> {
const n = Number(await siteSettings.get("min_staff_rank", "7"));
return Number.isFinite(n) ? n : 7;
}
export interface StaffUser { export interface StaffUser {
id: number; id: number;
rank: number; rank: number;
@@ -24,17 +16,9 @@ export interface StaffUser {
* and to / when authenticated but not staff. Returns the staff user otherwise. * and to / when authenticated but not staff. Returns the staff user otherwise.
*/ */
export async function requireStaff(): Promise<StaffUser> { export async function requireStaff(): Promise<StaffUser> {
const session = await auth(); const { session, permissions } = await getAdminContext();
if (!session?.user?.id) redirect("/login"); if (!canAccess(permissions, PERMS.ADMIN_DASHBOARD, session.user.rank)) redirect("/");
const minRank = await getMinStaffRank(); return { id: session.user.id, rank: session.user.rank, username: session.user.username };
const staff = await resolveStaffUser(session.user.id, minRank, (id) =>
prisma.user.findUnique({
where: { id },
select: { id: true, rank: true, username: true },
}),
);
if (!staff) redirect("/");
return staff;
} }
/** /**
+17
View File
@@ -0,0 +1,17 @@
import { describe, expect, it } from "vitest";
import { resolveAuthorizationState } from "@/lib/admin/rank-authority";
function db(user: { id: number; username: string; rank: number } | null, highest: number | null) {
return {
user: { findUnique: async () => user },
highestRank: async () => highest,
};
}
describe("resolveAuthorizationState", () => {
it("uses current database rank and highest rank 2000", async () => {
await expect(resolveAuthorizationState(7, db({ id: 7, username: "root", rank: 2000 }, 2000))).resolves.toEqual({ actor: { id: 7, username: "root", rank: 2000 }, highestRank: 2000 });
});
it("returns null for a deleted user", async () => expect(resolveAuthorizationState(7, db(null, 2000))).resolves.toBeNull());
it("fails closed when there are no ranks", async () => expect(resolveAuthorizationState(7, db({ id: 7, username: "root", rank: 1 }, null))).resolves.toEqual({ actor: { id: 7, username: "root", rank: 1 }, highestRank: null }));
});
+15
View File
@@ -0,0 +1,15 @@
import type { AuthorizationActor } from "@/lib/admin/authorization-policy";
export interface RankAuthorityDb {
user: { findUnique(args: { where: { id: number }; select: { id: true; username: true; rank: true } }): Promise<{ id: number; username: string; rank: number } | null> };
highestRank(): Promise<number | null>;
}
export async function resolveAuthorizationState(userId: number, db: RankAuthorityDb): Promise<{ actor: AuthorizationActor; highestRank: number | null } | null> {
const [user, highestRank] = await Promise.all([
db.user.findUnique({ where: { id: userId }, select: { id: true, username: true, rank: true } }),
db.highestRank(),
]);
if (!user) return null;
return { actor: user, highestRank };
}
+44 -39
View File
File diff suppressed because it is too large. Load diff
+4 -4
View File
@@ -2,13 +2,13 @@ import { describe, expect, it } from "vitest";
import { shouldRedirectAdminRequest } from "./proxy-access"; import { shouldRedirectAdminRequest } from "./proxy-access";
describe("shouldRedirectAdminRequest", () => { describe("shouldRedirectAdminRequest", () => {
it("redirects anonymous and non-staff admin requests before rendering", () => { it("redirects anonymous admin requests before rendering", () => {
expect(shouldRedirectAdminRequest("/admin", null)).toBe(true); expect(shouldRedirectAdminRequest("/admin", null)).toBe(true);
expect(shouldRedirectAdminRequest("/admin/tickets", { rank: 1 })).toBe(true);
}); });
it("allows staff admin requests and never affects public routes", () => { it("defers every authenticated rank to database authorization", () => {
expect(shouldRedirectAdminRequest("/admin/permissions", { rank: 7 })).toBe(false); expect(shouldRedirectAdminRequest("/admin/permissions", { rank: 1 })).toBe(false);
expect(shouldRedirectAdminRequest("/admin/permissions", { rank: 2000 })).toBe(false);
expect(shouldRedirectAdminRequest("/news", null)).toBe(false); expect(shouldRedirectAdminRequest("/news", null)).toBe(false);
}); });
}); });
+1 -2
View File
@@ -4,6 +4,5 @@ export interface ProxyToken {
export function shouldRedirectAdminRequest(pathname: string, token: ProxyToken | null): boolean { export function shouldRedirectAdminRequest(pathname: string, token: ProxyToken | null): boolean {
if (pathname !== "/admin" && !pathname.startsWith("/admin/")) return false; if (pathname !== "/admin" && !pathname.startsWith("/admin/")) return false;
const rank = typeof token?.rank === "number" ? token.rank : Number(token?.rank); return token === null;
return !Number.isInteger(rank) || rank < 7;
} }