diff --git a/src/main/java/dev/hypera/dragonfly/Dragonfly.java b/src/main/java/dev/hypera/dragonfly/Dragonfly.java index 3b99701..ef59387 100644 --- a/src/main/java/dev/hypera/dragonfly/Dragonfly.java +++ b/src/main/java/dev/hypera/dragonfly/Dragonfly.java @@ -40,6 +40,7 @@ import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; import java.util.stream.Collectors; import org.jetbrains.annotations.ApiStatus.Internal; +import org.jetbrains.annotations.NotNull; /** * Main Dragonfly class. @@ -47,16 +48,16 @@ import org.jetbrains.annotations.ApiStatus.Internal; */ public class Dragonfly { - private static final String VERSION = "0.3.0-SNAPSHOT"; + private static final @NotNull String VERSION = "0.3.1-SNAPSHOT"; private final int timeout; - private final Path directory; - private final Set repositories; - private final Consumer statusHandler; + private final @NotNull Path directory; + private final @NotNull Set repositories; + private final @NotNull Consumer statusHandler; - private final DependencyDownloader dependencyDownloader = new DependencyDownloader(this); - private final DependencyRelocator dependencyRelocator; - private final DependencyLoader dependencyLoader; + private final @NotNull DependencyDownloader dependencyDownloader = new DependencyDownloader(this); + private final @NotNull DependencyRelocator dependencyRelocator; + private final @NotNull DependencyLoader dependencyLoader; @Internal @@ -74,7 +75,7 @@ public class Dragonfly { } } - public static String getVersion() { + public static @NotNull String getVersion() { return VERSION; } @@ -84,7 +85,7 @@ public class Dragonfly { * @param dependencies Dependencies to be loaded. * @return If the load was successful, in the form of a {@link CompletableFuture}. */ - public CompletableFuture load(Dependency... dependencies) { + public @NotNull CompletableFuture load(@NotNull Dependency... dependencies) { return CompletableFuture.supplyAsync(() -> { try { statusHandler.accept(Status.STARTING); @@ -137,11 +138,11 @@ public class Dragonfly { return timeout; } - public Path getDirectory() { + public @NotNull Path getDirectory() { return directory; } - public Set getRepositories() { + public @NotNull Set getRepositories() { return repositories; } @@ -151,7 +152,7 @@ public class Dragonfly { * @return Stored instance of {@link DependencyDownloader}. */ @Internal - public DependencyDownloader getDependencyDownloader() { + public @NotNull DependencyDownloader getDependencyDownloader() { return dependencyDownloader; } diff --git a/src/main/java/dev/hypera/dragonfly/DragonflyBuilder.java b/src/main/java/dev/hypera/dragonfly/DragonflyBuilder.java index 6c4b8e8..3d35043 100644 --- a/src/main/java/dev/hypera/dragonfly/DragonflyBuilder.java +++ b/src/main/java/dev/hypera/dragonfly/DragonflyBuilder.java @@ -32,6 +32,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.function.Consumer; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; /** @@ -115,6 +116,7 @@ public class DragonflyBuilder { * * @return New {@link Dragonfly} instance. */ + @Contract(value = "-> new", pure = true) public @NotNull Dragonfly build() { try { return new Dragonfly(timeout, classLoader, directory, repositories, deleteOnRelocate, statusHandler); diff --git a/src/main/java/dev/hypera/dragonfly/downloaders/impl/MavenDownloader.java b/src/main/java/dev/hypera/dragonfly/downloaders/impl/MavenDownloader.java index eca6c92..767165a 100644 --- a/src/main/java/dev/hypera/dragonfly/downloaders/impl/MavenDownloader.java +++ b/src/main/java/dev/hypera/dragonfly/downloaders/impl/MavenDownloader.java @@ -41,8 +41,8 @@ import org.jetbrains.annotations.NotNull; @Downloader(MavenDependency.class) public class MavenDownloader implements IDownloader { - private final MavenResolver resolver = new MavenResolver(); - private final MavenSnapshotResolver snapshotResolver = new MavenSnapshotResolver(); + private final @NotNull MavenResolver resolver = new MavenResolver(); + private final @NotNull MavenSnapshotResolver snapshotResolver = new MavenSnapshotResolver(); @Override public void download(@NotNull Dragonfly dragonfly, @NotNull MavenDependency dependency) throws DownloadFailureException { diff --git a/src/main/java/dev/hypera/dragonfly/exceptions/DownloadFailureException.java b/src/main/java/dev/hypera/dragonfly/exceptions/DownloadFailureException.java index 1880a5c..41e818f 100644 --- a/src/main/java/dev/hypera/dragonfly/exceptions/DownloadFailureException.java +++ b/src/main/java/dev/hypera/dragonfly/exceptions/DownloadFailureException.java @@ -23,25 +23,29 @@ package dev.hypera.dragonfly.exceptions; +import org.jetbrains.annotations.NotNull; + public class DownloadFailureException extends DragonflyException { + private static final long serialVersionUID = 5648475409314204882L; + public DownloadFailureException() { super(); } - public DownloadFailureException(String message) { + public DownloadFailureException(@NotNull String message) { super(message); } - public DownloadFailureException(String message, Throwable cause) { + public DownloadFailureException(@NotNull String message, @NotNull Throwable cause) { super(message, cause); } - public DownloadFailureException(Throwable cause) { + public DownloadFailureException(@NotNull Throwable cause) { super(cause); } - protected DownloadFailureException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + protected DownloadFailureException(@NotNull String message, @NotNull Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } diff --git a/src/main/java/dev/hypera/dragonfly/exceptions/DragonflyException.java b/src/main/java/dev/hypera/dragonfly/exceptions/DragonflyException.java index 62ec283..79252da 100644 --- a/src/main/java/dev/hypera/dragonfly/exceptions/DragonflyException.java +++ b/src/main/java/dev/hypera/dragonfly/exceptions/DragonflyException.java @@ -23,29 +23,33 @@ package dev.hypera.dragonfly.exceptions; +import org.jetbrains.annotations.NotNull; + /** * Dragonfly Exception. * @author Joshua Sing */ public class DragonflyException extends Exception { + private static final long serialVersionUID = 3565376065959848642L; + public DragonflyException() { super(); } - public DragonflyException(String message) { + public DragonflyException(@NotNull String message) { super(message); } - public DragonflyException(String message, Throwable cause) { + public DragonflyException(@NotNull String message, @NotNull Throwable cause) { super(message, cause); } - public DragonflyException(Throwable cause) { + public DragonflyException(@NotNull Throwable cause) { super(cause); } - protected DragonflyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + protected DragonflyException(@NotNull String message, @NotNull Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } diff --git a/src/main/java/dev/hypera/dragonfly/exceptions/LoadFailureException.java b/src/main/java/dev/hypera/dragonfly/exceptions/LoadFailureException.java index 27fae4f..87850f6 100644 --- a/src/main/java/dev/hypera/dragonfly/exceptions/LoadFailureException.java +++ b/src/main/java/dev/hypera/dragonfly/exceptions/LoadFailureException.java @@ -23,25 +23,29 @@ package dev.hypera.dragonfly.exceptions; +import org.jetbrains.annotations.NotNull; + public class LoadFailureException extends DragonflyException { + private static final long serialVersionUID = -8555618454694039541L; + public LoadFailureException() { super(); } - public LoadFailureException(String message) { + public LoadFailureException(@NotNull String message) { super(message); } - public LoadFailureException(String message, Throwable cause) { + public LoadFailureException(@NotNull String message, @NotNull Throwable cause) { super(message, cause); } - public LoadFailureException(Throwable cause) { + public LoadFailureException(@NotNull Throwable cause) { super(cause); } - protected LoadFailureException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + protected LoadFailureException(@NotNull String message, @NotNull Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } diff --git a/src/main/java/dev/hypera/dragonfly/exceptions/RelocationFailureException.java b/src/main/java/dev/hypera/dragonfly/exceptions/RelocationFailureException.java index cccf256..9a2ca58 100644 --- a/src/main/java/dev/hypera/dragonfly/exceptions/RelocationFailureException.java +++ b/src/main/java/dev/hypera/dragonfly/exceptions/RelocationFailureException.java @@ -23,25 +23,29 @@ package dev.hypera.dragonfly.exceptions; +import org.jetbrains.annotations.NotNull; + public class RelocationFailureException extends DragonflyException { + private static final long serialVersionUID = -2817144091943660838L; + public RelocationFailureException() { super(); } - public RelocationFailureException(String message) { + public RelocationFailureException(@NotNull String message) { super(message); } - public RelocationFailureException(String message, Throwable cause) { + public RelocationFailureException(@NotNull String message, @NotNull Throwable cause) { super(message, cause); } - public RelocationFailureException(Throwable cause) { + public RelocationFailureException(@NotNull Throwable cause) { super(cause); } - protected RelocationFailureException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + protected RelocationFailureException(@NotNull String message, @NotNull Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } diff --git a/src/main/java/dev/hypera/dragonfly/exceptions/ResolveFailureException.java b/src/main/java/dev/hypera/dragonfly/exceptions/ResolveFailureException.java index 24f25b3..1f82577 100644 --- a/src/main/java/dev/hypera/dragonfly/exceptions/ResolveFailureException.java +++ b/src/main/java/dev/hypera/dragonfly/exceptions/ResolveFailureException.java @@ -23,25 +23,29 @@ package dev.hypera.dragonfly.exceptions; +import org.jetbrains.annotations.NotNull; + public class ResolveFailureException extends DownloadFailureException { + private static final long serialVersionUID = -8393996338726021911L; + public ResolveFailureException() { super(); } - public ResolveFailureException(String message) { + public ResolveFailureException(@NotNull String message) { super(message); } - public ResolveFailureException(String message, Throwable cause) { + public ResolveFailureException(@NotNull String message, @NotNull Throwable cause) { super(message, cause); } - public ResolveFailureException(Throwable cause) { + public ResolveFailureException(@NotNull Throwable cause) { super(cause); } - protected ResolveFailureException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + protected ResolveFailureException(@NotNull String message, @NotNull Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } diff --git a/src/main/java/dev/hypera/dragonfly/loading/DependencyLoader.java b/src/main/java/dev/hypera/dragonfly/loading/DependencyLoader.java index 940dc5c..aa46e4c 100644 --- a/src/main/java/dev/hypera/dragonfly/loading/DependencyLoader.java +++ b/src/main/java/dev/hypera/dragonfly/loading/DependencyLoader.java @@ -29,6 +29,7 @@ import dev.hypera.dragonfly.exceptions.LoadFailureException; import java.net.MalformedURLException; import java.util.List; import org.jetbrains.annotations.ApiStatus.Internal; +import org.jetbrains.annotations.NotNull; /** * Dependency loader. @@ -37,11 +38,11 @@ import org.jetbrains.annotations.ApiStatus.Internal; */ public class DependencyLoader { - private final Dragonfly dragonfly; - private final IClassLoader classLoader; + private final @NotNull Dragonfly dragonfly; + private final @NotNull IClassLoader classLoader; @Internal - public DependencyLoader(Dragonfly dragonfly, IClassLoader classLoader) { + public DependencyLoader(@NotNull Dragonfly dragonfly, @NotNull IClassLoader classLoader) { this.dragonfly = dragonfly; this.classLoader = classLoader; } @@ -52,7 +53,7 @@ public class DependencyLoader { * @param dependencies Dependencies to be loaded. * @throws LoadFailureException if something went wrong while loading the dependencies. */ - public void load(List dependencies) throws LoadFailureException { + public void load(@NotNull List dependencies) throws LoadFailureException { for (Dependency dependency : dependencies) { load(dependency); } @@ -64,7 +65,7 @@ public class DependencyLoader { * @param dependency Dependency to be loaded. * @throws LoadFailureException if something went wrong while loading the dependency. */ - private void load(Dependency dependency) throws LoadFailureException { + private void load(@NotNull Dependency dependency) throws LoadFailureException { try { classLoader.addURL(dragonfly.getDirectory().resolve(dependency.getFileName()).toUri().toURL()); } catch (MalformedURLException ex) { diff --git a/src/main/java/dev/hypera/dragonfly/loading/DragonflyClassLoader.java b/src/main/java/dev/hypera/dragonfly/loading/DragonflyClassLoader.java index 5cb38b6..889e039 100644 --- a/src/main/java/dev/hypera/dragonfly/loading/DragonflyClassLoader.java +++ b/src/main/java/dev/hypera/dragonfly/loading/DragonflyClassLoader.java @@ -26,6 +26,7 @@ package dev.hypera.dragonfly.loading; import java.net.URL; import java.net.URLClassLoader; import org.jetbrains.annotations.ApiStatus.Internal; +import org.jetbrains.annotations.NotNull; /** * Dragonfly class loader, a child-first {@link URLClassLoader} used for loading Dragonfly's internal dependencies. @@ -35,17 +36,17 @@ import org.jetbrains.annotations.ApiStatus.Internal; @Internal public class DragonflyClassLoader extends URLClassLoader { - public DragonflyClassLoader(ClassLoader classLoader) { + public DragonflyClassLoader(@NotNull ClassLoader classLoader) { super(new URL[0], classLoader); } @Override - public void addURL(URL url) { + public void addURL(@NotNull URL url) { super.addURL(url); } @Override - protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { + protected @NotNull Class loadClass(@NotNull String name, boolean resolve) throws ClassNotFoundException { Class loadedClass = findLoadedClass(name); if (null == loadedClass) { try { diff --git a/src/main/java/dev/hypera/dragonfly/relocation/DependencyRelocator.java b/src/main/java/dev/hypera/dragonfly/relocation/DependencyRelocator.java index 9132a76..bfb88db 100644 --- a/src/main/java/dev/hypera/dragonfly/relocation/DependencyRelocator.java +++ b/src/main/java/dev/hypera/dragonfly/relocation/DependencyRelocator.java @@ -41,6 +41,7 @@ import java.util.List; import java.util.Map; import org.jetbrains.annotations.ApiStatus.Internal; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Dependency relocator. @@ -49,21 +50,21 @@ import org.jetbrains.annotations.NotNull; */ public class DependencyRelocator { - private static final String RELOCATED_FILENAME = "%s-relocated.jar"; + private static final @NotNull String RELOCATED_FILENAME = "%s-relocated.jar"; - private final Dragonfly dragonfly; + private final @NotNull Dragonfly dragonfly; private final boolean deleteOld; - private final DragonflyClassLoader classLoader; - private final DependencyLoader dependencyLoader; + private final @NotNull DragonflyClassLoader classLoader; + private final @NotNull DependencyLoader dependencyLoader; private boolean dependenciesLoaded; - private Constructor constructor; - private Method method; + private @Nullable Constructor constructor; + private @Nullable Method method; @Internal - public DependencyRelocator(Dragonfly dragonfly, boolean deleteOld) { + public DependencyRelocator(@NotNull Dragonfly dragonfly, boolean deleteOld) { this.dragonfly = dragonfly; this.deleteOld = deleteOld; this.classLoader = new DragonflyClassLoader(getClass().getClassLoader()); diff --git a/src/main/java/dev/hypera/dragonfly/relocation/Relocation.java b/src/main/java/dev/hypera/dragonfly/relocation/Relocation.java index d88c529..8b80923 100644 --- a/src/main/java/dev/hypera/dragonfly/relocation/Relocation.java +++ b/src/main/java/dev/hypera/dragonfly/relocation/Relocation.java @@ -32,10 +32,10 @@ import org.jetbrains.annotations.NotNull; */ public class Relocation { - private final String from; - private final String to; + private final @NotNull String from; + private final @NotNull String to; - private Relocation(String from, String to) { + private Relocation(@NotNull String from, @NotNull String to) { this.from = from.replace("\\.", "."); this.to = to; } @@ -44,11 +44,11 @@ public class Relocation { return new Relocation(from, to); } - public String getFrom() { + public @NotNull String getFrom() { return from; } - public String getTo() { + public @NotNull String getTo() { return to; } diff --git a/src/main/java/dev/hypera/dragonfly/resolvers/impl/MavenResolver.java b/src/main/java/dev/hypera/dragonfly/resolvers/impl/MavenResolver.java index 16c9d1f..4f01643 100644 --- a/src/main/java/dev/hypera/dragonfly/resolvers/impl/MavenResolver.java +++ b/src/main/java/dev/hypera/dragonfly/resolvers/impl/MavenResolver.java @@ -40,7 +40,7 @@ import org.jetbrains.annotations.Nullable; */ public class MavenResolver implements IResolver { - private static final String FORMAT = "%s%s/%s/%s/%s-%s.jar"; + private static final @NotNull String FORMAT = "%s%s/%s/%s/%s-%s.jar"; @Override public @Nullable String resolve(@NotNull Dragonfly dragonfly, @NotNull MavenDependency dependency) throws ResolveFailureException { diff --git a/src/main/java/dev/hypera/dragonfly/resolvers/impl/MavenSnapshotResolver.java b/src/main/java/dev/hypera/dragonfly/resolvers/impl/MavenSnapshotResolver.java index 83043b8..c08df23 100644 --- a/src/main/java/dev/hypera/dragonfly/resolvers/impl/MavenSnapshotResolver.java +++ b/src/main/java/dev/hypera/dragonfly/resolvers/impl/MavenSnapshotResolver.java @@ -27,16 +27,19 @@ import dev.hypera.dragonfly.Dragonfly; import dev.hypera.dragonfly.dependency.impl.MavenDependency; import dev.hypera.dragonfly.exceptions.ResolveFailureException; import dev.hypera.dragonfly.resolvers.IResolver; +import java.io.IOException; import java.io.StringReader; import java.util.Set; import java.util.stream.Collectors; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; +import sun.tools.jstat.ParserException; /** * Maven snapshot resolver. @@ -45,8 +48,26 @@ import org.xml.sax.InputSource; */ public class MavenSnapshotResolver implements IResolver { - private static final String FORMAT = "%s%s/%s/%s/maven-metadata.xml"; - private static final String OUTPUT_FORMAT = "%s/%s-%s-%s-%s.jar"; + private static final @NotNull String FORMAT = "%s%s/%s/%s/maven-metadata.xml"; + private static final @NotNull String OUTPUT_FORMAT = "%s/%s-%s-%s-%s.jar"; + private final @NotNull DocumentBuilderFactory documentBuilderFactory; + + public MavenSnapshotResolver() { + try { + /* The below is an attempt to create an XML parser while preventing XML External Entity attacks */ + /* Read more: https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#java */ + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + factory.setFeature("http://xml.org/sax/features/external-general-entities", false); + factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + factory.setXIncludeAware(false); + factory.setExpandEntityReferences(false); + this.documentBuilderFactory = factory; + } catch (ParserConfigurationException ex) { + throw new RuntimeException("Failed to create DocumentBuilderFactory", ex); + } + } @Override public @Nullable String resolve(@NotNull Dragonfly dragonfly, @NotNull MavenDependency dependency) throws ResolveFailureException { @@ -72,7 +93,7 @@ public class MavenSnapshotResolver implements IResolver { } try { - DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(data))); Element root = document.getDocumentElement(); Element snapshotData = (Element) root.getElementsByTagName("snapshot").item(0);