Skip to content

Commit 8aa55bd

Browse files
committed
[ci skip] improve zip slip checks
1 parent ea0086b commit 8aa55bd

File tree

1 file changed

+13
-2
lines changed
  • common/src/main/java/eu/cloudnetservice/cloudnet/common/io

1 file changed

+13
-2
lines changed

common/src/main/java/eu/cloudnetservice/cloudnet/common/io/FileUtil.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public final class FileUtil {
6262

6363
private static final Logger LOGGER = LogManager.logger(FileUtil.class);
6464
private static final DirectoryStream.Filter<Path> ACCEPTING_FILTER = $ -> true;
65+
private static final boolean IS_WINDOWS = System.getProperty("os.name").contains("windows");
6566

6667
private static final Map<String, String> ZIP_FILE_SYSTEM_PROPERTIES = Map.of(
6768
"create", "false", "encoding", "UTF-8");
@@ -249,9 +250,9 @@ private static void extractEntry(
249250
@NonNull ZipEntry zipEntry,
250251
@NonNull Path targetDirectory
251252
) throws IOException {
252-
// get the target path and ensure that there is no path traversal
253+
// checks first if the zip entry name is malicious before extracting
254+
ensureSafeZipEntryName(zipEntry.getName());
253255
var file = targetDirectory.resolve(zipEntry.getName());
254-
ensureChild(targetDirectory, file);
255256

256257
if (zipEntry.isDirectory()) {
257258
FileUtil.createDirectory(file);
@@ -330,6 +331,16 @@ public static void ensureChild(@NonNull Path root, @NonNull Path child) {
330331
}
331332
}
332333

334+
public static void ensureSafeZipEntryName(@NonNull String name) {
335+
if (name.isEmpty()
336+
|| name.startsWith("/")
337+
|| name.startsWith("\\")
338+
|| name.contains("..")
339+
|| (name.contains(":") && IS_WINDOWS)) {
340+
throw new IllegalStateException(String.format("zip entry name %s contains unsafe characters", name));
341+
}
342+
}
343+
333344
public static @NonNull Path resolve(@NonNull Path base, String @NonNull ... more) {
334345
for (var child : more) {
335346
base = base.resolve(child);

0 commit comments

Comments
 (0)