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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ private List<UserSessionModel> handleLimitExceeded(AuthenticationFlowContext con
*/
private List<UserSessionModel> logoutOldestSessions(List<UserSessionModel> userSessions, long limit, EventBuilder eventBuilder) {
long numberOfSessionsThatNeedToBeLoggedOut = getNumberOfSessionsThatNeedToBeLoggedOut(userSessions.size(), limit);
if (numberOfSessionsThatNeedToBeLoggedOut <= 0) {
return Collections.emptyList();
}
if (numberOfSessionsThatNeedToBeLoggedOut == 1) {
logger.info("Logging out oldest session");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,40 @@ public void testClientSessionCountExceededAndNewSessionDeniedDirectGrantFlow() t
assertEquals(ERROR_TO_DISPLAY, response.getError());
}

@Test
public void testRealmSessionLimitExceededButClientLimitNotExceededShouldNotThrow() throws Exception {
// Reproduces: realm limit exceeded on client-a, then login to client-b whose client
// session count is below the client limit. Without the fix, logoutOldestSessions()
// receives a negative count and Stream.limit() throws IllegalArgumentException.
try {
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.BEHAVIOR, UserSessionLimitsAuthenticatorFactory.TERMINATE_OLDEST_SESSION);
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.USER_REALM_LIMIT, "2");
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.USER_CLIENT_LIMIT, "3");

AccessTokenResponse response = oauth.client("direct-grant-1", "password")
.doPasswordGrantRequest("test-user@localhost", "password");
assertEquals(200, response.getStatusCode());

response = oauth.client("direct-grant-1", "password")
.doPasswordGrantRequest("test-user@localhost", "password");
assertEquals(200, response.getStatusCode());

// realm limit reached (2 sessions on direct-grant-1); direct-grant-2 has 0 sessions,
// below the client limit of 3. login must succeed and must not throw a 500.
response = oauth.client("direct-grant-2", "password")
.doPasswordGrantRequest("test-user@localhost", "password");
assertEquals(200, response.getStatusCode());

testingClient.server(realmName).run(assertSessionCount(realmName, username, 2));
testingClient.server(realmName).run(assertClientSessionCount(realmName, username, "direct-grant-1", 1));
testingClient.server(realmName).run(assertClientSessionCount(realmName, username, "direct-grant-2", 1));
} finally {
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.BEHAVIOR, UserSessionLimitsAuthenticatorFactory.DENY_NEW_SESSION);
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.USER_REALM_LIMIT, "0");
setAuthenticatorConfigItem(DefaultAuthenticationFlows.DIRECT_GRANT_FLOW, UserSessionLimitsAuthenticatorFactory.USER_CLIENT_LIMIT, "1");
}
}

@Test
public void testRealmSessionCountAndClientSessionCountExceededAndOldestClientSessionShouldBePrioritized() throws Exception {
try {
Expand Down
0