You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Username/email cache lookup can return a managed user from a different realm #55554
I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.
Area
infinispan
Bug Description
UserCacheSession.getUserById() only returns a session-managed user for a given id if that managed user actually belongs to the requested realm; otherwise it falls back to a fresh lookup. getUserByUsername() and getUserByEmail() check the same realm.getId().equals(managed.realmId()) condition, but only use it to decide whether to also cache the username/email query. The managed user itself is returned unconditionally, even when it belongs to a different realm than the one passed to the method.
Version
nightly
Regression
The issue is a regression
Expected behavior
If a managed user in the session has the same id as the user resolved by the username/email lookup but belongs to a different realm, the managed user must not be returned, mirroring the behavior of getUserById().
Actual behavior
getUserByUsername()/getUserByEmail() return the managed user even when its realm does not match the realm passed to the method.
How to Reproduce?
Within a single KeycloakSession, cause a user with id X to become session-managed for realm A (for example through getUserById(realmA, X)).
In the same session, call getUserByUsername(realmB, username) (or getUserByEmail) such that the underlying store resolves the username to a user that also has id X, but belongs to realm B.
The managed user from realm A is returned instead of realm B's user, or instead of falling back to a fresh lookup.
Anything else?
Follow-up to #54645. Introduced in #54646, which added the managed-user fast path for getUserByUsername/getUserByEmail but did not reuse the realm check from getUserById to gate the returned user, only the query caching.
Activity
Only use a managed user for getUserByUsername/getUserByEmail if it ma…