From 4b754e3397088f851483e70bf49537c749f9a5d2 Mon Sep 17 00:00:00 2001 From: Lukas Lohoff Date: Fri, 15 Sep 2023 10:57:50 +0200 Subject: [PATCH 1/2] feat: improve content type detection allows to detect MS office files correctly --- pom.xml | 9 ++++++-- shogun-lib/pom.xml | 5 +++++ .../shogun/lib/service/BaseFileService.java | 21 ++++++++++--------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 3389ece9a..f7d29d823 100644 --- a/pom.xml +++ b/pom.xml @@ -137,7 +137,7 @@ 2.13.0 - 2.9.0 + 2.9.0 0.10.2 1.3 @@ -511,7 +511,12 @@ org.apache.tika tika-core - ${tika.core.version} + ${tika.version} + + + org.apache.tika + tika-parsers-standard-package + ${tika.version} diff --git a/shogun-lib/pom.xml b/shogun-lib/pom.xml index ee9d13525..dc2914f1c 100644 --- a/shogun-lib/pom.xml +++ b/shogun-lib/pom.xml @@ -228,6 +228,11 @@ tika-core + + org.apache.tika + tika-parsers-standard-package + + org.springframework diff --git a/shogun-lib/src/main/java/de/terrestris/shogun/lib/service/BaseFileService.java b/shogun-lib/src/main/java/de/terrestris/shogun/lib/service/BaseFileService.java index 359336e0a..eee2ddbf1 100644 --- a/shogun-lib/src/main/java/de/terrestris/shogun/lib/service/BaseFileService.java +++ b/shogun-lib/src/main/java/de/terrestris/shogun/lib/service/BaseFileService.java @@ -21,12 +21,11 @@ import de.terrestris.shogun.properties.UploadProperties; import lombok.extern.log4j.Log4j2; import org.apache.commons.io.FileUtils; -import org.apache.tika.config.TikaConfig; -import org.apache.tika.exception.TikaException; +import org.apache.commons.lang3.StringUtils; +import org.apache.tika.Tika; import org.apache.tika.io.TikaInputStream; import org.apache.tika.metadata.Metadata; import org.apache.tika.metadata.TikaCoreProperties; -import org.apache.tika.mime.MediaType; import org.apache.tomcat.util.http.fileupload.InvalidFileNameException; import org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException; import org.springframework.beans.factory.annotation.Autowired; @@ -66,21 +65,23 @@ public void isValid(MultipartFile file) throws Exception { this.verifyContentType(file); } - public void verifyContentType(MultipartFile file) throws IOException, TikaException { + public void verifyContentType(MultipartFile file) throws IOException { String contentType = file.getContentType(); String name = file.getName(); Metadata metadata = new Metadata(); metadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, name); - TikaConfig tika = new TikaConfig(); - MediaType mediaType = tika.getDetector().detect(TikaInputStream.get(file.getBytes()), metadata); - if (!mediaType.toString().equals(contentType)) { - throw new IOException("Mediatype validation failed. Passed content type is " + contentType + " but detected mediatype is " + mediaType); + + Tika tika = new Tika(); + String detectedMediaType = tika.detect(TikaInputStream.get(file.getBytes()), metadata); + + if (!StringUtils.equalsIgnoreCase(detectedMediaType, contentType)) { + throw new IOException("Media type validation failed. Passed content type is " + contentType + " but detected media type is " + detectedMediaType); } } public void isValidType(String contentType) throws InvalidContentTypeException { List supportedContentTypes = getSupportedContentTypes(); - boolean isMatch = PatternMatchUtils.simpleMatch(supportedContentTypes.toArray(new String[supportedContentTypes.size()]), contentType); + boolean isMatch = PatternMatchUtils.simpleMatch(supportedContentTypes.toArray(new String[0]), contentType); if (!isMatch) { log.warn("Unsupported content type {} for upload", contentType); throw new InvalidContentTypeException("Unsupported content type for upload!"); @@ -90,7 +91,7 @@ public void isValidType(String contentType) throws InvalidContentTypeException { public void isValidFileName(String fileName) throws InvalidFileNameException { List illegalCharacters = Arrays.asList("\\", "/", ":", "*", "?", "\"", "<", ">", "|", "\\0", "\\n"); if (illegalCharacters.stream().anyMatch(fileName::contains)) { - throw new InvalidFileNameException(fileName, "Filename contains illegal chracters. [\\, /, :, *, ?, \", <, >, |, \\0, \\n]"); + throw new InvalidFileNameException(fileName, "Filename contains illegal characters. [\\, /, :, *, ?, \", <, >, |, \\0, \\n]"); } } From af8fed81aa1dfd954688c0671322204925ecbb5b Mon Sep 17 00:00:00 2001 From: Lukas Lohoff Date: Fri, 15 Sep 2023 10:58:14 +0200 Subject: [PATCH 2/2] chore: optimize imports --- .../java/de/terrestris/shogun/config/WebSecurityConfig.java | 3 --- .../interceptor/config/InterceptorWebSecurityConfig.java | 2 -- .../de/terrestris/shogun/service/HttpProxyServiceTest.java | 1 - 3 files changed, 6 deletions(-) diff --git a/shogun-config/src/main/java/de/terrestris/shogun/config/WebSecurityConfig.java b/shogun-config/src/main/java/de/terrestris/shogun/config/WebSecurityConfig.java index 63e31f13b..252211c1c 100644 --- a/shogun-config/src/main/java/de/terrestris/shogun/config/WebSecurityConfig.java +++ b/shogun-config/src/main/java/de/terrestris/shogun/config/WebSecurityConfig.java @@ -16,11 +16,8 @@ */ package de.terrestris.shogun.config; -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.util.matcher.RequestMatcher; diff --git a/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/config/InterceptorWebSecurityConfig.java b/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/config/InterceptorWebSecurityConfig.java index 7f2e8c23e..b291b7ec7 100644 --- a/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/config/InterceptorWebSecurityConfig.java +++ b/shogun-gs-interceptor/src/main/java/de/terrestris/shogun/interceptor/config/InterceptorWebSecurityConfig.java @@ -18,10 +18,8 @@ import de.terrestris.shogun.config.DefaultWebSecurityConfig; import org.apache.commons.lang3.StringUtils; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.csrf.CookieCsrfTokenRepository; import org.springframework.security.web.util.matcher.RequestMatcher; diff --git a/shogun-proxy/src/test/java/de/terrestris/shogun/service/HttpProxyServiceTest.java b/shogun-proxy/src/test/java/de/terrestris/shogun/service/HttpProxyServiceTest.java index 0e93eb385..f56585297 100644 --- a/shogun-proxy/src/test/java/de/terrestris/shogun/service/HttpProxyServiceTest.java +++ b/shogun-proxy/src/test/java/de/terrestris/shogun/service/HttpProxyServiceTest.java @@ -30,7 +30,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles;