From a1a073eaaec1ab63af43679e48a97078bbe1daea Mon Sep 17 00:00:00 2001 From: Aikar Date: Fri, 16 Nov 2018 23:08:50 -0500 Subject: [PATCH] Book size limits Puts some limits on the size of books. --- .../ServerGamePacketListenerImpl.java.patch | 142 +++++++++++------- 1 file changed, 90 insertions(+), 52 deletions(-) diff --git a/paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch b/paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch index 977f0178f8..a607e32eb8 100644 --- a/paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch +++ b/paper-server/patches/sources/net/minecraft/server/network/ServerGamePacketListenerImpl.java.patch @@ -453,10 +453,48 @@ if (!containermerchant.stillValid(this.player)) { ServerGamePacketListenerImpl.LOGGER.debug("Player {} interacted with invalid menu {}", this.player, containermerchant); return; -@@ -879,6 +1117,13 @@ +@@ -879,6 +1117,51 @@ @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 pageList = packet.pages(); ++ 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); ++ 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(Component.literal("Book edited too quickly!")); @@ -467,7 +505,7 @@ int i = packet.slot(); if (Inventory.isHotbarSlot(i) || i == 40) { -@@ -899,12 +1144,16 @@ +@@ -899,12 +1182,16 @@ } private void updateBookContents(List pages, int slotId) { @@ -485,7 +523,7 @@ } } -@@ -915,12 +1164,13 @@ +@@ -915,12 +1202,13 @@ ItemStack itemstack1 = itemstack.transmuteCopy(Items.WRITTEN_BOOK); itemstack1.remove(DataComponents.WRITABLE_BOOK_CONTENT); @@ -501,7 +539,7 @@ } } -@@ -982,22 +1232,30 @@ +@@ -982,22 +1270,30 @@ } else { ServerLevel worldserver = this.player.serverLevel(); @@ -536,7 +574,7 @@ double d3 = this.player.getX(); double d4 = this.player.getY(); double d5 = this.player.getZ(); -@@ -1019,15 +1277,39 @@ +@@ -1019,15 +1315,39 @@ ++this.receivedMovePacketCount; int i = this.receivedMovePacketCount - this.knownMovePacketCount; @@ -578,7 +616,7 @@ ServerGamePacketListenerImpl.LOGGER.warn("{} moved too quickly! {},{},{}", new Object[]{this.player.getName().getString(), d6, d7, d8}); this.teleport(this.player.getX(), this.player.getY(), this.player.getZ(), this.player.getYRot(), this.player.getXRot()); return; -@@ -1043,12 +1325,40 @@ +@@ -1043,12 +1363,40 @@ boolean flag1 = d7 > 0.0D; if (this.player.onGround() && !packet.isOnGround() && flag1) { @@ -620,7 +658,7 @@ double d11 = d7; d6 = d0 - this.player.getX(); -@@ -1061,15 +1371,81 @@ +@@ -1061,15 +1409,81 @@ d10 = d6 * d6 + d7 * d7 + d8 * d8; boolean flag3 = false; @@ -704,7 +742,7 @@ this.player.absMoveTo(d0, d1, d2, f, f1); boolean flag4 = this.player.isAutoSpinAttack(); -@@ -1119,6 +1495,7 @@ +@@ -1119,6 +1533,7 @@ this.awaitingTeleportTime = this.tickCount; this.teleport(this.awaitingPositionFromClient.x, this.awaitingPositionFromClient.y, this.awaitingPositionFromClient.z, this.player.getYRot(), this.player.getXRot()); } @@ -712,7 +750,7 @@ return true; } else { -@@ -1147,23 +1524,83 @@ +@@ -1147,23 +1562,83 @@ } public void teleport(double x, double y, double z, float yaw, float pitch) { @@ -799,7 +837,7 @@ if (this.player.hasClientLoaded()) { BlockPos blockposition = packet.getPos(); -@@ -1175,14 +1612,46 @@ +@@ -1175,14 +1650,46 @@ if (!this.player.isSpectator()) { ItemStack itemstack = this.player.getItemInHand(InteractionHand.OFF_HAND); @@ -848,7 +886,7 @@ this.player.drop(false); } -@@ -1199,6 +1668,12 @@ +@@ -1199,6 +1706,12 @@ case START_DESTROY_BLOCK: case ABORT_DESTROY_BLOCK: case STOP_DESTROY_BLOCK: @@ -861,7 +899,7 @@ this.player.gameMode.handleBlockBreakAction(blockposition, packetplayinblockdig_enumplayerdigtype, packet.getDirection(), this.player.level().getMaxY(), packet.getSequence()); this.player.connection.ackBlockChangesUpTo(packet.getSequence()); return; -@@ -1218,9 +1693,31 @@ +@@ -1218,9 +1731,31 @@ } } @@ -893,7 +931,7 @@ if (this.player.hasClientLoaded()) { this.player.connection.ackBlockChangesUpTo(packet.getSequence()); ServerLevel worldserver = this.player.serverLevel(); -@@ -1244,6 +1741,7 @@ +@@ -1244,6 +1779,7 @@ if (blockposition.getY() <= i) { if (this.awaitingPositionFromClient == null && worldserver.mayInteract(this.player, blockposition)) { @@ -901,7 +939,7 @@ InteractionResult enuminteractionresult = this.player.gameMode.useItemOn(this.player, worldserver, itemstack, enumhand, movingobjectpositionblock); if (enuminteractionresult.consumesAction()) { -@@ -1281,6 +1779,8 @@ +@@ -1281,6 +1817,8 @@ @Override public void handleUseItem(ServerboundUseItemPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -910,7 +948,7 @@ if (this.player.hasClientLoaded()) { this.ackBlockChangesUpTo(packet.getSequence()); ServerLevel worldserver = this.player.serverLevel(); -@@ -1296,6 +1796,47 @@ +@@ -1296,6 +1834,47 @@ this.player.absRotateTo(f, f1); } @@ -958,7 +996,7 @@ InteractionResult enuminteractionresult = this.player.gameMode.useItem(this.player, worldserver, itemstack, enumhand); if (enuminteractionresult instanceof InteractionResult.Success) { -@@ -1321,7 +1862,7 @@ +@@ -1321,7 +1900,7 @@ Entity entity = packet.getEntity(worldserver); if (entity != null) { @@ -967,7 +1005,7 @@ return; } } -@@ -1342,6 +1883,13 @@ +@@ -1342,6 +1921,13 @@ @Override public void onDisconnect(DisconnectionDetails info) { @@ -981,7 +1019,7 @@ ServerGamePacketListenerImpl.LOGGER.info("{} lost connection: {}", this.player.getName().getString(), info.reason().getString()); this.removePlayerFromWorld(); super.onDisconnect(info); -@@ -1349,10 +1897,20 @@ +@@ -1349,10 +1935,20 @@ private void removePlayerFromWorld() { this.chatMessageChain.close(); @@ -1004,7 +1042,7 @@ this.player.getTextFilter().leave(); } -@@ -1367,7 +1925,16 @@ +@@ -1367,7 +1963,16 @@ @Override public void handleSetCarriedItem(ServerboundSetCarriedItemPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -1021,7 +1059,7 @@ if (this.player.getInventory().selected != packet.getSlot() && this.player.getUsedItemHand() == InteractionHand.MAIN_HAND) { this.player.stopUsingItem(); } -@@ -1376,11 +1943,18 @@ +@@ -1376,11 +1981,18 @@ this.player.resetLastActionTime(); } else { ServerGamePacketListenerImpl.LOGGER.warn("{} tried to set an invalid carried item", this.player.getName().getString()); @@ -1040,7 +1078,7 @@ Optional optional = this.unpackAndApplyLastSeen(packet.lastSeenMessages()); if (!optional.isEmpty()) { -@@ -1394,27 +1968,44 @@ +@@ -1394,27 +2006,44 @@ return; } @@ -1092,7 +1130,7 @@ ParseResults parseresults = this.parseCommand(command); if (this.server.enforceSecureProfile() && SignableCommand.hasSignableArguments(parseresults)) { -@@ -1431,19 +2022,37 @@ +@@ -1431,19 +2060,37 @@ if (!optional.isEmpty()) { this.tryHandleChat(packet.command(), () -> { @@ -1134,7 +1172,7 @@ } catch (SignedMessageChain.DecodeException signedmessagechain_a) { this.handleMessageDecodeFailure(signedmessagechain_a); return; -@@ -1451,10 +2060,10 @@ +@@ -1451,10 +2098,10 @@ CommandSigningContext.SignedArguments commandsigningcontext_a = new CommandSigningContext.SignedArguments(map); @@ -1147,7 +1185,7 @@ } private void handleMessageDecodeFailure(SignedMessageChain.DecodeException exception) { -@@ -1530,14 +2139,20 @@ +@@ -1530,14 +2177,20 @@ return com_mojang_brigadier_commanddispatcher.parse(command, this.player.createCommandSourceStack()); } @@ -1172,7 +1210,7 @@ } } -@@ -1566,6 +2181,127 @@ +@@ -1566,6 +2219,127 @@ return false; } @@ -1300,7 +1338,7 @@ private PlayerChatMessage getSignedMessage(ServerboundChatPacket packet, LastSeenMessages lastSeenMessages) throws SignedMessageChain.DecodeException { SignedMessageBody signedmessagebody = new SignedMessageBody(packet.message(), packet.timeStamp(), packet.salt(), lastSeenMessages); -@@ -1573,13 +2309,42 @@ +@@ -1573,13 +2347,42 @@ } private void broadcastChatMessage(PlayerChatMessage message) { @@ -1348,7 +1386,7 @@ this.disconnect((Component) Component.translatable("disconnect.spam")); } -@@ -1601,7 +2366,33 @@ +@@ -1601,7 +2404,33 @@ @Override public void handleAnimate(ServerboundSwingPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -1382,7 +1420,7 @@ this.player.swing(packet.getHand()); } -@@ -1609,6 +2400,29 @@ +@@ -1609,6 +2438,29 @@ public void handlePlayerCommand(ServerboundPlayerCommandPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); if (this.player.hasClientLoaded()) { @@ -1412,7 +1450,7 @@ this.player.resetLastActionTime(); Entity entity; PlayerRideableJumping ijumpable; -@@ -1616,6 +2430,11 @@ +@@ -1616,6 +2468,11 @@ switch (packet.getAction()) { case PRESS_SHIFT_KEY: this.player.setShiftKeyDown(true); @@ -1424,7 +1462,7 @@ break; case RELEASE_SHIFT_KEY: this.player.setShiftKeyDown(false); -@@ -1691,6 +2510,12 @@ +@@ -1691,6 +2548,12 @@ } public void sendPlayerChatMessage(PlayerChatMessage message, ChatType.Bound params) { @@ -1437,7 +1475,7 @@ this.send(new ClientboundPlayerChatPacket(message.link().sender(), message.link().index(), message.signature(), message.signedBody().pack(this.messageSignatureCache), message.unsignedContent(), message.filterMask(), params)); this.addPendingMessage(message); } -@@ -1703,6 +2528,13 @@ +@@ -1703,6 +2566,13 @@ return this.connection.getRemoteAddress(); } @@ -1451,7 +1489,7 @@ public void switchToConfig() { this.waitingForSwitchToConfig = true; this.removePlayerFromWorld(); -@@ -1718,9 +2550,17 @@ +@@ -1718,9 +2588,17 @@ @Override public void handleInteract(ServerboundInteractPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -1469,7 +1507,7 @@ this.player.resetLastActionTime(); this.player.setShiftKeyDown(packet.isUsingSecondaryAction()); -@@ -1733,20 +2573,58 @@ +@@ -1733,20 +2611,58 @@ if (this.player.canInteractWithEntity(axisalignedbb, 3.0D)) { packet.dispatch(new ServerboundInteractPacket.Handler() { @@ -1485,7 +1523,7 @@ + ItemStack itemInHand = ServerGamePacketListenerImpl.this.player.getItemInHand(enumhand); + boolean triggerLeashUpdate = itemInHand != null && itemInHand.getItem() == Items.LEAD && entity instanceof Mob; + Item origItem = ServerGamePacketListenerImpl.this.player.getInventory().getSelected() == null ? null : ServerGamePacketListenerImpl.this.player.getInventory().getSelected().getItem(); -+ + + ServerGamePacketListenerImpl.this.cserver.getPluginManager().callEvent(event); + + // Entity in bucket - SPIGOT-4048 and SPIGOT-6859a @@ -1493,7 +1531,7 @@ + entity.getBukkitEntity().update(ServerGamePacketListenerImpl.this.player); + ServerGamePacketListenerImpl.this.player.containerMenu.sendAllDataToRemote(); + } - ++ + if (triggerLeashUpdate && (event.isCancelled() || ServerGamePacketListenerImpl.this.player.getInventory().getSelected() == null || ServerGamePacketListenerImpl.this.player.getInventory().getSelected().getItem() != origItem)) { + // Refresh the current leash state + ServerGamePacketListenerImpl.this.send(new ClientboundSetEntityLinkPacket(entity, ((Mob) entity).getLeashHolder())); @@ -1532,7 +1570,7 @@ } } -@@ -1755,19 +2633,20 @@ +@@ -1755,19 +2671,20 @@ @Override public void onInteraction(InteractionHand hand) { @@ -1556,7 +1594,7 @@ label23: { if (entity instanceof AbstractArrow) { -@@ -1785,6 +2664,11 @@ +@@ -1785,6 +2702,11 @@ } ServerGamePacketListenerImpl.this.player.attack(entity); @@ -1568,7 +1606,7 @@ return; } } -@@ -1795,7 +2679,26 @@ +@@ -1795,7 +2717,26 @@ }); } } @@ -1595,7 +1633,7 @@ } } -@@ -1809,7 +2712,7 @@ +@@ -1809,7 +2750,7 @@ case PERFORM_RESPAWN: if (this.player.wonGame) { this.player.wonGame = false; @@ -1604,7 +1642,7 @@ this.resetPosition(); CriteriaTriggers.CHANGED_DIMENSION.trigger(this.player, Level.END, Level.OVERWORLD); } else { -@@ -1817,11 +2720,11 @@ +@@ -1817,11 +2758,11 @@ return; } @@ -1618,7 +1656,7 @@ } } break; -@@ -1833,16 +2736,27 @@ +@@ -1833,16 +2774,27 @@ @Override public void handleContainerClose(ServerboundContainerClosePacket packet) { @@ -1648,7 +1686,7 @@ this.player.containerMenu.sendAllDataToRemote(); } else if (!this.player.containerMenu.stillValid(this.player)) { ServerGamePacketListenerImpl.LOGGER.debug("Player {} interacted with invalid menu {}", this.player, this.player.containerMenu); -@@ -1855,7 +2769,284 @@ +@@ -1855,7 +2807,284 @@ boolean flag = packet.getStateId() != this.player.containerMenu.getStateId(); this.player.containerMenu.suppressRemoteUpdates(); @@ -1934,7 +1972,7 @@ ObjectIterator objectiterator = Int2ObjectMaps.fastIterable(packet.getChangedSlots()).iterator(); while (objectiterator.hasNext()) { -@@ -1901,8 +3092,22 @@ +@@ -1901,8 +3130,22 @@ return; } @@ -1958,7 +1996,7 @@ if (containerrecipebook_a == RecipeBookMenu.PostPlaceAction.PLACE_GHOST_RECIPE) { this.player.connection.send(new ClientboundPlaceGhostRecipePacket(this.player.containerMenu.containerId, craftingmanager_d.display().display())); } -@@ -1917,6 +3122,7 @@ +@@ -1917,6 +3160,7 @@ @Override public void handleContainerButtonClick(ServerboundContainerButtonClickPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -1966,7 +2004,7 @@ this.player.resetLastActionTime(); if (this.player.containerMenu.containerId == packet.containerId() && !this.player.isSpectator()) { if (!this.player.containerMenu.stillValid(this.player)) { -@@ -1945,7 +3151,44 @@ +@@ -1945,7 +3189,44 @@ boolean flag1 = packet.slotNum() >= 1 && packet.slotNum() <= 45; boolean flag2 = itemstack.isEmpty() || itemstack.getCount() <= itemstack.getMaxStackSize(); @@ -1974,7 +2012,7 @@ + // CraftBukkit start - Call click event + InventoryView inventory = this.player.inventoryMenu.getBukkitView(); + org.bukkit.inventory.ItemStack item = CraftItemStack.asBukkitCopy(packet.itemStack()); -+ + + SlotType type = SlotType.QUICKBAR; + if (flag) { + type = SlotType.OUTSIDE; @@ -1987,7 +2025,7 @@ + } + InventoryCreativeEvent event = new InventoryCreativeEvent(inventory, type, flag ? -999 : packet.slotNum(), item); + this.cserver.getPluginManager().callEvent(event); - ++ + itemstack = CraftItemStack.asNMSCopy(event.getCursor()); + + switch (event.getResult()) { @@ -2011,7 +2049,7 @@ if (flag1 && flag2) { this.player.inventoryMenu.getSlot(packet.slotNum()).setByPlayer(itemstack); this.player.inventoryMenu.setRemoteSlot(packet.slotNum(), itemstack); -@@ -1972,6 +3215,7 @@ +@@ -1972,6 +3253,7 @@ } private void updateSignText(ServerboundSignUpdatePacket packet, List signText) { @@ -2019,7 +2057,7 @@ this.player.resetLastActionTime(); ServerLevel worldserver = this.player.serverLevel(); BlockPos blockposition = packet.getPos(); -@@ -1993,7 +3237,17 @@ +@@ -1993,7 +3275,17 @@ @Override public void handlePlayerAbilities(ServerboundPlayerAbilitiesPacket packet) { PacketUtils.ensureRunningOnSameThread(packet, this, this.player.serverLevel()); @@ -2038,7 +2076,7 @@ } @Override -@@ -2002,6 +3256,7 @@ +@@ -2002,6 +3294,7 @@ boolean flag = this.player.isModelPartShown(PlayerModelPart.HAT); this.player.updateOptions(packet.information()); @@ -2046,7 +2084,7 @@ if (this.player.isModelPartShown(PlayerModelPart.HAT) != flag) { this.server.getPlayerList().broadcastAll(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_HAT, this.player)); } -@@ -2058,7 +3313,7 @@ +@@ -2058,7 +3351,7 @@ if (!this.waitingForSwitchToConfig) { throw new IllegalStateException("Client acknowledged config, but none was requested"); } else { @@ -2055,7 +2093,7 @@ } } -@@ -2083,8 +3338,10 @@ +@@ -2083,8 +3376,10 @@ }); }