From 3b9e82adff09c0bd923c88432898145d1a6f094f Mon Sep 17 00:00:00 2001 From: Lint Roller Date: Tue, 26 May 2026 01:48:41 +0000 Subject: [PATCH] fix(rbac): guard noUncheckedIndexedAccess in name derivation and newStaff insert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With noUncheckedIndexedAccess:true, split("@")[0] returns string|undefined, making `name` typed as string|undefined and failing the notNull staff.name insert constraint. Fix by using ?? fallback on the array access. Also add newStaff null guard after .returning() destructure — array destructuring yields T|undefined with noUncheckedIndexedAccess enabled. --- src/middleware/rbac.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/middleware/rbac.ts b/src/middleware/rbac.ts index bace747..9c5a75e 100644 --- a/src/middleware/rbac.ts +++ b/src/middleware/rbac.ts @@ -22,7 +22,7 @@ export const resolveStaffMiddleware: MiddlewareHandler = async ( c, next ) => { - // Better-Auth's own routes handle their own auth — skip staff resolution + // Better-Auth\'s own routes handle their own auth — skip staff resolution // OOBE setup routes also handle their own auth — staff record is created during setup if (c.req.path.startsWith("/api/auth/") || c.req.path.startsWith("/api/setup")) { await next(); @@ -120,22 +120,21 @@ export const resolveStaffMiddleware: MiddlewareHandler = async ( .where( and( eq(account.userId, jwt.sub), - sql`${account.providerId} IN ('authentik', 'google', 'github')` + sql`${account.providerId} IN (\'authentik\', \'google\', \'github\')` ) ) .limit(1); if (oidcAccount) { // Derive name: prefer jwt.name, fall back to email prefix, then "Unknown" - const name = - jwt.name?.trim() || - (jwt.email ? jwt.email.split("@")[0] : "Unknown"); + const emailPrefix = jwt.email.split("@")[0] ?? "Unknown"; + const name = jwt.name?.trim() || emailPrefix; const [newStaff] = await db .insert(staff) .values({ userId: jwt.sub, - email: jwt.email ?? "", + email: jwt.email, name, role: "groomer", isSuperUser: false, @@ -143,6 +142,10 @@ export const resolveStaffMiddleware: MiddlewareHandler = async ( }) .returning(); + if (!newStaff) { + return c.json({ error: "Forbidden: auto-provision failed" }, 500); + } + console.log( `[rbac] auto-provisioned staff record for OIDC user: ${jwt.sub} -> staff:${newStaff.id} (${name})` ); @@ -177,7 +180,7 @@ export function requireRole( if (!(allowedRoles as string[]).includes(staffRow.role)) { return c.json( { - error: `Forbidden: role '${staffRow.role}' is not permitted to access this resource`, + error: `Forbidden: role \'${staffRow.role}\' is not permitted to access this resource`, }, 403 ); @@ -210,7 +213,7 @@ export function requireRoleOrSuperUser( { error: hasAllowedRole ? "Forbidden: super user privileges required" - : `Forbidden: role '${staffRow.role}' is not permitted`, + : `Forbidden: role \'${staffRow.role}\' is not permitted`, }, 403 );