From ed223f0e4e3874587b165ad637adc453e8919305 Mon Sep 17 00:00:00 2001
From: Luck
Date: Mon, 15 Jan 2018 20:06:20 +0000
Subject: [PATCH] Don't allow empty context keys/values (#688)
---
.../api/context/AbstractContextSet.java | 26 +++++++++++++++++--
.../luckperms/api/context/ContextSet.java | 4 ++-
.../common/commands/utils/ArgumentUtils.java | 4 +--
3 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/api/src/main/java/me/lucko/luckperms/api/context/AbstractContextSet.java b/api/src/main/java/me/lucko/luckperms/api/context/AbstractContextSet.java
index b9c0334c2..03074a359 100644
--- a/api/src/main/java/me/lucko/luckperms/api/context/AbstractContextSet.java
+++ b/api/src/main/java/me/lucko/luckperms/api/context/AbstractContextSet.java
@@ -122,11 +122,33 @@ abstract class AbstractContextSet implements ContextSet {
}
static String sanitizeKey(String key) {
- return Objects.requireNonNull(key, "key is null").toLowerCase().intern();
+ Objects.requireNonNull(key, "key is null");
+ if (stringIsEmpty(key)) {
+ throw new IllegalArgumentException("key is (effectively) empty");
+ }
+
+ return key.toLowerCase().intern();
}
static String sanitizeValue(String value) {
- return Objects.requireNonNull(value, "value is null").intern();
+ Objects.requireNonNull(value, "value is null");
+ if (stringIsEmpty(value)) {
+ throw new IllegalArgumentException("value is (effectively) empty");
+ }
+
+ return value.intern();
+ }
+
+ private static boolean stringIsEmpty(String s) {
+ if (s.isEmpty()) {
+ return true;
+ }
+ for (char c : s.toCharArray()) {
+ if (c != ' ') {
+ return false;
+ }
+ }
+ return true;
}
}
diff --git a/api/src/main/java/me/lucko/luckperms/api/context/ContextSet.java b/api/src/main/java/me/lucko/luckperms/api/context/ContextSet.java
index 0ede2d2e1..e4f15fe7a 100644
--- a/api/src/main/java/me/lucko/luckperms/api/context/ContextSet.java
+++ b/api/src/main/java/me/lucko/luckperms/api/context/ContextSet.java
@@ -51,7 +51,9 @@ import javax.annotation.Nonnull;
* {@link String#toLowerCase() lowercase} by all implementations.
* Values however are case-sensitive.
*
- * Context keys and values may not be null.
+ * Context keys and values may not be null or empty. A key/value will be
+ * deemed empty if it's length is zero, or if it consists of only space
+ * characters.
*
* Two default ContextSet implementations are provided.
* {@link MutableContextSet} allows the addition and removal of context keys
diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java b/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java
index 9dc037d5c..5834e7c62 100644
--- a/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java
+++ b/common/src/main/java/me/lucko/luckperms/common/commands/utils/ArgumentUtils.java
@@ -134,12 +134,12 @@ public class ArgumentUtils {
}
String key = pair.substring(0, index);
- if (key.equals("")) {
+ if (key.equals("") || key.trim().isEmpty()) {
continue;
}
String value = pair.substring(index + 1);
- if (value.equals("")) {
+ if (value.equals("") || value.trim().isEmpty()) {
continue;
}