Skip to content

Commit

Permalink
Add trusted common JSON mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed Sep 2, 2023
1 parent 7eca366 commit c258290
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.jupiter.version>5.10.0</junit.jupiter.version>
<log4j2.version>2.20.0</log4j2.version>
<jackson.version>2.14.3</jackson.version>
<jackson.version>2.15.2</jackson.version>
<jline.version>3.23.0</jline.version>
<!-- 本地测试服务端插件目录 -->
<dir.api>../../_api</dir.api>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static void init() {

BlockEntry[] propertiesTable; // auto-generated
try (InputStream stream = Server.class.getClassLoader().getResourceAsStream("block_properties_table.json")) {
propertiesTable = JsonUtil.COMMON_JSON_MAPPER.readValue(stream, BlockEntry[].class);
propertiesTable = JsonUtil.TRUSTED_JSON_MAPPER.readValue(stream, BlockEntry[].class);
} catch (NullPointerException | IOException e) {
throw new AssertionError("Unable to load block_properties_table.json", e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/nukkit/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -3119,15 +3119,15 @@ public int getBiomeId(int x, int z) {
}

public void setBiomeId(int x, int z, int biomeId) {
this.getChunk(x >> 4, z >> 4, true).setBiomeId(x & 0x0f, z & 0x0f, biomeId & 0x0f);
this.getChunk(x >> 4, z >> 4, true).setBiomeId(x & 0x0f, z & 0x0f, biomeId);
}

public int getHeightMap(int x, int z) {
return this.getChunk(x >> 4, z >> 4, true).getHeightMap(x & 0x0f, z & 0x0f);
}

private void setHeightMap(int x, int z, int value) {
this.getChunk(x >> 4, z >> 4, true).setHeightMap(x & 0x0f, z & 0x0f, value & 0x0f);
this.getChunk(x >> 4, z >> 4, true).setHeightMap(x & 0x0f, z & 0x0f, value);
}

public Map<Long, BaseFullChunk> getChunks() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cn/nukkit/plugin/PluginBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ public void reloadConfig() {
if (configStream != null) {
LoadSettings settings = LoadSettings.builder()
.setParseComments(false)
.setMaxAliasesForCollections(Integer.MAX_VALUE)
.setCodePointLimit(Integer.MAX_VALUE)
.build();
Load yaml = new Load(settings);
try {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cn/nukkit/plugin/PluginDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public PluginDescription(Map<String, Object> yamlMap) {
public PluginDescription(String yamlString) {
LoadSettings settings = LoadSettings.builder()
.setParseComments(false)
.setMaxAliasesForCollections(Integer.MAX_VALUE)
.setCodePointLimit(Integer.MAX_VALUE)
.build();
Load yaml = new Load(settings);
this.loadMap((Map<String, Object>) yaml.loadFromString(yamlString));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/resourcepacks/PackManifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PackManifest {
private List<SubPack> subpacks = Collections.emptyList();

public static PackManifest load(InputStream stream) throws IOException {
return JsonUtil.COMMON_JSON_MAPPER.readValue(stream, PackManifest.class);
return JsonUtil.TRUSTED_JSON_MAPPER.readValue(stream, PackManifest.class);
}

public boolean isValid() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cn/nukkit/utils/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,8 @@ private void parseContent(String content) {
case Config.YAML:
LoadSettings settings = LoadSettings.builder()
.setParseComments(false)
.setMaxAliasesForCollections(Integer.MAX_VALUE)
.setCodePointLimit(Integer.MAX_VALUE)
.build();
Load yaml = new Load(settings);
this.config = new ConfigSection((LinkedHashMap<String, Object>) yaml.loadFromString(content));
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/cn/nukkit/utils/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.nukkit.utils;

import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
Expand All @@ -13,4 +14,14 @@ public class JsonUtil {
.addModule(new Jdk8Module())
.addModule(new GuavaModule())
.build();
public static final JsonMapper TRUSTED_JSON_MAPPER = COMMON_JSON_MAPPER.copy();

static {
TRUSTED_JSON_MAPPER.getFactory()
.setStreamReadConstraints(StreamReadConstraints.builder()
.maxNestingDepth(Integer.MAX_VALUE)
.maxNumberLength(Integer.MAX_VALUE)
.maxStringLength(Integer.MAX_VALUE)
.build());
}
}

0 comments on commit c258290

Please sign in to comment.