Skip to content

Commit 32d648b

Browse files
author
Denis
authored
fix: reset session field only when HTTP session is invalidated (#11416)
fixes #6959
1 parent dd6ff5d commit 32d648b

6 files changed

Lines changed: 276 additions & 1 deletion

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,13 @@ public void removeSession(WrappedSession wrappedSession) {
21932193
* the underlying HTTP session
21942194
*/
21952195
protected void removeFromHttpSession(WrappedSession wrappedSession) {
2196+
VaadinSession vaadinSession = readFromHttpSession(wrappedSession);
2197+
if (vaadinSession != null) {
2198+
// mark Vaadin session as closed explicitly (opposite to closing as
2199+
// a result of invalidated HTTP session)
2200+
vaadinSession.setAttribute(VaadinSession.CLOSE_SESSION_EXPLICITLY,
2201+
true);
2202+
}
21962203
wrappedSession.removeAttribute(getSessionAttributeName());
21972204

21982205
}

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable {
7878
@Deprecated
7979
public static final String UI_PARAMETER = InitParameters.UI_PARAMETER;
8080

81+
static final String CLOSE_SESSION_EXPLICITLY = "vaadin.close-session."
82+
+ UUID.randomUUID().toString();
83+
8184
/**
8285
* Configuration for the session.
8386
*/
@@ -202,7 +205,21 @@ && getCurrent() == this) {
202205
// it as soon as we acquire the lock.
203206
service.fireSessionDestroy(this);
204207
}
205-
session = null;
208+
// Vaadin session attribute is removed from HTTP session in two cases:
209+
// either Vaadin session is closed explicitly by VaadinService or HTTP
210+
// session is invalidated (and all attributes are removed from it). In
211+
// the latter case the session field should be set to {@code null}
212+
// immediately to avoid using HTTP session object which is invalid (all
213+
// methods throw exceptions).
214+
try {
215+
lock();
216+
if (getAttribute(CLOSE_SESSION_EXPLICITLY) == null) {
217+
session = null;
218+
}
219+
setAttribute(CLOSE_SESSION_EXPLICITLY, null);
220+
} finally {
221+
unlock();
222+
}
206223
}
207224

208225
/**
@@ -887,6 +904,9 @@ assert isValidChange(state) : "Invalid session state change "
887904
+ this.state + "->" + state;
888905

889906
this.state = state;
907+
if (VaadinSessionState.CLOSED.equals(state)) {
908+
session = null;
909+
}
890910
}
891911

892912
private boolean isValidChange(VaadinSessionState newState) {

‎flow-server/src/test/java/com/vaadin/flow/server/VaadinServiceTest.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Optional;
2727
import java.util.Set;
2828
import java.util.concurrent.atomic.AtomicBoolean;
29+
import java.util.concurrent.atomic.AtomicReference;
2930
import java.util.stream.Collectors;
3031
import java.util.stream.Stream;
3132

@@ -416,6 +417,46 @@ public void createRequestHandlers_pwaHandlerIsInList_webComponentHandlersAreInLi
416417
Assert.assertTrue(set.contains(WebComponentBootstrapHandler.class));
417418
}
418419

420+
@Test
421+
public void fireSessionDestroy_sessionStateIsSetToClosed()
422+
throws ServletException, ServiceException {
423+
VaadinService service = createService();
424+
425+
AtomicReference<VaadinSessionState> stateRef = new AtomicReference<>();
426+
MockVaadinSession vaadinSession = new MockVaadinSession(service) {
427+
@Override
428+
protected void setState(VaadinSessionState state) {
429+
stateRef.set(state);
430+
}
431+
};
432+
433+
service.fireSessionDestroy(vaadinSession);
434+
435+
Assert.assertEquals(VaadinSessionState.CLOSED, stateRef.get());
436+
}
437+
438+
@Test
439+
public void removeFromHttpSession_setExplicitSessionCloseAttribute()
440+
throws ServiceException {
441+
WrappedSession httpSession = Mockito.mock(WrappedSession.class);
442+
VaadinSession session = Mockito.mock(VaadinSession.class);
443+
VaadinService service = new MockVaadinServletService() {
444+
445+
@Override
446+
protected VaadinSession readFromHttpSession(
447+
WrappedSession wrappedSession) {
448+
return session;
449+
}
450+
};
451+
452+
service.init();
453+
454+
service.removeFromHttpSession(httpSession);
455+
456+
Mockito.verify(session)
457+
.setAttribute(VaadinSession.CLOSE_SESSION_EXPLICITLY, true);
458+
}
459+
419460
private InstantiatorFactory createInstantiatorFactory(Lookup lookup) {
420461
InstantiatorFactory factory = Mockito.mock(InstantiatorFactory.class);
421462

‎flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionTest.java‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,4 +386,101 @@ public void csrfToken_same_session_shouldBeSame() {
386386
"getCsrfToken() should always return the same value for the same session",
387387
token1, token2);
388388
}
389+
390+
@Test
391+
public void valueUnbound_explicitVaadinSessionClose_wrappedSessionIsNotCleanedUp() {
392+
ReentrantLock lock = Mockito.mock(ReentrantLock.class);
393+
Mockito.when(lock.isHeldByCurrentThread()).thenReturn(true);
394+
mockService = new MockVaadinServletService() {
395+
@Override
396+
protected Lock getSessionLock(WrappedSession wrappedSession) {
397+
return lock;
398+
}
399+
400+
};
401+
402+
VaadinSession vaadinSession = new VaadinSession(mockService) {
403+
@Override
404+
public boolean hasLock() {
405+
return true;
406+
}
407+
};
408+
409+
vaadinSession.setAttribute(VaadinSession.CLOSE_SESSION_EXPLICITLY,
410+
true);
411+
412+
WrappedSession httpSession = Mockito.mock(WrappedSession.class);
413+
vaadinSession.refreshTransients(httpSession, mockService);
414+
415+
VaadinSession.setCurrent(vaadinSession);
416+
mockService.setCurrentInstances(Mockito.mock(VaadinRequest.class),
417+
Mockito.mock(VaadinResponse.class));
418+
419+
try {
420+
vaadinSession
421+
.valueUnbound(Mockito.mock(HttpSessionBindingEvent.class));
422+
423+
Assert.assertNotNull(vaadinSession.getSession());
424+
} finally {
425+
CurrentInstance.clearAll();
426+
}
427+
}
428+
429+
@Test
430+
public void valueUnbound_implicitVaadinSessionClose_wrappedSessionIsCleanedUp() {
431+
ReentrantLock lock = Mockito.mock(ReentrantLock.class);
432+
Mockito.when(lock.isHeldByCurrentThread()).thenReturn(true);
433+
mockService = new MockVaadinServletService() {
434+
@Override
435+
protected Lock getSessionLock(WrappedSession wrappedSession) {
436+
return lock;
437+
}
438+
439+
};
440+
441+
VaadinSession vaadinSession = new VaadinSession(mockService) {
442+
@Override
443+
public boolean hasLock() {
444+
return true;
445+
}
446+
};
447+
448+
WrappedSession httpSession = Mockito.mock(WrappedSession.class);
449+
vaadinSession.refreshTransients(httpSession, mockService);
450+
451+
VaadinSession.setCurrent(vaadinSession);
452+
mockService.setCurrentInstances(Mockito.mock(VaadinRequest.class),
453+
Mockito.mock(VaadinResponse.class));
454+
455+
try {
456+
vaadinSession
457+
.valueUnbound(Mockito.mock(HttpSessionBindingEvent.class));
458+
459+
Assert.assertNull(vaadinSession.getSession());
460+
} finally {
461+
CurrentInstance.clearAll();
462+
}
463+
}
464+
465+
@Test
466+
public void setState_closedState_sessionFieldIsCleanedUp() {
467+
ReentrantLock lock = Mockito.mock(ReentrantLock.class);
468+
Mockito.when(lock.isHeldByCurrentThread()).thenReturn(true);
469+
mockService = new MockVaadinServletService() {
470+
@Override
471+
protected Lock getSessionLock(WrappedSession wrappedSession) {
472+
return lock;
473+
}
474+
};
475+
476+
VaadinSession vaadinSession = new VaadinSession(mockService);
477+
WrappedSession httpSession = Mockito.mock(WrappedSession.class);
478+
vaadinSession.refreshTransients(httpSession, mockService);
479+
480+
vaadinSession.setState(VaadinSessionState.CLOSING);
481+
vaadinSession.setState(VaadinSessionState.CLOSED);
482+
483+
Assert.assertNull(vaadinSession.getSession());
484+
}
485+
389486
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2000-2021 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.uitest.ui;
17+
18+
import com.vaadin.flow.component.AttachEvent;
19+
import com.vaadin.flow.component.html.Div;
20+
import com.vaadin.flow.component.html.NativeButton;
21+
import com.vaadin.flow.router.Route;
22+
import com.vaadin.flow.server.VaadinService;
23+
24+
@Route("com.vaadin.flow.uitest.ui.InvalidateHttpSessionView")
25+
public class InvalidateHttpSessionView extends Div {
26+
27+
private static class SessionId {
28+
29+
private String id;
30+
31+
private SessionId(String id) {
32+
this.id = id;
33+
}
34+
}
35+
36+
@Override
37+
protected void onAttach(AttachEvent attachEvent) {
38+
VaadinService service = attachEvent.getSession().getService();
39+
String id = attachEvent.getSession().getSession().getId();
40+
41+
Div div = new Div();
42+
div.setText(id);
43+
div.setId("current-session-id");
44+
45+
add(div);
46+
47+
SessionId closedSessionId = attachEvent.getSession().getService()
48+
.getContext().getAttribute(SessionId.class);
49+
if (closedSessionId != null) {
50+
div = new Div();
51+
div.setText(closedSessionId.id);
52+
div.setId("invalidated-session-id");
53+
add(div);
54+
}
55+
56+
service.addSessionDestroyListener(event -> {
57+
SessionId sessionId = new SessionId(id);
58+
service.getContext().setAttribute(SessionId.class, sessionId);
59+
});
60+
NativeButton button = new NativeButton("Invalidate HTTP session",
61+
event -> attachEvent.getSession().getSession().invalidate());
62+
add(button);
63+
button.setId("invalidate-session");
64+
}
65+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2000-2021 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.uitest.ui;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
import org.openqa.selenium.By;
21+
22+
import com.vaadin.flow.testutil.ChromeBrowserTest;
23+
24+
public class InvalidateHttpSessionIT extends ChromeBrowserTest {
25+
26+
@Test
27+
public void invalidateHttpSession_vaadinSessionIsClosed() {
28+
open();
29+
30+
String sessionId = findElement(By.id("current-session-id")).getText();
31+
32+
findElement(By.id("invalidate-session")).click();
33+
34+
waitForElementPresent(By.id("invalidated-session-id"));
35+
36+
String invalidatedSessionId = findElement(
37+
By.id("invalidated-session-id")).getText();
38+
39+
Assert.assertEquals(sessionId, invalidatedSessionId);
40+
41+
sessionId = findElement(By.id("current-session-id")).getText();
42+
Assert.assertNotEquals(sessionId, invalidatedSessionId);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)