Skip to content

Commit 87f38d8

Browse files
committed
Prevent Save @transient Authentication with existing HttpSession
Previously, @transient Authentication would get saved if an existing HttpSession existed but it shouldn't. This commit always prevents @transient Authentication from being saved. Closes gh-9992
1 parent fe99c3b commit 87f38d8

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

web/src/main/java/org/springframework/security/web/context/HttpSessionSecurityContextRepository.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ public void setSpringSecurityContextKey(String springSecurityContextKey) {
233233
}
234234

235235
private boolean isTransientAuthentication(Authentication authentication) {
236+
if (authentication == null) {
237+
return false;
238+
}
236239
return AnnotationUtils.getAnnotation(authentication.getClass(), Transient.class) != null;
237240
}
238241

@@ -327,6 +330,9 @@ final class SaveToSessionResponseWrapper extends SaveContextOnUpdateOrErrorRespo
327330
@Override
328331
protected void saveContext(SecurityContext context) {
329332
final Authentication authentication = context.getAuthentication();
333+
if (isTransientAuthentication(authentication)) {
334+
return;
335+
}
330336
HttpSession httpSession = this.request.getSession(false);
331337
String springSecurityContextKey = HttpSessionSecurityContextRepository.this.springSecurityContextKey;
332338
// See SEC-776
@@ -370,9 +376,6 @@ private boolean contextChanged(SecurityContext context) {
370376
}
371377

372378
private HttpSession createNewSessionIfAllowed(SecurityContext context, Authentication authentication) {
373-
if (isTransientAuthentication(authentication)) {
374-
return null;
375-
}
376379
if (this.httpSessionExistedAtStartOfRequest) {
377380
this.logger.debug("HttpSession is now null, but was not null at start of request; "
378381
+ "session was invalidated, so do not create a new session");

web/src/test/java/org/springframework/security/web/context/HttpSessionSecurityContextRepositoryTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.Retention;
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
24+
import java.util.Collections;
2425

2526
import javax.servlet.Filter;
2627
import javax.servlet.ServletException;
@@ -614,6 +615,21 @@ public void saveContextWhenTransientAuthenticationSubclassThenSkipped() {
614615
assertThat(session).isNull();
615616
}
616617

618+
@Test
619+
public void saveContextWhenTransientAuthenticationAndSessionExistsThenSkipped() {
620+
HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
621+
MockHttpServletRequest request = new MockHttpServletRequest();
622+
request.getSession(); // ensure the session exists
623+
MockHttpServletResponse response = new MockHttpServletResponse();
624+
HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
625+
SecurityContext context = repo.loadContext(holder);
626+
SomeTransientAuthentication authentication = new SomeTransientAuthentication();
627+
context.setAuthentication(authentication);
628+
repo.saveContext(context, holder.getRequest(), holder.getResponse());
629+
MockHttpSession session = (MockHttpSession) request.getSession(false);
630+
assertThat(Collections.list(session.getAttributeNames())).isEmpty();
631+
}
632+
617633
@Test
618634
public void saveContextWhenTransientAuthenticationWithCustomAnnotationThenSkipped() {
619635
HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();

0 commit comments

Comments
 (0)