diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/server/ServerModpackCompletionTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/server/ServerModpackCompletionTask.java index 5edc37760..4b3e094ab 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/server/ServerModpackCompletionTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/mod/server/ServerModpackCompletionTask.java @@ -21,6 +21,7 @@ import com.google.gson.JsonParseException; import org.jackhuang.hmcl.download.DefaultDependencyManager; import org.jackhuang.hmcl.download.GameBuilder; import org.jackhuang.hmcl.game.DefaultGameRepository; +import org.jackhuang.hmcl.mod.ModManager; import org.jackhuang.hmcl.mod.ModpackConfiguration; import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.GetTask; @@ -120,7 +121,7 @@ public class ServerModpackCompletionTask extends Task { dependencies.add(builder.buildAsync()); } - Path rootPath = repository.getVersionRoot(version).toPath(); + Path rootPath = repository.getVersionRoot(version).toPath().toAbsolutePath().normalize(); Map files = manifest.getManifest().getFiles().stream() .collect(Collectors.toMap(ModpackConfiguration.FileInformation::getPath, Function.identity())); @@ -128,12 +129,24 @@ public class ServerModpackCompletionTask extends Task { Set remoteFiles = remoteManifest.getFiles().stream().map(ModpackConfiguration.FileInformation::getPath) .collect(Collectors.toSet()); + Path runDirectory = repository.getRunDirectory(version).toPath().toAbsolutePath().normalize(); + Path modsDirectory = runDirectory.resolve("mods"); + int total = 0; // for files in new modpack for (ModpackConfiguration.FileInformation file : remoteManifest.getFiles()) { - Path actualPath = rootPath.resolve(file.getPath()); + Path actualPath = rootPath.resolve(file.getPath()).toAbsolutePath().normalize(); + String fileName = actualPath.getFileName().toString(); + + if (!actualPath.startsWith(rootPath)) { + throw new IOException("Unsecure path: " + file.getPath()); + } + boolean download; - if (!files.containsKey(file.getPath())) { + if (!files.containsKey(file.getPath()) || + modsDirectory.equals(actualPath.getParent()) + && Files.notExists(actualPath.resolveSibling(fileName + ModManager.DISABLED_EXTENSION)) + && Files.notExists(actualPath.resolveSibling(fileName + ModManager.OLD_EXTENSION))) { // If old modpack does not have this entry, download it download = true; } else if (!Files.exists(actualPath)) {