Paper/CraftBukkit-Patches/0129-Better-item-validation.patch
2014-11-28 15:01:21 -06:00

160 lines
6.4 KiB
Diff

From b8b26fc29e5aa514a8e5986dba2bd809424d67cb Mon Sep 17 00:00:00 2001
From: Thinkofdeath <thinkofdeath@spigotmc.org>
Date: Wed, 2 Jul 2014 23:35:51 +0100
Subject: [PATCH] Better item validation
diff --git a/src/main/java/net/minecraft/server/DataWatcher.java b/src/main/java/net/minecraft/server/DataWatcher.java
index 966d737..ca3469f 100644
--- a/src/main/java/net/minecraft/server/DataWatcher.java
+++ b/src/main/java/net/minecraft/server/DataWatcher.java
@@ -154,6 +154,17 @@ public class DataWatcher {
arraylist = Lists.newArrayList();
}
+ // Spigot start - copy ItemStacks to prevent ConcurrentModificationExceptions
+ if ( watchableobject.b() instanceof ItemStack )
+ {
+ watchableobject = new WatchableObject(
+ watchableobject.c(),
+ watchableobject.a(),
+ ( (ItemStack) watchableobject.b() ).cloneItemStack()
+ );
+ }
+ // Spigot end
+
arraylist.add(watchableobject);
}
}
@@ -185,6 +196,21 @@ public class DataWatcher {
this.f.readLock().lock();
arraylist.addAll(this.dataValues.valueCollection()); // Spigot
+ // Spigot start - copy ItemStacks to prevent ConcurrentModificationExceptions
+ for ( int i = 0; i < arraylist.size(); i++ )
+ {
+ WatchableObject watchableobject = (WatchableObject) arraylist.get( i );
+ if ( watchableobject.b() instanceof ItemStack )
+ {
+ watchableobject = new WatchableObject(
+ watchableobject.c(),
+ watchableobject.a(),
+ ( (ItemStack) watchableobject.b() ).cloneItemStack()
+ );
+ arraylist.set( i, watchableobject );
+ }
+ }
+ // Spigot end
this.f.readLock().unlock();
return arraylist;
diff --git a/src/main/java/net/minecraft/server/PacketDataSerializer.java b/src/main/java/net/minecraft/server/PacketDataSerializer.java
index 8b7dc17..5227007 100644
--- a/src/main/java/net/minecraft/server/PacketDataSerializer.java
+++ b/src/main/java/net/minecraft/server/PacketDataSerializer.java
@@ -172,6 +172,10 @@ public class PacketDataSerializer extends ByteBuf {
NBTTagCompound nbttagcompound = null;
if (itemstack.getItem().usesDurability() || itemstack.getItem().p()) {
+ // Spigot start - filter
+ itemstack = itemstack.cloneItemStack();
+ CraftItemStack.setItemMeta(itemstack, CraftItemStack.getItemMeta(itemstack));
+ // Spigot end
nbttagcompound = itemstack.getTag();
}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
index 9a24d2e..d3b5002 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
@@ -20,6 +20,10 @@ import net.minecraft.server.ChatSerializer;
import net.minecraft.server.NBTTagString;
import org.bukkit.craftbukkit.util.CraftChatMessage;
+// Spigot start
+import static org.spigotmc.ValidateUtils.*;
+// Spigot end
+
@DelegateDeserialization(SerializableMeta.class)
class CraftMetaBook extends CraftMetaItem implements BookMeta {
static final ItemMetaKey BOOK_TITLE = new ItemMetaKey("title");
@@ -54,11 +58,11 @@ class CraftMetaBook extends CraftMetaItem implements BookMeta {
super(tag);
if (tag.hasKey(BOOK_TITLE.NBT)) {
- this.title = tag.getString(BOOK_TITLE.NBT);
+ this.title = limit( tag.getString(BOOK_TITLE.NBT), 1024 ); // Spigot
}
if (tag.hasKey(BOOK_AUTHOR.NBT)) {
- this.author = tag.getString(BOOK_AUTHOR.NBT);
+ this.author = limit( tag.getString(BOOK_AUTHOR.NBT), 1024 ); // Spigot
}
if (tag.hasKey(RESOLVED.NBT)) {
@@ -78,7 +82,7 @@ class CraftMetaBook extends CraftMetaItem implements BookMeta {
if (resolved != null && resolved) {
page = CraftChatMessage.fromComponent(ChatSerializer.a(page));
}
- pageArray[i] = page;
+ pageArray[i] = limit( page, 2048 ); // Spigot
}
addPage(pageArray);
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
index 32d4a67..53c3f6c 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
@@ -36,6 +36,10 @@ import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
+// Spigot start
+import static org.spigotmc.ValidateUtils.*;
+// Spigot end
+
/**
* Children must include the following:
*
@@ -227,7 +231,7 @@ class CraftMetaItem implements ItemMeta, Repairable {
NBTTagCompound display = tag.getCompound(DISPLAY.NBT);
if (display.hasKey(NAME.NBT)) {
- displayName = display.getString(NAME.NBT);
+ displayName = limit( display.getString(NAME.NBT), 1024 ); // Spigot
}
if (display.hasKey(LORE.NBT)) {
@@ -235,7 +239,7 @@ class CraftMetaItem implements ItemMeta, Repairable {
lore = new ArrayList<String>(list.size());
for (int index = 0; index < list.size(); index++) {
- String line = list.getString(index);
+ String line = limit( list.getString(index), 1024 ); // Spigot
lore.add(line);
}
}
diff --git a/src/main/java/org/spigotmc/ValidateUtils.java b/src/main/java/org/spigotmc/ValidateUtils.java
new file mode 100644
index 0000000..58a9534
--- /dev/null
+++ b/src/main/java/org/spigotmc/ValidateUtils.java
@@ -0,0 +1,14 @@
+package org.spigotmc;
+
+public class ValidateUtils
+{
+
+ public static String limit(String str, int limit)
+ {
+ if ( str.length() > limit )
+ {
+ return str.substring( 0, limit );
+ }
+ return str;
+ }
+}
--
2.1.0