Skip to content

Commit 45550fe

Browse files
committed
feat(webapp): require the user is an admin during an impersonation session
1 parent 921285c commit 45550fe

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

apps/webapp/app/services/session.server.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ import { getImpersonationId } from "./impersonation.server";
66
export async function getUserId(request: Request): Promise<string | undefined> {
77
const impersonatedUserId = await getImpersonationId(request);
88

9-
if (impersonatedUserId) return impersonatedUserId;
9+
if (impersonatedUserId) {
10+
// Verify the real user (from the session cookie) is still an admin
11+
const authUser = await authenticator.isAuthenticated(request);
12+
if (authUser?.userId) {
13+
const realUser = await getUserById(authUser.userId);
14+
if (realUser?.admin) {
15+
return impersonatedUserId;
16+
}
17+
}
18+
// Admin revoked or session invalid — fall through to return the real user's ID
19+
return authUser?.userId;
20+
}
1021

1122
let authUser = await authenticator.isAuthenticated(request);
1223
return authUser?.userId;
@@ -54,7 +65,7 @@ export async function requireUser(request: Request) {
5465
dashboardPreferences: user.dashboardPreferences,
5566
confirmedBasicDetails: user.confirmedBasicDetails,
5667
mfaEnabledAt: user.mfaEnabledAt,
57-
isImpersonating: !!impersonationId,
68+
isImpersonating: !!impersonationId && impersonationId === userId,
5869
};
5970
}
6071

0 commit comments

Comments
 (0)