From a8c89da6a190b188e6aed3ebed73cc1bdb8c330a Mon Sep 17 00:00:00 2001 From: William Date: Wed, 17 Aug 2022 21:25:32 +0100 Subject: [PATCH] Properly escape MineDown in locales --- .../huskhomes/command/EditHomeCommand.java | 22 +++++++------ .../william278/huskhomes/config/Locales.java | 31 +++++++++++++++++-- common/src/main/resources/locales/en-gb.yml | 6 ++-- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java index 69962348..f9f0f52f 100644 --- a/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/EditHomeCommand.java @@ -2,11 +2,14 @@ package net.william278.huskhomes.command; import de.themoep.minedown.MineDown; import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.position.Home; import net.william278.huskhomes.position.PositionMeta; import net.william278.huskhomes.util.Permission; import net.william278.huskhomes.util.RegexUtil; +import org.apache.commons.text.StringEscapeUtils; +import org.apache.commons.text.WordUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -140,17 +143,21 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons final String oldHomeDescription = home.meta.description; final String newDescription = editArgs; + plugin.getSavedPositionManager().updateHomeMeta(home, new PositionMeta(home.meta.name, newDescription)) .thenAccept(descriptionUpdateResult -> (switch (descriptionUpdateResult.resultType()) { case SUCCESS -> { if (home.owner.uuid.equals(editor.uuid)) { yield plugin.getLocales().getLocale("edit_home_update_description", home.meta.name, - oldHomeDescription, newDescription); + oldHomeDescription, + newDescription); } else { yield plugin.getLocales().getLocale("edit_home_update_description_other", - home.owner.username, home.meta.name, - oldHomeDescription, newDescription); + home.owner.username, + home.meta.name, + oldHomeDescription, + newDescription); } } case FAILED_DESCRIPTION_LENGTH -> @@ -270,13 +277,10 @@ public class EditHomeCommand extends CommandBase implements TabCompletable, Cons .ifPresent(this::add); if (home.meta.description.length() > 0) { - final String escapedDescription = MineDown.escape(home.meta.description - .replaceAll(Pattern.quote("("), "\\(") - .replaceAll(Pattern.quote(")"), "\\)")); plugin.getLocales().getLocale("edit_home_menu_description", - escapedDescription.length() > 50 - ? escapedDescription.substring(0, 49).trim() + "…" : escapedDescription, - String.join("\n", escapedDescription.split("(?<=\\G.{40,}\\s)"))) + home.meta.description.length() > 50 + ? home.meta.description.substring(0, 49).trim() + "…" : home.meta.description, + String.join("\n", home.meta.description.split("(?<=\\G.{40,}\\s)"))) .ifPresent(this::add); } diff --git a/common/src/main/java/net/william278/huskhomes/config/Locales.java b/common/src/main/java/net/william278/huskhomes/config/Locales.java index bd1db69c..3f620169 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Locales.java +++ b/common/src/main/java/net/william278/huskhomes/config/Locales.java @@ -1,14 +1,18 @@ package net.william278.huskhomes.config; import de.themoep.minedown.MineDown; +import de.themoep.minedown.MineDownParser; +import de.themoep.minedown.Util; import net.william278.annotaml.RootedMap; import net.william278.annotaml.YamlFile; import org.apache.commons.text.StringEscapeUtils; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.regex.Pattern; /** * Loaded locales used by the plugin to display styled messages @@ -37,7 +41,9 @@ public class Locales { } /** - * Returnsa raw, un-formatted locale loaded from the locales file, with replacements applied + * Returns a raw, un-formatted locale loaded from the locales file, with replacements applied + *

+ * Note that replacements will not be MineDown-escaped; use {@link #escapeMineDown(String)} to escape replacements * * @param localeId String identifier of the locale, corresponding to a key in the file * @param replacements Ordered array of replacement strings to fill in placeholders with @@ -59,13 +65,16 @@ public class Locales { /** * Returns a MineDown-formatted locale from the locales file, with replacements applied + *

+ * Note that replacements will be MineDown-escaped before application * * @param localeId String identifier of the locale, corresponding to a key in the file * @param replacements Ordered array of replacement strings to fill in placeholders with * @return An {@link Optional} containing the replacement-applied, formatted locale corresponding to the id, if it exists */ public Optional getLocale(@NotNull String localeId, @NotNull String... replacements) { - return getRawLocale(localeId, replacements).map(MineDown::new); + return getRawLocale(localeId, Arrays.stream(replacements).map(Locales::escapeMineDown) + .toArray(String[]::new)).map(MineDown::new); } /** @@ -86,6 +95,24 @@ public class Locales { return rawLocale; } + @NotNull + private static String escapeMineDown(@NotNull String string) { + final StringBuilder value = new StringBuilder(); + for (int i = 0; i < string.length(); ++i) { + char c = string.charAt(i); + boolean isEscape = c == '\\'; + boolean isColorCode = i + 1 < string.length() && (c == 167 || c == '&'); + boolean isEvent = c == '[' || c == ']' || c == '(' || c == ')'; + boolean isFormatting = (c == '_' || c == '*' || c == '~' || c == '?' || c == '#') && Util.isDouble(string, i); + if (isEscape || isColorCode || isEvent || isFormatting) { + value.append('\\'); + } + + value.append(c); + } + return value.toString(); + } + @SuppressWarnings("unused") public Locales() { } diff --git a/common/src/main/resources/locales/en-gb.yml b/common/src/main/resources/locales/en-gb.yml index 8440eb7b..f3a2aa4a 100644 --- a/common/src/main/resources/locales/en-gb.yml +++ b/common/src/main/resources/locales/en-gb.yml @@ -18,7 +18,7 @@ edit_home_menu_metadata_public: '[⌚ %1%](#999999-#ababab show_text=&7Set Times edit_home_menu_description: '[ℹ %1%](#c160ff-#b43fff show_text=&#c160ff&Description:\n&8%2%)\n' edit_home_menu_world: '[⭘ %1%](#3b76ff-#5286ff show_text=b76ff&World:\n&8The world where the home is set)' edit_home_menu_world_server: '[⭘ %1%](#3b76ff-#477fff show_text=b76ff&World:\n&8The world where the home is set) [☁ %2%](#477fff-#5286ff show_text=b76ff&Server:\n&8The server where the home is set)' -edit_home_menu_coordinates: '[⚐ x: %1%, y: %2%, z: %3%](#ffc43b-#f5c962 show_text=&#ffc43b&Coordinates:\n&8Location of the home in the world\n&8\(yaw: %4%, pitch: %5%\) )\n' +edit_home_menu_coordinates: '[⚐ x: %1%, y: %2%, z: %3%](#ffc43b-#f5c962 show_text=&#ffc43b&Coordinates:\n&8Location of the home in the world\n&8yaw: %4%, pitch: %5%)\n' edit_home_menu_use_buttons: '[Use:](gray) [[⏩ Teleport…]](#00fb9a show_text=&7Click to teleport to this home run_command=/huskhomes:home %1%)' edit_home_menu_manage_buttons: '[Manage:](gray) [[❌ Delete…]](#ff3300 show_text=&7Click to delete this home\n&#ff3300&⚠ This cannot be undone! run_command=/huskhomes:delhome %1%) [[⚐ Relocate…]](#ffc43b-#f5c962 show_text=&7Click to relocate this home to your position run_command=/huskhomes:edithome %1% relocate -m) [[☀ Public/Private…]](#6fff00 show_text=&7Click to toggle the privacy of this home run_command=/huskhomes:edithome %1% privacy -m)' edit_home_menu_meta_edit_buttons: '[Edit:](gray) [[✎ Rename…]](#c160ff-#bb50ff show_text=&7Click to suggest the rename command suggest_command=/huskhomes:edithome %1% rename ) [[ℹ Description…]](#bb50ff-#b43fff show_text=&7Click to suggest the edit description command suggest_command=/huskhomes:edithome %1% description )' @@ -29,8 +29,8 @@ edit_home_privacy_public_success_other: '[Successfully made %1%''s home,](#00fb9 edit_home_privacy_private_success_other: '[Successfully made %1%''s home,](#00fb9a) [%2%](#00fb9a bold)[, private.](#00fb9a)' edit_home_update_location: '[Re-located](#00fb9a) [%1%](#00fb9a bold) [to your current position.](#00fb9a)' edit_home_update_location_other: '[Re-located %1%''s home,](#00fb9a) [%2%](#00fb9a bold)[, to your current position.](#00fb9a)' -edit_home_update_description: '[Successfully changed the description of your home, %1%:](#00fb9a) \n[←](white) [Original:](#00fb9a) ["%2%"](gray)\n[→](white) [New:](#00fb9a) ["%3%"](gray)' -edit_home_update_description_other: '[Successfully changed the description of %1%''s home, %2%:](#00fb9a) \n[←](white) [Original:](#00fb9a) ["%3%"](gray)\n[→](white) [New:](#00fb9a) ["%4%"](gray)' +edit_home_update_description: '[Successfully changed the description of your home, %1%:](#00fb9a) \n[←](white) [Old:](#00fb9a) ["%2%"](gray)\n[→](white) [New:](#00fb9a) ["%3%" ](gray)' +edit_home_update_description_other: '[Successfully changed the description of %1%''s home, %2%:](#00fb9a) \n[←](white) [Old:](#00fb9a) ["%3%"](gray)\n[→](white) [New:](#00fb9a) ["%4%" ](gray)' edit_home_update_name: '[Successfully changed your home''s name: %1% →](#00fb9a) [%2%](#00fb9a bold)' edit_home_update_name_other: '[Successfully renamed %1%''s home: %2% →](#00fb9a) [%3%](#00fb9a bold)' edit_warp_title: '\n[Edit details for the warp](#00fb9a) [%1%](#00fb9a bold)[:](#00fb9a)'