diff --git a/patches/server/Prevent-sending-oversized-item-data-in-equipment-and.patch b/patches/server/Prevent-sending-oversized-item-data-in-equipment-and.patch new file mode 100644 index 0000000000..c7d8e5e471 --- /dev/null +++ b/patches/server/Prevent-sending-oversized-item-data-in-equipment-and.patch @@ -0,0 +1,228 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Nassim Jahnke +Date: Wed, 1 Dec 2021 12:36:25 +0100 +Subject: [PATCH] Prevent sending oversized item data in equipment and metadata + +Co-authored-by: Jake Potrebic + +diff --git a/src/main/java/io/papermc/paper/util/DataSanitizationUtil.java b/src/main/java/io/papermc/paper/util/DataSanitizationUtil.java +new file mode 100644 +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/util/DataSanitizationUtil.java +@@ -0,0 +0,0 @@ ++package io.papermc.paper.util; ++ ++import java.util.ArrayList; ++import java.util.List; ++import java.util.concurrent.atomic.AtomicBoolean; ++import java.util.function.UnaryOperator; ++import net.minecraft.network.RegistryFriendlyByteBuf; ++import net.minecraft.network.codec.StreamCodec; ++import net.minecraft.world.item.ItemStack; ++import net.minecraft.world.item.Items; ++import net.minecraft.world.item.component.BundleContents; ++import net.minecraft.world.item.component.ChargedProjectiles; ++import net.minecraft.world.item.component.ItemContainerContents; ++import org.checkerframework.checker.nullness.qual.NonNull; ++import org.checkerframework.framework.qual.DefaultQualifier; ++ ++@DefaultQualifier(NonNull.class) ++public final class DataSanitizationUtil { ++ ++ private static final ThreadLocal DATA_SANITIZER = ThreadLocal.withInitial(DataSanitizer::new); ++ ++ public static DataSanitizer start(final boolean sanitize) { ++ final DataSanitizer sanitizer = DATA_SANITIZER.get(); ++ if (sanitize) { ++ sanitizer.start(); ++ } ++ return sanitizer; ++ } ++ ++ public static final StreamCodec CHARGED_PROJECTILES = codec(ChargedProjectiles.STREAM_CODEC, DataSanitizationUtil::sanitizeChargedProjectiles); ++ public static final StreamCodec BUNDLE_CONTENTS = codec(BundleContents.STREAM_CODEC, DataSanitizationUtil::sanitizeBundleContents); ++ public static final StreamCodec CONTAINER = codec(ItemContainerContents.STREAM_CODEC, contents -> ItemContainerContents.EMPTY); ++ ++ private static ChargedProjectiles sanitizeChargedProjectiles(final ChargedProjectiles projectiles) { ++ if (projectiles.isEmpty()) { ++ return projectiles; ++ } ++ final List items = projectiles.getItems(); ++ final List sanitized = new ArrayList<>(); ++ for (int i = 0; i < Math.min(items.size(), 3); i++) { ++ // we want to preserve item type as vanilla client can change visuals based on type ++ sanitized.add(new ItemStack(items.get(i).getItemHolder())); ++ } ++ return ChargedProjectiles.of(sanitized); ++ } ++ ++ private static BundleContents sanitizeBundleContents(final BundleContents contents) { ++ // Bundles change their texture based on their fullness. ++ int sizeUsed = 0; ++ for (final ItemStack item : contents.items()) { ++ final int scale = 64 / item.getMaxStackSize(); ++ sizeUsed += scale * item.getCount(); ++ } ++ // Now we add a single fake item that uses the same amount of slots as all other items. ++ final List items = new ArrayList<>(); ++ items.add(new ItemStack(Items.PAPER, sizeUsed)); ++ return new BundleContents(items); ++ } ++ ++ private static StreamCodec codec(final StreamCodec delegate, final UnaryOperator sanitizer) { ++ return new DataSanitizationCodec<>(delegate, sanitizer); ++ } ++ ++ private record DataSanitizationCodec(StreamCodec delegate, UnaryOperator sanitizer) implements StreamCodec { ++ ++ @Override ++ public @NonNull A decode(final @NonNull B buf) { ++ return this.delegate.decode(buf); ++ } ++ ++ @Override ++ public void encode(final @NonNull B buf, final @NonNull A value) { ++ if (!DATA_SANITIZER.get().value().get()) { ++ this.delegate.encode(buf, value); ++ } else { ++ this.delegate.encode(buf, this.sanitizer.apply(value)); ++ } ++ } ++ } ++ ++ public record DataSanitizer(AtomicBoolean value) implements AutoCloseable { ++ ++ public DataSanitizer() { ++ this(new AtomicBoolean(false)); ++ } ++ ++ public void start() { ++ this.value.compareAndSet(false, true); ++ } ++ ++ @Override ++ public void close() { ++ this.value.compareAndSet(true, false); ++ } ++ } ++ ++ private DataSanitizationUtil() { ++ } ++ ++} +diff --git a/src/main/java/net/minecraft/core/component/DataComponents.java b/src/main/java/net/minecraft/core/component/DataComponents.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/core/component/DataComponents.java ++++ b/src/main/java/net/minecraft/core/component/DataComponents.java +@@ -0,0 +0,0 @@ public class DataComponents { + "map_post_processing", builder -> builder.networkSynchronized(MapPostProcessing.STREAM_CODEC) + ); + public static final DataComponentType CHARGED_PROJECTILES = register( +- "charged_projectiles", builder -> builder.persistent(ChargedProjectiles.CODEC).networkSynchronized(ChargedProjectiles.STREAM_CODEC).cacheEncoding() ++ "charged_projectiles", builder -> builder.persistent(ChargedProjectiles.CODEC).networkSynchronized(io.papermc.paper.util.DataSanitizationUtil.CHARGED_PROJECTILES).cacheEncoding() // Paper - sanitize charged projectiles + ); + public static final DataComponentType BUNDLE_CONTENTS = register( +- "bundle_contents", builder -> builder.persistent(BundleContents.CODEC).networkSynchronized(BundleContents.STREAM_CODEC).cacheEncoding() ++ "bundle_contents", builder -> builder.persistent(BundleContents.CODEC).networkSynchronized(io.papermc.paper.util.DataSanitizationUtil.BUNDLE_CONTENTS).cacheEncoding() // Paper - sanitize bundle contents + ); + public static final DataComponentType POTION_CONTENTS = register( + "potion_contents", builder -> builder.persistent(PotionContents.CODEC).networkSynchronized(PotionContents.STREAM_CODEC).cacheEncoding() +@@ -0,0 +0,0 @@ public class DataComponents { + "pot_decorations", builder -> builder.persistent(PotDecorations.CODEC).networkSynchronized(PotDecorations.STREAM_CODEC).cacheEncoding() + ); + public static final DataComponentType CONTAINER = register( +- "container", builder -> builder.persistent(ItemContainerContents.CODEC).networkSynchronized(ItemContainerContents.STREAM_CODEC).cacheEncoding() ++ "container", builder -> builder.persistent(ItemContainerContents.CODEC).networkSynchronized(io.papermc.paper.util.DataSanitizationUtil.CONTAINER).cacheEncoding() // Paper - sanitize container contents + ); + public static final DataComponentType BLOCK_STATE = register( + "block_state", builder -> builder.persistent(BlockItemStateProperties.CODEC).networkSynchronized(BlockItemStateProperties.STREAM_CODEC).cacheEncoding() +diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEntityDataPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEntityDataPacket.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEntityDataPacket.java ++++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEntityDataPacket.java +@@ -0,0 +0,0 @@ public record ClientboundSetEntityDataPacket(int id, List> trackedValues, RegistryFriendlyByteBuf buf) { ++ try (var ignored = io.papermc.paper.util.DataSanitizationUtil.start(true)) { // Paper - data sanitization + for (SynchedEntityData.DataValue dataValue : trackedValues) { + dataValue.write(buf); + } ++ } // Paper - data sanitization + + buf.writeByte(255); + } +diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEquipmentPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEquipmentPacket.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEquipmentPacket.java ++++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundSetEquipmentPacket.java +@@ -0,0 +0,0 @@ public class ClientboundSetEquipmentPacket implements Packet> slots; + + public ClientboundSetEquipmentPacket(int id, List> equipmentList) { ++ // Paper start - data sanitization ++ this(id, equipmentList, false); ++ } ++ private boolean sanitize = false; ++ public ClientboundSetEquipmentPacket(int id, List> equipmentList, boolean sanitize) { ++ this.sanitize = sanitize; ++ // Paper end - data sanitization + this.entity = id; + this.slots = equipmentList; + } +@@ -0,0 +0,0 @@ public class ClientboundSetEquipmentPacket implements Packet pair = this.slots.get(j); + EquipmentSlot equipmentSlot = pair.getFirst(); +@@ -0,0 +0,0 @@ public class ClientboundSetEquipmentPacket implements Packet Pair.of(slot, ((LivingEntity) entity).getItemBySlot(slot).copy())).collect(Collectors.toList()))); ++ ServerGamePacketListenerImpl.this.send(new ClientboundSetEquipmentPacket(entity.getId(), Arrays.stream(net.minecraft.world.entity.EquipmentSlot.values()).map((slot) -> Pair.of(slot, ((LivingEntity) entity).getItemBySlot(slot).copy())).collect(Collectors.toList()), true)); // Paper - sanitize + ServerGamePacketListenerImpl.this.player.containerMenu.sendAllDataToRemote(); + } + } +diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java ++++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java +@@ -0,0 +0,0 @@ public abstract class LivingEntity extends Entity implements Attackable { + } + + }); +- ((ServerLevel) this.level()).getChunkSource().broadcast(this, new ClientboundSetEquipmentPacket(this.getId(), list)); ++ ((ServerLevel) this.level()).getChunkSource().broadcast(this, new ClientboundSetEquipmentPacket(this.getId(), list, true)); // Paper - data sanitization + } + + private ItemStack getLastArmorItem(EquipmentSlot slot) { diff --git a/removed-patches-1-20-5/0675-Prevent-sending-oversized-item-data-in-equipment-and.patch b/removed-patches-1-20-5/0675-Prevent-sending-oversized-item-data-in-equipment-and.patch deleted file mode 100644 index bf907b7723..0000000000 --- a/removed-patches-1-20-5/0675-Prevent-sending-oversized-item-data-in-equipment-and.patch +++ /dev/null @@ -1,88 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Nassim Jahnke -Date: Wed, 1 Dec 2021 12:36:25 +0100 -Subject: [PATCH] Prevent sending oversized item data in equipment and metadata - -TODO: Check if still needed with compacted items over the network and limits - - -diff --git a/src/main/java/net/minecraft/network/syncher/EntityDataSerializers.java b/src/main/java/net/minecraft/network/syncher/EntityDataSerializers.java -index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 ---- a/src/main/java/net/minecraft/network/syncher/EntityDataSerializers.java -+++ b/src/main/java/net/minecraft/network/syncher/EntityDataSerializers.java -@@ -0,0 +0,0 @@ public class EntityDataSerializers { - public static final EntityDataSerializer ITEM_STACK = new EntityDataSerializer() { - @Override - public void write(FriendlyByteBuf buf, ItemStack value) { -- buf.writeItem(value); -+ buf.writeItem(net.minecraft.world.entity.LivingEntity.sanitizeItemStack(value, true)); // Paper - prevent oversized data - } - - @Override -diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java -index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 ---- a/src/main/java/net/minecraft/server/level/ServerEntity.java -+++ b/src/main/java/net/minecraft/server/level/ServerEntity.java -@@ -0,0 +0,0 @@ public class ServerEntity { - ItemStack itemstack = ((LivingEntity) this.entity).getItemBySlot(enumitemslot); - - if (!itemstack.isEmpty()) { -- list.add(Pair.of(enumitemslot, itemstack.copy())); -+ // Paper start - prevent oversized data -+ final ItemStack sanitized = LivingEntity.sanitizeItemStack(itemstack.copy(), false); -+ list.add(Pair.of(enumitemslot, sanitized)); -+ // Paper end - prevent oversized data - } - } - -diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java -index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 ---- a/src/main/java/net/minecraft/world/entity/LivingEntity.java -+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java -@@ -0,0 +0,0 @@ public abstract class LivingEntity extends Entity implements Attackable { - equipmentChanges.forEach((enumitemslot, itemstack) -> { - ItemStack itemstack1 = itemstack.copy(); - -- list.add(Pair.of(enumitemslot, itemstack1)); -+ // Paper start - prevent oversized data -+ ItemStack toSend = sanitizeItemStack(itemstack1, true); -+ list.add(Pair.of(enumitemslot, toSend)); -+ // Paper end - prevent oversized data - switch (enumitemslot.getType()) { - case HAND: - this.setLastHandItem(enumitemslot, itemstack1); -@@ -0,0 +0,0 @@ public abstract class LivingEntity extends Entity implements Attackable { - ((ServerLevel) this.level()).getChunkSource().broadcast(this, new ClientboundSetEquipmentPacket(this.getId(), list)); - } - -+ // Paper start - prevent oversized data -+ public static ItemStack sanitizeItemStack(final ItemStack itemStack, final boolean copyItemStack) { -+ if (itemStack.isEmpty() || !itemStack.hasTag()) { -+ return itemStack; -+ } -+ -+ final ItemStack copy = copyItemStack ? itemStack.copy() : itemStack; -+ final CompoundTag tag = copy.getTag(); -+ if (copy.is(Items.BUNDLE) && tag.get("Items") instanceof ListTag oldItems && !oldItems.isEmpty()) { -+ // Bundles change their texture based on their fullness. -+ org.bukkit.inventory.meta.BundleMeta bundleMeta = (org.bukkit.inventory.meta.BundleMeta) copy.asBukkitMirror().getItemMeta(); -+ int sizeUsed = 0; -+ for (org.bukkit.inventory.ItemStack item : bundleMeta.getItems()) { -+ int scale = 64 / item.getMaxStackSize(); -+ sizeUsed += scale * item.getAmount(); -+ } -+ // Now we add a single fake item that uses the same amount of slots as all other items. -+ ListTag items = new ListTag(); -+ items.add(new ItemStack(Items.PAPER, sizeUsed).save(new CompoundTag())); -+ tag.put("Items", items); -+ } -+ if (tag.get("BlockEntityTag") instanceof CompoundTag blockEntityTag) { -+ blockEntityTag.remove("Items"); -+ } -+ return copy; -+ } -+ // Paper end - prevent oversized data -+ - private ItemStack getLastArmorItem(EquipmentSlot slot) { - return (ItemStack) this.lastArmorItemStacks.get(slot.getIndex()); - }