Merge pull request #86 from ashim-hq/fix/issue-72-auth-false-admin

fix: prevent admin escalation when AUTH_ENABLED=false
This commit is contained in:
Ashim
2026-04-21 23:42:18 +08:00
committed by GitHub
2 changed files with 10 additions and 19 deletions
+7 -9
View File
@@ -659,16 +659,14 @@ function isPublicRoute(url: string): boolean {
export async function authMiddleware(app: FastifyInstance): Promise<void> {
app.addHook("preHandler", async (request: FastifyRequest, reply: FastifyReply) => {
// When auth is disabled, attach the first admin user so requireAuth/requireAdmin pass
// When auth is disabled, attach a synthetic non-admin user so tools work
// but admin-only routes (user management, settings write, etc.) stay locked
if (!env.AUTH_ENABLED) {
const adminUser = db.select().from(schema.users).where(eq(schema.users.role, "admin")).get();
if (adminUser) {
(request as FastifyRequest & { user?: AuthUser }).user = {
id: adminUser.id,
username: adminUser.username,
role: "admin",
};
}
(request as FastifyRequest & { user?: AuthUser }).user = {
id: "anonymous",
username: "anonymous",
role: "user",
};
return;
}