From 9a66605616adfb619dd731b0ae8ad23f712cd392 Mon Sep 17 00:00:00 2001 From: Glavo Date: Sat, 18 Dec 2021 10:48:58 +0800 Subject: [PATCH] Disable JndiLookup using log4j-patch --- .../jackhuang/hmcl/download/MaintainTask.java | 47 +++++++++++++++++-- minecraft/libraries/log4j-patch/README.md | 3 +- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/MaintainTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/MaintainTask.java index 70764c317..290aaf61a 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/MaintainTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/MaintainTask.java @@ -63,18 +63,57 @@ public class MaintainTask extends Task { String mainClass = version.resolve(null).getMainClass(); if (mainClass != null && mainClass.equals(LibraryAnalyzer.LAUNCH_WRAPPER_MAIN)) { - return maintainOptiFineLibrary(repository, maintainGameWithLaunchWrapper(unique(version), true), false); + version = maintainOptiFineLibrary(repository, maintainGameWithLaunchWrapper(unique(version), true), false); } else if (mainClass != null && mainClass.equals(LibraryAnalyzer.MOD_LAUNCHER_MAIN)) { // Forge 1.13 and OptiFine - return maintainOptiFineLibrary(repository, maintainGameWithCpwModLauncher(repository, unique(version)), true); + version = maintainOptiFineLibrary(repository, maintainGameWithCpwModLauncher(repository, unique(version)), true); } else if (mainClass != null && mainClass.equals(LibraryAnalyzer.BOOTSTRAP_LAUNCHER_MAIN)) { // Forge 1.17 - return maintainGameWithCpwBoostrapLauncher(repository, unique(version)); + version = maintainGameWithCpwBoostrapLauncher(repository, unique(version)); } else { // Vanilla Minecraft does not need maintain // Fabric does not need maintain, nothing compatible with fabric now. - return maintainOptiFineLibrary(repository, unique(version), false); + version = maintainOptiFineLibrary(repository, unique(version), false); } + + Library log4jPatch = null; + + List libraries = version.getLibraries(); + for (Library library : libraries) { + if (library.is("org.apache.logging.log4j", "log4j-core")) { + if (library.getVersion().startsWith("2.0-beta")) { + if ("2.0-beta9".equals(library.getVersion())) { + log4jPatch = new Library(new Artifact("org.glavo", "log4j-patch-beta9", "1.0")); + } else { + Logging.LOG.warning("Log4j " + library.getVersion() + " cannot be patched"); + } + } else if (VersionNumber.VERSION_COMPARATOR.compare(library.getVersion(), "2.16") < 0) { + log4jPatch = new Library(new Artifact("org.glavo", "log4j-patch", "1.0")); + } + break; + } + } + + if (log4jPatch != null) { + ArrayList patchedLibraries = new ArrayList<>(libraries.size() + 1); + patchedLibraries.add(log4jPatch); + patchedLibraries.addAll(libraries); + version = version.setLibraries(patchedLibraries); + + Path log4jPatchPath = repository.getLibraryFile(version, log4jPatch).toPath(); + String patchName = log4jPatch.getArtifactId() + "-" + log4jPatch.getVersion(); + if (Files.notExists(log4jPatchPath)) { + try (InputStream input = MaintainTask.class.getResourceAsStream("/assets/game/" + patchName + ".jar")) { + Files.createDirectories(log4jPatchPath.getParent()); + Files.copy(input, log4jPatchPath, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + Logging.LOG.log(Level.WARNING, "Unable to unpack " + patchName, e); + } + } + Logging.LOG.info("Apply patch " + patchName + " to log4j"); + } + + return version; } public static Version maintainPreservingPatches(GameRepository repository, Version version) { diff --git a/minecraft/libraries/log4j-patch/README.md b/minecraft/libraries/log4j-patch/README.md index d52884d7a..600d8dc8d 100644 --- a/minecraft/libraries/log4j-patch/README.md +++ b/minecraft/libraries/log4j-patch/README.md @@ -2,4 +2,5 @@ Copy from [Glavo/log4j-patch](https://github.com/Glavo/log4j-patch/). -It is licensed under the WTFPL 2.0 license. \ No newline at end of file +`org.apache.logging.log4j.core.lookup.JndiLookup` is licensed under the WTFPL 2.0 license, +and `org.apache.logging.log4j.core.lookup.Interpolator` is licensed under the Apache 2.0 license. \ No newline at end of file