Paper/patches/server/0295-Book-Size-Limits.patch
Nassim Jahnke 26734e83b0
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#7454)
* Updated Upstream (Bukkit/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

Bukkit Changes:
8085edde SPIGOT-6918: Add SpawnCategory API and configurations for Axolotls
04c7e13c PR-719: Add Player Profile API
71564210 SPIGOT-6910: Add BlockDamageAbortEvent

CraftBukkit Changes:
febaa1c6 SPIGOT-6918: Add SpawnCategory API and configurations for Axolotls
9dafd109 Don't send updates over large distances
bdac46b0 SPIGOT-6782: EntityPortalEvent should not destroy entity when setTo() uses same world as getFrom()
8f361ece PR-1002: Add Player Profile API
911875d4 Increase outdated build delay
e5f8a767 SPIGOT-6917: Use main scoreboard for /trigger
a672a531 Clean up callBlockDamageEvent
8e1bdeef SPIGOT-6910: Add BlockDamageAbortEvent

Spigot Changes:
6edb62f3 Rebuild patches
7fbc6a1e Rebuild patches

* Updated Upstream (CraftBukkit)

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:
de951355 SPIGOT-6927: Fix default value of spawn-limits in Worlds
2022-02-12 14:20:33 +01:00

76 lines
3.8 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/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java
index c48b175d5511b733bcff9a93a874f5ffc0174691..e683e5bf47abe7bd3d2f7e9811a377549308ded4 100644
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
@@ -369,6 +369,13 @@ public class PaperConfig {
}
}
+ public static int maxBookPageSize = 2560;
+ public static double maxBookTotalSizeMultiplier = 0.98D;
+ private static void maxBookSize() {
+ maxBookPageSize = Math.min(8192, getInt("settings.book-size.page-max", maxBookPageSize));
+ maxBookTotalSizeMultiplier = getDouble("settings.book-size.total-multiplier", maxBookTotalSizeMultiplier);
+ }
+
public static boolean asyncChunks = false;
private static void asyncChunks() {
ConfigurationSection section;
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index c5800f0e502815bf0e71a389e30e9b604ac6b107..b1ea73320b7927b23d48ee6e80b63b37d1806c97 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -1005,6 +1005,45 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser
@Override
public void handleEditBook(ServerboundEditBookPacket packet) {
+ // Paper start
+ if (!this.cserver.isPrimaryThread()) {
+ List<String> pageList = packet.getPages();
+ long byteTotal = 0;
+ int maxBookPageSize = com.destroystokyo.paper.PaperConfig.maxBookPageSize;
+ double multiplier = Math.max(0.3D, Math.min(1D, com.destroystokyo.paper.PaperConfig.maxBookTotalSizeMultiplier));
+ long byteAllowed = maxBookPageSize;
+ for (String testString : pageList) {
+ int byteLength = testString.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
+ if (byteLength > 256 * 4) {
+ ServerGamePacketListenerImpl.LOGGER.warn(this.player.getScoreboardName() + " tried to send a book with with a page too large!");
+ server.scheduleOnMain(() -> this.disconnect("Book too large!"));
+ return;
+ }
+ byteTotal += byteLength;
+ int length = testString.length();
+ int multibytes = 0;
+ if (byteLength != length) {
+ for (char c : testString.toCharArray()) {
+ if (c > 127) {
+ multibytes++;
+ }
+ }
+ }
+ byteAllowed += (maxBookPageSize * Math.min(1, Math.max(0.1D, (double) length / 255D))) * multiplier;
+
+ if (multibytes > 1) {
+ // penalize MB
+ byteAllowed -= multibytes;
+ }
+ }
+
+ if (byteTotal > byteAllowed) {
+ ServerGamePacketListenerImpl.LOGGER.warn(this.player.getScoreboardName() + " tried to send too large of a book. Book Size: " + byteTotal + " - Allowed: "+ byteAllowed + " - Pages: " + pageList.size());
+ server.scheduleOnMain(() -> this.disconnect("Book too large!"));
+ return;
+ }
+ }
+ // Paper end
// CraftBukkit start
if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
this.disconnect("Book edited too quickly!");