Skip to content

Commit 3371c23

Browse files
committed
Reinstate removal of jsessionid from lookup path
Closes gh-25864
1 parent da84155 commit 3371c23

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
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
@@ -522,7 +522,8 @@ protected String determineEncoding(HttpServletRequest request) {
522522
* @return the updated URI string
523523
*/
524524
public String removeSemicolonContent(String requestUri) {
525-
return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri);
525+
return (this.removeSemicolonContent ?
526+
removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
526527
}
527528

528529
private String removeSemicolonContentInternal(String requestUri) {
@@ -536,6 +537,22 @@ private String removeSemicolonContentInternal(String requestUri) {
536537
return requestUri;
537538
}
538539

540+
private String removeJsessionid(String requestUri) {
541+
String key = ";jsessionid=";
542+
int index = requestUri.toLowerCase().indexOf(key);
543+
if (index == -1) {
544+
return requestUri;
545+
}
546+
String start = requestUri.substring(0, index);
547+
for (int i = key.length(); i < requestUri.length(); i++) {
548+
char c = requestUri.charAt(i);
549+
if (c == ';' || c == '/') {
550+
return start + requestUri.substring(i);
551+
}
552+
}
553+
return start;
554+
}
555+
539556
/**
540557
* Decode the given URI path variables via {@link #decodeRequestString} unless
541558
* {@link #setUrlDecode} is set to {@code true} in which case it is assumed
@@ -640,7 +657,13 @@ private boolean shouldRemoveTrailingServletPathSlash(HttpServletRequest request)
640657
* <li>{@code defaultEncoding=}{@link WebUtils#DEFAULT_CHARACTER_ENCODING}
641658
* </ul>
642659
*/
643-
public static final UrlPathHelper rawPathInstance = new UrlPathHelper();
660+
public static final UrlPathHelper rawPathInstance = new UrlPathHelper() {
661+
662+
@Override
663+
public String removeSemicolonContent(String requestUri) {
664+
return requestUri;
665+
}
666+
};
644667

645668
static {
646669
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
assertEquals("/foo;a=b;c=d", helper.getRequestUri(request));
134134

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

139139
@Test

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,16 @@ public void addContentDispositionHeader() throws Exception {
375375
Collections.singletonList(new StringHttpMessageConverter()),
376376
factory.getObject());
377377

378-
assertContentDisposition(processor, false, "/hello.json", "whitelisted extension");
378+
assertContentDisposition(processor, false, "/hello.json", "safe extension");
379379
assertContentDisposition(processor, false, "/hello.pdf", "registered extension");
380380
assertContentDisposition(processor, true, "/hello.dataless", "unknown extension");
381381

382382
// path parameters
383383
assertContentDisposition(processor, false, "/hello.json;a=b", "path param shouldn't cause issue");
384384
assertContentDisposition(processor, true, "/hello.json;a=b;setup.dataless", "unknown ext in path params");
385385
assertContentDisposition(processor, true, "/hello.dataless;a=b;setup.json", "unknown ext in filename");
386-
assertContentDisposition(processor, false, "/hello.json;a=b;setup.json", "whitelisted extensions");
386+
assertContentDisposition(processor, false, "/hello.json;a=b;setup.json", "safe extensions");
387+
assertContentDisposition(processor, true, "/hello.json;jsessionid=foo.bar", "jsessionid shouldn't cause issue");
387388

388389
// encoded dot
389390
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-2016 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
assertEquals("test-42-7", response.getContentAsString());
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+
assertEquals(200, response.getStatus());
80+
assertEquals("test", response.getContentAsString());
81+
82+
request = new MockHttpServletRequest("GET", "/data;foo=bar");
83+
response = new MockHttpServletResponse();
84+
getServlet().service(request, response);
85+
assertEquals(404, response.getStatus());
86+
87+
request = new MockHttpServletRequest("GET", "/data;jsessionid=123");
88+
response = new MockHttpServletResponse();
89+
getServlet().service(request, response);
90+
assertEquals(200, response.getStatus());
91+
assertEquals("test", response.getContentAsString());
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)