From 7db06061f025b20874f5801f49a1923508e73f27 Mon Sep 17 00:00:00 2001 From: NotMyFault Date: Mon, 28 Feb 2022 15:42:03 +0100 Subject: [PATCH] Update upstream e2ab12c Change the class source validator to use classloaders for detection. (2039) --- .../bukkit/util/ClassSourceValidator.java | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/bukkit/util/ClassSourceValidator.java b/worldedit-bukkit/src/main/java/com/sk89q/bukkit/util/ClassSourceValidator.java index b0de56101..1b1d59fc0 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/bukkit/util/ClassSourceValidator.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/bukkit/util/ClassSourceValidator.java @@ -23,10 +23,10 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; import com.sk89q.worldedit.internal.util.LogManagerCompat; import org.apache.logging.log4j.Logger; +import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import javax.annotation.Nullable; -import java.security.CodeSource; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -45,7 +45,7 @@ public class ClassSourceValidator { private final Plugin plugin; @Nullable - private final CodeSource expectedCodeSource; + private final ClassLoader expectedClassLoader; /** * Create a new instance. @@ -55,7 +55,7 @@ public class ClassSourceValidator { public ClassSourceValidator(Plugin plugin) { checkNotNull(plugin, "plugin"); this.plugin = plugin; - this.expectedCodeSource = plugin.getClass().getProtectionDomain().getCodeSource(); + this.expectedClassLoader = plugin.getClass().getClassLoader(); } /** @@ -64,19 +64,29 @@ public class ClassSourceValidator { * @param classes A list of classes to check * @return The results */ - public Map, CodeSource> findMismatches(List> classes) { + public Map, Plugin> findMismatches(List> classes) { checkNotNull(classes, "classes"); - if (expectedCodeSource == null) { + if (expectedClassLoader == null) { return ImmutableMap.of(); } - Map, CodeSource> mismatches = new HashMap<>(); + Map, Plugin> mismatches = new HashMap<>(); - for (Class testClass : classes) { - CodeSource testSource = testClass.getProtectionDomain().getCodeSource(); - if (!expectedCodeSource.equals(testSource)) { - mismatches.put(testClass, testSource); + for (Plugin target : Bukkit.getPluginManager().getPlugins()) { + if (target == plugin) { + continue; + } + ClassLoader targetLoader = target.getClass().getClassLoader(); + for (Class testClass : classes) { + ClassLoader testSource = null; + try { + testSource = targetLoader.loadClass(testClass.getName()).getClassLoader(); + } catch (ClassNotFoundException ignored) { + } + if (testSource != null && testSource != expectedClassLoader) { + mismatches.putIfAbsent(testClass, testSource == targetLoader ? target : null); + } } } @@ -94,7 +104,7 @@ public class ClassSourceValidator { if (Boolean.getBoolean("enginehub.disable.class.source.validation")) { return; } - Map, CodeSource> mismatches = findMismatches(classes); + Map, Plugin> mismatches = findMismatches(classes); if (mismatches.isEmpty()) { return; @@ -118,9 +128,11 @@ public class ClassSourceValidator { builder.append("**\n"); builder.append("** Here are some files that have been overridden:\n"); builder.append("** \n"); - for (Map.Entry, CodeSource> entry : mismatches.entrySet()) { - CodeSource codeSource = entry.getValue(); - String url = codeSource != null ? codeSource.getLocation().toExternalForm() : "(unknown)"; + for (Map.Entry, Plugin> entry : mismatches.entrySet()) { + Plugin badPlugin = entry.getValue(); + String url = badPlugin == null + ? "(unknown)" + : badPlugin.getName() + " (" + badPlugin.getClass().getProtectionDomain().getCodeSource().getLocation() + ")"; builder.append("** '").append(entry.getKey().getSimpleName()).append("' came from '").append(url).append("'\n"); } builder.append("**\n");