Paper/patches/server/0273-Book-Size-Limits.patch
Nassim Jahnke 6e71f41536
Updated Upstream (CraftBukkit/Spigot)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

CraftBukkit Changes:
65247583f SPIGOT-7857: Improve ItemMeta block data deserialization
05d80500d SPIGOT-7857: Fix spurious internal NBT tag when deserializing BlockStateMeta
cebb58e9a SPIGOT-7804: Fix written book serialization
efcdd5d38 SPIGOT-7794: Cancelling InventoryItemMoveEvent destroys items
b568ba572 SPIGOT-7789: Fix NPE in CraftMetaFirework applyToItem
f057cf449 Remove outdated build delay

Spigot Changes:
f6a48054 SPIGOT-7835: Fix issue with custom hopper settings
bb63b137 Rebuild patches
e1142b4d Rebuild patches
2024-08-24 11:23:57 +02:00

72 lines
4.3 KiB
Diff

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
Puts some limits on the size of books.
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
);
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index e880b41e1824f986d04a0a32cc735d77f25cf987..987bb9caa952a09615b0a819fc682b354be2b07c 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -1040,6 +1040,43 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
@Override
public void handleEditBook(ServerboundEditBookPacket packet) {
+ // Paper start - Book size limits
+ if (!this.cserver.isPrimaryThread()) {
+ final List<String> pageList = packet.pages();
+ long byteTotal = 0;
+ final int maxBookPageSize = io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.pageMax;
+ final double multiplier = Math.clamp(io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.totalMultiplier, 0.3D, 1D);
+ long byteAllowed = maxBookPageSize;
+ for (final String page : pageList) {
+ final int byteLength = page.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
+ byteTotal += byteLength;
+ final int length = page.length();
+ int multiByteCharacters = 0;
+ if (byteLength != length) {
+ // Count the number of multi byte characters
+ for (final char c : page.toCharArray()) {
+ if (c > 127) {
+ multiByteCharacters++;
+ }
+ }
+ }
+
+ // Allow pages with fewer characters to consume less of the allowed byte quota
+ byteAllowed += maxBookPageSize * Math.clamp((double) length / 255D, 0.1D, 1) * multiplier;
+
+ if (multiByteCharacters > 1) {
+ // Penalize multibyte characters
+ byteAllowed -= multiByteCharacters;
+ }
+ }
+
+ 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!"));
+ return;
+ }
+ }
+ // Paper end - Book size limits
// CraftBukkit start
if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
this.disconnect("Book edited too quickly!");