diff --git a/client/lib/zip4j_1.3.3.jar b/client/lib/zip4j_1.3.3.jar deleted file mode 100644 index 23f720fb03..0000000000 Binary files a/client/lib/zip4j_1.3.3.jar and /dev/null differ diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 789e7ac22d..023830600e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -266,6 +266,7 @@ velocity-tools-generic = { module = "org.apache.velocity.tools:velocity-tools-ge xmpbox = { module = "org.apache.pdfbox:xmpbox", version.ref = "pdfbox" } xpp3 = { module = "xpp3:xpp3", version = "1.1.4c" } xstream = { module = "com.thoughtworks.xstream:xstream", version = "1.4.20" } +zip4j = { module = "net.lingala.zip4j:zip4j", version = "2.11.6" } [bundles] client-main = [ @@ -336,6 +337,7 @@ client-main = [ "velocity-tools-generic", "xpp3", "xstream", + "zip4j", ] command-main = [ "bcpkix-jdk18on", @@ -653,6 +655,7 @@ server-main = [ "velocity-tools-generic", "xpp3", "xstream", + "zip4j", ] server-test = [ "byte-buddy", diff --git a/gradle/vendored-layout.json b/gradle/vendored-layout.json index 7de8bee5bd..6ee8c9fbe3 100644 --- a/gradle/vendored-layout.json +++ b/gradle/vendored-layout.json @@ -27,6 +27,7 @@ "javax.ws.rs:javax.ws.rs-api": "", "javax.xml.bind:jaxb-api": "", "joda-time:joda-time": "", + "net.lingala.zip4j:zip4j": "", "org.apache.commons:commons-collections4": "", "org.apache.commons:commons-compress": "", "org.apache.commons:commons-configuration2": "", @@ -245,6 +246,7 @@ "joda-time:joda-time": "", "net.java.dev.jna:jna": "", "net.java.dev.jna:jna-platform": "", + "net.lingala.zip4j:zip4j": "", "org.apache.commons:commons-collections4": "commons", "org.apache.commons:commons-compress": "commons", "org.apache.commons:commons-configuration2": "commons", diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 04c10db547..64e4d963af 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -1270,6 +1270,14 @@ + + + + + + + + diff --git a/server/build.gradle b/server/build.gradle index 71fda05588..1356c2301f 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -93,7 +93,6 @@ def clientCoreJar = tasks.register('clientCoreJar', Jar) { include 'org/mozilla/**' include 'org/glassfish/jersey/**' include 'de/**' - include 'net/lingala/zip4j/unzip/**' } from generateVersionProperties from('src/main/resources/com/mirth/connect/util') { diff --git a/server/docs/thirdparty/THIRD-PARTY-README.txt b/server/docs/thirdparty/THIRD-PARTY-README.txt index 7ffd374932..ec8a22a073 100644 --- a/server/docs/thirdparty/THIRD-PARTY-README.txt +++ b/server/docs/thirdparty/THIRD-PARTY-README.txt @@ -42,20 +42,7 @@ The following are distributed under the terms of the Apache License version 2.0 Swagger Core 2.0.10 [incl. CLI] (https://github.com/swagger-api/swagger-core) Swagger UI 2.2.10 (http://swagger.io/swagger-ui/) webdavclient4j 0.92 (http://sourceforge.net/projects/webdavclient4j/) - Zip4j 1.3.3 (http://www.lingala.net/zip4j/) - - --------------------------------------------------------------------------------- - - -Modified versions of files from Zip4j are distributed under the terms of the -Apache License version 2.0 (see APACHE-LICENSE-2.0.txt). The following files -have changed: - - net/lingala/zip4j/unzip/Unzip.java - net/lingala/zip4j/unzip/UnzipEngine.java - - Changes: Modified to protect against the Zip Slip vulnerability. + Zip4j 2.11.6 (http://www.lingala.net/zip4j/) -------------------------------------------------------------------------------- diff --git a/server/lib/zip4j_1.3.3.jar b/server/lib/zip4j_1.3.3.jar deleted file mode 100644 index 23f720fb03..0000000000 Binary files a/server/lib/zip4j_1.3.3.jar and /dev/null differ diff --git a/server/src/main/java/com/mirth/connect/util/ArchiveUtils.java b/server/src/main/java/com/mirth/connect/util/ArchiveUtils.java index 461a87b551..5989500099 100644 --- a/server/src/main/java/com/mirth/connect/util/ArchiveUtils.java +++ b/server/src/main/java/com/mirth/connect/util/ArchiveUtils.java @@ -1,31 +1,28 @@ -/* - * Copyright (c) Mirth Corporation. All rights reserved. - * - * http://www.mirthcorp.com - * - * The software in this package is published under the terms of the MPL license a copy of which has - * been included with this distribution in the LICENSE.txt file. - */ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: Mirth Corporation +// SPDX-FileCopyrightText: 2026 Tony Germano package com.mirth.connect.util; import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipInputStream; -import net.lingala.zip4j.io.ZipOutputStream; +import net.lingala.zip4j.io.outputstream.ZipOutputStream; import net.lingala.zip4j.model.ZipParameters; -import net.lingala.zip4j.util.Zip4jConstants; +import net.lingala.zip4j.model.enums.CompressionLevel; +import net.lingala.zip4j.model.enums.CompressionMethod; +import net.lingala.zip4j.model.enums.EncryptionMethod; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; @@ -41,10 +38,23 @@ public class ArchiveUtils { /** * The buffer size to use when copying files into an archive */ - private final static int BUFFER_SIZE = 1048576; private static Logger logger = LogManager.getLogger(ArchiveUtils.class); + public static final String PASSWORD_UNSUPPORTED_MESSAGE = "Password protection is only supported for ZIP archives without a compressor"; + + /** + * Only plain ZIP archives can be password protected. Callers can use this to reject a password + * up front, before doing the work of writing the files that would go into the archive. + */ + public static boolean isPasswordSupported(String archiver, String compressor) { + return isPlainZip(archiver, compressor); + } + + private static boolean isPlainZip(String archiver, String compressor) { + return ArchiveStreamFactory.ZIP.equals(archiver) && compressor == null; + } + /** * Create an archive file from files/subfolders in a given source folder. * @@ -81,18 +91,28 @@ public static void createArchive(File sourceFolder, File destinationFile, String throw new CompressException("Invalid source folder: " + sourceFolder.getAbsolutePath()); } + boolean plainZip = isPlainZip(archiver, compressor); + + if (StringUtils.isNotBlank(password) && !plainZip) { + // Only the plain ZIP path can encrypt; refuse rather than silently write an unprotected archive + throw new CompressException(PASSWORD_UNSUPPORTED_MESSAGE); + } + logger.debug("Creating archive \"" + destinationFile.getAbsolutePath() + "\" from folder \"" + sourceFolder.getAbsolutePath() + "\""); OutputStream outputStream = null; OutputStream archiveOutputStream = null; try { /* - * The commons-compress documentation recommends constructing a ZipArchiveOutputStream - * with the archive file when using the ZIP archive format. See - * http://commons.apache.org/proper/commons-compress/zip.html + * Plain ZIP archives are written with zip4j, the only library here that can encrypt + * them. Every other combination, including ZIP with a compressor, goes through + * commons-compress, which cannot encrypt (hence the password check above). */ - if (archiver.equals(ArchiveStreamFactory.ZIP) && compressor == null) { - archiveOutputStream = new ZipOutputStream(new FileOutputStream(destinationFile)); + if (plainZip) { + char[] zipPassword = StringUtils.isNotBlank(password) ? password.toCharArray() : null; + // zip4j does not close the underlying stream when finishing the archive fails, so keep it to close below + outputStream = new FileOutputStream(destinationFile); + archiveOutputStream = new ZipOutputStream(outputStream, zipPassword); } else { // if not using ZIP format, use the archiver/compressor stream factories to initialize the archive output stream outputStream = new BufferedOutputStream(new FileOutputStream(destinationFile)); @@ -105,6 +125,15 @@ public static void createArchive(File sourceFolder, File destinationFile, String } createFolderArchive(sourceFolder, archiveOutputStream, sourceFolder.getAbsolutePath() + IOUtils.DIR_SEPARATOR, password, encryptionType); + + /* + * Closing the archive writes its trailer (the zip central directory, the tar end of + * archive records, a compressor's footer) and flushes it. Do it here so a failure is + * reported; the closeQuietly calls below only release the streams after a failure. + */ + OutputStream finished = archiveOutputStream; + archiveOutputStream = null; + finished.close(); } catch (Exception e) { throw new CompressException(e); } finally { @@ -115,74 +144,53 @@ public static void createArchive(File sourceFolder, File destinationFile, String } private static void createFolderArchive(File folder, OutputStream outputStream, String rootFolder, String password, EncryptionType encryptionType) throws CompressException { - byte[] buffer = new byte[BUFFER_SIZE]; - ZipOutputStream zipOutputStream = null; - - try { - for (File file : folder.listFiles()) { - if (file.isDirectory()) { - createFolderArchive(file, outputStream, rootFolder, password, encryptionType); - } else { - try { - // extract/remove the rootFolder from the file's absolute path before adding it to the archive - String entryName = file.getAbsolutePath(); - - if (entryName.substring(0, rootFolder.length()).equals(rootFolder)) { - entryName = entryName.substring(rootFolder.length()); - } - - ArchiveOutputStream archiveOutputStream = null; - InputStream inputStream = new FileInputStream(file); - try { - logger.debug("Adding \"" + entryName + "\" to archive"); - - if (outputStream instanceof ArchiveOutputStream) { - archiveOutputStream = (ArchiveOutputStream) outputStream; - archiveOutputStream.putArchiveEntry(archiveOutputStream.createArchiveEntry(file, entryName)); - } else if (outputStream instanceof ZipOutputStream) { - zipOutputStream = (ZipOutputStream) outputStream; - - ZipParameters parameters = new ZipParameters(); - parameters.setSourceExternalStream(true); - parameters.setFileNameInZip(entryName); - parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); - parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); - - if (StringUtils.isNotBlank(password)) { - parameters.setEncryptFiles(true); - - boolean isAes = encryptionType != EncryptionType.STANDARD; - parameters.setEncryptionMethod(isAes ? Zip4jConstants.ENC_METHOD_AES : Zip4jConstants.ENC_METHOD_STANDARD); + for (File file : folder.listFiles()) { + if (file.isDirectory()) { + createFolderArchive(file, outputStream, rootFolder, password, encryptionType); + } else { + try { + // extract/remove the rootFolder from the file's absolute path before adding it to the archive + String entryName = file.getAbsolutePath(); - if (isAes) { - parameters.setAesKeyStrength(encryptionType.getKeyStrength()); - } + if (entryName.substring(0, rootFolder.length()).equals(rootFolder)) { + entryName = entryName.substring(rootFolder.length()); + } - parameters.setPassword(password); + try (InputStream inputStream = Files.newInputStream(file.toPath())) { + logger.debug("Adding \"" + entryName + "\" to archive"); + + /* + * The entry is closed only after a successful copy. Closing it in a + * finally block would replace the original failure with an error from + * closing a half-written (or never opened) entry. + */ + if (outputStream instanceof ArchiveOutputStream archiveOutputStream) { + archiveOutputStream.putArchiveEntry(archiveOutputStream.createArchiveEntry(file, entryName)); + inputStream.transferTo(outputStream); + archiveOutputStream.closeArchiveEntry(); + } else if (outputStream instanceof ZipOutputStream zipOutputStream) { + ZipParameters parameters = new ZipParameters(); + // Zip entry names use forward slashes whatever the platform's separator is + parameters.setFileNameInZip(entryName.replace(File.separatorChar, '/')); + parameters.setCompressionMethod(CompressionMethod.DEFLATE); + parameters.setCompressionLevel(CompressionLevel.NORMAL); + + if (StringUtils.isNotBlank(password)) { + parameters.setEncryptFiles(true); + + boolean isAes = encryptionType != EncryptionType.STANDARD; + parameters.setEncryptionMethod(isAes ? EncryptionMethod.AES : EncryptionMethod.ZIP_STANDARD); + + if (isAes) { + parameters.setAesKeyStrength(encryptionType.getKeyStrength()); } - - zipOutputStream.putNextEntry(null, parameters); } - IOUtils.copyLarge(inputStream, outputStream, buffer); - } finally { - IOUtils.closeQuietly(inputStream); - - if (outputStream instanceof ArchiveOutputStream) { - archiveOutputStream.closeArchiveEntry(); - } else if (outputStream instanceof ZipOutputStream) { - zipOutputStream.closeEntry(); - } + zipOutputStream.putNextEntry(parameters); + inputStream.transferTo(outputStream); + zipOutputStream.closeEntry(); } - } catch (Exception e) { - throw new CompressException(e); } - } - } - } finally { - if (zipOutputStream != null) { - try { - zipOutputStream.finish(); } catch (Exception e) { throw new CompressException(e); } diff --git a/server/src/main/java/com/mirth/connect/util/messagewriter/EncryptionType.java b/server/src/main/java/com/mirth/connect/util/messagewriter/EncryptionType.java index 55b0f2a0c9..8bd55c4578 100644 --- a/server/src/main/java/com/mirth/connect/util/messagewriter/EncryptionType.java +++ b/server/src/main/java/com/mirth/connect/util/messagewriter/EncryptionType.java @@ -1,24 +1,18 @@ -/* - * Copyright (c) Mirth Corporation. All rights reserved. - * - * http://www.mirthcorp.com - * - * The software in this package is published under the terms of the MPL license a copy of which has - * been included with this distribution in the LICENSE.txt file. - */ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: Mirth Corporation package com.mirth.connect.util.messagewriter; -import net.lingala.zip4j.util.Zip4jConstants; +import net.lingala.zip4j.model.enums.AesKeyStrength; public enum EncryptionType { - STANDARD("Standard", -1), AES128("AES-128", Zip4jConstants.AES_STRENGTH_128), AES256("AES-256", - Zip4jConstants.AES_STRENGTH_256); + STANDARD("Standard", null), AES128("AES-128", AesKeyStrength.KEY_STRENGTH_128), AES256("AES-256", + AesKeyStrength.KEY_STRENGTH_256); private String name; - private int keyStrength; + private AesKeyStrength keyStrength; - private EncryptionType(String name, int keyStrength) { + private EncryptionType(String name, AesKeyStrength keyStrength) { this.name = name; this.keyStrength = keyStrength; } @@ -27,7 +21,8 @@ public String getDisplayName() { return name; } - public int getKeyStrength() { + /** The AES key strength, or null for standard (ZipCrypto) encryption. */ + public AesKeyStrength getKeyStrength() { return keyStrength; } diff --git a/server/src/main/java/com/mirth/connect/util/messagewriter/MessageWriterArchive.java b/server/src/main/java/com/mirth/connect/util/messagewriter/MessageWriterArchive.java index 235c2e9c5b..82ce5d60d1 100644 --- a/server/src/main/java/com/mirth/connect/util/messagewriter/MessageWriterArchive.java +++ b/server/src/main/java/com/mirth/connect/util/messagewriter/MessageWriterArchive.java @@ -1,11 +1,6 @@ -/* - * Copyright (c) Mirth Corporation. All rights reserved. - * - * http://www.mirthcorp.com - * - * The software in this package is published under the terms of the MPL license a copy of which has - * been included with this distribution in the LICENSE.txt file. - */ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: Mirth Corporation +// SPDX-FileCopyrightText: 2026 Tony Germano package com.mirth.connect.util.messagewriter; @@ -73,9 +68,10 @@ public void finishWrite() throws MessageWriterException { fileWriter.close(); if (messagesWritten) { + File tempFile = new File(archiveFile.getParent() + IOUtils.DIR_SEPARATOR + "." + archiveFile.getName()); + boolean archiveCreated = false; + try { - File tempFile = new File(archiveFile.getParent() + IOUtils.DIR_SEPARATOR + "." + archiveFile.getName()); - if (tempFile.exists()) { try { FileUtils.forceDelete(tempFile); @@ -84,6 +80,7 @@ public void finishWrite() throws MessageWriterException { } ArchiveUtils.createArchive(rootFolder, tempFile, archiver, compressor, password, encryptionType); + archiveCreated = true; if (archiveFile.exists()) { try { @@ -96,6 +93,11 @@ public void finishWrite() throws MessageWriterException { } catch (Exception e) { throw new MessageWriterException(e); } finally { + if (!archiveCreated) { + // Do not leave a partly written archive behind. A finished one stays if only moving it failed. + FileUtils.deleteQuietly(tempFile); + } + FileUtils.deleteQuietly(rootFolder); } } diff --git a/server/src/main/java/com/mirth/connect/util/messagewriter/MessageWriterFactory.java b/server/src/main/java/com/mirth/connect/util/messagewriter/MessageWriterFactory.java index 31afeab117..19a7abce5c 100644 --- a/server/src/main/java/com/mirth/connect/util/messagewriter/MessageWriterFactory.java +++ b/server/src/main/java/com/mirth/connect/util/messagewriter/MessageWriterFactory.java @@ -1,11 +1,6 @@ -/* - * Copyright (c) Mirth Corporation. All rights reserved. - * - * http://www.mirthcorp.com - * - * The software in this package is published under the terms of the MPL license a copy of which has - * been included with this distribution in the LICENSE.txt file. - */ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: Mirth Corporation +// SPDX-FileCopyrightText: 2026 Tony Germano package com.mirth.connect.util.messagewriter; @@ -19,6 +14,7 @@ import org.apache.commons.lang3.StringUtils; import com.mirth.commons.encryption.Encryptor; +import com.mirth.connect.util.ArchiveUtils; import com.mirth.connect.util.FilenameUtils; public class MessageWriterFactory { @@ -53,6 +49,11 @@ public MessageWriter getMessageWriter(MessageWriterOptions options, Encryptor en return fileWriter; } + if (options.isPasswordEnabled() && StringUtils.isNotBlank(options.getPassword()) && !ArchiveUtils.isPasswordSupported(options.getArchiveFormat(), options.getCompressFormat())) { + // Fail now, not after every message has been written, and never write an unprotected archive instead + throw new MessageWriterException(ArchiveUtils.PASSWORD_UNSUPPORTED_MESSAGE); + } + if (options.getArchiveFileName() == null) { options.setArchiveFileName(new SimpleDateFormat(ARCHIVE_DATE_PATTERN).format(Calendar.getInstance().getTime())); } diff --git a/server/src/main/java/net/lingala/zip4j/unzip/Unzip.java b/server/src/main/java/net/lingala/zip4j/unzip/Unzip.java deleted file mode 100644 index c3b6468077..0000000000 --- a/server/src/main/java/net/lingala/zip4j/unzip/Unzip.java +++ /dev/null @@ -1,244 +0,0 @@ -/* -* Copyright 2010 Srikanth Reddy Lingala -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -package net.lingala.zip4j.unzip; - -import java.io.File; -import java.util.ArrayList; - -import net.lingala.zip4j.exception.ZipException; -import net.lingala.zip4j.io.ZipInputStream; -import net.lingala.zip4j.model.CentralDirectory; -import net.lingala.zip4j.model.FileHeader; -import net.lingala.zip4j.model.UnzipParameters; -import net.lingala.zip4j.model.ZipModel; -import net.lingala.zip4j.progress.ProgressMonitor; -import net.lingala.zip4j.util.InternalZipConstants; -import net.lingala.zip4j.util.Zip4jUtil; - -public class Unzip { - - private ZipModel zipModel; - - public Unzip(ZipModel zipModel) throws ZipException { - - if (zipModel == null) { - throw new ZipException("ZipModel is null"); - } - - this.zipModel = zipModel; - } - - public void extractAll(final UnzipParameters unzipParameters, final String outPath, - final ProgressMonitor progressMonitor, boolean runInThread) throws ZipException { - - CentralDirectory centralDirectory = zipModel.getCentralDirectory(); - - if (centralDirectory == null || - centralDirectory.getFileHeaders() == null) { - throw new ZipException("invalid central directory in zipModel"); - } - - final ArrayList fileHeaders = centralDirectory.getFileHeaders(); - - progressMonitor.setCurrentOperation(ProgressMonitor.OPERATION_EXTRACT); - progressMonitor.setTotalWork(calculateTotalWork(fileHeaders)); - progressMonitor.setState(ProgressMonitor.STATE_BUSY); - - if (runInThread) { - Thread thread = new Thread(InternalZipConstants.THREAD_NAME) { - public void run() { - try { - initExtractAll(fileHeaders, unzipParameters, progressMonitor, outPath); - progressMonitor.endProgressMonitorSuccess(); - } catch (ZipException e) { - } - } - }; - thread.start(); - } else { - initExtractAll(fileHeaders, unzipParameters, progressMonitor, outPath); - } - - } - - private void initExtractAll(ArrayList fileHeaders, UnzipParameters unzipParameters, - ProgressMonitor progressMonitor, String outPath) throws ZipException { - - for (int i = 0; i < fileHeaders.size(); i++) { - FileHeader fileHeader = (FileHeader)fileHeaders.get(i); - initExtractFile(fileHeader, outPath, unzipParameters, null, progressMonitor); - if (progressMonitor.isCancelAllTasks()) { - progressMonitor.setResult(ProgressMonitor.RESULT_CANCELLED); - progressMonitor.setState(ProgressMonitor.STATE_READY); - return; - } - } - } - - public void extractFile(final FileHeader fileHeader, final String outPath, - final UnzipParameters unzipParameters, final String newFileName, - final ProgressMonitor progressMonitor, boolean runInThread) throws ZipException { - if (fileHeader == null) { - throw new ZipException("fileHeader is null"); - } - - progressMonitor.setCurrentOperation(ProgressMonitor.OPERATION_EXTRACT); - progressMonitor.setTotalWork(fileHeader.getCompressedSize()); - progressMonitor.setState(ProgressMonitor.STATE_BUSY); - progressMonitor.setPercentDone(0); - progressMonitor.setFileName(fileHeader.getFileName()); - - if (runInThread) { - Thread thread = new Thread(InternalZipConstants.THREAD_NAME) { - public void run() { - try { - initExtractFile(fileHeader, outPath, unzipParameters, newFileName, progressMonitor); - progressMonitor.endProgressMonitorSuccess(); - } catch (ZipException e) { - } - } - }; - thread.start(); - } else { - initExtractFile(fileHeader, outPath, unzipParameters, newFileName, progressMonitor); - progressMonitor.endProgressMonitorSuccess(); - } - - } - - private void initExtractFile(FileHeader fileHeader, String outPath, - UnzipParameters unzipParameters, String newFileName, ProgressMonitor progressMonitor) throws ZipException { - - if (fileHeader == null) { - throw new ZipException("fileHeader is null"); - } - - try { - progressMonitor.setFileName(fileHeader.getFileName()); - - if (!outPath.endsWith(InternalZipConstants.FILE_SEPARATOR)) { - outPath += InternalZipConstants.FILE_SEPARATOR; - } - - // make sure no file is extracted outside of the target directory (a.k.a zip slip) - String fileName = fileHeader.getFileName(); - String completePath = outPath + fileName; - if (!new File(completePath).getCanonicalPath().startsWith(new File(outPath).getCanonicalPath())) { - throw new ZipException( - "illegal file name that breaks out of the target directory: " + fileHeader.getFileName()); - } - - // If file header is a directory, then check if the directory exists - // If not then create a directory and return - if (fileHeader.isDirectory()) { - try { - if (!Zip4jUtil.isStringNotNullAndNotEmpty(fileName)) { - return; - } - File file = new File(completePath); - if (!file.exists()) { - file.mkdirs(); - } - } catch (Exception e) { - progressMonitor.endProgressMonitorError(e); - throw new ZipException(e); - } - } else { - //Create Directories - checkOutputDirectoryStructure(fileHeader, outPath, newFileName); - - UnzipEngine unzipEngine = new UnzipEngine(zipModel, fileHeader); - try { - unzipEngine.unzipFile(progressMonitor, outPath, newFileName, unzipParameters); - } catch (Exception e) { - progressMonitor.endProgressMonitorError(e); - throw new ZipException(e); - } - } - } catch (ZipException e) { - progressMonitor.endProgressMonitorError(e); - throw e; - } catch (Exception e) { - progressMonitor.endProgressMonitorError(e); - throw new ZipException(e); - } - } - - public ZipInputStream getInputStream(FileHeader fileHeader) throws ZipException { - UnzipEngine unzipEngine = new UnzipEngine(zipModel, fileHeader); - return unzipEngine.getInputStream(); - } - - private void checkOutputDirectoryStructure(FileHeader fileHeader, String outPath, String newFileName) throws ZipException { - if (fileHeader == null || !Zip4jUtil.isStringNotNullAndNotEmpty(outPath)) { - throw new ZipException("Cannot check output directory structure...one of the parameters was null"); - } - - String fileName = fileHeader.getFileName(); - - if (Zip4jUtil.isStringNotNullAndNotEmpty(newFileName)) { - fileName = newFileName; - } - - if (!Zip4jUtil.isStringNotNullAndNotEmpty(fileName)) { - // Do nothing - return; - } - - String compOutPath = outPath + fileName; - try { - File file = new File(compOutPath); - - // make sure no file is extracted outside of the target directory (a.k.a zip slip) - if (!file.getCanonicalPath().startsWith(new File(outPath).getCanonicalPath())) { - throw new ZipException( - "illegal file name that breaks out of the target directory: " + fileHeader.getFileName()); - } - - String parentDir = file.getParent(); - File parentDirFile = new File(parentDir); - if (!parentDirFile.exists()) { - parentDirFile.mkdirs(); - } - } catch (Exception e) { - throw new ZipException(e); - } - } - - private long calculateTotalWork(ArrayList fileHeaders) throws ZipException { - - if (fileHeaders == null) { - throw new ZipException("fileHeaders is null, cannot calculate total work"); - } - - long totalWork = 0; - - for (int i = 0; i < fileHeaders.size(); i++) { - FileHeader fileHeader = (FileHeader)fileHeaders.get(i); - if (fileHeader.getZip64ExtendedInfo() != null && - fileHeader.getZip64ExtendedInfo().getUnCompressedSize() > 0) { - totalWork += fileHeader.getZip64ExtendedInfo().getCompressedSize(); - } else { - totalWork += fileHeader.getCompressedSize(); - } - - } - - return totalWork; - } - -} diff --git a/server/src/main/java/net/lingala/zip4j/unzip/UnzipEngine.java b/server/src/main/java/net/lingala/zip4j/unzip/UnzipEngine.java deleted file mode 100644 index 6679536779..0000000000 --- a/server/src/main/java/net/lingala/zip4j/unzip/UnzipEngine.java +++ /dev/null @@ -1,529 +0,0 @@ -/* -* Copyright 2010 Srikanth Reddy Lingala -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -package net.lingala.zip4j.unzip; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.RandomAccessFile; -import java.util.Arrays; -import java.util.zip.CRC32; - -import net.lingala.zip4j.core.HeaderReader; -import net.lingala.zip4j.crypto.AESDecrypter; -import net.lingala.zip4j.crypto.IDecrypter; -import net.lingala.zip4j.crypto.StandardDecrypter; -import net.lingala.zip4j.exception.ZipException; -import net.lingala.zip4j.io.InflaterInputStream; -import net.lingala.zip4j.io.PartInputStream; -import net.lingala.zip4j.io.ZipInputStream; -import net.lingala.zip4j.model.AESExtraDataRecord; -import net.lingala.zip4j.model.FileHeader; -import net.lingala.zip4j.model.LocalFileHeader; -import net.lingala.zip4j.model.UnzipParameters; -import net.lingala.zip4j.model.ZipModel; -import net.lingala.zip4j.progress.ProgressMonitor; -import net.lingala.zip4j.util.InternalZipConstants; -import net.lingala.zip4j.util.Raw; -import net.lingala.zip4j.util.Zip4jConstants; -import net.lingala.zip4j.util.Zip4jUtil; - -public class UnzipEngine { - - private ZipModel zipModel; - private FileHeader fileHeader; - private int currSplitFileCounter = 0; - private LocalFileHeader localFileHeader; - private IDecrypter decrypter; - private CRC32 crc; - - public UnzipEngine(ZipModel zipModel, FileHeader fileHeader) throws ZipException { - if (zipModel == null || fileHeader == null) { - throw new ZipException("Invalid parameters passed to StoreUnzip. One or more of the parameters were null"); - } - - this.zipModel = zipModel; - this.fileHeader = fileHeader; - this.crc = new CRC32(); - } - - public void unzipFile(ProgressMonitor progressMonitor, - String outPath, String newFileName, UnzipParameters unzipParameters) throws ZipException { - if (zipModel == null || fileHeader == null || !Zip4jUtil.isStringNotNullAndNotEmpty(outPath)) { - throw new ZipException("Invalid parameters passed during unzipping file. One or more of the parameters were null"); - } - InputStream is = null; - OutputStream os = null; - try { - byte[] buff = new byte[InternalZipConstants.BUFF_SIZE]; - int readLength = -1; - - is = getInputStream(); - os = getOutputStream(outPath, newFileName); - - while ((readLength = is.read(buff)) != -1) { - os.write(buff, 0, readLength); - progressMonitor.updateWorkCompleted(readLength); - if (progressMonitor.isCancelAllTasks()) { - progressMonitor.setResult(ProgressMonitor.RESULT_CANCELLED); - progressMonitor.setState(ProgressMonitor.STATE_READY); - return; - } - } - - closeStreams(is, os); - - UnzipUtil.applyFileAttributes(fileHeader, new File(getOutputFileNameWithPath(outPath, newFileName)), unzipParameters); - - } catch (IOException e) { - throw new ZipException(e); - } catch (Exception e) { - throw new ZipException(e); - } finally { - closeStreams(is, os); - } - } - - public ZipInputStream getInputStream() throws ZipException { - if (fileHeader == null) { - throw new ZipException("file header is null, cannot get inputstream"); - } - - RandomAccessFile raf = null; - try { - raf = createFileHandler(InternalZipConstants.READ_MODE); - String errMsg = "local header and file header do not match"; - //checkSplitFile(); - - if (!checkLocalHeader()) - throw new ZipException(errMsg); - - init(raf); - - long comprSize = localFileHeader.getCompressedSize(); - long offsetStartOfData = localFileHeader.getOffsetStartOfData(); - - if (localFileHeader.isEncrypted()) { - if (localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) { - if (decrypter instanceof AESDecrypter) { - comprSize -= (((AESDecrypter)decrypter).getSaltLength() + - ((AESDecrypter)decrypter).getPasswordVerifierLength() + 10); - offsetStartOfData += (((AESDecrypter)decrypter).getSaltLength() + - ((AESDecrypter)decrypter).getPasswordVerifierLength()); - } else { - throw new ZipException("invalid decryptor when trying to calculate " + - "compressed size for AES encrypted file: " + fileHeader.getFileName()); - } - } else if (localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) { - comprSize -= InternalZipConstants.STD_DEC_HDR_SIZE; - offsetStartOfData += InternalZipConstants.STD_DEC_HDR_SIZE; - } - } - - int compressionMethod = fileHeader.getCompressionMethod(); - if (fileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) { - if (fileHeader.getAesExtraDataRecord() != null) { - compressionMethod = fileHeader.getAesExtraDataRecord().getCompressionMethod(); - } else { - throw new ZipException("AESExtraDataRecord does not exist for AES encrypted file: " + fileHeader.getFileName()); - } - } - raf.seek(offsetStartOfData); - switch (compressionMethod) { - case Zip4jConstants.COMP_STORE: - return new ZipInputStream(new PartInputStream(raf, offsetStartOfData, comprSize, this)); - case Zip4jConstants.COMP_DEFLATE: - return new ZipInputStream(new InflaterInputStream(raf, offsetStartOfData, comprSize, this)); - default: - throw new ZipException("compression type not supported"); - } - } catch (ZipException e) { - if (raf != null) { - try { - raf.close(); - } catch (IOException e1) { - //ignore - } - } - throw e; - } catch (Exception e) { - if (raf != null) { - try { - raf.close(); - } catch (IOException e1) { - } - } - throw new ZipException(e); - } - - } - - private void init(RandomAccessFile raf) throws ZipException { - - if (localFileHeader == null) { - throw new ZipException("local file header is null, cannot initialize input stream"); - } - - try { - initDecrypter(raf); - } catch (ZipException e) { - throw e; - } catch (Exception e) { - throw new ZipException(e); - } - } - - private void initDecrypter(RandomAccessFile raf) throws ZipException { - if (localFileHeader == null) { - throw new ZipException("local file header is null, cannot init decrypter"); - } - - if (localFileHeader.isEncrypted()) { - if (localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) { - decrypter = new StandardDecrypter(fileHeader, getStandardDecrypterHeaderBytes(raf)); - } else if (localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) { - decrypter = new AESDecrypter(localFileHeader, getAESSalt(raf), getAESPasswordVerifier(raf)); - } else { - throw new ZipException("unsupported encryption method"); - } - } - } - - private byte[] getStandardDecrypterHeaderBytes(RandomAccessFile raf) throws ZipException { - try { - byte[] headerBytes = new byte[InternalZipConstants.STD_DEC_HDR_SIZE]; - raf.seek(localFileHeader.getOffsetStartOfData()); - raf.read(headerBytes, 0, 12); - return headerBytes; - } catch (IOException e) { - throw new ZipException(e); - } catch (Exception e) { - throw new ZipException(e); - } - } - - private byte[] getAESSalt(RandomAccessFile raf) throws ZipException { - if (localFileHeader.getAesExtraDataRecord() == null) - return null; - - try { - AESExtraDataRecord aesExtraDataRecord = localFileHeader.getAesExtraDataRecord(); - byte[] saltBytes = new byte[calculateAESSaltLength(aesExtraDataRecord)]; - raf.seek(localFileHeader.getOffsetStartOfData()); - raf.read(saltBytes); - return saltBytes; - } catch (IOException e) { - throw new ZipException(e); - } - } - - private byte[] getAESPasswordVerifier(RandomAccessFile raf) throws ZipException { - try { - byte[] pvBytes = new byte[2]; - raf.read(pvBytes); - return pvBytes; - } catch (IOException e) { - throw new ZipException(e); - } - } - - private int calculateAESSaltLength(AESExtraDataRecord aesExtraDataRecord) throws ZipException { - if (aesExtraDataRecord == null) { - throw new ZipException("unable to determine salt length: AESExtraDataRecord is null"); - } - switch (aesExtraDataRecord.getAesStrength()) { - case Zip4jConstants.AES_STRENGTH_128: - return 8; - case Zip4jConstants.AES_STRENGTH_192: - return 12; - case Zip4jConstants.AES_STRENGTH_256: - return 16; - default: - throw new ZipException("unable to determine salt length: invalid aes key strength"); - } - } - - public void checkCRC() throws ZipException { - if (fileHeader != null) { - if (fileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) { - if (decrypter != null && decrypter instanceof AESDecrypter) { - byte[] tmpMacBytes = ((AESDecrypter)decrypter).getCalculatedAuthenticationBytes(); - byte[] storedMac = ((AESDecrypter)decrypter).getStoredMac(); - byte[] calculatedMac = new byte[InternalZipConstants.AES_AUTH_LENGTH]; - - if (calculatedMac == null || storedMac == null) { - throw new ZipException("CRC (MAC) check failed for " + fileHeader.getFileName()); - } - - System.arraycopy(tmpMacBytes, 0, calculatedMac, 0, InternalZipConstants.AES_AUTH_LENGTH); - - if (!Arrays.equals(calculatedMac, storedMac)) { - throw new ZipException("invalid CRC (MAC) for file: " + fileHeader.getFileName()); - } - } - } else { - long calculatedCRC = crc.getValue() & 0xffffffffL; - if (calculatedCRC != fileHeader.getCrc32()) { - String errMsg = "invalid CRC for file: " + fileHeader.getFileName(); - if (localFileHeader.isEncrypted() && - localFileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) { - errMsg += " - Wrong Password?"; - } - throw new ZipException(errMsg); - } - } - } - } - -// private void checkCRC() throws ZipException { -// if (fileHeader != null) { -// if (fileHeader.getEncryptionMethod() == Zip4jConstants.ENC_METHOD_AES) { -// if (decrypter != null && decrypter instanceof AESDecrypter) { -// byte[] tmpMacBytes = ((AESDecrypter)decrypter).getCalculatedAuthenticationBytes(); -// byte[] actualMacBytes = ((AESDecrypter)decrypter).getStoredMac(); -// if (tmpMacBytes == null || actualMacBytes == null) { -// throw new ZipException("null mac value for AES encrypted file: " + fileHeader.getFileName()); -// } -// byte[] calcMacBytes = new byte[10]; -// System.arraycopy(tmpMacBytes, 0, calcMacBytes, 0, 10); -// if (!Arrays.equals(calcMacBytes, actualMacBytes)) { -// throw new ZipException("invalid CRC(mac) for file: " + fileHeader.getFileName()); -// } -// } else { -// throw new ZipException("invalid decryptor...cannot calculate mac value for file: " -// + fileHeader.getFileName()); -// } -// } else if (unzipEngine != null) { -// long calculatedCRC = unzipEngine.getCRC(); -// long actualCRC = fileHeader.getCrc32(); -// if (calculatedCRC != actualCRC) { -// throw new ZipException("invalid CRC for file: " + fileHeader.getFileName()); -// } -// } -// } -// } - - private boolean checkLocalHeader() throws ZipException { - RandomAccessFile rafForLH = null; - try { - rafForLH = checkSplitFile(); - - if (rafForLH == null) { - rafForLH = new RandomAccessFile(new File(this.zipModel.getZipFile()), InternalZipConstants.READ_MODE); - } - - HeaderReader headerReader = new HeaderReader(rafForLH); - this.localFileHeader = headerReader.readLocalFileHeader(fileHeader); - - if (localFileHeader == null) { - throw new ZipException("error reading local file header. Is this a valid zip file?"); - } - - //TODO Add more comparision later - if (localFileHeader.getCompressionMethod() != fileHeader.getCompressionMethod()) { - return false; - } - - return true; - } catch (FileNotFoundException e) { - throw new ZipException(e); - } finally { - if (rafForLH != null) { - try { - rafForLH.close(); - } catch (IOException e) { - // Ignore this - } catch (Exception e) { - //Ignore this - } - } - } - } - - private RandomAccessFile checkSplitFile() throws ZipException { - if (zipModel.isSplitArchive()) { - int diskNumberStartOfFile = fileHeader.getDiskNumberStart(); - currSplitFileCounter = diskNumberStartOfFile + 1; - String curZipFile = zipModel.getZipFile(); - String partFile = null; - if (diskNumberStartOfFile == zipModel.getEndCentralDirRecord().getNoOfThisDisk()) { - partFile = zipModel.getZipFile(); - } else { - if (diskNumberStartOfFile >= 9) { - partFile = curZipFile.substring(0, curZipFile.lastIndexOf(".")) + ".z" + (diskNumberStartOfFile+ 1); - } else{ - partFile = curZipFile.substring(0, curZipFile.lastIndexOf(".")) + ".z0" + (diskNumberStartOfFile+ 1); - } - } - - try { - RandomAccessFile raf = new RandomAccessFile(partFile, InternalZipConstants.READ_MODE); - - if (currSplitFileCounter == 1) { - byte[] splitSig = new byte[4]; - raf.read(splitSig); - if (Raw.readIntLittleEndian(splitSig, 0) != InternalZipConstants.SPLITSIG) { - throw new ZipException("invalid first part split file signature"); - } - } - return raf; - } catch (FileNotFoundException e) { - throw new ZipException(e); - } catch (IOException e) { - throw new ZipException(e); - } - } - return null; - } - - private RandomAccessFile createFileHandler(String mode) throws ZipException { - if (this.zipModel == null || !Zip4jUtil.isStringNotNullAndNotEmpty(this.zipModel.getZipFile())) { - throw new ZipException("input parameter is null in getFilePointer"); - } - - try { - RandomAccessFile raf = null; - if (zipModel.isSplitArchive()) { - raf = checkSplitFile(); - } else { - raf = new RandomAccessFile(new File(this.zipModel.getZipFile()), mode); - } - return raf; - } catch (FileNotFoundException e) { - throw new ZipException(e); - } catch (Exception e) { - throw new ZipException(e); - } - } - - private FileOutputStream getOutputStream(String outPath, String newFileName) throws ZipException { - if (!Zip4jUtil.isStringNotNullAndNotEmpty(outPath)) { - throw new ZipException("invalid output path"); - } - - try { - File file = new File(getOutputFileNameWithPath(outPath, newFileName)); - - // make sure no file is extracted outside of the target directory (a.k.a zip slip) - if (!file.getCanonicalPath().startsWith(new File(outPath).getCanonicalPath())) { - throw new ZipException( - "illegal file name that breaks out of the target directory: " + fileHeader.getFileName()); - } - - if (!file.getParentFile().exists()) { - file.getParentFile().mkdirs(); - } - - if (file.exists()) { - file.delete(); - } - - FileOutputStream fileOutputStream = new FileOutputStream(file); - return fileOutputStream; - } catch (FileNotFoundException e) { - throw new ZipException(e); - } catch (IOException e) { - throw new ZipException(e); - } - } - - private String getOutputFileNameWithPath(String outPath, String newFileName) throws ZipException { - String fileName = null; - if (Zip4jUtil.isStringNotNullAndNotEmpty(newFileName)) { - fileName = newFileName; - } else { - fileName = fileHeader.getFileName(); - } - return outPath + System.getProperty("file.separator") + fileName; - } - - public RandomAccessFile startNextSplitFile() throws IOException, FileNotFoundException { - String currZipFile = zipModel.getZipFile(); - String partFile = null; - if (currSplitFileCounter == zipModel.getEndCentralDirRecord().getNoOfThisDisk()) { - partFile = zipModel.getZipFile(); - } else { - if (currSplitFileCounter >= 9) { - partFile = currZipFile.substring(0, currZipFile.lastIndexOf(".")) + ".z" + (currSplitFileCounter + 1); - } else { - partFile = currZipFile.substring(0, currZipFile.lastIndexOf(".")) + ".z0" + (currSplitFileCounter + 1); - } - } - currSplitFileCounter++; - try { - if(!Zip4jUtil.checkFileExists(partFile)) { - throw new IOException("zip split file does not exist: " + partFile); - } - } catch (ZipException e) { - throw new IOException(e.getMessage()); - } - return new RandomAccessFile(partFile, InternalZipConstants.READ_MODE); - } - - private void closeStreams(InputStream is, OutputStream os) throws ZipException { - try { - if (is != null) { - is.close(); - is = null; - } - } catch (IOException e) { - if (e != null && Zip4jUtil.isStringNotNullAndNotEmpty(e.getMessage())) { - if (e.getMessage().indexOf(" - Wrong Password?") >= 0) { - throw new ZipException(e.getMessage()); - } - } - } finally { - try { - if (os != null) { - os.close(); - os = null; - } - } catch (IOException e) { - //do nothing - } - } - } - - public void updateCRC(int b) { - crc.update(b); - } - - public void updateCRC(byte[] buff, int offset, int len) { - if (buff != null) { - crc.update(buff, offset, len); - } - } - - public FileHeader getFileHeader() { - return fileHeader; - } - - public IDecrypter getDecrypter() { - return decrypter; - } - - public ZipModel getZipModel() { - return zipModel; - } - - public LocalFileHeader getLocalFileHeader() { - return localFileHeader; - } -} diff --git a/server/src/test/java/com/mirth/connect/util/ArchiveUtilsTest.java b/server/src/test/java/com/mirth/connect/util/ArchiveUtilsTest.java index e9ada7d582..340749446f 100644 --- a/server/src/test/java/com/mirth/connect/util/ArchiveUtilsTest.java +++ b/server/src/test/java/com/mirth/connect/util/ArchiveUtilsTest.java @@ -1,31 +1,50 @@ -/* - * Copyright (c) Mirth Corporation. All rights reserved. - * - * http://www.mirthcorp.com - * - * The software in this package is published under the terms of the MPL license a copy of which has - * been included with this distribution in the LICENSE.txt file. - */ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: Mirth Corporation +// SPDX-FileCopyrightText: 2026 Tony Germano package com.mirth.connect.util; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.BufferedInputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import java.util.zip.GZIPInputStream; +import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipInputStream; +import net.lingala.zip4j.ZipFile; +import net.lingala.zip4j.model.FileHeader; +import net.lingala.zip4j.model.enums.AesKeyStrength; +import net.lingala.zip4j.model.enums.EncryptionMethod; + +import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.junit.Assume; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import com.mirth.connect.util.messagewriter.EncryptionType; + public class ArchiveUtilsTest { @Rule @@ -71,4 +90,243 @@ public void testExtractEmptyZipFile() throws Exception { private static ZipInputStream openZip(File file) throws IOException { return new ZipInputStream(new BufferedInputStream(Files.newInputStream(file.toPath()))); } + + @Test + public void testCreateZipUnencrypted() throws Exception { + File source = createSourceFolder(); + File archive = new File(tempFolder.getRoot(), "plain.zip"); + + ArchiveUtils.createArchive(source, archive, ArchiveStreamFactory.ZIP, null, null, null); + + try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(archive)) { + assertEquals("alpha", readEntry(zipFile, "a.txt")); + assertEquals("bravo", readEntry(zipFile, "sub/b.txt")); + } + } + + @Test + public void testCreateZipStandardEncryption() throws Exception { + assertEncryptedRoundTrip(EncryptionType.STANDARD, EncryptionMethod.ZIP_STANDARD, null); + } + + @Test + public void testCreateZipAes128() throws Exception { + assertEncryptedRoundTrip(EncryptionType.AES128, EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_128); + } + + @Test + public void testCreateZipAes256() throws Exception { + assertEncryptedRoundTrip(EncryptionType.AES256, EncryptionMethod.AES, AesKeyStrength.KEY_STRENGTH_256); + } + + @Test + public void testCreateTar() throws Exception { + File archive = new File(tempFolder.getRoot(), "plain.tar"); + + ArchiveUtils.createArchive(createSourceFolder(), archive, ArchiveStreamFactory.TAR, null, null, null); + + try (InputStream in = Files.newInputStream(archive.toPath())) { + assertSourceFolderContents(readTar(in)); + } + } + + @Test + public void testCreateTarGz() throws Exception { + File archive = new File(tempFolder.getRoot(), "plain.tar.gz"); + + ArchiveUtils.createArchive(createSourceFolder(), archive, ArchiveStreamFactory.TAR, CompressorStreamFactory.GZIP, null, null); + + try (InputStream in = new GZIPInputStream(Files.newInputStream(archive.toPath()))) { + assertSourceFolderContents(readTar(in)); + } + } + + @Test + public void testCreateZipGz() throws Exception { + File archive = new File(tempFolder.getRoot(), "plain.zip.gz"); + + ArchiveUtils.createArchive(createSourceFolder(), archive, ArchiveStreamFactory.ZIP, CompressorStreamFactory.GZIP, null, null); + + Map found = new HashMap<>(); + try (ZipInputStream zis = new ZipInputStream(new GZIPInputStream(Files.newInputStream(archive.toPath())))) { + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + if (!entry.isDirectory()) { + found.put(entry.getName(), new String(zis.readAllBytes(), StandardCharsets.UTF_8)); + } + } + } + assertSourceFolderContents(found); + } + + @Test + public void testCreateZipFromEmptyFolder() throws Exception { + File archive = new File(tempFolder.getRoot(), "empty.zip"); + + ArchiveUtils.createArchive(tempFolder.newFolder(), archive, ArchiveStreamFactory.ZIP, null, null, null); + + // A valid archive with no entries, which needs its central directory to have been written + try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(archive)) { + assertEquals(0, zipFile.size()); + } + } + + private static Map readTar(InputStream in) throws IOException { + Map found = new HashMap<>(); + try (TarArchiveInputStream tar = new TarArchiveInputStream(in)) { + TarArchiveEntry entry; + while ((entry = tar.getNextTarEntry()) != null) { + if (!entry.isDirectory()) { + found.put(entry.getName(), new String(tar.readAllBytes(), StandardCharsets.UTF_8)); + } + } + } + return found; + } + + private static void assertSourceFolderContents(Map found) { + Map expected = new HashMap<>(); + expected.put("a.txt", "alpha"); + expected.put("sub/b.txt", "bravo"); + assertEquals(expected, found); + } + + @Test + public void testZipWriteFailureIsReportedAndFileIsClosed() throws Exception { + assertWriteFailureIsReportedAndFileIsClosed(ArchiveStreamFactory.ZIP, null); + } + + @Test + public void testTarWriteFailureIsReportedAndFileIsClosed() throws Exception { + assertWriteFailureIsReportedAndFileIsClosed(ArchiveStreamFactory.TAR, null); + } + + @Test + public void testTarGzWriteFailureIsReportedAndFileIsClosed() throws Exception { + assertWriteFailureIsReportedAndFileIsClosed(ArchiveStreamFactory.TAR, CompressorStreamFactory.GZIP); + } + + @Test + public void testZipGzWriteFailureIsReportedAndFileIsClosed() throws Exception { + assertWriteFailureIsReportedAndFileIsClosed(ArchiveStreamFactory.ZIP, CompressorStreamFactory.GZIP); + } + + /** + * /dev/full accepts the open but fails every write with ENOSPC. The failure must be reported, + * including when it only surfaces as the archive is finished, and not hidden by an error from + * closing an entry; and the file must still be closed. + */ + private void assertWriteFailureIsReportedAndFileIsClosed(String archiver, String compressor) throws Exception { + Path devFull = Path.of("/dev/full"); + Path openFiles = Path.of("/proc/self/fd"); + // Opening it for writing is what the test needs; it can exist but be unusable in a container + Assume.assumeTrue("Requires Linux (/dev/full and /proc)", Files.isWritable(devFull) && Files.isDirectory(openFiles)); + + File source = createSourceFolder(); + int before = countOpenFilesMatching(openFiles, devFull); + + try { + ArchiveUtils.createArchive(source, devFull.toFile(), archiver, compressor, null, null); + fail("Expected the write to fail"); + } catch (ArchiveUtils.CompressException e) { + Throwable cause = e; + boolean foundIOException = false; + while (cause != null) { + assertFalse("The write failure was hidden by " + cause, cause instanceof NullPointerException); + assertFalse("The archive was not opened: " + cause, cause instanceof FileNotFoundException); + foundIOException |= cause instanceof IOException; + cause = cause.getCause(); + } + assertTrue("Expected the IOException from the failed write in the cause chain", foundIOException); + } + + assertEquals("The archive file was left open", before, countOpenFilesMatching(openFiles, devFull)); + } + + private static int countOpenFilesMatching(Path fdDirectory, Path target) throws IOException { + int count = 0; + try (Stream fds = Files.list(fdDirectory)) { + for (Path fd : (Iterable) fds::iterator) { + try { + if (Files.readSymbolicLink(fd).equals(target)) { + count++; + } + } catch (IOException e) { + // The descriptor was closed while listing (including the one used to list) + } + } + } + return count; + } + + @Test + public void testPasswordRejectedForNonZipArchive() throws Exception { + assertPasswordRejectedAndNothingWritten(ArchiveStreamFactory.TAR, null); + } + + @Test + public void testPasswordRejectedForZipWithCompressor() throws Exception { + assertPasswordRejectedAndNothingWritten(ArchiveStreamFactory.ZIP, CompressorStreamFactory.GZIP); + } + + private void assertPasswordRejectedAndNothingWritten(String archiver, String compressor) throws Exception { + File archive = new File(tempFolder.getRoot(), "rejected.out"); + + File source = createSourceFolder(); + + assertThrows(ArchiveUtils.CompressException.class, () -> ArchiveUtils.createArchive(source, archive, archiver, compressor, "secret", EncryptionType.AES256)); + assertFalse(archive.exists()); + } + + private void assertEncryptedRoundTrip(EncryptionType type, EncryptionMethod method, AesKeyStrength strength) throws Exception { + File source = createSourceFolder(); + File archive = new File(tempFolder.getRoot(), type.name() + ".zip"); + + ArchiveUtils.createArchive(source, archive, ArchiveStreamFactory.ZIP, null, "secret", type); + + // Independent evidence that it is encrypted: the JDK refuses archives whose entries are + java.util.zip.ZipException rejected = assertThrows(java.util.zip.ZipException.class, () -> new java.util.zip.ZipFile(archive).close()); + assertTrue(rejected.getMessage(), rejected.getMessage().contains("encrypted")); + + File extracted = tempFolder.newFolder(type.name() + "-extracted"); + + try (ZipFile zipFile = new ZipFile(archive, "secret".toCharArray())) { + assertTrue(zipFile.isEncrypted()); + + List headers = zipFile.getFileHeaders(); + assertEquals(2, headers.size()); + for (FileHeader header : headers) { + assertTrue(header.isEncrypted()); + assertEquals(method, header.getEncryptionMethod()); + if (strength != null) { + assertEquals(strength, header.getAesExtraDataRecord().getAesKeyStrength()); + } + } + + zipFile.extractAll(extracted.getPath()); + } + + assertEquals("alpha", Files.readString(extracted.toPath().resolve("a.txt"), StandardCharsets.UTF_8)); + assertEquals("bravo", Files.readString(extracted.toPath().resolve("sub/b.txt"), StandardCharsets.UTF_8)); + + File wrongPasswordTarget = tempFolder.newFolder(type.name() + "-wrong"); + assertThrows(net.lingala.zip4j.exception.ZipException.class, () -> { + try (ZipFile zipFile = new ZipFile(archive, "wrong".toCharArray())) { + zipFile.extractAll(wrongPasswordTarget.getPath()); + } + }); + } + + private File createSourceFolder() throws Exception { + Path source = tempFolder.newFolder().toPath(); + Files.writeString(source.resolve("a.txt"), "alpha", StandardCharsets.UTF_8); + Files.writeString(Files.createDirectory(source.resolve("sub")).resolve("b.txt"), "bravo", StandardCharsets.UTF_8); + return source.toFile(); + } + + private static String readEntry(java.util.zip.ZipFile zipFile, String name) throws Exception { + try (InputStream in = zipFile.getInputStream(zipFile.getEntry(name))) { + return new String(in.readAllBytes(), StandardCharsets.UTF_8); + } + } } diff --git a/server/src/test/java/com/mirth/connect/util/ZipTestUtils.java b/server/src/test/java/com/mirth/connect/util/ZipTestUtils.java index c72771624d..5567ad2555 100644 --- a/server/src/test/java/com/mirth/connect/util/ZipTestUtils.java +++ b/server/src/test/java/com/mirth/connect/util/ZipTestUtils.java @@ -1,11 +1,6 @@ -/* - * Copyright (c) Mirth Corporation. All rights reserved. - * - * http://www.mirthcorp.com - * - * The software in this package is published under the terms of the MPL license a copy of which has - * been included with this distribution in the LICENSE.txt file. - */ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: Mirth Corporation +// SPDX-FileCopyrightText: 2026 Tony Germano package com.mirth.connect.util; @@ -20,12 +15,9 @@ public class ZipTestUtils { - public static File createTempZipFile(String fileName) throws Exception { - return createTempZipFile(null, fileName); // system defined temp - } - + /** Creates a zip with one entry (or none, if fileName is empty) in the given directory. */ public static File createTempZipFile(File directory, String fileName) throws Exception { - Path tempFile = directory == null ? Files.createTempFile("temp_zip", ".zip") : Files.createTempFile(directory.toPath(), "temp_zip", ".zip"); + Path tempFile = Files.createTempFile(directory.toPath(), "temp_zip", ".zip"); try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(tempFile)))) { if (!Strings.isNullOrEmpty(fileName)) { diff --git a/server/src/test/java/com/mirth/connect/util/messagewriter/MessageWriterArchiveTest.java b/server/src/test/java/com/mirth/connect/util/messagewriter/MessageWriterArchiveTest.java new file mode 100644 index 0000000000..472412030a --- /dev/null +++ b/server/src/test/java/com/mirth/connect/util/messagewriter/MessageWriterArchiveTest.java @@ -0,0 +1,160 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Tony Germano + +package com.mirth.connect.util.messagewriter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.PosixFilePermissions; +import java.util.List; +import java.util.stream.Stream; + +import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import com.mirth.connect.donkey.model.message.Message; + +import net.lingala.zip4j.ZipFile; + +public class MessageWriterArchiveTest { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + private Path exportFolder; + private Path rootFolder; + private File archiveFile; + + @Before + public void createFolders() throws Exception { + exportFolder = tempFolder.newFolder("export").toPath(); + // Where the messages are written before they are archived, as MessageWriterFactory sets it up + rootFolder = exportFolder.resolve(".archive"); + archiveFile = exportFolder.resolve("archive.zip").toFile(); + } + + @Test + public void testFinishWriteMovesPasswordProtectedArchiveIntoPlace() throws Exception { + MessageWriterArchive writer = new MessageWriterArchive(new FolderWriter(true), rootFolder.toFile(), archiveFile, ArchiveStreamFactory.ZIP, null, "secret", EncryptionType.AES256); + + assertTrue(writer.write(new Message())); + writer.finishWrite(); + writer.close(); + + // Only the archive is left: neither the folder the messages were written to nor the file it was built in + assertEquals(List.of("archive.zip"), listing()); + + try (ZipFile zipFile = new ZipFile(archiveFile, "secret".toCharArray())) { + assertTrue(zipFile.isEncrypted()); + + File extracted = tempFolder.newFolder("extracted"); + zipFile.extractAll(extracted.getPath()); + assertEquals("message", Files.readString(extracted.toPath().resolve("message.txt"), StandardCharsets.UTF_8)); + } + } + + @Test + public void testFinishWriteCreatesNoArchiveWhenNothingWasWritten() throws Exception { + MessageWriterArchive writer = new MessageWriterArchive(new FolderWriter(false), rootFolder.toFile(), archiveFile, ArchiveStreamFactory.ZIP, null, null, null); + + assertFalse(writer.write(new Message())); + writer.finishWrite(); + writer.close(); + + assertFalse(archiveFile.exists()); + } + + @Test + public void testFinishWriteLeavesNoPartialArchiveWhenArchivingFails() throws Exception { + MessageWriterArchive writer = new MessageWriterArchive(new FolderWriter(true), rootFolder.toFile(), archiveFile, "not-an-archiver", null, null, null); + + assertTrue(writer.write(new Message())); + try { + writer.finishWrite(); + fail("Expected the archive to fail"); + } catch (MessageWriterException e) { + // expected + } + + // Neither an archive, nor the file it was being built in, nor the exported messages + assertEquals(List.of(), listing()); + } + + @Test + public void testFinishWriteKeepsFinishedArchiveWhenMovingItFails() throws Exception { + // An archive at the destination that cannot be replaced: a folder that cannot be emptied + Path blocking = Files.createDirectories(archiveFile.toPath()); + Files.writeString(blocking.resolve("keep.txt"), "keep", StandardCharsets.UTF_8); + Assume.assumeTrue("Requires POSIX permissions", Files.getFileStore(blocking).supportsFileAttributeView("posix")); + Files.setPosixFilePermissions(blocking, PosixFilePermissions.fromString("r-xr-xr-x")); + + try { + Assume.assumeFalse("Running as a user that ignores permissions", Files.isWritable(blocking)); + + MessageWriterArchive writer = new MessageWriterArchive(new FolderWriter(true), rootFolder.toFile(), archiveFile, ArchiveStreamFactory.ZIP, null, null, null); + assertTrue(writer.write(new Message())); + try { + writer.finishWrite(); + fail("Expected moving the archive to fail"); + } catch (MessageWriterException e) { + // expected + } + + // The finished archive is kept where it was built, and the exported messages are removed + assertEquals(List.of(".archive.zip", "archive.zip"), listing()); + try (java.util.zip.ZipFile zip = new java.util.zip.ZipFile(exportFolder.resolve(".archive.zip").toFile())) { + assertTrue(zip.getEntry("message.txt") != null); + } + } finally { + Files.setPosixFilePermissions(blocking, PosixFilePermissions.fromString("rwxr-xr-x")); + } + } + + private List listing() throws Exception { + try (Stream files = Files.list(exportFolder)) { + return files.map(file -> file.getFileName().toString()).sorted().toList(); + } + } + + /** Stands in for the writer that puts each message into a file in the folder to archive. */ + private class FolderWriter implements MessageWriter { + + private final boolean written; + + FolderWriter(boolean written) { + this.written = written; + } + + @Override + public boolean write(Message message) throws MessageWriterException { + if (written) { + try { + Files.createDirectories(rootFolder); + Files.writeString(rootFolder.resolve("message.txt"), "message", StandardCharsets.UTF_8); + } catch (Exception e) { + throw new MessageWriterException(e); + } + } + return written; + } + + @Override + public void finishWrite() { + } + + @Override + public void close() { + } + } +} diff --git a/server/src/test/java/com/mirth/connect/util/messagewriter/MessageWriterFactoryTest.java b/server/src/test/java/com/mirth/connect/util/messagewriter/MessageWriterFactoryTest.java new file mode 100644 index 0000000000..d11bf1f9fd --- /dev/null +++ b/server/src/test/java/com/mirth/connect/util/messagewriter/MessageWriterFactoryTest.java @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: 2026 Tony Germano + +package com.mirth.connect.util.messagewriter; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Stream; + +import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import com.mirth.connect.util.ArchiveUtils; + +public class MessageWriterFactoryTest { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void passwordIsAcceptedForPlainZip() throws Exception { + MessageWriter writer = MessageWriterFactory.getInstance().getMessageWriter(options(ArchiveStreamFactory.ZIP, null, "secret"), null); + + assertTrue(writer instanceof MessageWriterArchive); + } + + @Test + public void passwordIsRejectedForNonZipArchive() throws Exception { + assertPasswordRejected(ArchiveStreamFactory.TAR, null); + } + + @Test + public void passwordIsRejectedForZipWithCompressor() throws Exception { + assertPasswordRejected(ArchiveStreamFactory.ZIP, CompressorStreamFactory.GZIP); + } + + @Test + public void blankPasswordIsNotRejected() throws Exception { + MessageWriter writer = MessageWriterFactory.getInstance().getMessageWriter(options(ArchiveStreamFactory.TAR, CompressorStreamFactory.GZIP, ""), null); + + assertTrue(writer instanceof MessageWriterArchive); + } + + @Test + public void passwordIsIgnoredWhenNotEnabled() throws Exception { + MessageWriterOptions options = options(ArchiveStreamFactory.TAR, null, "secret"); + options.setPasswordEnabled(false); + + assertTrue(MessageWriterFactory.getInstance().getMessageWriter(options, null) instanceof MessageWriterArchive); + } + + private void assertPasswordRejected(String archiver, String compressor) throws Exception { + try { + MessageWriterFactory.getInstance().getMessageWriter(options(archiver, compressor, "secret"), null); + fail("Expected the password to be rejected"); + } catch (MessageWriterException e) { + assertEquals(ArchiveUtils.PASSWORD_UNSUPPORTED_MESSAGE, e.getMessage()); + } + + // Nothing should have been created for a run that cannot succeed + try (Stream created = Files.list(tempFolder.getRoot().toPath())) { + assertEquals(0, created.count()); + } + } + + private MessageWriterOptions options(String archiver, String compressor, String password) { + MessageWriterOptions options = new MessageWriterOptions(); + options.setBaseFolder(tempFolder.getRoot().getPath()); + options.setRootFolder(new File(tempFolder.getRoot(), "export").getPath()); + options.setFilePattern("message"); + options.setArchiveFormat(archiver); + options.setCompressFormat(compressor); + options.setPasswordEnabled(true); + options.setPassword(password); + options.setEncryptionType(EncryptionType.AES256); + return options; + } +} diff --git a/server/src/test/java/net/lingala/zip4j/unzip/UnzipEngineTest.java b/server/src/test/java/net/lingala/zip4j/unzip/UnzipEngineTest.java deleted file mode 100644 index efdd6541d0..0000000000 --- a/server/src/test/java/net/lingala/zip4j/unzip/UnzipEngineTest.java +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (c) Mirth Corporation. All rights reserved. - * - * http://www.mirthcorp.com - * - * The software in this package is published under the terms of the MPL license a copy of which has - * been included with this distribution in the LICENSE.txt file. - */ - -package net.lingala.zip4j.unzip; - -import static org.junit.Assert.assertTrue; - -import java.io.File; - -import net.lingala.zip4j.exception.ZipException; -import net.lingala.zip4j.model.FileHeader; -import net.lingala.zip4j.model.UnzipParameters; -import net.lingala.zip4j.model.ZipModel; -import net.lingala.zip4j.progress.ProgressMonitor; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.mirth.connect.util.ZipTestUtils; - -public class UnzipEngineTest { - - @Before - public void createTestFolder() { - File installTempDir = new File("tests/zipextraction"); - if (!installTempDir.exists()) { - installTempDir.mkdir(); - } else { - cleanupTestFolder(); - } - } - - @After - public void cleanupTestFolder() { - File tempDir = new File("tests/zipextraction"); - if (tempDir.exists()) { - for (File file : tempDir.listFiles()) { - file.delete(); - } - } - } - - @Test(expected = ZipException.class) - public void testUnzipWithMaliciousFile1() throws Exception { - String maliciousFileName = "../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"; - File maliciousZipFile = ZipTestUtils.createTempZipFile(maliciousFileName); - - ZipModel zipModel = new ZipModel(); - zipModel.setZipFile(maliciousZipFile.getAbsolutePath()); - FileHeader fileHeader = new FileHeader(); - fileHeader.setFileName(maliciousFileName); - fileHeader.setCompressionMethod(8); - UnzipEngine engine = new UnzipEngine(zipModel, fileHeader); - - ProgressMonitor progressMonitor = new ProgressMonitor(); - String outPath = "tests/zipextraction"; - String newFileName = null; - UnzipParameters unzipParams = new UnzipParameters(); - - engine.unzipFile(progressMonitor, outPath, newFileName, unzipParams); - } - - /* - * This case should not throw a ZipSlip related exception because we are passing in a - * newFileName which will be used for the destination file - */ - @Test - public void testUnzipWithMaliciousFileButValidNewFileName() throws Exception { - String maliciousFileName = "../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"; - File maliciousZipFile = ZipTestUtils.createTempZipFile(maliciousFileName); - - ZipModel zipModel = new ZipModel(); - zipModel.setZipFile(maliciousZipFile.getAbsolutePath()); - FileHeader fileHeader = new FileHeader(); - fileHeader.setFileName(maliciousFileName); - fileHeader.setCompressionMethod(8); - UnzipEngine engine = new UnzipEngine(zipModel, fileHeader); - - ProgressMonitor progressMonitor = new ProgressMonitor(); - String outPath = "tests/zipextraction"; - String newFileName = "evil.txt"; - UnzipParameters unzipParams = new UnzipParameters(); - - engine.unzipFile(progressMonitor, outPath, newFileName, unzipParams); - - File outputFile = new File(outPath + File.separator + newFileName); - assertTrue(outputFile.exists()); - } - - @Test(expected = ZipException.class) - public void testUnzipWithMaliciousFileAndMaliciousNewFileName() throws Exception { - String maliciousFileName = "../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"; - File maliciousZipFile = ZipTestUtils.createTempZipFile(maliciousFileName); - - ZipModel zipModel = new ZipModel(); - zipModel.setZipFile(maliciousZipFile.getAbsolutePath()); - FileHeader fileHeader = new FileHeader(); - fileHeader.setFileName(maliciousFileName); - fileHeader.setCompressionMethod(8); - UnzipEngine engine = new UnzipEngine(zipModel, fileHeader); - - ProgressMonitor progressMonitor = new ProgressMonitor(); - String outPath = "tests/zipextraction"; - String newFileName = maliciousFileName; - UnzipParameters unzipParams = new UnzipParameters(); - - engine.unzipFile(progressMonitor, outPath, newFileName, unzipParams); - - File outputFile = new File(outPath + File.separator + newFileName); - assertTrue(outputFile.exists()); - } - - @Test - public void testUnzipWithNormalFile1() throws Exception { - String fileName = "good.txt"; - File zipFile = ZipTestUtils.createTempZipFile(fileName); - - ZipModel zipModel = new ZipModel(); - zipModel.setZipFile(zipFile.getAbsolutePath()); - FileHeader fileHeader = new FileHeader(); - fileHeader.setFileName(fileName); - fileHeader.setCompressionMethod(8); - UnzipEngine engine = new UnzipEngine(zipModel, fileHeader); - - ProgressMonitor progressMonitor = new ProgressMonitor(); - String outPath = "tests/zipextraction"; - String newFileName = null; - UnzipParameters unzipParams = new UnzipParameters(); - - engine.unzipFile(progressMonitor, outPath, newFileName, unzipParams); - File outputFile = new File(outPath + File.separator + fileName); - assertTrue(outputFile.exists()); - } - - @Test - public void testUnzipWithNormalFile2() throws Exception { - String fileName = "good.txt"; - File zipFile = ZipTestUtils.createTempZipFile(fileName); - - ZipModel zipModel = new ZipModel(); - zipModel.setZipFile(zipFile.getAbsolutePath()); - FileHeader fileHeader = new FileHeader(); - fileHeader.setFileName(fileName); - fileHeader.setCompressionMethod(8); - UnzipEngine engine = new UnzipEngine(zipModel, fileHeader); - - ProgressMonitor progressMonitor = new ProgressMonitor(); - String outPath = "tests/zipextraction"; - String newFileName = "good2.txt"; - UnzipParameters unzipParams = new UnzipParameters(); - - engine.unzipFile(progressMonitor, outPath, newFileName, unzipParams); - - engine.unzipFile(progressMonitor, outPath, newFileName, unzipParams); - File outputFile = new File(outPath + File.separator + newFileName); - assertTrue(outputFile.exists()); - } - - @Test(expected = ZipException.class) - public void testUnzipWithNormalFileButMaliciousNewFileName() throws Exception { - String fileName = "good.txt"; - File zipFile = ZipTestUtils.createTempZipFile(fileName); - - ZipModel zipModel = new ZipModel(); - zipModel.setZipFile(zipFile.getAbsolutePath()); - FileHeader fileHeader = new FileHeader(); - fileHeader.setFileName(fileName); - fileHeader.setCompressionMethod(8); - UnzipEngine engine = new UnzipEngine(zipModel, fileHeader); - - ProgressMonitor progressMonitor = new ProgressMonitor(); - String outPath = "tests/zipextraction"; - String newFileName = "../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"; - UnzipParameters unzipParams = new UnzipParameters(); - - engine.unzipFile(progressMonitor, outPath, newFileName, unzipParams); - - engine.unzipFile(progressMonitor, outPath, newFileName, unzipParams); - File outputFile = new File(outPath + File.separator + newFileName); - assertTrue(outputFile.exists()); - } -} diff --git a/server/src/test/java/net/lingala/zip4j/unzip/UnzipTest.java b/server/src/test/java/net/lingala/zip4j/unzip/UnzipTest.java deleted file mode 100644 index fecfc0fdd5..0000000000 --- a/server/src/test/java/net/lingala/zip4j/unzip/UnzipTest.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright (c) Mirth Corporation. All rights reserved. - * - * http://www.mirthcorp.com - * - * The software in this package is published under the terms of the MPL license a copy of which has - * been included with this distribution in the LICENSE.txt file. - */ - -package net.lingala.zip4j.unzip; - -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.util.ArrayList; - -import net.lingala.zip4j.exception.ZipException; -import net.lingala.zip4j.model.CentralDirectory; -import net.lingala.zip4j.model.FileHeader; -import net.lingala.zip4j.model.UnzipParameters; -import net.lingala.zip4j.model.ZipModel; -import net.lingala.zip4j.progress.ProgressMonitor; - -import org.apache.commons.lang3.tuple.Pair; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.mirth.connect.util.ZipTestUtils; - -public class UnzipTest { - - @Before - public void createTestFolder() { - File installTempDir = new File("tests/zipextraction"); - if (!installTempDir.exists()) { - installTempDir.mkdir(); - } else { - cleanupTestFolder(); - } - } - - @After - public void cleanupTestFolder() { - File tempDir = new File("tests/zipextraction"); - if (tempDir.exists()) { - for (File file : tempDir.listFiles()) { - file.delete(); - } - } - } - - @Test(expected = ZipException.class) - public void testExtractMaliciousFile1() throws Exception { - String maliciousFileName = "../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"; - Pair pair = createUnzipPair(maliciousFileName); - Unzip unzip = pair.getLeft(); - FileHeader fileHeader = pair.getRight(); - - UnzipParameters unzipParams = new UnzipParameters(); - String outPath = "tests/zipextraction"; - ProgressMonitor progressMonitor = new ProgressMonitor(); - String newFileName = null; - - unzip.extractFile(fileHeader, outPath, unzipParams, newFileName, progressMonitor, false); - } - - /* - * Unzip.initExtractFile tests the fileName from the fileHeader, regardless if you pass in a - * newFileName, so this should throw an exception. - */ - @Test(expected = ZipException.class) - public void testExtractMaliciousFile2() throws Exception { - String maliciousFileName = "../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"; - Pair pair = createUnzipPair(maliciousFileName); - Unzip unzip = pair.getLeft(); - FileHeader fileHeader = pair.getRight(); - - UnzipParameters unzipParams = new UnzipParameters(); - String outPath = "tests/zipextraction"; - ProgressMonitor progressMonitor = new ProgressMonitor(); - String newFileName = "evil.txt"; - - unzip.extractFile(fileHeader, outPath, unzipParams, newFileName, progressMonitor, false); - } - - @Test - public void testExtractNormalFile1() throws Exception { - String fileName = "good.txt"; - Pair pair = createUnzipPair(fileName); - Unzip unzip = pair.getLeft(); - FileHeader fileHeader = pair.getRight(); - - UnzipParameters unzipParams = new UnzipParameters(); - String outPath = "tests/zipextraction"; - ProgressMonitor progressMonitor = new ProgressMonitor(); - String newFileName = null; - - unzip.extractFile(fileHeader, outPath, unzipParams, newFileName, progressMonitor, false); - File outputFile = new File(outPath + File.separator + fileName); - assertTrue(outputFile.exists()); - } - - @Test - public void testExtractNormalFile2() throws Exception { - String fileName = "good.txt"; - Pair pair = createUnzipPair(fileName); - Unzip unzip = pair.getLeft(); - FileHeader fileHeader = pair.getRight(); - - UnzipParameters unzipParams = new UnzipParameters(); - String outPath = "tests/zipextraction"; - ProgressMonitor progressMonitor = new ProgressMonitor(); - String newFileName = "good2.txt"; - - unzip.extractFile(fileHeader, outPath, unzipParams, newFileName, progressMonitor, false); - File outputFile = new File(outPath + File.separator + newFileName); - assertTrue(outputFile.exists()); - } - - @Test(expected = ZipException.class) - public void testExtractNormalFileWithMaliciousNewFileName() throws Exception { - String fileName = "good.txt"; - Pair pair = createUnzipPair(fileName); - Unzip unzip = pair.getLeft(); - FileHeader fileHeader = pair.getRight(); - - UnzipParameters unzipParams = new UnzipParameters(); - String outPath = "tests/zipextraction"; - ProgressMonitor progressMonitor = new ProgressMonitor(); - String newFileName = "../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"; - - unzip.extractFile(fileHeader, outPath, unzipParams, newFileName, progressMonitor, false); - File outputFile = new File(outPath + File.separator + newFileName); - assertTrue(outputFile.exists()); - } - - @Test(expected = ZipException.class) - public void testExtractAllFromMaliciousFile() throws Exception { - String maliciousFileName = "../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt"; - Pair pair = createUnzipPair(maliciousFileName); - Unzip unzip = pair.getLeft(); - - UnzipParameters unzipParams = new UnzipParameters(); - String outPath = "tests/zipextraction"; - ProgressMonitor progressMonitor = new ProgressMonitor(); - - unzip.extractAll(unzipParams, outPath, progressMonitor, false); - } - - @Test - public void testExtractAllFromNormalFile() throws Exception { - String fileName = "good.txt"; - Pair pair = createUnzipPair(fileName); - Unzip unzip = pair.getLeft(); - - UnzipParameters unzipParams = new UnzipParameters(); - String outPath = "tests/zipextraction"; - ProgressMonitor progressMonitor = new ProgressMonitor(); - - unzip.extractAll(unzipParams, outPath, progressMonitor, false); - File outputFile = new File(outPath + File.separator + fileName); - assertTrue(outputFile.exists()); - } - - private Pair createUnzipPair(String fileName) throws Exception { - File zipFile = ZipTestUtils.createTempZipFile(fileName); - ZipModel zipModel = new ZipModel(); - zipModel.setZipFile(zipFile.getAbsolutePath()); - CentralDirectory centralDirectory = new CentralDirectory(); - ArrayList fileHeaders = new ArrayList<>(); - FileHeader fileHeader = new FileHeader(); - fileHeader.setFileName(fileName); - fileHeader.setCompressionMethod(8); - fileHeaders.add(fileHeader); - centralDirectory.setFileHeaders(fileHeaders); - zipModel.setCentralDirectory(centralDirectory); - - Unzip unzip = new Unzip(zipModel); - - return Pair.of(unzip, fileHeader); - } - -} diff --git a/smoketest/src/test/java/org/openintegrationengine/smoketest/admin/MessageExportArchiveTest.java b/smoketest/src/test/java/org/openintegrationengine/smoketest/admin/MessageExportArchiveTest.java index 0032663600..bca0d4ce0e 100644 --- a/smoketest/src/test/java/org/openintegrationengine/smoketest/admin/MessageExportArchiveTest.java +++ b/smoketest/src/test/java/org/openintegrationengine/smoketest/admin/MessageExportArchiveTest.java @@ -4,6 +4,7 @@ package org.openintegrationengine.smoketest.admin; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -37,6 +38,7 @@ import org.openintegrationengine.smoketest.SharedServer; import com.mirth.connect.client.core.Client; +import com.mirth.connect.client.core.ClientException; import com.mirth.connect.client.core.PaginatedMessageList; import com.mirth.connect.donkey.model.message.ContentType; import com.mirth.connect.donkey.model.message.Message; @@ -45,6 +47,7 @@ import com.mirth.connect.util.MessageExporter; import com.mirth.connect.util.messagewriter.EncryptionType; import com.mirth.connect.util.messagewriter.MessageWriter; +import com.mirth.connect.util.messagewriter.MessageWriterException; import com.mirth.connect.util.messagewriter.MessageWriterFactory; import com.mirth.connect.util.messagewriter.MessageWriterOptions; @@ -172,6 +175,24 @@ void serverSideEncryptedZip() throws Exception { assertEquals(MESSAGES, admin().exportMessagesServer(channelId, new MessageFilter(), 2, options)); } + /** A password cannot be applied to a TAR, so the export is refused up front, not written unprotected. */ + @Test + @DisplayName("refuses a password for a TAR") + void passwordIsRefusedForTar() throws Exception { + MessageWriterOptions options = options("refused", ArchiveStreamFactory.TAR, null, PASSWORD, EncryptionType.AES256); + options.setBaseFolder(exportDirectory.toString()); + options.setRootFolder(exportDirectory.resolve("refused").toString()); + + assertThrows(MessageWriterException.class, () -> MessageWriterFactory.getInstance().getMessageWriter(options, null)); + assertFalse(Files.exists(exportDirectory.resolve("refused")), "export directory of a refused export"); + + // The server refuses it too, before it writes anything + MessageWriterOptions serverOptions = options("refused-on-server", ArchiveStreamFactory.TAR, null, PASSWORD, EncryptionType.AES256); + serverOptions.setBaseFolder(null); + serverOptions.setRootFolder(serverFolder("-refused")); + assertThrows(ClientException.class, () -> admin().exportMessagesServer(channelId, new MessageFilter(), 2, serverOptions)); + } + private static String serverFolder(String suffix) { return SERVER_EXPORT_PREFIX + suffix + "-" + TOKEN; }