feat(webapp): require the user is an admin during an impersonation session (#3078)
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
area: webapp
|
||||
type: fix
|
||||
---
|
||||
|
||||
Require the user is an admin during an impersonation session. Previously only the impersonation cookie was checked; now the real user's admin flag is verified on every request. If admin has been revoked, the session falls back to the real user's ID.
|
||||
@@ -6,7 +6,18 @@ import { getImpersonationId } from "./impersonation.server";
|
||||
export async function getUserId(request: Request): Promise<string | undefined> {
|
||||
const impersonatedUserId = await getImpersonationId(request);
|
||||
|
||||
if (impersonatedUserId) return impersonatedUserId;
|
||||
if (impersonatedUserId) {
|
||||
// Verify the real user (from the session cookie) is still an admin
|
||||
const authUser = await authenticator.isAuthenticated(request);
|
||||
if (authUser?.userId) {
|
||||
const realUser = await getUserById(authUser.userId);
|
||||
if (realUser?.admin) {
|
||||
return impersonatedUserId;
|
||||
}
|
||||
}
|
||||
// Admin revoked or session invalid — fall through to return the real user's ID
|
||||
return authUser?.userId;
|
||||
}
|
||||
|
||||
let authUser = await authenticator.isAuthenticated(request);
|
||||
return authUser?.userId;
|
||||
@@ -54,7 +65,7 @@ export async function requireUser(request: Request) {
|
||||
dashboardPreferences: user.dashboardPreferences,
|
||||
confirmedBasicDetails: user.confirmedBasicDetails,
|
||||
mfaEnabledAt: user.mfaEnabledAt,
|
||||
isImpersonating: !!impersonationId,
|
||||
isImpersonating: !!impersonationId && impersonationId === userId,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user