Re-evaluate SAML admin groups on Admin Mode re-entry
What does this MR do and why?
Re-entering Admin Mode through SAML now re-evaluates the user's SAML admin_groups membership. A user removed from the IdP admin group can no longer enter Admin Mode, and loses the admin flag at that point instead of at their next full sign-in.
Details
admin_mode_flow only checked that the SAML identity matched the signed-in user. The admin flag is recomputed from admin_groups only in sign_in_user_flow, so after the six-hour Admin Mode timeout a demoted user could re-enter Admin Mode with an existing GitLab session and IdP session.
For SAML providers, admin_mode_flow now calls auth_user.save (in the EE controller, where admin_groups lives) and refuses Admin Mode unless the saved user is still active and can access the admin area. This persists the admin, auditor and external flags recomputed from the assertion, so user_admin_status_updated still fires. EE find_user also blocks users who are no longer in required_groups, which now refuses Admin Mode in the same request.
We deliberately call save rather than find_and_update!, the sign-in entry point. Admin Mode re-entry is not a sign-in, so it does not re-run the SAML group membership sync or the Duo seat sync. Running those on every re-entry could strip memberships or a billable seat mid-session from a thinner re-auth assertion, and would put more failure points between an admin and Admin Mode.
New behaviour: if the SAML provider is in disabled_oauth_sign_in_sources, save raises SigninDisabledForProviderError, which is rescued, and Admin Mode re-entry through that provider is refused. On master, re-entry through a disabled provider was allowed. The Admin Mode sign-in page already hides disabled providers, so only a direct or IdP-initiated request reaches this path. The user sees "Invalid login or password" rather than the disabled-provider message sign-in shows.
This is option (b) from the issue discussion and is not a breaking change: it only affects instances that configure admin_groups or required_groups, and users still in those groups see no change. Sending ForceAuthn and validating AuthnInstant (option (a)) is a breaking change and is tracked separately.
Out of scope, noted for follow-up:
- OIDC
admin_groupshas the same re-entry gap: fixed in the stacked !264764 (merged). - Password-based Admin Mode re-entry (
Admin::SessionsController) does not re-check SAML groups. - A SAML identity with
trusted_extern_uid: falsealready returns a 500 on Admin Mode re-entry onmaster, becausebuild_auth_userraises before this code runs.
The admin_groups section of doc/integration/saml.md now says that SAML Admin Mode re-entry re-evaluates it.
References
Screenshots or screen recordings
Not applicable: no UI changes. A refused re-entry uses the existing "Invalid login or password" redirect to the Admin Mode sign-in page.
How to set up and validate locally
- Configure a SAML provider in GDK (for example Keycloak) with
groups_attributeandadmin_groups: ['Admins']. - Sign in through SAML as a user in the
AdminsIdP group, then enter Admin Mode. - In the IdP, remove the user from
Admins. - In GitLab, leave Admin Mode (or wait for it to expire) and enter it again through SAML.
- Confirm Admin Mode is refused and that
User.find_by(username: '<user>').admin?returnsfalsein the Rails console. - Add the user back to
Adminsand confirm Admin Mode re-entry works again.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
This touches authentication and Admin Mode, so it needs AppSec review. A backport to 19.5 was requested in the issue.