mirror of
https://github.com/dataease/dataease.git
synced 2026-06-13 01:01:20 +08:00
fix: 修复路径篡改(ZIP)
This commit is contained in:
@@ -12,21 +12,23 @@ import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.ProtectionDomain;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @Author Junjun
|
||||
*/
|
||||
public abstract class DataEaseDatasourcePlugin extends Provider implements DataEasePlugin {
|
||||
private final String DEFAULT_FILE_PATH = "/opt/dataease2.0/drivers/plugin";
|
||||
private static final Pattern SAFE_DRIVER_FILE_NAME = Pattern.compile("[A-Za-z0-9._-]+\\.jar");
|
||||
|
||||
|
||||
@Override
|
||||
@@ -56,10 +58,13 @@ public abstract class DataEaseDatasourcePlugin extends Provider implements DataE
|
||||
while (entries.hasMoreElements()) {
|
||||
JarEntry entry = entries.nextElement();
|
||||
String name = entry.getName();
|
||||
if (StringUtils.endsWith(name, ".jar")) {
|
||||
File file = new File(localPath, Paths.get(name).getFileName().toString());
|
||||
if (!entry.isDirectory() && StringUtils.endsWith(name, ".jar")) {
|
||||
String fileName = extractSafeDriverFileName(name);
|
||||
File file = resolveDriverFile(localPath, fileName);
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
if (!file.getParentFile().mkdirs()) {
|
||||
DEException.throwException("Failed to create driver directory");
|
||||
}
|
||||
}
|
||||
|
||||
try (InputStream inputStream = jarFile.getInputStream(entry);
|
||||
@@ -97,8 +102,9 @@ public abstract class DataEaseDatasourcePlugin extends Provider implements DataE
|
||||
while (entries.hasMoreElements()) {
|
||||
JarEntry entry = entries.nextElement();
|
||||
String name = entry.getName();
|
||||
if (StringUtils.endsWith(name, ".jar")) {
|
||||
File file = new File(DEFAULT_FILE_PATH, Paths.get(name).getFileName().toString());
|
||||
if (!entry.isDirectory() && StringUtils.endsWith(name, ".jar")) {
|
||||
String fileName = extractSafeDriverFileName(name);
|
||||
File file = resolveDriverFile(DEFAULT_FILE_PATH, fileName);
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
@@ -107,4 +113,34 @@ public abstract class DataEaseDatasourcePlugin extends Provider implements DataE
|
||||
DEException.throwException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private String extractSafeDriverFileName(String entryName) {
|
||||
if (StringUtils.isBlank(entryName)
|
||||
|| StringUtils.contains(entryName, "..")
|
||||
|| StringUtils.startsWith(entryName, "/")
|
||||
|| StringUtils.startsWith(entryName, "\\")
|
||||
|| StringUtils.contains(entryName, ":")) {
|
||||
DEException.throwException("Invalid driver entry path");
|
||||
}
|
||||
String normalizedEntryName = entryName.replace('\\', '/');
|
||||
int lastSeparatorIndex = normalizedEntryName.lastIndexOf('/');
|
||||
String fileName = lastSeparatorIndex >= 0 ? normalizedEntryName.substring(lastSeparatorIndex + 1) : normalizedEntryName;
|
||||
if (!SAFE_DRIVER_FILE_NAME.matcher(fileName).matches()) {
|
||||
DEException.throwException("Invalid driver file name");
|
||||
}
|
||||
return fileName;
|
||||
}
|
||||
|
||||
private File resolveDriverFile(String localPath, String fileName) {
|
||||
File dirFile = new File(localPath);
|
||||
File file = new File(dirFile, fileName);
|
||||
try {
|
||||
if (!file.getCanonicalPath().startsWith(dirFile.getCanonicalPath() + File.separator)) {
|
||||
DEException.throwException("Invalid driver file path");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
DEException.throwException(e);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user