Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed client/lib/zip4j_1.3.3.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -336,6 +337,7 @@ client-main = [
"velocity-tools-generic",
"xpp3",
"xstream",
"zip4j",
]
command-main = [
"bcpkix-jdk18on",
Expand Down Expand Up @@ -653,6 +655,7 @@ server-main = [
"velocity-tools-generic",
"xpp3",
"xstream",
"zip4j",
]
server-test = [
"byte-buddy",
Expand Down
2 changes: 2 additions & 0 deletions gradle/vendored-layout.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,14 @@
<sha256 value="fa62e1efcbd19071be4b37ed1053dadc0ca60880b3980f58abd497f2a9f13d05" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="net.lingala.zip4j" name="zip4j" version="2.11.6">
<artifact name="zip4j-2.11.6.jar">
<sha256 value="5f1eb1e9d67cfee200a695e4079751b49e73a226b154193e13f7c602efbe72fb" origin="Generated by Gradle"/>
</artifact>
<artifact name="zip4j-2.11.6.pom">
<sha256 value="9fe419f4b4bf26e8980c0cfcabdb24ff913a3a48b4f9d702ee9bf1fee9d6410b" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.anarres.jdiagnostics" name="jdiagnostics" version="1.0.7">
<artifact name="jdiagnostics-1.0.7.jar">
<sha256 value="7c7fe5347ce2d147ff7bc372f4b2e110d60261fb0f2809e719e3c56ca52ee3d7" origin="Generated by Gradle"/>
Expand Down
1 change: 0 additions & 1 deletion server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
15 changes: 1 addition & 14 deletions server/docs/thirdparty/THIRD-PARTY-README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/)


--------------------------------------------------------------------------------
Expand Down
Binary file removed server/lib/zip4j_1.3.3.jar
Binary file not shown.
164 changes: 86 additions & 78 deletions server/src/main/java/com/mirth/connect/util/ArchiveUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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));
Expand All @@ -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 {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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;
}

Expand Down
Loading
Loading