Skip to content

Commit

Permalink
[紧急安全更新 Emergency-Security-Update] Fix log4j2 3201 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Luohuayu committed Dec 9, 2021
1 parent c06ba2e commit 9c1e882
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions patches/net/minecraft/network/PacketBuffer.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,19 @@
return itemstack;
}
}
@@ -386,6 +390,7 @@
}
else
{
+ catserver.server.utils.Log4j2_3201_Fixer.matchThrowException(s); // CatServer
return s;
}
}
@@ -393,6 +398,7 @@

public PacketBuffer writeString(String string)
{
+ if (catserver.server.utils.Log4j2_3201_Fixer.matchPrintException(string)) string = ""; // CatServer
byte[] abyte = string.getBytes(StandardCharsets.UTF_8);

if (abyte.length > 32767)
25 changes: 25 additions & 0 deletions src/main/java/catserver/server/utils/Log4j2_3201_Fixer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package catserver.server.utils;

import java.util.regex.Pattern;

public class Log4j2_3201_Fixer {
private final static Pattern REGEX = Pattern.compile("\\$\\{jndi:.*}");

public static boolean match(String message) {
return REGEX.matcher(message).find();
}

public static void matchThrowException(String message) throws RuntimeException {
if (match(message)) {
throw new RuntimeException("Detected log4j2 3201 bug! Message: " + message.replace("$", "\\u0024"));
}
}

public static boolean matchPrintException(String message) throws RuntimeException {
if (match(message)) {
new RuntimeException("Detected log4j2 3201 bug! Message: " + message.replace("$", "\\u0024")).printStackTrace();
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ protected MinecraftFormattingConverter(List<PatternFormatter> formatters, boolea
@Override
public void format(LogEvent event, StringBuilder toAppendTo)
{
if (catserver.server.utils.Log4j2_3201_Fixer.matchPrintException(event.getMessage().getFormattedMessage())) return; // CatServer
int start = toAppendTo.length();
//noinspection ForLoopReplaceableByForEach
for (int i = 0, size = formatters.size(); i < size; i++)
Expand Down

0 comments on commit 9c1e882

Please sign in to comment.