Scissors/patches/server/0040-Add-depth-limit-to-SNBT.patch

111 lines
3.7 KiB
Diff
Raw Normal View History

2023-06-02 22:07:55 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Allink <arclicious@vivaldi.net>
Date: Fri, 2 Jun 2023 22:13:54 +0100
Subject: [PATCH] Add depth limit to SNBT
diff --git a/src/main/java/net/minecraft/nbt/TagParser.java b/src/main/java/net/minecraft/nbt/TagParser.java
2024-02-18 18:44:06 +00:00
index 94cb73e7f60171aa57bd1dbe7e91ef4db94e70b7..9ed1c477620b41317c4bf2b14ebc672c2fdd4656 100644
2023-06-02 22:07:55 +00:00
--- a/src/main/java/net/minecraft/nbt/TagParser.java
+++ b/src/main/java/net/minecraft/nbt/TagParser.java
2024-02-11 01:15:15 +00:00
@@ -182,9 +182,56 @@ public class TagParser {
2023-06-02 22:07:55 +00:00
this.expect('}');
2024-02-11 01:15:15 +00:00
this.depth--; // Paper
2023-06-02 22:07:55 +00:00
- return compoundTag;
+ return exceedsDepthLimit(compoundTag) ? new CompoundTag() : compoundTag; // Scissors - Add depth limit to SNBT
}
2023-06-02 22:07:55 +00:00
+ // Scissors start - Add depth limit to SNBT
+ private boolean exceedsDepthLimit(Tag tag) {
+ return this.exceedsDepthLimit(0, tag);
+ }
+
+ private boolean exceedsDepthLimit(long depth, Tag tag)
+ {
+ if (depth > 256)
+ {
+ return true;
+ }
+
+ if (tag instanceof ListTag listTag)
+ {
+ for (Tag childTag : listTag)
+ {
+ boolean returnValue = this.exceedsDepthLimit(depth + 1, childTag);
+
+ if (returnValue)
+ {
+ return true;
+ }
+ }
+ } else if (tag instanceof CompoundTag compoundTag)
+ {
+ for (String key: compoundTag.getAllKeys())
+ {
+ Tag childTag = compoundTag.get(key);
+
+ if (childTag == null)
+ {
+ continue;
+ }
+
+ boolean returnValue = this.exceedsDepthLimit(depth + 1, childTag);
+
+ if (returnValue)
+ {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
2023-06-02 22:07:55 +00:00
+ // Scissors end
+
2023-06-02 22:07:55 +00:00
private Tag readListTag() throws CommandSyntaxException {
this.expect('[');
this.reader.skipWhitespace();
2024-02-11 01:15:15 +00:00
@@ -218,7 +265,7 @@ public class TagParser {
2023-06-02 22:07:55 +00:00
this.expect(']');
2024-02-11 01:15:15 +00:00
this.depth--; // Paper
2023-06-02 22:07:55 +00:00
- return listTag;
+ return exceedsDepthLimit(listTag) ? new ListTag() : listTag; // Scissors - Add depth limit to SNBT
2023-06-02 22:07:55 +00:00
}
}
2024-02-11 01:15:15 +00:00
@@ -243,7 +290,7 @@ public class TagParser {
2023-06-02 22:07:55 +00:00
}
private <T extends Number> List<T> readArray(TagType<?> arrayTypeReader, TagType<?> typeReader) throws CommandSyntaxException {
- List<T> list = Lists.newArrayList();
+ List<Number> list = Lists.newArrayList(); // Scissors - List<T> -> List<Number>
while(true) {
if (this.reader.peek() != ']') {
2024-02-11 01:15:15 +00:00
@@ -256,11 +303,11 @@ public class TagParser {
2023-06-02 22:07:55 +00:00
}
if (typeReader == ByteTag.TYPE) {
2024-02-11 01:15:15 +00:00
- list.add((T)((NumericTag)tag).getAsNumber()); // Paper - decompile fix
+ list.add(((NumericTag)tag).getAsNumber()); // Scissors - Remove (T) cast
2023-06-02 22:07:55 +00:00
} else if (typeReader == LongTag.TYPE) {
2024-02-11 01:15:15 +00:00
- list.add((T)((NumericTag)tag).getAsNumber()); // Paper - decompile fix
+ list.add(((NumericTag)tag).getAsNumber()); // Scissors - Remove (T) cast
2023-06-02 22:07:55 +00:00
} else {
2024-02-11 01:15:15 +00:00
- list.add((T)((NumericTag)tag).getAsNumber()); // Paper - decompile fix
+ list.add(((NumericTag)tag).getAsNumber()); // Scissors - Remove (T) cast
2023-06-02 22:07:55 +00:00
}
if (this.hasElementSeparator()) {
2024-02-11 01:15:15 +00:00
@@ -272,7 +319,7 @@ public class TagParser {
2023-06-02 22:07:55 +00:00
}
this.expect(']');
- return list;
+ return (List<T>) list; // Scissors - Cast to List<T>
}
}