Skip to content

Commit eb11c6f

Browse files
committed
Reinstate removal of jsessionid from lookup path
Closes gh-25864
1 parent ca7fb23 commit eb11c6f

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,8 @@ protected String determineEncoding(HttpServletRequest request) {
556556
* @return the updated URI string
557557
*/
558558
public String removeSemicolonContent(String requestUri) {
559-
return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri);
559+
return (this.removeSemicolonContent ?
560+
removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
560561
}
561562

562563
private String removeSemicolonContentInternal(String requestUri) {
@@ -570,6 +571,22 @@ private String removeSemicolonContentInternal(String requestUri) {
570571
return requestUri;
571572
}
572573

574+
private String removeJsessionid(String requestUri) {
575+
String key = ";jsessionid=";
576+
int index = requestUri.toLowerCase().indexOf(key);
577+
if (index == -1) {
578+
return requestUri;
579+
}
580+
String start = requestUri.substring(0, index);
581+
for (int i = key.length(); i < requestUri.length(); i++) {
582+
char c = requestUri.charAt(i);
583+
if (c == ';' || c == '/') {
584+
return start + requestUri.substring(i);
585+
}
586+
}
587+
return start;
588+
}
589+
573590
/**
574591
* Decode the given URI path variables via {@link #decodeRequestString} unless
575592
* {@link #setUrlDecode} is set to {@code true} in which case it is assumed
@@ -675,7 +692,13 @@ private boolean shouldRemoveTrailingServletPathSlash(HttpServletRequest request)
675692
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
676693
* </ul>
677694
*/
678-
public static final UrlPathHelper rawPathInstance = new UrlPathHelper();
695+
public static final UrlPathHelper rawPathInstance = new UrlPathHelper() {
696+
697+
@Override
698+
public String removeSemicolonContent(String requestUri) {
699+
return requestUri;
700+
}
701+
};
679702

680703
static {
681704
rawPathInstance.setAlwaysUseFullPath(true);

spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void getRequestKeepSemicolonContent() {
133133
assertThat(helper.getRequestUri(request)).isEqualTo("/foo;a=b;c=d");
134134

135135
request.setRequestURI("/foo;jsessionid=c0o7fszeb1");
136-
assertThat(helper.getRequestUri(request)).isEqualTo("/foo;jsessionid=c0o7fszeb1");
136+
assertThat(helper.getRequestUri(request)).isEqualTo("/foo");
137137
}
138138

139139
@Test

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ public void addContentDispositionHeader() throws Exception {
389389
assertContentDisposition(processor, true, "/hello.json;a=b;setup.dataless", "unknown ext in path params");
390390
assertContentDisposition(processor, true, "/hello.dataless;a=b;setup.json", "unknown ext in filename");
391391
assertContentDisposition(processor, false, "/hello.json;a=b;setup.json", "safe extensions");
392+
assertContentDisposition(processor, true, "/hello.json;jsessionid=foo.bar", "jsessionid shouldn't cause issue");
392393

393394
// encoded dot
394395
assertContentDisposition(processor, true, "/hello%2Edataless;a=b;setup.json", "encoded dot in filename");

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/UriTemplateServletAnnotationControllerHandlerMethodTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,6 +69,28 @@ public void simple() throws Exception {
6969
assertThat(response.getContentAsString()).isEqualTo("test-42-7");
7070
}
7171

72+
@Test // gh-25864
73+
public void literalMappingWithPathParams() throws Exception {
74+
initServletWithControllers(MultipleUriTemplateController.class);
75+
76+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/data");
77+
MockHttpServletResponse response = new MockHttpServletResponse();
78+
getServlet().service(request, response);
79+
assertThat(response.getStatus()).isEqualTo(200);
80+
assertThat(response.getContentAsString()).isEqualTo("test");
81+
82+
request = new MockHttpServletRequest("GET", "/data;foo=bar");
83+
response = new MockHttpServletResponse();
84+
getServlet().service(request, response);
85+
assertThat(response.getStatus()).isEqualTo(404);
86+
87+
request = new MockHttpServletRequest("GET", "/data;jsessionid=123");
88+
response = new MockHttpServletResponse();
89+
getServlet().service(request, response);
90+
assertThat(response.getStatus()).isEqualTo(200);
91+
assertThat(response.getContentAsString()).isEqualTo("test");
92+
}
93+
7294
@Test
7395
public void multiple() throws Exception {
7496
initServletWithControllers(MultipleUriTemplateController.class);
@@ -388,6 +410,10 @@ public void handle(@PathVariable("hotel") String hotel,
388410
writer.write("test-" + hotel + "-q" + qHotel + "-" + booking + "-" + other + "-q" + qOther);
389411
}
390412

413+
@RequestMapping("/data")
414+
void handleWithLiteralMapping(Writer writer) throws IOException {
415+
writer.write("test");
416+
}
391417
}
392418

393419
@Controller

0 commit comments

Comments
 (0)