From b0ce047eae7b20bb7f2b05606a0ee55ace6b28f2 Mon Sep 17 00:00:00 2001 From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com> Date: Sun, 19 Feb 2023 13:17:51 -0500 Subject: [PATCH] Fix plugin updater logic, allow null update directory (#8864) --- patches/server/Paper-Plugins.patch | 47 ++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/patches/server/Paper-Plugins.patch b/patches/server/Paper-Plugins.patch index cf53fdf829..bdfc1f2ca0 100644 --- a/patches/server/Paper-Plugins.patch +++ b/patches/server/Paper-Plugins.patch @@ -467,9 +467,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import joptsimple.OptionSet; +import org.bukkit.configuration.file.YamlConfiguration; +import org.jetbrains.annotations.NotNull; ++import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; + +import java.io.File; ++import java.io.IOException; ++import java.nio.file.Files; +import java.nio.file.Path; + +public class PluginInitializerManager { @@ -483,7 +486,37 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + // We have to load the bukkit configuration inorder to get the update folder location. + File configFileLocationBukkit = (File) minecraftOptionSet.valueOf("bukkit-settings"); + this.pluginDirectory = ((File) minecraftOptionSet.valueOf("plugins")).toPath(); -+ this.updateDirectory = this.pluginDirectory.resolve(YamlConfiguration.loadConfiguration(configFileLocationBukkit).getString("settings.update-folder", "update")); ++ ++ String updateDirectory = YamlConfiguration.loadConfiguration(configFileLocationBukkit).getString("settings.update-folder", "update"); ++ if (updateDirectory.isBlank()) { ++ this.updateDirectory = null; ++ } else { ++ Path resolvedUpdateDirectory = this.pluginDirectory.resolve(updateDirectory); ++ if (!Files.isDirectory(resolvedUpdateDirectory)) { ++ this.updateDirectory = null; ++ return; ++ } ++ ++ boolean isSameFile = true; ++ try { ++ isSameFile = Files.isSameFile(resolvedUpdateDirectory, this.pluginDirectory); ++ } catch (IOException e) { ++ LOGGER.error("Misconfigured update directory!"); ++ LOGGER.error("Failed to compare update/plugin directory", e); ++ } ++ ++ if (isSameFile) { ++ LOGGER.error("Misconfigured update directory!"); ++ LOGGER.error(("Your configured update directory (%s) in bukkit.yml is pointing to the same location as the plugin directory (%s). " + ++ "Disabling auto updating functionality.").formatted(resolvedUpdateDirectory, this.pluginDirectory)); ++ ++ this.updateDirectory = null; ++ } else { ++ this.updateDirectory = resolvedUpdateDirectory; ++ } ++ ++ } ++ + } + + public static PluginInitializerManager init(OptionSet optionSet) { @@ -500,7 +533,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + return pluginDirectory; + } + -+ @NotNull ++ @Nullable + public Path pluginUpdatePath() { + return updateDirectory; + } @@ -4691,10 +4724,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + + try { -+ this.checkUpdate(context); ++ context = this.checkUpdate(context); + + JarFile file = new JarFile(context.toFile()); -+ PluginFileType type = PluginFileType.guessType(file); ++ PluginFileType type = PluginFileType.guessType(file); + if (type == null) { + throw new IllegalArgumentException(source + " is not a valid plugin file, cannot load a plugin from it!"); + } @@ -4712,14 +4745,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + */ + private Path checkUpdate(Path file) throws Exception { + PluginInitializerManager pluginSystem = PluginInitializerManager.instance(); -+ if (!Files.isDirectory(pluginSystem.pluginUpdatePath())) { ++ Path updateDirectory = pluginSystem.pluginUpdatePath(); ++ if (updateDirectory == null || !Files.isDirectory(updateDirectory)) { + return file; + } + + try { + String pluginName = this.getPluginName(file); + UpdateFileVisitor visitor = new UpdateFileVisitor(pluginName); -+ Files.walkFileTree(pluginSystem.pluginUpdatePath(), Set.of(), 1, visitor); ++ Files.walkFileTree(updateDirectory, Set.of(), 1, visitor); + if (visitor.getValidPlugin() != null) { + Path updateLocation = visitor.getValidPlugin(); + @@ -4733,6 +4767,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + File newName = new File(file.toFile().getParentFile(), updateLocation.toFile().getName()); + file.toFile().renameTo(newName); + updateLocation.toFile().delete(); ++ return newName.toPath(); + } + } catch (Exception e) { + throw new InvalidPluginException(e);