From a1a37cccbb670816025da7fa2a2c30ac5fd99567 Mon Sep 17 00:00:00 2001 From: Benjamin Geer Date: Thu, 28 Nov 2019 10:24:07 +0100 Subject: [PATCH] fix(sipi): Try to fix MIME type consistency check. --- sipi/scripts/convert_from_file.lua | 2 +- sipi/scripts/convert_from_path.lua | 34 +++++++++---------- sipi/scripts/make_thumbnail.lua | 2 +- sipi/scripts/upload.lua | 2 +- .../e2e/v1/KnoraSipiIntegrationV1ITSpec.scala | 26 +++++++++++++- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/sipi/scripts/convert_from_file.lua b/sipi/scripts/convert_from_file.lua index 113e810f6b..07e2192f63 100644 --- a/sipi/scripts/convert_from_file.lua +++ b/sipi/scripts/convert_from_file.lua @@ -139,7 +139,7 @@ local check success, check = fullImg:mimetype_consistency(submitted_mimetype.mimetype, originalFilename) if not success then - send_error(500, "fullImg:mimetype_consistency() failed: " .. check) + send_error(500, "convert_from_file.lua: fullImg:mimetype_consistency() failed: " .. check) return end diff --git a/sipi/scripts/convert_from_path.lua b/sipi/scripts/convert_from_path.lua index 74ce50dac8..ca9c2bb065 100644 --- a/sipi/scripts/convert_from_path.lua +++ b/sipi/scripts/convert_from_path.lua @@ -69,6 +69,21 @@ end local mime_type = mime_info["mimetype"] +-- check that the submitted mimetype is the same as the real mimetype of the file + +local submitted_mimetype +success, submitted_mimetype = server.parse_mimetype(originalMimeType) + +if not success then + send_error(400, "Couldn't parse mimetype: " .. originalMimeType) + return +end + +if (mime_type ~= submitted_mimetype.mimetype) then + send_error(400, MIMETYPES_INCONSISTENCY) + return +end + -- handle the file depending on its media type (image, text file) local file_info = get_file_info(originalFilename, mime_type) @@ -125,10 +140,10 @@ if media_type == IMAGE then end local check - success, check = fullImg:mimetype_consistency(originalMimeType, originalFilename) + success, check = fullImg:mimetype_consistency(submitted_mimetype.mimetype, originalFilename) if not success then - send_error(500, "fullImg:mimetype_consistency() failed: " .. check) + send_error(500, "convert_from_path.lua: fullImg:mimetype_consistency() failed: " .. check) return end @@ -205,21 +220,6 @@ elseif media_type == TEXT then return end - -- check that the submitted mimetype is the same as the real mimetype of the file - - local submitted_mimetype - success, submitted_mimetype = server.parse_mimetype(originalMimeType) - - if not success then - send_error(400, "Couldn't parse mimetype: " .. originalMimeType) - return - end - - if (mime_type ~= submitted_mimetype.mimetype) then - send_error(400, MIMETYPES_INCONSISTENCY) - return - end - local filename = baseName .. "." .. file_info["extension"] local filePath = projectFileDir .. filename diff --git a/sipi/scripts/make_thumbnail.lua b/sipi/scripts/make_thumbnail.lua index cf61ad7724..824b41af05 100644 --- a/sipi/scripts/make_thumbnail.lua +++ b/sipi/scripts/make_thumbnail.lua @@ -126,7 +126,7 @@ for imgindex, imgparam in pairs(server.uploads) do local check success, check = thumbImg:mimetype_consistency(submitted_mimetype.mimetype, filename) if not success then - send_error(500, "thumbImg:mimetype_consistency() failed: " .. check) + send_error(500, "make_thumbnail.lua: thumbImg:mimetype_consistency() failed: " .. check) return end diff --git a/sipi/scripts/upload.lua b/sipi/scripts/upload.lua index 58d27c7146..d192b1e97e 100644 --- a/sipi/scripts/upload.lua +++ b/sipi/scripts/upload.lua @@ -131,7 +131,7 @@ for file_index, file_params in pairs(server.uploads) do success, check = uploaded_image:mimetype_consistency(mime_type, original_filename) if not success then - send_error(500, "uploaded_image:mimetype_consistency() failed: " .. check) + send_error(500, "upload.lua: uploaded_image:mimetype_consistency() failed: " .. check) return end diff --git a/webapi/src/it/scala/org/knora/webapi/e2e/v1/KnoraSipiIntegrationV1ITSpec.scala b/webapi/src/it/scala/org/knora/webapi/e2e/v1/KnoraSipiIntegrationV1ITSpec.scala index add2021b05..9aa30ab296 100644 --- a/webapi/src/it/scala/org/knora/webapi/e2e/v1/KnoraSipiIntegrationV1ITSpec.scala +++ b/webapi/src/it/scala/org/knora/webapi/e2e/v1/KnoraSipiIntegrationV1ITSpec.scala @@ -61,6 +61,7 @@ class KnoraSipiIntegrationV1ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV private val password = "test" private val pathToChlaus = "_test_data/test_route/images/Chlaus.jpg" private val pathToMarbles = "_test_data/test_route/images/marbles.tif" + private val pathToMarblesWithWrongExtension = "_test_data/test_route/images/marbles_with_wrong_extension.jpg" private val pathToXSLTransformation = "_test_data/test_route/texts/letterToHtml.xsl" private val pathToMappingWithXSLT = "_test_data/test_route/texts/mappingForLetterWithXSLTransformation.xml" private val firstPageIri = new MutableTestIri @@ -264,6 +265,30 @@ class KnoraSipiIntegrationV1ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV checkResponseOK(knoraPutRequest) } + "reject an 'incunabula:page' with binary data if the file extension is incorrect" in { + // The image to be uploaded. + val fileToSend = new File(pathToMarblesWithWrongExtension) + assert(fileToSend.exists(), s"File $pathToMarblesWithWrongExtension does not exist") + + // A multipart/form-data request containing the image. + val formData = Multipart.FormData( + Multipart.FormData.BodyPart( + "file", + HttpEntity.fromPath(MediaTypes.`image/tiff`, fileToSend.toPath), + Map("filename" -> fileToSend.getName) + ) + ) + + // Send the image in a PUT request to the Knora API server. + val knoraPutRequest = Put(baseApiUrl + "/v1/filevalue/" + URLEncoder.encode(firstPageIri.get, "UTF-8"), formData) ~> addCredentials(BasicHttpCredentials(username, password)) + + val exception = intercept[AssertionException] { + checkResponseOK(knoraPutRequest) + } + + assert(exception.getMessage.contains("MIME type and/or file extension are inconsistent")) + } + "create an 'incunabula:page' with parameters" in { // The image to be uploaded. val fileToSend = new File(pathToChlaus) @@ -808,7 +833,6 @@ class KnoraSipiIntegrationV1ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV xmlDiff.hasDifferences should be(false) } - } }