Skip to content

Commit 81bd6bd

Browse files
ruslanyseleftherias
authored andcommitted
Fix memory leak with null principal in Redis
Closes gh-1987
1 parent 2e8c429 commit 81bd6bd

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

spring-session-data-redis/src/integration-test/java/org/springframework/session/data/redis/RedisIndexedSessionRepositoryITests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,30 @@ void changeSessionIdWhenPrincipalNameChangesThenNewPrincipalMapsToNewSessionId()
527527
assertThat(findByPrincipalName.keySet()).containsOnly(changeSessionId);
528528
}
529529

530+
@Test // gh-1987
531+
void changeSessionIdWhenPrincipalNameChangesFromNullThenIndexShouldNotBeCreated() {
532+
String principalName = null;
533+
String principalNameChanged = "findByChangedPrincipalName" + UUID.randomUUID();
534+
RedisSession toSave = this.repository.createSession();
535+
toSave.setAttribute(INDEX_NAME, principalName);
536+
537+
this.repository.save(toSave);
538+
539+
RedisSession findById = this.repository.findById(toSave.getId());
540+
String changeSessionId = findById.changeSessionId();
541+
findById.setAttribute(INDEX_NAME, principalNameChanged);
542+
this.repository.save(findById);
543+
544+
Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME,
545+
principalName);
546+
assertThat(findByPrincipalName).isEmpty();
547+
548+
findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalNameChanged);
549+
550+
assertThat(findByPrincipalName).hasSize(1);
551+
assertThat(findByPrincipalName.keySet()).containsOnly(changeSessionId);
552+
}
553+
530554
@Test
531555
void changeSessionIdWhenOnlyChangeId() {
532556
String attrName = "changeSessionId";

spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,13 @@ private void saveChangeSessionId() {
858858
catch (NonTransientDataAccessException ex) {
859859
handleErrNoSuchKeyError(ex);
860860
}
861-
String originalPrincipalRedisKey = getPrincipalKey(this.originalPrincipalName);
862-
RedisIndexedSessionRepository.this.sessionRedisOperations.boundSetOps(originalPrincipalRedisKey)
863-
.remove(this.originalSessionId);
864-
RedisIndexedSessionRepository.this.sessionRedisOperations.boundSetOps(originalPrincipalRedisKey)
865-
.add(sessionId);
861+
if (this.originalPrincipalName != null) {
862+
String originalPrincipalRedisKey = getPrincipalKey(this.originalPrincipalName);
863+
RedisIndexedSessionRepository.this.sessionRedisOperations.boundSetOps(originalPrincipalRedisKey)
864+
.remove(this.originalSessionId);
865+
RedisIndexedSessionRepository.this.sessionRedisOperations.boundSetOps(originalPrincipalRedisKey)
866+
.add(sessionId);
867+
}
866868
}
867869
this.originalSessionId = sessionId;
868870
}

0 commit comments

Comments
 (0)