Skip to content

Commit ff2cb45

Browse files
authored
Jetty 9.4.x 7801 duplicate set session cookies (#7809)
* Issue #7801 Duplicate session cookies after session id change. Signed-off-by: Jan Bartel <[email protected]>
1 parent 5b4d1dd commit ff2cb45

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

jetty-security/src/test/java/org/eclipse/jetty/security/ConstraintTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,6 @@ public void testFormRedirect() throws Exception
10131013
"Cookie: JSESSIONID=" + session + "\r\n" +
10141014
"\r\n");
10151015
assertThat(response, startsWith("HTTP/1.1 200 OK"));
1016-
assertThat(response, containsString("JSESSIONID=" + session));
10171016

10181017
response = _connector.getResponse("GET /ctx/admin/info HTTP/1.0\r\n" +
10191018
"Cookie: JSESSIONID=" + session + "\r\n" +

jetty-server/src/main/java/org/eclipse/jetty/server/session/AbstractSessionCache.java

+3
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,9 @@ public void release(String id, Session session) throws Exception
548548
//don't do anything with the session until the last request for it has finished
549549
if ((session.getRequests() <= 0))
550550
{
551+
//reset the idchanged flag
552+
session.setIdChanged(false);
553+
551554
//save the session
552555
if (!_sessionDataStore.isPassivating())
553556
{

tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionRenewTest.java

+11-16
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.junit.jupiter.api.Assertions.assertFalse;
4141
import static org.junit.jupiter.api.Assertions.assertNotNull;
4242
import static org.junit.jupiter.api.Assertions.assertNotSame;
43+
import static org.junit.jupiter.api.Assertions.assertNull;
4344
import static org.junit.jupiter.api.Assertions.assertTrue;
4445

4546
/**
@@ -183,8 +184,6 @@ public void doTest(RenewalVerifier verifier) throws Exception
183184
String contextPath = "";
184185
String servletMapping = "/server";
185186
WebAppContext context = _server.addWebAppContext(".", contextPath);
186-
TestHttpChannelCompleteListener scopeListener = new TestHttpChannelCompleteListener();
187-
_server.getServerConnector().addBean(scopeListener);
188187
context.setParentLoaderPriority(true);
189188
context.addServlet(TestServlet.class, servletMapping);
190189
TestHttpSessionIdListener testListener = new TestHttpSessionIdListener();
@@ -199,33 +198,29 @@ public void doTest(RenewalVerifier verifier) throws Exception
199198
client.start();
200199

201200
//make a request to create a session
202-
CountDownLatch synchronizer = new CountDownLatch(1);
203-
scopeListener.setExitSynchronizer(synchronizer);
204201
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
205202
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
206-
207-
//ensure request has finished being handled
208-
synchronizer.await(5, TimeUnit.SECONDS);
209203

210204
String sessionCookie = response.getHeaders().get("Set-Cookie");
211205
assertTrue(sessionCookie != null);
212206
assertFalse(testListener.isCalled());
213207

214208
//make a request to change the sessionid
215-
synchronizer = new CountDownLatch(1);
216-
scopeListener.setExitSynchronizer(synchronizer);
217209
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=renew");
218210
ContentResponse renewResponse = request.send();
219211
assertEquals(HttpServletResponse.SC_OK, renewResponse.getStatus());
220-
221-
//ensure request has finished being handled
222-
synchronizer.await(5, TimeUnit.SECONDS);
223212

224213
String renewSessionCookie = renewResponse.getHeaders().get("Set-Cookie");
225214
assertNotNull(renewSessionCookie);
226215
assertNotSame(sessionCookie, renewSessionCookie);
227216
assertTrue(testListener.isCalled());
228217

218+
//make another request and check the cookie isn't set again
219+
request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=check");
220+
ContentResponse checkResponse = request.send();
221+
assertEquals(HttpServletResponse.SC_OK, checkResponse.getStatus());
222+
assertNull(checkResponse.getHeaders().get("Set-Cookie"));
223+
229224
if (verifier != null)
230225
verifier.verify(context, TestServer.extractSessionId(sessionCookie), TestServer.extractSessionId(renewSessionCookie));
231226
}
@@ -315,10 +310,10 @@ else if ("renew".equals(action))
315310

316311
assertTrue(sessionIdManager.isIdInUse(afterSessionId)); //new session id should be in use
317312
assertFalse(sessionIdManager.isIdInUse(beforeSessionId));
318-
319-
320-
if (((Session)afterSession).isIdChanged())
321-
((org.eclipse.jetty.server.Response)response).replaceCookie(sessionManager.getSessionCookie(afterSession, request.getContextPath(), request.isSecure()));
313+
}
314+
else
315+
{
316+
request.getSession(false);
322317
}
323318
}
324319
}

0 commit comments

Comments
 (0)