Skip to content

Commit fdb79d6

Browse files
committed
Catch books with invalid JSON as page content and kick the player.
This has so far oly happened once with ProtocolLib, so for now we only catch it there. Kicking the corresponding player should prevent spamming of this error. See also https://pastebin.com/9XQsf5rT and https://hub.spigotmc.org/jira/projects/SPIGOT/issues/SPIGOT-7155
1 parent d64d1d1 commit fdb79d6

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/main/java/me/minoneer/bukkit/bookexploit/ProtocolLibHook.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.comphenix.protocol.events.ListenerPriority;
77
import com.comphenix.protocol.events.PacketAdapter;
88
import com.comphenix.protocol.events.PacketEvent;
9+
import com.google.gson.JsonParseException;
910
import org.bukkit.entity.Player;
1011
import org.bukkit.inventory.ItemStack;
1112
import org.bukkit.plugin.Plugin;
@@ -29,15 +30,33 @@ public static void activate(
2930
public void onPacketReceiving(PacketEvent event) {
3031
final Player player = event.getPlayer();
3132
ItemStack item = event.getPacket().getItemModifier().readSafely(0);
32-
ItemStack filteredItem = bookFilter.filterBook(item, player, FilterAction.CREATE);
33-
if (filteredItem != null) {
33+
try {
34+
ItemStack filteredItem = bookFilter.filterBook(item, player, FilterAction.CREATE);
35+
36+
if (filteredItem != null) {
37+
event.setCancelled(true);
38+
event.getPlayer().updateInventory();
39+
logger.log(
40+
Level.WARNING,
41+
"Player {0} {1} tried to create a book with illegal click events!",
42+
new Object[]{player.getName(), player.getUniqueId()}
43+
);
44+
if (config.getPlayerMessage() != null) {
45+
player.sendMessage(config.getPlayerMessage());
46+
}
47+
}
48+
} catch (JsonParseException exception) { // thrown if the book pages contain invalid JSON
49+
logger.log(
50+
Level.SEVERE,
51+
"Detected a book with invalid page content that could not be parsed in inventory of " +
52+
"player " + player.getName() + " (" + player.getUniqueId() + "), removing the item."
53+
,
54+
exception
55+
);
3456
event.setCancelled(true);
3557
event.getPlayer().updateInventory();
36-
logger.log(Level.WARNING, "Player {0} {1} tried to create a book with illegal click events!",
37-
new Object[]{player.getName(), player.getUniqueId()});
38-
if (config.getPlayerMessage() != null) {
39-
player.sendMessage(config.getPlayerMessage());
40-
}
58+
player.kickPlayer("Invalid book content detected, please contact an administrator.");
59+
logger.log(Level.SEVERE, item.toString());
4160
}
4261
}
4362
});

0 commit comments

Comments
 (0)