8000
  • fix(session-limits): guard against negative stream limit when realm limit exceeded but client is not by mateenali66 · Pull Request #48062 · keycloak/keycloak · GitHub
    [go: up one dir, main page]

    Skip to content

    fix(session-limits): guard against negative stream limit when realm limit exceeded but client is not - #48062

    Closed
    mateenali66 wants to merge 1 commit into
    keycloak:mainfrom
    mateenali66:fix/48040-session-limit-negative-stream-limit
    Closed

    mateenali66 wants to merge 1 commit into
    keycloak:mainfrom
    mateenali66:fix/48040-session-limit-negative-stream-limit

    Conversation

    @mateenali66
    @mateenali66 mateenali66 commented Apr 14, 2026 •
    Copy link
    Copy Markdown
    Contributor

    when the realm session limit is exceeded and behavior is set to Terminate oldest session, the authenticator calls
    handleLimitExceeded(userSessionsForClient, userClientLimit) first.

    if the user has fewer existing sessions for the current client than userClientLimit - 1, getNumberOfSessionsThatNeedToBeLoggedOut() returns a negative value and Stream.limit(negative) throws
    IllegalArgumentException, producing a 500 on every login to that client.

    added an early return in logoutOldestSessions() for the case where numberOfSessionsThatNeedToBeLoggedOut <= 0. the existing second check (exceedsLimit(realmCount - removed.size(), realmLimit)) then picks up
    and removes a session using the realm-scoped list and realm limit, which always produces a non-negative count.

    also added a regression test in UserSessionLimitsTest that reproduces the scenario: realm limit=2, client limit=3, two logins on client-a to saturate the realm, then login on client-b (0 existing client sessions).

    Fixes #48040

    …xceeded but client is not
    
    When the realm session limit is exceeded but the user has fewer sessions
    for the current client than the configured client limit,
    getNumberOfSessionsThatNeedToBeLoggedOut() returns a negative value.
    Passing that to Stream.limit() throws IllegalArgumentException, causing
    an internal server error on login.
    
    Add an early return in logoutOldestSessions() when the computed count
    is <= 0 so the call to handleLimitExceeded() for the realm-level check
    proceeds to the correct path that uses the realm sessions list and the
    realm limit, which always yields a non-negative count.
    
    Fixes keycloak#48040
    Signed-off-by: Mateen Anjum <mateenali66@gmail.com>
    @mabartos
    Copy link
    Copy Markdown
    Member

    @mabartos mabartos closed this Apr 15, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    User session limit generates fatal error

    2 participants

    0