Skip to content

Commit b4b78dc

Browse files
CORE: Validate Type for String Settings
* Special handling for `String` in the generic Setting class because we parse raw values String values for all types: * If we parse out a `String` setting validate that it came from a scalar raw value * Closes elastic#33135
1 parent 79cd638 commit b4b78dc

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

server/src/main/java/org/elasticsearch/common/settings/Setting.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.common.settings;
2121

22+
import java.util.Collection;
2223
import org.apache.logging.log4j.Logger;
2324
import org.elasticsearch.ElasticsearchException;
2425
import org.elasticsearch.ElasticsearchParseException;
@@ -385,6 +386,16 @@ private T get(Settings settings, boolean validate) {
385386
try {
386387
T parsed = parser.apply(value);
387388
if (validate) {
389+
if (parsed instanceof String) {
390+
Class<?> rawValueClass = settings.getRawType(getKey());
391+
if (rawValueClass != null
392+
&& (Map.class.isAssignableFrom(rawValueClass) || Collection.class.isAssignableFrom(rawValueClass))
393+
) {
394+
throw new IllegalArgumentException(
395+
"Failed to parse [" + value + "] for string setting [" + getKey() + "] because it is not scalar."
396+
);
397+
}
398+
}
388399
final Iterator<Setting<T>> it = validator.settings();
389400
final Map<Setting<T>, T> map;
390401
if (it.hasNext()) {

server/src/main/java/org/elasticsearch/common/settings/Settings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ public String get(String setting, String defaultValue) {
245245
return retVal == null ? defaultValue : retVal;
246246
}
247247

248+
Class<?> getRawType(String setting) {
249+
Object value = settings.get(setting);
250+
return value == null ? null : value.getClass();
251+
}
252+
248253
/**
249254
* Returns the setting value (as float) associated with the setting key. If it does not exists,
250255
* returns the default value provided.

server/src/test/java/org/elasticsearch/common/settings/SettingTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ public void testSimpleUpdate() {
180180
}
181181
}
182182

183+
public void testValidateStringSetting() {
184+
Settings settings = Settings.builder().putList("foo.bar", Arrays.asList("bla-a", "bla-b")).build();
185+
Setting<String> stringSetting = Setting.simpleString("foo.bar", Property.NodeScope);
186+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> stringSetting.get(settings));
187+
assertEquals("Failed to parse [[bla-a, bla-b]] for string setting [foo.bar] because it is not scalar.", e.getMessage());
188+
}
189+
183190
private static final Setting<String> FOO_BAR_SETTING = new Setting<>(
184191
"foo.bar",
185192
"foobar",

0 commit comments

Comments
 (0)