From f274e20f84de5fe4fd8ee81b04acaa8f78c53756 Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Sat, 9 Apr 2022 23:24:44 -0400 Subject: [PATCH] sanitize minimessage + strip legacy colors --- src/main/java/dev/plex/util/PlexUtils.java | 39 ++++++++++++------- .../java/dev/plex/util/UpdateChecker.java | 9 +---- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index 4015414..4ac0db1 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -15,6 +15,7 @@ import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.standard.*; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.apache.commons.lang.math.NumberUtils; import org.bukkit.*; import org.bukkit.command.Command; @@ -153,27 +154,35 @@ public class PlexUtils extends PlexBase return ChatColor.translateAlternateColorCodes('&', string); } - private static final MiniMessage eggMessage = MiniMessage.builder().tags(new TagResolver() - { - @Override - public @Nullable Tag resolve(@NotNull String name, @NotNull ArgumentQueue arguments, @NotNull Context ctx) throws ParsingException - { - return StandardTags.rainbow().resolve("rainbow", arguments, ctx); - } + private static final MiniMessage safeMessage = MiniMessage.builder().tags(TagResolver.builder().resolvers( + StandardTags.color(), + StandardTags.decorations(), + StandardTags.gradient(), + StandardTags.rainbow(), + StandardTags.reset() + ).build()).build(); - @Override - public boolean has(@NotNull String name) - { - return true; - } - } + private static final MiniMessage eggMessage = MiniMessage.builder().tags(new TagResolver() + { + @Override + public @Nullable Tag resolve(@NotNull String name, @NotNull ArgumentQueue arguments, @NotNull Context ctx) throws ParsingException + { + return StandardTags.rainbow().resolve("rainbow", arguments, ctx); + } + + @Override + public boolean has(@NotNull String name) + { + return true; + } + } ).build(); public static Component mmDeserialize(String input) { Calendar calendar = Calendar.getInstance(); - MiniMessage mm = (calendar.get(Calendar.MONTH) == Calendar.APRIL && calendar.get(Calendar.DAY_OF_MONTH) == 1 && (!plugin.config.contains("april_fools") || plugin.config.getBoolean("april_fools"))) ? eggMessage : MiniMessage.miniMessage(); - return mm.deserialize(input); + MiniMessage mm = (calendar.get(Calendar.MONTH) == Calendar.APRIL && calendar.get(Calendar.DAY_OF_MONTH) == 1 && (!plugin.config.contains("april_fools") || plugin.config.getBoolean("april_fools"))) ? eggMessage : safeMessage; + return mm.deserialize(PlainTextComponentSerializer.plainText().serialize(LegacyComponentSerializer.legacySection().deserialize(input))); } public static Component messageComponent(String entry, Object... objects) diff --git a/src/main/java/dev/plex/util/UpdateChecker.java b/src/main/java/dev/plex/util/UpdateChecker.java index 7fadc07..15b1065 100644 --- a/src/main/java/dev/plex/util/UpdateChecker.java +++ b/src/main/java/dev/plex/util/UpdateChecker.java @@ -147,11 +147,6 @@ public class UpdateChecker extends PlexBase } } - private void sendMini(CommandSender sender, String message) - { - sender.sendMessage(PlexUtils.mmDeserialize(message)); - } - public void updateJar(CommandSender sender) { CloseableHttpClient client = HttpClients.createDefault(); @@ -162,7 +157,7 @@ public class UpdateChecker extends PlexBase JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)); JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0); String name = artifact.getString("fileName"); - sendMini(sender, "Downloading latest Plex jar file: " + name); + sender.sendMessage(PlexUtils.mmDeserialize("Downloading latest Plex jar file: " + name)); CompletableFuture.runAsync(() -> { try @@ -171,7 +166,7 @@ public class UpdateChecker extends PlexBase new URL(DOWNLOAD_PAGE + "job/" + branch + "/lastSuccessfulBuild/artifact/build/libs/" + name), new File(Bukkit.getUpdateFolderFile(), name) ); - sendMini(sender, "Saved new jar. Please restart your server."); + sender.sendMessage(PlexUtils.mmDeserialize("Saved new jar. Please restart your server.")); } catch (IOException e) {