Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .server-changes/require-admin-during-impersonation.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 13 additions & 2 deletions apps/webapp/app/services/session.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
};
}

Expand Down