Skip to content

fix: Honor SUPER_ADMIN_EMAILS for the first user and on all login paths (OHE-4161) - #1370

Merged
tofarr merged 4 commits into
mainfrom
ohe-4161-super-admin-emails
Nov 19, 2026
Merged

tofarr merged 4 commits into
mainfrom
ohe-4161-super-admin-emails

Conversation

@tofarr

@tofarr tofarr commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

  • A human has tested these changes.

Tested locally with enviroment variable set and not set.

AGENT:


Why

SUPER_ADMIN_EMAILS is the operator-defined list of super admins, but (1) UserStore.create_user and 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 in oauth_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.
  • New 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).
  • IDP bootstrap signup accepts only listed emails when the list is set.
  • 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.
  • Migration 138 skips the "promote oldest user" backfill when SUPER_ADMIN_EMAILS is 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 three create_user cases 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

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • Behavior change for installs with SUPER_ADMIN_EMAILS set: the first user is no longer auto-promoted unless listed.
  • No schema changes.

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:

ghcr.io/openhands/enterprise-server:sha-5b6d315

Co-authored-by: openhands <openhands@all-hands.dev>
@tofarr
tofarr requested a review from all-hands-bot November 13, 2026 16:33
@tofarr
tofarr marked this pull request as ready for review November 13, 2026 16:33
@all-hands-bot

all-hands-bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Review posted for 5b6d3158fa2b

Verdict: 🔄 CHANGES REQUESTED. See the review.

This is an automated status.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This PR contains migrations. Please synchronize before merging to prevent conflicts.

@tofarr tofarr changed the title Honor SUPER_ADMIN_EMAILS for the first user and on all login paths Honor SUPER_ADMIN_EMAILS for the first user and on all login paths (OHE-4161) Nov 19, 2026
@tofarr tofarr changed the title Honor SUPER_ADMIN_EMAILS for the first user and on all login paths (OHE-4161) fix: Honor SUPER_ADMIN_EMAILS for the first user and on all login paths (OHE-4161) Nov 19, 2026
@github-actions github-actions Bot added the type: fix A bug fix label Nov 19, 2026
Co-authored-by: openhands <openhands@all-hands.dev>

@hieptl hieptl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🙏

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  migrations/versions
  138_backfill_initial_superadmin.py 23-24
  server/routes
  auth.py
  idp.py
  oauth_v2.py 790
  saml.py 376
  storage
  user_store.py
Project Total  

This report was generated by python-coverage-comment-action

@tofarr
tofarr merged commit 763c8e2 into main Nov 19, 2026
29 checks passed
@tofarr
tofarr deleted the ohe-4161-super-admin-emails branch November 19, 2026 13:23

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_user now promotes any listed email at creation and never reads user_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 the admin role.
  • grant_super_admin_if_listed(user) runs right after the user is loaded, before the link_provider return leg and before the email_verified gate.

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 when user_info.get('email_verified') is true.
  • In keycloak_callback, move grant_super_admin_if_listed below the email_verified gate.

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

Comment thread storage/user_store.py
)
if existing_user_count == 0:
superadmin_role = None
if is_super_admin_email(user_info.get('email')):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread server/routes/auth.py
detail=f'Failed to authenticate user {user_info.email}',
)

await UserStore.grant_super_admin_if_listed(user)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@openhands-release-bot openhands-release-bot Bot added the released: 1.95.0 Shipped in 1.95.0 label Nov 19, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.95.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released: 1.95.0 Shipped in 1.95.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants