Test verbose filters before registering them (#178)

This commit is contained in:
Luck 2017-02-12 17:19:16 +00:00
parent 140e6b08ca
commit 3f17e8c3c8
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 44 additions and 0 deletions

View File

@ -65,6 +65,12 @@ public class VerboseCommand extends SingleCommand {
}
String filter = filters.isEmpty() ? "" : filters.stream().collect(Collectors.joining(" "));
if (!DebugListener.isValidFilter(filter)) {
Message.VERBOSE_INVALID_FILTER.send(sender, filter);
return CommandResult.FAILURE;
}
boolean notify = !mode.equals("record");
plugin.getDebugHandler().register(sender, filter, notify);

View File

@ -96,6 +96,7 @@ public enum Message {
/*
* Commands
*/
VERBOSE_INVALID_FILTER("&cInvalid verbose filter: &f{0}", true),
VERBOSE_ON("&bVerbose checking output set to &aTRUE &bfor all permissions.", true),
VERBOSE_ON_QUERY("&bVerbose checking output set to &aTRUE &bfor permissions matching the following filters: &f{0}", true),
VERBOSE_OFF("&bVerbose checking output set to &cFALSE&b.", true),

View File

@ -116,6 +116,42 @@ public class DebugListener {
return false;
}
public static boolean isValidFilter(String filter) {
if (filter.equals("")) {
return true;
}
ScriptEngine engine = Scripting.getScriptEngine();
if (engine == null) {
return false;
}
StringTokenizer tokenizer = new StringTokenizer(filter, " |&()!", true);
StringBuilder expression = new StringBuilder();
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (!isDelim(token)) {
token = "true"; // dummy result
}
expression.append(token);
}
try {
String exp = expression.toString().replace("&", "&&").replace("|", "||");
String result = engine.eval(exp).toString();
if (!result.equals("true") && !result.equals("false")) {
throw new IllegalArgumentException(exp + " - " + result);
}
return true;
} catch (Throwable t) {
return false;
}
}
private static boolean isDelim(String token) {
return token.equals(" ") || token.equals("|") || token.equals("&") || token.equals("(") || token.equals(")") || token.equals("!");
}

View File

@ -50,6 +50,7 @@ use-inherit-command: "Use the 'parent add' command instead of specifying the nod
verbose-invalid-filter: "&cInvalid verbose filter: &f{0}"
verbose-on: "&bVerbose checking output set to &aTRUE &bfor all permissions."
verbose-on_query: "&bVerbose checking output set to &aTRUE &bfor permissions matching the following filters: &f{0}"
verbose-off: "&bVerbose checking output set to &cFALSE&b."