From 8ebe74d31ec6d7848c9b6dd8d56ecd6e8e8936f8 Mon Sep 17 00:00:00 2001 From: Henning Date: Fri, 11 Oct 2019 10:10:43 +0200 Subject: [PATCH 1/2] Fix invalid names for file uploads --- .../kotlin/de/code_freak/codefreak/util/TarUtil.kt | 8 +++++++- .../de/code_freak/codefreak/util/TarUtilTest.kt | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt b/src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt index 5ce02aecc..d533f0e3b 100644 --- a/src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt +++ b/src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt @@ -174,11 +174,17 @@ object TarUtil { private fun wrapUploadInTar(file: MultipartFile, out: OutputStream) { val outputStream = TarArchiveOutputStream(out) - val entry = TarArchiveEntry(file.originalFilename) + val entry = TarArchiveEntry(basename(file.originalFilename ?: "file")) entry.size = file.size outputStream.putArchiveEntry(entry) file.inputStream.use { StreamUtils.copy(it, outputStream) } outputStream.closeArchiveEntry() outputStream.finish() } + + private fun basename(path: String): String { + path.split("[\\\\|/]".toRegex()).apply { + return if (isEmpty()) "" else last() + } + } } diff --git a/src/test/kotlin/de/code_freak/codefreak/util/TarUtilTest.kt b/src/test/kotlin/de/code_freak/codefreak/util/TarUtilTest.kt index b8757ab7d..1ae014a62 100644 --- a/src/test/kotlin/de/code_freak/codefreak/util/TarUtilTest.kt +++ b/src/test/kotlin/de/code_freak/codefreak/util/TarUtilTest.kt @@ -4,8 +4,10 @@ import org.apache.commons.compress.archivers.tar.TarArchiveInputStream import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.`is` import org.hamcrest.Matchers.containsInAnyOrder +import org.hamcrest.Matchers.notNullValue import org.junit.Test import org.springframework.core.io.ClassPathResource +import org.springframework.mock.web.MockMultipartFile import java.io.ByteArrayOutputStream internal class TarUtilTest { @@ -30,4 +32,15 @@ internal class TarUtilTest { assertThat(result.mode, `is`(33252)) } } + + @Test + fun `file uploads are wrapped in tar`() { + val file = MockMultipartFile("file", "C:\\Users\\jdoe\\main.c", "text/plain", "".toByteArray()) + val out = ByteArrayOutputStream() + TarUtil.writeUploadAsTar(file, out) + TarArchiveInputStream(out.toByteArray().inputStream()).use { + val result = generateSequence { it.nextTarEntry }.filter { it.name == "main.c" }.firstOrNull() + assertThat(result, notNullValue()) + } + } } From 77516c8f9e9ac7f1d52220a1531a47c773490aa7 Mon Sep 17 00:00:00 2001 From: Henning Date: Fri, 11 Oct 2019 10:12:18 +0200 Subject: [PATCH 2/2] Fix RegEx --- src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt b/src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt index d533f0e3b..965e4494e 100644 --- a/src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt +++ b/src/main/kotlin/de/code_freak/codefreak/util/TarUtil.kt @@ -183,7 +183,7 @@ object TarUtil { } private fun basename(path: String): String { - path.split("[\\\\|/]".toRegex()).apply { + path.split("[\\\\/]".toRegex()).apply { return if (isEmpty()) "" else last() } }