Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the ziputil dependency to fix a potential security concern #824

Merged
merged 7 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import ai.djl.translate.TranslateException;
import ai.djl.translate.Translator;
import ai.djl.translate.TranslatorFactory;
import ai.djl.util.ZipUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FileUtils;
import org.opensearch.ml.common.FunctionName;
Expand All @@ -27,9 +26,9 @@
import org.opensearch.ml.engine.MLEngine;
import org.opensearch.ml.engine.ModelHelper;
import org.opensearch.ml.engine.Predictable;
import org.opensearch.ml.engine.utils.ZipUtils;

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedActionException;
Expand Down Expand Up @@ -185,9 +184,7 @@ private void loadModel(File modelZipFile, String modelId, String modelName, Stri
if (pathFile.exists()) {
FileUtils.deleteDirectory(pathFile);
}
try (FileInputStream fileInputStream = new FileInputStream(modelZipFile)) {
ZipUtils.unzip(fileInputStream, modelPath);
}
ZipUtils.unzip(modelZipFile, modelPath);
boolean findModelFile = false;
for (File file : pathFile.listFiles()) {
String name = file.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.engine.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import lombok.extern.log4j.Log4j2;

/**
* A util class contains zip file related operations.
*/
@Log4j2
public class ZipUtils {

/**
* Uncompressed a zip file.
* @param zipFile zip file to be uncompressed
* @param dest the destination path of this uncompress
*/
public static void unzip(File zipFile, Path dest) {
ylwu-amzn marked this conversation as resolved.
Show resolved Hide resolved
b4sjoo marked this conversation as resolved.
Show resolved Hide resolved
try {
ZipFile file = new ZipFile(zipFile);
Enumeration<ZipArchiveEntry> en = file.getEntries();
ZipArchiveEntry ze;
while (en.hasMoreElements()) {
ze = en.nextElement();
String name = ze.getName();
Path f = dest.resolve(name).toAbsolutePath();
b4sjoo marked this conversation as resolved.
Show resolved Hide resolved
if (ze.isDirectory()) {
Files.createDirectories(f);
} else {
Path parentFile = f.getParent();
if (parentFile == null) {
throw new AssertionError(
"Parent path should never be null: " + f);
}
Files.createDirectories(parentFile);
InputStream is = file.getInputStream(ze);
Files.copy(is, f, StandardCopyOption.REPLACE_EXISTING);
is.close();
}
}
} catch (IOException e) {
throw new IllegalArgumentException("Input file should never be null", e);
b4sjoo marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.opensearch.ml.engine.utils;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

public class ZipUtilsTest {
@Rule
public ExpectedException exceptionRule = ExpectedException.none();

@Test
public void testEmptyZipFile() throws IOException {
exceptionRule.expect(RuntimeException.class);
Path path = Paths.get("build/empty.zip");
File file = new File(path.toUri());
Path output = Paths.get("build/output");
Files.createDirectories(output);
ZipUtils.unzip(file, output);
}

@Test
public void testUnzipFile() throws IOException, URISyntaxException {
File testZipFile = new File(Objects.requireNonNull(getClass().getResource("foo.zip")).toURI());
b4sjoo marked this conversation as resolved.
Show resolved Hide resolved
Path output = Paths.get("build/output");
Files.createDirectories(output);
ZipUtils.unzip(testZipFile, output);
ylwu-amzn marked this conversation as resolved.
Show resolved Hide resolved
Path testOutputPath = Paths.get("build/output/foo");
Assert.assertTrue(Files.exists(testOutputPath));
}
}
Binary file not shown.