Repository navigation
fix: Honor SUPER_ADMIN_EMAILS for the first user and on all login paths (OHE-4161) - #1370
Conversation
Co-authored-by: openhands <openhands@all-hands.dev>
Review posted for
|
|
|
Co-authored-by: openhands <openhands@all-hands.dev>
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
The PR was merged at 13:23Z while this review was running, and the head was still 5b6d315. Treat the finding below as a follow-up fix.
The overall approach works. The first user is no longer auto-promoted when the list is set, the list now applies on every login path, and bootstrap sign-up is limited to listed emails. Migration 138 only skips its backfill on installs that have not run it yet. CI passed on this head. I also ran the focused suites locally (test_user_store, test_idp, test_saml_routes, test_oauth_v2_routes, test_auth_onboarding, test_auth_routes) against a real Postgres database: 456 passed.
Blocking: the legacy Keycloak path grants super admin before the email is verified. keycloak_callback treats email_verified=false as "not logged in yet": it sends a verification email and redirects without setting the session cookie. This PR adds two grants that run before that check, and both look only at the email:
UserStore.create_usernow promotes any listed email at creation and never readsuser_info['email_verified']. I checked this with a throwaway test:create_user(id, {'email': 'boss@example.com', 'email_verified': False})with that email listed returns a user with theadminrole.grant_super_admin_if_listed(user)runs right after the user is loaded, before thelink_providerreturn leg and before theemail_verifiedgate.
As a result, anyone whose Keycloak account carries an unverified listed address gets a super-admin role saved on their account. That can happen through an IdP with trustEmail=false or through realm self-registration. On a fresh install they also become setup_user_id. Before this PR, the Keycloak path never granted super admin based on an email address.
The link_provider return leg also sets the auth cookie before the verification check, and REQUIRE_VERIFIED_EMAILS defaults to off. So that leg is a pre-existing way to get a session without verifying, and the new grant turns it into a super-admin session.
Suggested fix:
- In
create_user, only apply the list whenuser_info.get('email_verified')is true. - In
keycloak_callback, movegrant_super_admin_if_listedbelow theemail_verifiedgate.
OAuth v2 and SAML deliberately trust the email from an admin-configured provider (SAML hard-codes email_verified: True), so those paths are unaffected.
🔄 CHANGES REQUESTED
| ) | ||
| if existing_user_count == 0: | ||
| superadmin_role = None | ||
| if is_super_admin_email(user_info.get('email')): |
There was a problem hiding this comment.
This promotes a listed email no matter what user_info['email_verified'] says. keycloak_callback calls create_user before its email-verification gate, so an unverified Keycloak account that claims a listed address gets saved as super admin, and on a fresh install as setup_user_id too. I reproduced this with {'email': 'boss@example.com', 'email_verified': False}: the user is created with the admin role. Suggest also requiring user_info.get('email_verified') here. OAuth v2 and SAML still grant on login through grant_super_admin_if_listed.
| detail=f'Failed to authenticate user {user_info.email}', | ||
| ) | ||
|
|
||
| await UserStore.grant_super_admin_if_listed(user) |
There was a problem hiding this comment.
This grant runs before the link_provider return leg and before the email_verified check further down. A user whose Keycloak email is unverified, and who is about to be redirected to email verification, still gets the super-admin role. Through the link_provider leg they also get a session cookie, because REQUIRE_VERIFIED_EMAILS is off by default. Please move this call below the if not email_verified: block, or check user_info.email_verified here.
|
🚀 Released in 1.95.0. |
HUMAN:
Tested locally with enviroment variable set and not set.
AGENT:
Why
SUPER_ADMIN_EMAILSis the operator-defined list of super admins, but (1)UserStore.create_userand migration 138 always promoted the first/oldest user even when the list was set and they weren't on it, and (2) the list was only applied inoauth_v2_callback, so listed users logging in via Keycloak, SAML or the local IDP were never promoted.Summary
create_user: a listed email (case-insensitive) becomes super admin on creation; with the list set, the first user is promoted only if listed; with the list unset, behavior is unchanged.UserStore.grant_super_admin_if_listed, called on login in the legacy Keycloak callback, SAML ACS, the IDP login/signup/invite flow (_complete_idp_login) and OAuth v2 (replaces its inline check).setup_user_id(install wizard) goes to the first user who becomes super admin while none exists, so a listed user runs the wizard even if an unlisted user signed up first. Instances that already have a super admin never get one.SUPER_ADMIN_EMAILSis set (reads the env var directly). No effect on instances that already ran it.Issue Number
Linear OHE-4161
How to Test
uv run pytest tests/unit/test_user_store.py tests/unit/server/routes/test_idp.py tests/unit/server/routes/test_saml_routes.py tests/unit/server/routes/test_oauth_v2_routes.py tests/unit/server/routes/test_auth_onboarding.py tests/unit/server/auth(671 passed locally). New tests cover the threecreate_usercases from the issue, setup-user assignment,grant_super_admin_if_listed, IDP signup restriction and IDP login promotion. Not covered by tests: Keycloak/SAML login-path wiring and migration 138 (not exercised end to end).Video/Screenshots
N/A (backend only).
Type
Notes
SUPER_ADMIN_EMAILSset: the first user is no longer auto-promoted unless listed.This PR was created by an AI agent (OpenHands) on behalf of the requester.
@tofarr can click here to continue refining the PR
Enterprise server image for this PR: