Add depth limit to updateCustomBlockEntityTag (#146)

This commit is contained in:
Nathan Curran 2024-02-11 19:59:43 +11:00 committed by GitHub
parent e7b13dc764
commit 5f694b695b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,75 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Focusvity <nathan.curran10012@gmail.com>
Date: Sun, 11 Feb 2024 19:44:20 +1100
Subject: [PATCH] Add depth limit to updateCustomBlockEntityTag
diff --git a/src/main/java/net/minecraft/world/item/BlockItem.java b/src/main/java/net/minecraft/world/item/BlockItem.java
index 8d2c0accadaf0c5d28e7db6e62a05f6c619cf02f..a59b3e5dde7325e43b8041bccc51d370fdc17c55 100644
--- a/src/main/java/net/minecraft/world/item/BlockItem.java
+++ b/src/main/java/net/minecraft/world/item/BlockItem.java
@@ -4,12 +4,19 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+// Scissors start
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+// Scissors end
import java.util.stream.Stream;
import javax.annotation.Nullable;
+
+import me.totalfreedom.scissors.ScissorsConfig;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
+import net.minecraft.nbt.Tag; // Scissors
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
@@ -98,7 +105,7 @@ public class BlockItem extends Item {
iblockdata1 = this.updateBlockStateFromTag(blockposition, world, itemstack, iblockdata1);
// Paper start - Reset placed block on exception
try {
- this.updateCustomBlockEntityTag(blockposition, world, entityhuman, itemstack, iblockdata1);
+ this.updateCustomBlockEntityTag(blockposition, world, entityhuman, itemstack, iblockdata1);
} catch (Exception e) {
oldBlockstate.update(true, false);
if (entityhuman instanceof ServerPlayer player) {
@@ -240,6 +247,35 @@ public class BlockItem extends Item {
if (nbttagcompound != null) {
BlockEntity tileentity = world.getBlockEntity(pos);
+ // Scissors start
+ if (nbttagcompound.contains("CustomName")) {
+ String customName = nbttagcompound.getString("CustomName");
+ Pattern EXTRA_PATTERN = Pattern.compile("\"extra\":(\\[(.*?)\\{|\\[\\{)");
+ Matcher matcher = EXTRA_PATTERN.matcher(customName);
+ if (matcher.find()) {
+ String matcherString = matcher.group();
+ int penalty = (matcherString.startsWith("\"extra\":[{") ? (int) matcher.results().count() : matcher.group().replace("\"extra\":", "").replace("{", "").length()) * 12;
+ if (penalty > ScissorsConfig.componentDepthLimit) {
+ return false;
+ }
+ }
+ }
+
+ for (Tag tag : nbttagcompound.tags.values()) {
+ if (tag instanceof CompoundTag compoundTag && compoundTag.contains("messages")) {
+ ListTag messagesList = compoundTag.getList("messages", 8);
+ Pattern TRANSLATE_PLACEHOLDER_PATTERN = Pattern.compile("%[0-9]+\\$s");
+ Matcher matcher = TRANSLATE_PLACEHOLDER_PATTERN.matcher(messagesList.toString());
+ if (matcher.find()) {
+ int penalty = (int) matcher.results().count() * 12;
+ if (penalty > ScissorsConfig.componentDepthLimit) {
+ return false;
+ }
+ }
+ }
+ }
+ // Scissors end
+
if (tileentity != null) {
if (!world.isClientSide && tileentity.onlyOpCanSetNbt() && (player == null || !(player.canUseGameMasterBlocks() || (player.getAbilities().instabuild && player.getBukkitEntity().hasPermission("minecraft.nbt.place"))))) { // Spigot - add permission
return false;