From 7bf35977f083ea0bb4f4a91a019b06f9f79aa11b Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 23 Jul 2020 09:23:29 +0200 Subject: [PATCH] Added validity check for commands --- .../data/commands/SystemCommandsMap.java | 59 ++++++++++++++++--- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java b/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java index 4ddeb70..0add151 100644 --- a/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java +++ b/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java @@ -3,6 +3,7 @@ package envoy.client.data.commands; import java.util.*; import java.util.function.Function; import java.util.logging.Level; +import java.util.regex.Pattern; import java.util.stream.Collectors; import envoy.util.EnvoyLog; @@ -23,45 +24,58 @@ public class SystemCommandsMap { private final HashMap systemCommands = new HashMap<>(); + private final Pattern commandBounds = Pattern.compile("^[a-zA-Z0-9_:!\\(\\)\\?\\.\\,\\;\\-]+$"); + /** + * Adds a command with according action and the number of arguments that should + * be parsed if the command does not violate API constrictions to the map. + * * @param command the string that must be inputted to execute the * given action * @param action the action that should be performed * @param numberOfArguments the amount of arguments that need to be parsed for * the underlying function + * @see SystemCommandsMap#isValidKey(String) * @since Envoy Client v0.1-beta */ public void addCommand(String command, Function action, int numberOfArguments) { - systemCommands.put(command, new SystemCommand(action, numberOfArguments, "")); + if (isValidKey(command)) systemCommands.put(command, new SystemCommand(action, numberOfArguments, "")); } /** + * Adds a command with according action, the number of arguments that should be + * parsed and a description of the system command, if the command does not + * violate API constrictions, to the map. + * * @param command the string that must be inputted to execute the * given action * @param action the action that should be performed * @param numberOfArguments the amount of arguments that need to be parsed for * the underlying function * @param description the description of this {@link SystemCommand} + * @see SystemCommandsMap#isValidKey(String) * @since Envoy Client v0.1-beta */ public void addCommand(String command, Function action, int numberOfArguments, String description) { - systemCommands.put(command, new SystemCommand(action, numberOfArguments, description)); + if (isValidKey(command)) systemCommands.put(command, new SystemCommand(action, numberOfArguments, description)); } /** - * Adds a new {@link SystemCommand} to the map that does not depend upon - * arguments in the given String + * Adds a command with according action that does not depend on arguments, if + * the command does not violate API constrictions, to the map. * * @param command the string that must be inputted to execute the given action * @param action the action that should be performed. To see why this Function * takes a {@code String[]}, see {@link SystemCommand} + * @see SystemCommandsMap#isValidKey(String) * @since Envoy Client v0.1-beta */ public void addNoArgCommand(String command, Function action) { addCommand(command, action, 0); } /** - * Adds a new {@link SystemCommand} to the map that does not depend upon - * arguments in the given String + * Adds a command with according action that does not depend on arguments and a + * description of that action, if the command does not violate API + * constrictions, to the map. * * @param command the string that must be inputted to execute the given * action @@ -69,11 +83,18 @@ public class SystemCommandsMap { * Function takes a {@code String[]}, see * {@link SystemCommand} * @param description the description of this {@link SystemCommand} + * @see SystemCommandsMap#isValidKey(String) * @since Envoy Client v0.1-beta */ public void addNoArgCommand(String command, Function action, String description) { addCommand(command, action, 0); } /** + * Convenience method that does the same as + * {@link SystemCommandsMap#addCommand(String, Function, int)}. + *

+ * Adds a command with according action and the number of arguments that should + * be parsed if the command does not violate API constrictions to the map. + * * @param command the string that must be inputted to execute the * given action * @param action the action that should be performed. To see why this @@ -81,10 +102,32 @@ public class SystemCommandsMap { * {@link SystemCommand} * @param numberOfArguments the amount of arguments that need to be parsed for * the underlying function + * @see SystemCommandsMap#isValidKey(String) * @since Envoy Client v0.1-beta */ - public void add(String command, Function action, int numberOfArguments) { - systemCommands.put(command, new SystemCommand(action, numberOfArguments, "")); + public void add(String command, Function action, int numberOfArguments) { addCommand(command, action, numberOfArguments); } + + /** + * Examines whether a key can be put in the map and logs it with + * {@code Level.WARNING} if that key violates API constrictions
+ * (allowed chars are a-zA-Z0-9_:!()?.,;-) + *

+ * The approach to not throw an exception was taken so that an ugly try-catch + * block for every addition to the system commands map could be avoided, an + * error that + * should only occur during implementation and not in production. + * + * @param command the key to examine + * @return whether this key can be used in the map + * @since Envoy Client v0.1-beta + */ + public final boolean isValidKey(String command) { + final boolean valid = commandBounds.matcher(command).matches(); + if (!valid) EnvoyLog.getLogger(SystemCommandsMap.class) + .log(Level.WARNING, + "The command \"" + command + "\" is not valid. It will cause problems in execution: Only the characters " + commandBounds + + "are allowed"); + return valid; } /**