Skip to content

Commit 5d9d88c

Browse files
committed
CommonsMultipartFile removes mixed separator paths from original filename
Issue: SPR-13662
1 parent e6b1f0a commit 5d9d88c

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ public String getOriginalFilename() {
7878
// Should never happen.
7979
return "";
8080
}
81+
8182
// Check for Unix-style path
82-
int pos = filename.lastIndexOf("/");
83-
if (pos == -1) {
84-
// Check for Windows-style path
85-
pos = filename.lastIndexOf("\\");
86-
}
83+
int unixSep = filename.lastIndexOf("/");
84+
// Check for Windows-style path
85+
int winSep = filename.lastIndexOf("\\");
86+
// Cut off at latest possible point
87+
int pos = (winSep > unixSep ? winSep : unixSep);
8788
if (pos != -1) {
8889
// Any sort of path separator found...
8990
return filename.substring(pos + 1);

spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,9 @@ public List<FileItem> parseRequest(HttpServletRequest request) {
383383
MockFileItem fileItem1x = new MockFileItem(
384384
"field1", "type1", empty ? "" : "field1.txt", empty ? "" : "text1");
385385
MockFileItem fileItem2 = new MockFileItem(
386-
"field2", "type2", empty ? "" : "C:/field2.txt", empty ? "" : "text2");
386+
"field2", "type2", empty ? "" : "C:\\mypath/field2.txt", empty ? "" : "text2");
387387
MockFileItem fileItem2x = new MockFileItem(
388-
"field2x", "type2", empty ? "" : "C:\\field2x.txt", empty ? "" : "text2");
388+
"field2x", "type2", empty ? "" : "C:/mypath\\field2x.txt", empty ? "" : "text2");
389389
MockFileItem fileItem3 = new MockFileItem("field3", null, null, "value3");
390390
MockFileItem fileItem4 = new MockFileItem("field4", "text/html; charset=iso-8859-1", null, "value4");
391391
MockFileItem fileItem5 = new MockFileItem("field4", null, null, "value5");

0 commit comments

Comments
 (0)