PaperMC/patches/server/0268-Book-size-limits.patch

57 lines
3 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Fri, 16 Nov 2018 23:08:50 -0500
Subject: [PATCH] Book size limits
2021-06-11 14:02:28 +02:00
Puts some limits on the size of books.
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-10-27 18:11:15 +01:00
index c3e281754a1cd9d5ce5f4aab36822f9f5d34be39..542899c382309986f3aaa16bf534b677aaac82f5 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-10-24 19:29:35 +02:00
@@ -1056,6 +1056,44 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
2021-06-11 14:02:28 +02:00
@Override
public void handleEditBook(ServerboundEditBookPacket packet) {
+ // Paper start - Book size limits
+ final io.papermc.paper.configuration.type.number.IntOr.Disabled pageMax = io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.pageMax;
+ if (!this.cserver.isPrimaryThread() && pageMax.enabled()) {
+ final List<String> pageList = packet.pages();
2021-06-11 14:02:28 +02:00
+ long byteTotal = 0;
+ final int maxBookPageSize = pageMax.intValue();
+ final double multiplier = Math.clamp(io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.totalMultiplier, 0.3D, 1D);
2021-06-11 14:02:28 +02:00
+ long byteAllowed = maxBookPageSize;
+ for (final String page : pageList) {
+ final int byteLength = page.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
2021-06-11 14:02:28 +02:00
+ byteTotal += byteLength;
+ final int length = page.length();
+ int multiByteCharacters = 0;
2021-06-11 14:02:28 +02:00
+ if (byteLength != length) {
+ // Count the number of multi byte characters
+ for (final char c : page.toCharArray()) {
2021-06-11 14:02:28 +02:00
+ if (c > 127) {
+ multiByteCharacters++;
2021-06-11 14:02:28 +02:00
+ }
+ }
+ }
+
+ // Allow pages with fewer characters to consume less of the allowed byte quota
+ byteAllowed += maxBookPageSize * Math.clamp((double) length / 255D, 0.1D, 1) * multiplier;
2021-06-11 14:02:28 +02:00
+
+ if (multiByteCharacters > 1) {
+ // Penalize multibyte characters
+ byteAllowed -= multiByteCharacters;
2021-06-11 14:02:28 +02:00
+ }
+ }
+
+ if (byteTotal > byteAllowed) {
+ ServerGamePacketListenerImpl.LOGGER.warn("{} tried to send a book too large. Book size: {} - Allowed: {} - Pages: {}", this.player.getScoreboardName(), byteTotal, byteAllowed, pageList.size());
+ this.disconnect(Component.literal("Book too large!"));
2021-06-11 14:02:28 +02:00
+ return;
+ }
+ }
+ // Paper end - Book size limits
2021-06-11 14:02:28 +02:00
// CraftBukkit start
if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
2024-06-13 21:04:27 +02:00
this.disconnect(Component.literal("Book edited too quickly!"));