From 37bf078394169832393ad02ea59af4e9d21cb1d5 Mon Sep 17 00:00:00 2001
From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com>
Date: Fri, 3 Dec 2021 16:55:50 -0500
Subject: [PATCH] Sanitize sent BlockEntity NBT

---
 .../ClientboundBlockEntityDataPacket.java.patch | 11 +++++++++++
 .../ClientboundLevelChunkPacketData.java.patch  | 10 ++++++++++
 .../level/block/entity/BlockEntity.java.patch   | 17 ++++++++++++-----
 3 files changed, 33 insertions(+), 5 deletions(-)
 create mode 100644 paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java.patch
 create mode 100644 paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java.patch

diff --git a/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java.patch b/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java.patch
new file mode 100644
index 0000000000..4967c17ea2
--- /dev/null
+++ b/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java.patch
@@ -0,0 +1,11 @@
+--- a/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java
++++ b/net/minecraft/network/protocol/game/ClientboundBlockEntityDataPacket.java
+@@ -29,7 +29,7 @@
+ 
+     public static ClientboundBlockEntityDataPacket create(BlockEntity blockEntity, BiFunction<BlockEntity, RegistryAccess, CompoundTag> nbtGetter) {
+         RegistryAccess registryAccess = blockEntity.getLevel().registryAccess();
+-        return new ClientboundBlockEntityDataPacket(blockEntity.getBlockPos(), blockEntity.getType(), nbtGetter.apply(blockEntity, registryAccess));
++        return new ClientboundBlockEntityDataPacket(blockEntity.getBlockPos(), blockEntity.getType(), blockEntity.sanitizeSentNbt(nbtGetter.apply(blockEntity, registryAccess)));  // Paper - Sanitize sent data
+     }
+ 
+     public static ClientboundBlockEntityDataPacket create(BlockEntity blockEntity) {
diff --git a/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java.patch b/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java.patch
new file mode 100644
index 0000000000..3430e6ce78
--- /dev/null
+++ b/paper-server/patches/sources/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java.patch
@@ -0,0 +1,10 @@
+--- a/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
++++ b/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
+@@ -154,6 +154,7 @@
+             CompoundTag compoundTag = blockEntity.getUpdateTag(blockEntity.getLevel().registryAccess());
+             BlockPos blockPos = blockEntity.getBlockPos();
+             int i = SectionPos.sectionRelative(blockPos.getX()) << 4 | SectionPos.sectionRelative(blockPos.getZ());
++            blockEntity.sanitizeSentNbt(compoundTag); // Paper - Sanitize sent data
+             return new ClientboundLevelChunkPacketData.BlockEntityInfo(i, blockPos.getY(), blockEntity.getType(), compoundTag.isEmpty() ? null : compoundTag);
+         }
+     }
diff --git a/paper-server/patches/sources/net/minecraft/world/level/block/entity/BlockEntity.java.patch b/paper-server/patches/sources/net/minecraft/world/level/block/entity/BlockEntity.java.patch
index 1d84f433f3..3635b5393e 100644
--- a/paper-server/patches/sources/net/minecraft/world/level/block/entity/BlockEntity.java.patch
+++ b/paper-server/patches/sources/net/minecraft/world/level/block/entity/BlockEntity.java.patch
@@ -27,7 +27,7 @@
      }
  
      private void validateBlockState(BlockState state) {
-@@ -74,8 +85,17 @@
+@@ -74,7 +85,16 @@
          return this.level != null;
      }
  
@@ -35,17 +35,16 @@
 +    // CraftBukkit start - read container
 +    protected void loadAdditional(CompoundTag nbt, HolderLookup.Provider registries) {
 +        this.persistentDataContainer.clear(); // Paper - clear instead of init
- 
++
 +        net.minecraft.nbt.Tag persistentDataTag = nbt.get("PublicBukkitValues");
 +        if (persistentDataTag instanceof CompoundTag) {
 +            this.persistentDataContainer.putAll((CompoundTag) persistentDataTag);
 +        }
 +    }
 +    // CraftBukkit end
-+
+ 
      public final void loadWithComponents(CompoundTag nbt, HolderLookup.Provider registries) {
          this.loadAdditional(nbt, registries);
-         BlockEntity.ComponentHelper.COMPONENTS_CODEC.parse(registries.createSerializationContext(NbtOps.INSTANCE), nbt).resultOrPartial((s) -> {
 @@ -114,6 +134,11 @@
          }).ifPresent((nbtbase) -> {
              nbttagcompound.merge((CompoundTag) nbtbase);
@@ -109,7 +108,7 @@
      }
  
      protected void collectImplicitComponents(DataComponentMap.Builder builder) {}
-@@ -321,6 +361,22 @@
+@@ -321,6 +361,30 @@
          }
      }
  
@@ -128,6 +127,14 @@
 +        return null;
 +    }
 +    // CraftBukkit end
++
++    // Paper start - Sanitize sent data
++    public CompoundTag sanitizeSentNbt(CompoundTag tag) {
++        tag.remove("PublicBukkitValues");
++
++        return tag;
++    }
++    // Paper end - Sanitize sent data
 +
      private static class ComponentHelper {