Skip to content

Commit d1d3753

Browse files
committed
Reinstate removal of jsessionid from lookup path
Closes gh-25864
1 parent 45322a7 commit d1d3753

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
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
@@ -521,7 +521,8 @@ protected String determineEncoding(HttpServletRequest request) {
521521
* @return the updated URI string
522522
*/
523523
public String removeSemicolonContent(String requestUri) {
524-
return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri);
524+
return (this.removeSemicolonContent ?
525+
removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
525526
}
526527

527528
private String removeSemicolonContentInternal(String requestUri) {
@@ -535,6 +536,22 @@ private String removeSemicolonContentInternal(String requestUri) {
535536
return requestUri;
536537
}
537538

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

644667
static {
645668
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,16 @@ public void addContentDispositionHeader() throws Exception {
380380
Collections.singletonList(new StringHttpMessageConverter()),
381381
factory.getObject());
382382

383-
assertContentDisposition(processor, false, "/hello.json", "whitelisted extension");
383+
assertContentDisposition(processor, false, "/hello.json", "safe extension");
384384
assertContentDisposition(processor, false, "/hello.pdf", "registered extension");
385385
assertContentDisposition(processor, true, "/hello.dataless", "uknown extension");
386386

387387
// path parameters
388388
assertContentDisposition(processor, false, "/hello.json;a=b", "path param shouldn't cause issue");
389-
assertContentDisposition(processor, true, "/hello.json;a=b;setup.dataless", "uknown ext in path params");
390-
assertContentDisposition(processor, true, "/hello.dataless;a=b;setup.json", "uknown ext in filename");
391-
assertContentDisposition(processor, false, "/hello.json;a=b;setup.json", "whitelisted extensions");
389+
assertContentDisposition(processor, true, "/hello.json;a=b;setup.dataless", "unknown ext in path params");
390+
assertContentDisposition(processor, true, "/hello.dataless;a=b;setup.json", "unknown ext in filename");
391+
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-2015 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.
@@ -86,6 +86,28 @@ public void simple() throws Exception {
8686
assertEquals("test-42-7", response.getContentAsString());
8787
}
8888

89+
@Test // gh-25864
90+
public void literalMappingWithPathParams() throws Exception {
91+
initServletWithControllers(MultipleUriTemplateController.class);
92+
93+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/data");
94+
MockHttpServletResponse response = new MockHttpServletResponse();
95+
getServlet().service(request, response);
96+
assertEquals(200, response.getStatus());
97+
assertEquals("test", response.getContentAsString());
98+
99+
request = new MockHttpServletRequest("GET", "/data;foo=bar");
100+
response = new MockHttpServletResponse();
101+
getServlet().service(request, response);
102+
assertEquals(404, response.getStatus());
103+
104+
request = new MockHttpServletRequest("GET", "/data;jsessionid=123");
105+
response = new MockHttpServletResponse();
106+
getServlet().service(request, response);
107+
assertEquals(200, response.getStatus());
108+
assertEquals("test", response.getContentAsString());
109+
}
110+
89111
@Test
90112
public void multiple() throws Exception {
91113
initServletWithControllers(MultipleUriTemplateController.class);
@@ -405,6 +427,10 @@ public void handle(@PathVariable("hotel") String hotel,
405427
writer.write("test-" + hotel + "-q" + qHotel + "-" + booking + "-" + other + "-q" + qOther);
406428
}
407429

430+
@RequestMapping("/data")
431+
void handleWithLiteralMapping(Writer writer) throws IOException {
432+
writer.write("test");
433+
}
408434
}
409435

410436
@Controller

0 commit comments

Comments
 (0)