From 84c7e6870bd67e39c896ba5afc8d46be2279c19a Mon Sep 17 00:00:00 2001
From: xGinko <rachel.audrey@mail.de>
Date: Fri, 16 Feb 2024 15:15:52 +0100
Subject: [PATCH] patch tagparser crash exploit

---
 .../modules/AnarchyExploitFixesModule.java    |  2 +
 .../tagparser/AntiTagParserCrash.java         | 50 +++++++++++++++++
 .../tagparser/TabCompleteListener.java        | 53 +++++++++++++++++++
 AnarchyExploitFixesLegacy/build.gradle.kts    |  2 +-
 .../modules/AnarchyExploitFixesModule.java    |  2 +
 .../tagparser/AntiTagParserCrash.java         | 47 ++++++++++++++++
 .../tagparser/TabCompleteListener.java        | 53 +++++++++++++++++++
 ...moo.anarchyexploitfixes.wrapper.gradle.kts |  2 +-
 8 files changed, 209 insertions(+), 2 deletions(-)
 create mode 100755 AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/AntiTagParserCrash.java
 create mode 100755 AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/TabCompleteListener.java
 create mode 100755 AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/AntiTagParserCrash.java
 create mode 100755 AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/TabCompleteListener.java

diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java
index 3e7ef40c5..70f165466 100755
--- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java
+++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java
@@ -45,6 +45,7 @@
 import me.moomoo.anarchyexploitfixes.modules.preventions.portals.*;
 import me.moomoo.anarchyexploitfixes.modules.preventions.withers.*;
 import me.moomoo.anarchyexploitfixes.modules.protocollib.boatfly.AntiBoatFlyModule;
+import me.moomoo.anarchyexploitfixes.modules.protocollib.tagparser.AntiTagParserCrash;
 import me.moomoo.anarchyexploitfixes.modules.protocollib.windowclick.AntiWindowClickCrash;
 
 import java.util.HashSet;
@@ -213,6 +214,7 @@ static void reloadModules() {
 
         modules.add(new AntiBoatFlyModule());
         modules.add(new AntiWindowClickCrash());
+        modules.add(new AntiTagParserCrash());
 
         if (!AnarchyExploitFixes.getConfiguration().protocolLib_IsDisabled && !protocolLibIsInstalled) {
             AnarchyExploitFixes.getLog().severe("Could not find ProtocolLib. Packet exploits cannot be patched without it.");
diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/AntiTagParserCrash.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/AntiTagParserCrash.java
new file mode 100755
index 000000000..0f9872973
--- /dev/null
+++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/AntiTagParserCrash.java
@@ -0,0 +1,50 @@
+package me.moomoo.anarchyexploitfixes.modules.protocollib.tagparser;
+
+import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
+import me.moomoo.anarchyexploitfixes.config.Config;
+import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
+import me.moomoo.anarchyexploitfixes.utils.LogUtil;
+
+import java.util.logging.Level;
+
+public class AntiTagParserCrash implements AnarchyExploitFixesModule {
+
+    public AntiTagParserCrash() {
+        AnarchyExploitFixes.getConfiguration().addComment("patches.tag-parser-crash-patch.enable", "Patches TagParser crash exploit.");
+    }
+
+    @Override
+    public String name() {
+        return "tag-parser-crash-patch";
+    }
+
+    @Override
+    public String category() {
+        return "patches";
+    }
+
+    @Override
+    public void enable() {
+        new TabCompleteListener().register();
+    }
+
+    @Override
+    public boolean shouldEnable() {
+        Config config = AnarchyExploitFixes.getConfiguration();
+        if (config.getBoolean("patches.tag-parser-crash-patch.enable", true)) {
+            if (config.protocolLib_IsDisabled) {
+                LogUtil.moduleLog(Level.WARNING, name(), "Not patching exploit because you disabled ProtocolLib in config!");
+                return false;
+            }
+            if (!AnarchyExploitFixes.isProtocolLibInstalled()) {
+                LogUtil.moduleLog(Level.SEVERE, name(), "Unable to patch exploit because ProtocolLib is not installed!");
+                return false;
+            }
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public void disable() {}
+}
diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/TabCompleteListener.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/TabCompleteListener.java
new file mode 100755
index 000000000..1171905af
--- /dev/null
+++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/TabCompleteListener.java
@@ -0,0 +1,53 @@
+package me.moomoo.anarchyexploitfixes.modules.protocollib.tagparser;
+
+import com.comphenix.protocol.PacketType;
+import com.comphenix.protocol.ProtocolLibrary;
+import com.comphenix.protocol.events.ListenerPriority;
+import com.comphenix.protocol.events.PacketAdapter;
+import com.comphenix.protocol.events.PacketContainer;
+import com.comphenix.protocol.events.PacketEvent;
+import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
+import me.moomoo.anarchyexploitfixes.utils.LogUtil;
+
+import java.util.logging.Level;
+
+public class TabCompleteListener extends PacketAdapter {
+
+    public TabCompleteListener() {
+        super(AnarchyExploitFixes.getInstance(), ListenerPriority.HIGHEST, PacketType.Play.Client.TAB_COMPLETE);
+    }
+
+    protected void register() {
+        ProtocolLibrary.getProtocolManager().addPacketListener(this);
+    }
+
+    @Override
+    public void onPacketReceiving(PacketEvent event) {
+        if (event.isPlayerTemporary()) return;
+
+        final PacketContainer packet = event.getPacket();
+
+        try {
+            final String command = packet.getStrings().read(0);
+
+            if (command.contains("@") || command.contains("nbt")) {
+                if (!event.getPlayer().isOp()) {
+                    event.setCancelled(true);
+                    return;
+                }
+            }
+
+            if (command.length() > 64) {
+                final int index = command.indexOf(' ');
+                if (index == -1 || index >= 64) {
+                    event.setCancelled(true);
+                }
+            }
+        } catch (Exception e) {
+            LogUtil.moduleLog(Level.WARNING, "tag-parser-crash",
+                    "Error reading TabComplete Request Packet - " + e.getLocalizedMessage() + "\n" +
+                            "This might be due to version incompatibilities.\n" +
+                            "Packet in question: " + packet.toString());
+        }
+    }
+}
\ No newline at end of file
diff --git a/AnarchyExploitFixesLegacy/build.gradle.kts b/AnarchyExploitFixesLegacy/build.gradle.kts
index 569148759..493387b69 100755
--- a/AnarchyExploitFixesLegacy/build.gradle.kts
+++ b/AnarchyExploitFixesLegacy/build.gradle.kts
@@ -5,7 +5,7 @@ plugins {
 
 dependencies {
     compileOnly("com.destroystokyo.paper:paper-api:1.12.2-R0.1-SNAPSHOT")
-    api("com.github.cryptomorin:XSeries:9.8.0") // XSeries for cross-version support
+    api("com.github.cryptomorin:XSeries:9.9.0") // XSeries for cross-version support
     api("com.github.ben-manes.caffeine:caffeine:2.9.3") // Fast caching // Use 2.x for Java8 compatibility
 }
 
diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java
index c6bc0629e..db225d425 100755
--- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java
+++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/AnarchyExploitFixesModule.java
@@ -48,6 +48,7 @@
 import me.moomoo.anarchyexploitfixes.modules.protocollib.craftrecipe.CraftRecipeCooldownModule;
 import me.moomoo.anarchyexploitfixes.modules.protocollib.lecterncrash.AntiLecternCrash;
 import me.moomoo.anarchyexploitfixes.modules.protocollib.nocom.NoComModule;
+import me.moomoo.anarchyexploitfixes.modules.protocollib.tagparser.AntiTagParserCrash;
 import me.moomoo.anarchyexploitfixes.modules.protocollib.windowclick.AntiWindowClickCrash;
 import org.bukkit.event.HandlerList;
 
@@ -228,6 +229,7 @@ static void reloadModules() {
         modules.add(new CraftRecipeCooldownModule());
         modules.add(new AntiWindowClickCrash());
         modules.add(new AntiLecternCrash());
+        modules.add(new AntiTagParserCrash());
 
         if (!AnarchyExploitFixes.getConfiguration().protocolLib_IsDisabled && !protocolLibIsInstalled) {
             AnarchyExploitFixes.getLog().severe("Could not find ProtocolLib. Many gamebreaking exploits cannot be patched without it.");
diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/AntiTagParserCrash.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/AntiTagParserCrash.java
new file mode 100755
index 000000000..c0cdc4182
--- /dev/null
+++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/AntiTagParserCrash.java
@@ -0,0 +1,47 @@
+package me.moomoo.anarchyexploitfixes.modules.protocollib.tagparser;
+
+import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
+import me.moomoo.anarchyexploitfixes.config.Config;
+import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
+import me.moomoo.anarchyexploitfixes.utils.LogUtil;
+
+import java.util.logging.Level;
+
+public class AntiTagParserCrash implements AnarchyExploitFixesModule {
+
+    public AntiTagParserCrash() {
+        AnarchyExploitFixes.getConfiguration().addComment("patches.tag-parser-crash-patch.enable", "Patches TagParser crash exploit.");
+    }
+
+    @Override
+    public String name() {
+        return "tag-parser-crash-patch";
+    }
+
+    @Override
+    public String category() {
+        return "patches";
+    }
+
+    @Override
+    public void enable() {
+        new TabCompleteListener().register();
+    }
+
+    @Override
+    public boolean shouldEnable() {
+        Config config = AnarchyExploitFixes.getConfiguration();
+        if (config.getBoolean("patches.tag-parser-crash-patch.enable", true)) {
+            if (config.protocolLib_IsDisabled) {
+                LogUtil.moduleLog(Level.WARNING, name(), "Not patching exploit because you disabled ProtocolLib in config!");
+                return false;
+            }
+            if (!AnarchyExploitFixes.isProtocolLibInstalled()) {
+                LogUtil.moduleLog(Level.SEVERE, name(), "Unable to patch exploit because ProtocolLib is not installed!");
+                return false;
+            }
+            return true;
+        }
+        return false;
+    }
+}
diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/TabCompleteListener.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/TabCompleteListener.java
new file mode 100755
index 000000000..1171905af
--- /dev/null
+++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/tagparser/TabCompleteListener.java
@@ -0,0 +1,53 @@
+package me.moomoo.anarchyexploitfixes.modules.protocollib.tagparser;
+
+import com.comphenix.protocol.PacketType;
+import com.comphenix.protocol.ProtocolLibrary;
+import com.comphenix.protocol.events.ListenerPriority;
+import com.comphenix.protocol.events.PacketAdapter;
+import com.comphenix.protocol.events.PacketContainer;
+import com.comphenix.protocol.events.PacketEvent;
+import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
+import me.moomoo.anarchyexploitfixes.utils.LogUtil;
+
+import java.util.logging.Level;
+
+public class TabCompleteListener extends PacketAdapter {
+
+    public TabCompleteListener() {
+        super(AnarchyExploitFixes.getInstance(), ListenerPriority.HIGHEST, PacketType.Play.Client.TAB_COMPLETE);
+    }
+
+    protected void register() {
+        ProtocolLibrary.getProtocolManager().addPacketListener(this);
+    }
+
+    @Override
+    public void onPacketReceiving(PacketEvent event) {
+        if (event.isPlayerTemporary()) return;
+
+        final PacketContainer packet = event.getPacket();
+
+        try {
+            final String command = packet.getStrings().read(0);
+
+            if (command.contains("@") || command.contains("nbt")) {
+                if (!event.getPlayer().isOp()) {
+                    event.setCancelled(true);
+                    return;
+                }
+            }
+
+            if (command.length() > 64) {
+                final int index = command.indexOf(' ');
+                if (index == -1 || index >= 64) {
+                    event.setCancelled(true);
+                }
+            }
+        } catch (Exception e) {
+            LogUtil.moduleLog(Level.WARNING, "tag-parser-crash",
+                    "Error reading TabComplete Request Packet - " + e.getLocalizedMessage() + "\n" +
+                            "This might be due to version incompatibilities.\n" +
+                            "Packet in question: " + packet.toString());
+        }
+    }
+}
\ No newline at end of file
diff --git a/build-logic/src/main/kotlin/me.moomoo.anarchyexploitfixes.wrapper.gradle.kts b/build-logic/src/main/kotlin/me.moomoo.anarchyexploitfixes.wrapper.gradle.kts
index d4e12ded4..41ab9d198 100755
--- a/build-logic/src/main/kotlin/me.moomoo.anarchyexploitfixes.wrapper.gradle.kts
+++ b/build-logic/src/main/kotlin/me.moomoo.anarchyexploitfixes.wrapper.gradle.kts
@@ -4,7 +4,7 @@ plugins {
 }
 
 group = "me.moomoo.anarchyexploitfixes"
-version = "2.6.5"
+version = "2.6.6"
 description = "Prevent many exploits that affect anarchy servers."
 var url: String? = "github.com/moom0o/AnarchyExploitFixes"