aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0368-Limit-Book-Sizes.patch
blob: 9245ba9782a83e07705edcd24a2539fdc7dda32a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
From cb5abe5576e8400081e6a1a552178d3f14bf7604 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 17 Nov 2018 00:08:54 -0500
Subject: [PATCH] Limit Book Sizes


diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java
index 332e90f8..27203eea 100644
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
@@ -320,4 +320,18 @@ public class PaperConfig {
         tabSpamIncrement = getInt("settings.spam-limiter.tab-spam-increment", tabSpamIncrement);
         tabSpamLimit = getInt("settings.spam-limiter.tab-spam-limit", tabSpamLimit);
     }
+
+
+    public static int maxBookPageSize = 2560;
+    public static double maxBookTotalSizeMultiplier = 0.98D;
+    private static void maxBookSize() {
+        maxBookPageSize = getInt("settings.book-size.page-max", maxBookPageSize);
+        maxBookTotalSizeMultiplier = getDouble("settings.book-size.total-multiplier", maxBookTotalSizeMultiplier);
+        if (maxBookPageSize == 1024 && maxBookTotalSizeMultiplier == 0.90D) {
+            config.set("settings.book-size.page-max", 2560);
+            config.set("settings.book-size.total-multiplier", 0.98D);
+            maxBookPageSize = 2560;
+            maxBookTotalSizeMultiplier = 0.98D;
+        }
+    }
 }
diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java
index d6d2010d..de62c3b7 100644
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
@@ -2329,6 +2329,45 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
         this.player.a(packetplayinsettings);
     }
 
+    // Paper start
+    private boolean validateBook(ItemStack testStack) {
+        NBTTagList pageList = testStack.getTag().getList("pages", 8);
+        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 (int i = 0; i < pageList.size(); ++i) {
+            String testString = pageList.getString(i);
+            int byteLength = testString.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
+            byteTotal += byteLength;
+
+            int length = testString.length();
+            int multibytes = 0;
+            if (length != byteLength) {
+                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) {
+            PlayerConnection.LOGGER.warn(this.player.getName() + " tried to send too large of a book. Book Size: " + byteTotal + " - Allowed:  "+ byteAllowed + " - Pages: " + pageList.size());
+            minecraftServer.postToMainThread(() -> this.disconnect("Book too large!"));
+            return false;
+        }
+
+        return true;
+    }
+    // Paper end
+
     public void a(PacketPlayInCustomPayload packetplayincustompayload) {
         PlayerConnectionUtils.ensureMainThread(packetplayincustompayload, this, this.player.x());
         String s = packetplayincustompayload.a();
@@ -2362,6 +2401,7 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
                 }
 
                 if (itemstack.getItem() == Items.WRITABLE_BOOK && itemstack.getItem() == itemstack1.getItem()) {
+                    if (!validateBook(itemstack)) return; // Paper
                     itemstack1.a("pages", (NBTBase) itemstack.getTag().getList("pages", 8));
                     CraftEventFactory.handleEditBookEvent(player, itemstack1); // CraftBukkit
                 }
@@ -2397,6 +2437,7 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
                     }
 
                     if (itemstack.getItem() == Items.WRITABLE_BOOK && itemstack1.getItem() == Items.WRITABLE_BOOK) {
+                        if (!validateBook(itemstack)) return; // Paper
                         ItemStack itemstack2 = new ItemStack(Items.WRITTEN_BOOK);
 
                         itemstack2.a("author", (NBTBase) (new NBTTagString(this.player.getName())));
-- 
2.20.1