Improve load command safety

This commit is contained in:
jglrxavpok 2020-11-06 16:10:44 +01:00
parent d83bec4732
commit 69f424eb6a
2 changed files with 26 additions and 9 deletions

View File

@ -16,7 +16,6 @@ import java.io.*;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.*; import java.util.*;
@ -553,7 +552,7 @@ public class ExtensionManager {
loadExtensionList(extensionsToReload); loadExtensionList(extensionsToReload);
} }
public void loadDynamicExtension(File jarFile) throws FileNotFoundException { public boolean loadDynamicExtension(File jarFile) throws FileNotFoundException {
if(!jarFile.exists()) { if(!jarFile.exists()) {
throw new FileNotFoundException("File '"+jarFile.getAbsolutePath()+"' does not exists. Cannot load extension."); throw new FileNotFoundException("File '"+jarFile.getAbsolutePath()+"' does not exists. Cannot load extension.");
} }
@ -561,10 +560,10 @@ public class ExtensionManager {
log.info("Discover dynamic extension from jar {}", jarFile.getAbsolutePath()); log.info("Discover dynamic extension from jar {}", jarFile.getAbsolutePath());
DiscoveredExtension discoveredExtension = discoverFromJar(jarFile); DiscoveredExtension discoveredExtension = discoverFromJar(jarFile);
List<DiscoveredExtension> extensionsToLoad = Collections.singletonList(discoveredExtension); List<DiscoveredExtension> extensionsToLoad = Collections.singletonList(discoveredExtension);
loadExtensionList(extensionsToLoad); return loadExtensionList(extensionsToLoad);
} }
private void loadExtensionList(List<DiscoveredExtension> extensionsToLoad) { private boolean loadExtensionList(List<DiscoveredExtension> extensionsToLoad) {
// ensure correct order of dependencies // ensure correct order of dependencies
log.debug("Reorder extensions to ensure proper load order"); log.debug("Reorder extensions to ensure proper load order");
extensionsToLoad = generateLoadOrder(extensionsToLoad); extensionsToLoad = generateLoadOrder(extensionsToLoad);
@ -585,7 +584,14 @@ public class ExtensionManager {
// reload extensions // reload extensions
log.info("Actually load extension {}", toReload.getName()); log.info("Actually load extension {}", toReload.getName());
Extension loadedExtension = attemptSingleLoad(toReload); Extension loadedExtension = attemptSingleLoad(toReload);
newExtensions.add(loadedExtension); if(loadedExtension != null) {
newExtensions.add(loadedExtension);
}
}
if(newExtensions.isEmpty()) {
log.error("No extensions to load, skipping callbacks");
return false;
} }
log.info("Load complete, firing preinit, init and then postinit callbacks"); log.info("Load complete, firing preinit, init and then postinit callbacks");
@ -593,6 +599,7 @@ public class ExtensionManager {
newExtensions.forEach(Extension::preInitialize); newExtensions.forEach(Extension::preInitialize);
newExtensions.forEach(Extension::initialize); newExtensions.forEach(Extension::initialize);
newExtensions.forEach(Extension::postInitialize); newExtensions.forEach(Extension::postInitialize);
return true;
} }
public void unloadExtension(String extensionName) { public void unloadExtension(String extensionName) {

View File

@ -38,14 +38,24 @@ public class LoadExtensionCommand extends Command {
ExtensionManager extensionManager = MinecraftServer.getExtensionManager(); ExtensionManager extensionManager = MinecraftServer.getExtensionManager();
Path extensionFolder = extensionManager.getExtensionFolder().toPath().toAbsolutePath(); Path extensionFolder = extensionManager.getExtensionFolder().toPath().toAbsolutePath();
Path extensionJar = extensionFolder.resolve(name); Path extensionJar = extensionFolder.resolve(name);
if(!extensionJar.toAbsolutePath().startsWith(extensionFolder)) { try {
sender.sendMessage("File name '"+name+"' does not represent a file inside the extensions folder. Will not load"); if(!extensionJar.toFile().getCanonicalPath().startsWith(extensionFolder.toFile().getCanonicalPath())) {
sender.sendMessage("File name '"+name+"' does not represent a file inside the extensions folder. Will not load");
return;
}
} catch (IOException e) {
e.printStackTrace();
sender.sendMessage("Failed to load extension: "+e.getMessage());
return; return;
} }
try { try {
extensionManager.loadDynamicExtension(extensionJar.toFile()); boolean managed = extensionManager.loadDynamicExtension(extensionJar.toFile());
sender.sendMessage("Extension loaded!"); if(managed) {
sender.sendMessage("Extension loaded!");
} else {
sender.sendMessage("Failed to load extension, check your logs.");
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
sender.sendMessage("Failed to load extension: "+e.getMessage()); sender.sendMessage("Failed to load extension: "+e.getMessage());