Skip to content

Commit 2cfe7c4

Browse files
authored
Issue #10804 isRequestedSessionId should be false for invalid session (#10807)
* Issue #10804 isRequestedSessionId should be false for invalid session
1 parent 1ae620b commit 2cfe7c4

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletApiRequest.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import org.eclipse.jetty.server.Session;
8484
import org.eclipse.jetty.session.AbstractSessionManager;
8585
import org.eclipse.jetty.session.ManagedSession;
86+
import org.eclipse.jetty.session.SessionManager;
8687
import org.eclipse.jetty.util.Callback;
8788
import org.eclipse.jetty.util.Fields;
8889
import org.eclipse.jetty.util.HostPort;
@@ -161,9 +162,9 @@ public ServletRequestInfo getServletRequestInfo()
161162

162163
/**
163164
* @return The core {@link Request} associated with the servlet API request. This may differ
164-
* from {@link ServletContextRequest} as wrapped by the {@link ServletContextHandler} as it
165-
* may have been further wrapped before being passed
166-
* to {@link ServletChannel#associate(Request, Response, Callback)}.
165+
* from {@link ServletContextRequest} as wrapped by the {@link ServletContextHandler} as it
166+
* may have been further wrapped before being passed
167+
* to {@link ServletChannel#associate(Request, Response, Callback)}.
167168
* @see #getServletRequestInfo()
168169
* @see ServletChannel#associate(Request, Response, Callback)
169170
*/
@@ -420,7 +421,14 @@ public String changeSessionId()
420421
public boolean isRequestedSessionIdValid()
421422
{
422423
AbstractSessionManager.RequestedSession requestedSession = getServletRequestInfo().getRequestedSession();
423-
return requestedSession != null && requestedSession.sessionId() != null && requestedSession.session() != null;
424+
HttpSession session = getSession(false);
425+
SessionManager manager = getServletRequestInfo().getSessionManager();
426+
return requestedSession != null &&
427+
requestedSession.sessionId() != null &&
428+
requestedSession.session() != null &&
429+
requestedSession.session().isValid() &&
430+
manager != null &&
431+
manager.getSessionIdManager().getId(requestedSession.sessionId()).equals(session.getId());
424432
}
425433

426434
@Override
@@ -1049,7 +1057,6 @@ public BufferedReader getReader() throws IOException
10491057
if (charset == null)
10501058
charset = StandardCharsets.ISO_8859_1;
10511059
}
1052-
10531060
}
10541061
catch (IllegalCharsetNameException | UnsupportedCharsetException e)
10551062
{
@@ -1334,7 +1341,9 @@ private static class ServletCookieList extends AbstractList<HttpCookie>
13341341
_cookies = new Cookie[_httpCookies.size()];
13351342
int i = 0;
13361343
for (HttpCookie httpCookie : _httpCookies)
1344+
{
13371345
_cookies[i++] = convertCookie(httpCookie, compliance);
1346+
}
13381347
}
13391348

13401349
@Override

jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/SessionHandlerTest.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ public void testRequestedSessionIdFromCookie() throws Exception
434434
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
435435
content = response.getContentAsString();
436436
assertThat(content, containsString("valid=true"));
437+
438+
//Invalidate it
439+
response = client.GET(url + "?action=invalidate");
440+
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
441+
content = response.getContentAsString();
442+
assertThat(content, containsString("valid=false"));
437443
}
438444
finally
439445
{
@@ -504,14 +510,23 @@ public void testRequestedSessionIdFromURL() throws Exception
504510
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
505511
content = response.getContentAsString();
506512
assertThat(content, containsString("createdId="));
507-
String sessionId = content.substring(content.indexOf("createdId=") + 10);
513+
int i = content.indexOf("createdId=");
514+
String sessionId = content.substring(i + 10);
515+
i = sessionId.indexOf("\n");
516+
sessionId = sessionId.substring(0, i);
508517
sessionId = sessionId.trim();
509518

510519
//Check the requestedSessionId is valid
511520
response = client.GET(url + ";" + SessionConfig.__DefaultSessionIdPathParameterName + "=" + sessionId);
512521
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
513522
content = response.getContentAsString();
514523
assertThat(content, containsString("valid=true"));
524+
525+
//Invalidate it
526+
response = client.GET(url + "?action=invalidate;" + SessionConfig.__DefaultSessionIdPathParameterName + "=" + sessionId);
527+
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
528+
content = response.getContentAsString();
529+
assertThat(content, containsString("valid=false"));
515530
}
516531
finally
517532
{
@@ -556,13 +571,18 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
556571
{
557572
PrintWriter writer = response.getWriter();
558573
writer.println("requestedId=" + request.getRequestedSessionId());
559-
writer.println("valid=" + request.isRequestedSessionIdValid());
560574

561575
if ("create".equals(request.getParameter("action")))
562576
{
563577
HttpSession session = request.getSession(true);
564578
writer.println("createdId=" + session.getId());
565579
}
580+
else if ("invalidate".equals(request.getParameter("action")))
581+
{
582+
HttpSession session = request.getSession(false);
583+
session.invalidate();
584+
}
585+
writer.println("valid=" + request.isRequestedSessionIdValid());
566586
}
567587
}
568588

jetty-ee9/jetty-ee9-nested/src/test/java/org/eclipse/jetty/ee9/nested/SessionHandlerTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
124124
if (session == null)
125125
throw new IllegalStateException("No Session");
126126
session.invalidate();
127+
session = null;
127128
}
128129

129130
case "change" ->
@@ -432,6 +433,16 @@ public void testRequestedSessionIdFromCookie() throws Exception
432433
""".formatted(id));
433434
response = HttpTester.parseResponse(endPoint.getResponse());
434435
assertThat(response.getContent(), containsString("requestedSessionIdValid=true"));
436+
437+
//Invalidate and check requestedSessionId is invalid
438+
endPoint.addInput("""
439+
GET /invalidate HTTP/1.1
440+
Host: localhost
441+
Cookie: JSESSIONID=%s
442+
443+
""".formatted(id));
444+
response = HttpTester.parseResponse(endPoint.getResponse());
445+
assertThat(response.getContent(), containsString("requestedSessionIdValid=false"));
435446
}
436447

437448
@Test

0 commit comments

Comments
 (0)