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
2024-08-12 00:30:23 +02:00
Subject: [PATCH] Book size limits
2021-06-11 14:02:28 +02:00
Puts some limits on the size of books.
2024-08-19 11:33:17 +02:00
diff --git a/src/main/java/net/minecraft/network/protocol/game/ServerboundEditBookPacket.java b/src/main/java/net/minecraft/network/protocol/game/ServerboundEditBookPacket.java
index ed61767a64cdce37dc7c226ebd0d693a60de24a9..f634a830a2b58a419e84f969bd53eeae4f4513bb 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ServerboundEditBookPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ServerboundEditBookPacket.java
@@ -16,9 +16,9 @@ public record ServerboundEditBookPacket(int slot, List<String> pages, Optional<S
public static final StreamCodec<FriendlyByteBuf, ServerboundEditBookPacket> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.VAR_INT,
ServerboundEditBookPacket::slot,
- ByteBufCodecs.stringUtf8(8192).apply(ByteBufCodecs.list(200)),
+ ByteBufCodecs.stringUtf8(net.minecraft.world.item.component.WritableBookContent.PAGE_EDIT_LENGTH).apply(ByteBufCodecs.list(net.minecraft.world.item.component.WritableBookContent.MAX_PAGES)), // Paper - limit books
ServerboundEditBookPacket::pages,
- ByteBufCodecs.stringUtf8(128).apply(ByteBufCodecs::optional),
+ ByteBufCodecs.stringUtf8(net.minecraft.world.item.component.WrittenBookContent.TITLE_MAX_LENGTH).apply(ByteBufCodecs::optional), // Paper - limit books
ServerboundEditBookPacket::title,
ServerboundEditBookPacket::new
);
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-08-19 11:46:39 +02:00
index ce06a41d98bafc878918507404bd5d6cabc45776..d8f940016b68f488b5061d2826607c0db7e8f852 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-08-19 11:46:39 +02:00
@@ -1043,6 +1043,44 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
2021-06-11 14:02:28 +02:00
@Override
public void handleEditBook(ServerboundEditBookPacket packet) {
2024-01-21 13:56:22 +01:00
+ // Paper start - Book size limits
2024-08-19 11:46:39 +02:00
+ 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()) {
2024-08-19 11:33:17 +02:00
+ final List<String> pageList = packet.pages();
2021-06-11 14:02:28 +02:00
+ long byteTotal = 0;
2024-08-19 11:46:39 +02:00
+ final int maxBookPageSize = pageMax.intValue();
2024-08-19 11:33:17 +02:00
+ 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;
2024-08-19 11:33:17 +02:00
+ 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;
2024-08-19 11:33:17 +02:00
+ final int length = page.length();
+ int multiByteCharacters = 0;
2021-06-11 14:02:28 +02:00
+ if (byteLength != length) {
2024-08-19 11:33:17 +02:00
+ // Count the number of multi byte characters
+ for (final char c : page.toCharArray()) {
2021-06-11 14:02:28 +02:00
+ if (c > 127) {
2024-08-19 11:33:17 +02:00
+ multiByteCharacters++;
2021-06-11 14:02:28 +02:00
+ }
+ }
+ }
2024-08-19 11:33:17 +02:00
+
+ // Allow pages with fewer characters to consume less of the allowed byte quota
2024-08-12 00:30:23 +02:00
+ byteAllowed += maxBookPageSize * Math.clamp((double) length / 255D, 0.1D, 1) * multiplier;
2021-06-11 14:02:28 +02:00
+
2024-08-19 11:33:17 +02:00
+ if (multiByteCharacters > 1) {
+ // Penalize multibyte characters
+ byteAllowed -= multiByteCharacters;
2021-06-11 14:02:28 +02:00
+ }
+ }
+
+ if (byteTotal > byteAllowed) {
2024-08-12 00:30:23 +02:00
+ ServerGamePacketListenerImpl.LOGGER.warn("{} tried to send a book too large. Book size: {} - Allowed: {} - Pages: {}", this.player.getScoreboardName(), byteTotal, byteAllowed, pageList.size());
2024-06-16 12:56:00 +02:00
+ this.disconnect(Component.literal("Book too large!"));
2021-06-11 14:02:28 +02:00
+ return;
+ }
+ }
2024-01-21 13:56:22 +01:00
+ // 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!"));