3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 08:30:09 +01:00

Safely handle bad configuration of 'block-protocols' and 'block-versions' (#2983)

Dieser Commit ist enthalten in:
_tomcraft 2022-06-28 11:22:42 +02:00 committet von GitHub
Ursprung 548f80bb7c
Commit 34f0dbf642
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
2 geänderte Dateien mit 24 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -31,6 +31,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.IntPredicate;
@ -160,10 +161,12 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
}
private BlockedProtocolVersions loadBlockedProtocolVersions() {
IntSet blockedProtocols = new IntOpenHashSet(getIntegerList("block-protocols"));
List<Integer> blockProtocols = getListSafe("block-protocols", Integer.class, "Invalid blocked version protocol found in config: '%s'");
List<String> blockVersions = getListSafe("block-versions", String.class, "Invalid blocked version found in config: '%s'");
IntSet blockedProtocols = new IntOpenHashSet(blockProtocols);
int lowerBound = -1;
int upperBound = -1;
for (String s : getStringList("block-versions")) {
for (String s : blockVersions) {
if (s.isEmpty()) {
continue;
}

Datei anzeigen

@ -18,6 +18,7 @@
package com.viaversion.viaversion.util;
import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@ -26,11 +27,7 @@ import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
@ -214,6 +211,23 @@ public abstract class Config implements ConfigurationProvider {
return o != null ? (List<String>) o : new ArrayList<>();
}
public <T> List<T> getListSafe(String key, Class<T> type, String invalidValueMessage) {
Object o = this.config.get(key);
if (o instanceof List) {
List<?> list = (List<?>) o;
List<T> filteredValues = new ArrayList<>();
for (Object o1 : list) {
if (type.isInstance(o1)) {
filteredValues.add(type.cast(o1));
} else if (invalidValueMessage != null) {
Via.getPlatform().getLogger().warning(String.format(invalidValueMessage, o1));
}
}
return filteredValues;
}
return new ArrayList<>();
}
public @Nullable JsonElement getSerializedComponent(String key) {
final Object o = this.config.get(key);
if (o != null && !((String) o).isEmpty()) {