Skip to content

Commit 0ac5f4f

Browse files
committed
fix elastic-apm-agent-1.28.1.jar miss
1 parent acbcc81 commit 0ac5f4f

File tree

3 files changed

+1175
-6
lines changed

3 files changed

+1175
-6
lines changed

src/main/java/com/mergebase/log4j/Log4JDetector.java

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/*
2+
* This file is licensed to the public under the terms of the GNU Public License 3.0
3+
* (aka GPLv3).
4+
*
5+
* To be clear, for the purposes of copyright law, any program ["The Importing Program"] that
6+
* imports this file (via Java's "import" mechanism or via Java reflection or via any
7+
* other software technique for importing or referencing functionality) is considered
8+
* a derivative work of this work, and must also comply with the conditions of the GPLv3
9+
* license in The Importing Program's totality to be granted a copyright license to this work,
10+
* and must also use the same definition as defined here for what constitutes a derivative work
11+
* of itself.
12+
*
13+
*/
114
package com.mergebase.log4j;
215

316
import java.io.BufferedInputStream;
@@ -14,11 +27,15 @@
1427
import java.util.Iterator;
1528
import java.util.List;
1629
import java.util.Locale;
30+
import java.util.Properties;
1731
import java.util.zip.ZipEntry;
1832
import java.util.zip.ZipInputStream;
1933

34+
import static com.mergebase.log4j.VersionComparator.compare;
35+
2036
public class Log4JDetector {
2137

38+
private static final String POM_PROPERTIES = "log4j-core/pom.properties".toLowerCase(Locale.ROOT);
2239
private static final String FILE_OLD_LOG4J = "log4j/DailyRollingFileAppender.class".toLowerCase(Locale.ROOT);
2340
private static final String FILE_LOG4J_1 = "core/LogEvent.class".toLowerCase(Locale.ROOT);
2441
private static final String FILE_LOG4J_2 = "core/Appender.class".toLowerCase(Locale.ROOT);
@@ -146,13 +163,20 @@ public int compare(File f1, File f2) {
146163

147164
/**
148165
* @param fileName name to examine for type
149-
* @return 0 == zip, 1 == class, -1 = who knows...
166+
* @return 0 == zip, 1 == class, 2 = log4j-core/pom.properties, -1 = who knows...
150167
*/
151168
private static int fileType(String fileName) {
152169
int c = fileName.lastIndexOf('.');
153170
if (c >= 0) {
154171
String suffix = fileName.substring(c + 1);
155-
if ("class".equalsIgnoreCase(suffix)) {
172+
173+
// Special logic for "log4j-core/pom.properties" last-resort version source.
174+
if ("properties".equalsIgnoreCase(suffix)) {
175+
String lower = fileName.toLowerCase(Locale.ROOT);
176+
if (lower.endsWith(POM_PROPERTIES)) {
177+
return 2;
178+
}
179+
} else if ("class".equalsIgnoreCase(suffix)) {
156180
return 1;
157181
} else if ("zip".equalsIgnoreCase(suffix)
158182
|| "jar".equalsIgnoreCase(suffix)
@@ -206,6 +230,7 @@ private static void findLog4jRecursive(
206230
boolean isLog4j2_15_override = false;
207231
boolean isLog4j2_12_2 = false;
208232
boolean isLog4j2_12_2_override = false;
233+
byte[] pomProperties = null;
209234
ZipEntry ze;
210235
while (true) {
211236
try {
@@ -233,9 +258,12 @@ private static void findLog4jRecursive(
233258
int fileType = fileType(path);
234259
boolean isSubZip = fileType == 0;
235260
boolean isClassEntry = fileType == 1;
261+
boolean isPomProperties = fileType == 2;
236262
boolean needClassBytes = false;
237263

238-
if (isClassEntry && pathLower.endsWith(FILE_LOG4J_VULNERABLE)) {
264+
if (isPomProperties) {
265+
needClassBytes = true;
266+
} else if (isClassEntry && pathLower.endsWith(FILE_LOG4J_VULNERABLE)) {
239267
needClassBytes = true;
240268
} else if (isClassEntry && pathLower.endsWith(FILE_LOG4J_SAFE_CONDITION1)) {
241269
needClassBytes = true;
@@ -288,12 +316,14 @@ public void close() {
288316
findLog4jRecursive(fullPath, recursiveZipper);
289317
} catch (Exception e) {
290318
System.err.println(fullPath + " FAILED: " + e);
291-
e.printStackTrace(System.out);
319+
e.printStackTrace(System.err);
292320
}
293321

294322

295323
} else {
296-
if (pathLower.endsWith(FILE_OLD_LOG4J)) {
324+
if (pathLower.endsWith(POM_PROPERTIES)) {
325+
pomProperties = bytes;
326+
} else if (pathLower.endsWith(FILE_OLD_LOG4J)) {
297327
isLog4J1_X = true;
298328
} else if (pathLower.endsWith(FILE_LOG4J_1)) {
299329
log4jProbe[0] = true;
@@ -330,7 +360,42 @@ public void close() {
330360
}
331361
}
332362

363+
333364
if (conditionsChecked) {
365+
if (!log4jProbe[0] || !log4jProbe[1] || !log4jProbe[2] || !log4jProbe[3] || !log4jProbe[4]) {
366+
if (pomProperties != null) {
367+
System.err.println("-- Warning: " + zipPath + " does not contain Log4J bytecode, but claims it does.");
368+
ByteArrayInputStream byteIn = new ByteArrayInputStream(pomProperties);
369+
Properties p = new Properties();
370+
try {
371+
p.load(byteIn);
372+
String version = p.getProperty("version");
373+
if (version != null) {
374+
boolean isLog4j2 = compare("2", version) <= 0;
375+
if (isLog4j2) {
376+
log4jProbe = new boolean[]{true, true, true, true, true};
377+
hasJndiLookup = compare("2.0-beta9", version) <= 0;
378+
hasJndiManager = compare("2.1", version) <= 0;
379+
isLog4j2_10 = compare("2.10.0", version) <= 0;
380+
isLog4j2_12_2 = version.startsWith("2.12.") && compare("2.12.2", version) <= 0;
381+
if (isLog4j2_12_2) {
382+
isLog4j2_12_2_override = false;
383+
}
384+
isLog4j2_15 = version.startsWith("2.15.");
385+
isLog4j2_16 = version.startsWith("2.16.");
386+
isLog4j2_17 = compare("2.17.0", version) <= 0;
387+
if (isLog4j2_15 || isLog4j2_16 || isLog4j2_17) {
388+
isLog4j2_15_override = false;
389+
}
390+
}
391+
}
392+
} catch (IOException ioe) {
393+
// invalid properties file!?!
394+
}
395+
}
396+
}
397+
398+
334399
boolean isLog4j = false;
335400
boolean isLog4j_2_10_0 = false;
336401
boolean isLog4j_2_12_2 = false;
@@ -460,7 +525,7 @@ public void close() {
460525
findLog4jRecursive(zip, myZipper);
461526
} catch (Exception e) {
462527
System.err.println("-- Problem: " + zipFile.getPath() + " FAILED: " + e);
463-
e.printStackTrace(System.out);
528+
e.printStackTrace(System.err);
464529
} finally {
465530
myZipper.close();
466531
}

0 commit comments

Comments
 (0)