Fix crash when yggdrasil server gives response with malformed UUID

This commit is contained in:
huanghongxun
2018-09-24 17:24:13 +08:00
parent c4cebe72b4
commit 1611d11544
3 changed files with 19 additions and 2 deletions

View File

@@ -17,7 +17,10 @@
*/ */
package org.jackhuang.hmcl.auth.yggdrasil; package org.jackhuang.hmcl.auth.yggdrasil;
import com.google.gson.JsonParseException;
import org.jackhuang.hmcl.util.Immutable; import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.Validation;
import java.util.UUID; import java.util.UUID;
@@ -26,7 +29,7 @@ import java.util.UUID;
* @author huangyuhui * @author huangyuhui
*/ */
@Immutable @Immutable
public final class GameProfile { public final class GameProfile implements Validation {
private final UUID id; private final UUID id;
private final String name; private final String name;
@@ -61,4 +64,11 @@ public final class GameProfile {
return properties; return properties;
} }
@Override
public void validate() throws JsonParseException {
if (id == null)
throw new JsonParseException("Game profile id cannot be null or malformed");
if (StringUtils.isBlank(name))
throw new JsonParseException("Game profile name cannot be null or blank");
}
} }

View File

@@ -8,6 +8,7 @@ import org.jackhuang.hmcl.auth.ServerDisconnectException;
import org.jackhuang.hmcl.auth.ServerResponseMalformedException; import org.jackhuang.hmcl.auth.ServerResponseMalformedException;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter; import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter;
import org.jackhuang.hmcl.util.gson.ValidationTypeAdapterFactory;
import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jackhuang.hmcl.util.io.NetworkUtils;
import java.io.IOException; import java.io.IOException;
@@ -187,6 +188,7 @@ public class YggdrasilService {
private static final Gson GSON = new GsonBuilder() private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(PropertyMap.class, PropertyMap.Serializer.INSTANCE) .registerTypeAdapter(PropertyMap.class, PropertyMap.Serializer.INSTANCE)
.registerTypeAdapter(UUID.class, UUIDTypeAdapter.INSTANCE) .registerTypeAdapter(UUID.class, UUIDTypeAdapter.INSTANCE)
.registerTypeAdapterFactory(ValidationTypeAdapterFactory.INSTANCE)
.create(); .create();
} }

View File

@@ -17,6 +17,7 @@
*/ */
package org.jackhuang.hmcl.util.gson; package org.jackhuang.hmcl.util.gson;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
@@ -50,7 +51,11 @@ public final class UUIDTypeAdapter extends TypeAdapter<UUID> {
} }
public static UUID fromString(String input) { public static UUID fromString(String input) {
return UUID.fromString(input.replaceFirst("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5")); try {
return UUID.fromString(input.replaceFirst("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5"));
} catch (IllegalArgumentException e) {
throw new JsonParseException("UUID malformed");
}
} }
} }