Skip to content

Commit eb71dbd

Browse files
Updated FileUtil to make file extension and mime type validation case insensitive
1 parent 40ec02f commit eb71dbd

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Consistent use of try-with-resources when handling streams
88
* Updated AbstractRequest to remove deprecated methods uploadFileItems and readBytes (were protected static). Use StreamUtils instead.
99
* Replaced org.apache.tika:tika library with org.overviewproject:mime-types in FileUtil to validate uploaded file mime types.
10+
* Updated FileUtil to make file extension and mime type validation case insensitive.
1011

1112
### Bug Fixes
1213

wcomponents-core/src/main/java/com/github/bordertech/wcomponents/util/FileUtil.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,41 @@ public static boolean validateFileType(final FileItemWrap newFile, final List<St
5151
return true;
5252
}
5353

54+
// Filter extensions
5455
final List<String> fileExts = fileTypes.stream()
5556
.filter(fileType -> fileType.startsWith("."))
5657
.collect(Collectors.toList());
57-
// filter mime types from fileTypes.
58+
// Filter mime types
5859
final List<String> fileMimes = fileTypes.stream()
5960
.filter(fileType -> !fileExts.contains(fileType))
6061
.collect(Collectors.toList());
6162

6263
// First validate newFile against fileExts list
6364
// If extensions are supplied, then check if newFile has a name
64-
if (fileExts.size() > 0 && newFile.getName() != null) {
65+
if (!fileExts.isEmpty() && newFile.getName() != null) {
6566
// Then see if newFile has an extension
6667
String[] split = newFile.getName().split(("\\.(?=[^\\.]+$)"));
6768
// If it exists, then check if it matches supplied extension(s)
6869
if (split.length == 2
69-
&& fileExts.stream().anyMatch(fileExt -> fileExt.equals("." + split[1]))) {
70+
&& fileExts.stream().anyMatch(fileExt -> fileExt.equalsIgnoreCase("." + split[1]))) {
7071
return true;
7172
}
7273
}
7374
// If extension match is unsucessful, then move to fileMimes list
74-
if (fileMimes.size() > 0) {
75+
if (!fileMimes.isEmpty()) {
7576
final String mimeType = getFileMimeType(newFile);
77+
if (mimeType == null) {
78+
return false;
79+
}
7680
LOG.debug("File mime-type is: " + mimeType);
7781
for (String fileMime : fileMimes) {
78-
if (StringUtils.equals(mimeType, fileMime)) {
82+
if (mimeType.equalsIgnoreCase(fileMime)) {
7983
return true;
8084
}
8185
if (fileMime.indexOf("*") == fileMime.length() - 1) {
82-
fileMime = fileMime.substring(0, fileMime.length() - 1);
83-
if (mimeType.indexOf(fileMime) == 0) {
86+
String fileMimePrefix = fileMime.substring(0, fileMime.length() - 1).toLowerCase();
87+
String lcMimeType = mimeType.toLowerCase();
88+
if (lcMimeType.startsWith(fileMimePrefix)) {
8489
return true;
8590
}
8691
}
@@ -93,7 +98,7 @@ public static boolean validateFileType(final FileItemWrap newFile, final List<St
9398
* Identify the mime type of a file.
9499
*
95100
* @param file the File to detect.
96-
* @return mime type as detected by Apache tika, otherwise null.
101+
* @return mime type as detected otherwise null.
97102
*/
98103
public static String getFileMimeType(final File file) {
99104
if (file != null) {

wcomponents-core/src/test/java/com/github/bordertech/wcomponents/util/FileUtil_Test.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ public void testGetMimeTypeForTextFileWithAsciiAndNameHint() throws IOException
133133
Assert.assertEquals("Incorrect type for text file that should be detected as text with name hint", "text/plain", FileUtil.getFileMimeType(file));
134134
}
135135

136+
@Test
137+
public void testValidateFileExtensionInCaseSensitive() throws IOException {
138+
MockFileItem file = new MockFileItem();
139+
file.setName("test.DocX");
140+
boolean validateFileType = FileUtil.validateFileType(new FileItemWrap(file), Arrays.asList(".dOCx"));
141+
Assert.assertTrue(validateFileType);
142+
}
143+
144+
@Test
145+
public void testValidateFileTypeCaseInSensitive() throws IOException {
146+
FileItem newFileItem = createFileItem("/content/test.pdf");
147+
boolean validateFileType = FileUtil.validateFileType(new FileItemWrap(newFileItem), Arrays.asList("AppLICATION/PDF"));
148+
Assert.assertTrue(validateFileType);
149+
}
150+
151+
@Test
152+
public void testValidateFileTypeCaseInSensitiveWildCard() throws IOException {
153+
FileItem newFileItem = createFileItem("/content/test.pdf");
154+
boolean validateFileType = FileUtil.validateFileType(new FileItemWrap(newFileItem), Arrays.asList("AppLICATION/*"));
155+
Assert.assertTrue(validateFileType);
156+
}
157+
136158
/**
137159
* Create a new fileitem.
138160
*

0 commit comments

Comments
 (0)