Skip to content

Commit 6952f7b

Browse files
olcbeanrjernst
authored andcommitted
Validate top-level keys for create index request (elastic#23755) (elastic#23869)
This commit ensures create index requests do not ignore unknown keys passed to the request. closes elastic#23755
1 parent 5df77a8 commit 6952f7b

File tree

6 files changed

+63
-33
lines changed

6 files changed

+63
-33
lines changed

core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,38 +374,32 @@ public CreateIndexRequest source(BytesReference source, XContentType xContentTyp
374374
*/
375375
@SuppressWarnings("unchecked")
376376
public CreateIndexRequest source(Map<String, ?> source) {
377-
boolean found = false;
378377
for (Map.Entry<String, ?> entry : source.entrySet()) {
379378
String name = entry.getKey();
380379
if (name.equals("settings")) {
381-
found = true;
382380
settings((Map<String, Object>) entry.getValue());
383381
} else if (name.equals("mappings")) {
384-
found = true;
385382
Map<String, Object> mappings = (Map<String, Object>) entry.getValue();
386383
for (Map.Entry<String, Object> entry1 : mappings.entrySet()) {
387384
mapping(entry1.getKey(), (Map<String, Object>) entry1.getValue());
388385
}
389386
} else if (name.equals("aliases")) {
390-
found = true;
391387
aliases((Map<String, Object>) entry.getValue());
392388
} else {
393389
// maybe custom?
394390
IndexMetaData.Custom proto = IndexMetaData.lookupPrototype(name);
395391
if (proto != null) {
396-
found = true;
397392
try {
398393
customs.put(name, proto.fromMap((Map<String, Object>) entry.getValue()));
399394
} catch (IOException e) {
400395
throw new ElasticsearchParseException("failed to parse custom metadata for [{}]", name);
401396
}
397+
} else {
398+
// found a key which is neither custom defined nor one of the supported ones
399+
throw new ElasticsearchParseException("unknown key [{}] for create index", name);
402400
}
403401
}
404402
}
405-
if (!found) {
406-
// the top level are settings, use them
407-
settings(source);
408-
}
409403
return this;
410404
}
411405

core/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestBuilderTests.java

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

2020
package org.elasticsearch.action.admin.indices.create;
2121

22+
import org.elasticsearch.ElasticsearchParseException;
2223
import org.elasticsearch.common.settings.Settings;
2324
import org.elasticsearch.common.xcontent.XContentBuilder;
2425
import org.elasticsearch.common.xcontent.XContentFactory;
@@ -31,6 +32,7 @@
3132
import java.io.ByteArrayOutputStream;
3233
import java.io.IOException;
3334
import java.util.HashMap;
35+
import java.util.Locale;
3436
import java.util.Map;
3537

3638
public class CreateIndexRequestBuilderTests extends ESTestCase {
@@ -58,16 +60,23 @@ public void tearDown() throws Exception {
5860
*/
5961
public void testSetSource() throws IOException {
6062
CreateIndexRequestBuilder builder = new CreateIndexRequestBuilder(this.testClient, CreateIndexAction.INSTANCE);
61-
builder.setSource("{\""+KEY+"\" : \""+VALUE+"\"}", XContentType.JSON);
63+
64+
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class,
65+
() -> {builder.setSource("{\""+KEY+"\" : \""+VALUE+"\"}", XContentType.JSON);});
66+
assertEquals(String.format(Locale.ROOT, "unknown key [%s] for create index", KEY), e.getMessage());
67+
68+
builder.setSource("{\"settings\" : {\""+KEY+"\" : \""+VALUE+"\"}}", XContentType.JSON);
6269
assertEquals(VALUE, builder.request().settings().get(KEY));
6370

64-
XContentBuilder xContent = XContentFactory.jsonBuilder().startObject().field(KEY, VALUE).endObject();
71+
XContentBuilder xContent = XContentFactory.jsonBuilder().startObject()
72+
.startObject("settings").field(KEY, VALUE).endObject().endObject();
6573
xContent.close();
6674
builder.setSource(xContent);
6775
assertEquals(VALUE, builder.request().settings().get(KEY));
6876

6977
ByteArrayOutputStream docOut = new ByteArrayOutputStream();
70-
XContentBuilder doc = XContentFactory.jsonBuilder(docOut).startObject().field(KEY, VALUE).endObject();
78+
XContentBuilder doc = XContentFactory.jsonBuilder(docOut).startObject()
79+
.startObject("settings").field(KEY, VALUE).endObject().endObject();
7180
doc.close();
7281
builder.setSource(docOut.toByteArray(), XContentType.JSON);
7382
assertEquals(VALUE, builder.request().settings().get(KEY));

core/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java

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

2020
package org.elasticsearch.action.admin.indices.create;
2121

22+
import org.elasticsearch.ElasticsearchParseException;
2223
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2324
import org.elasticsearch.common.io.stream.StreamInput;
2425
import org.elasticsearch.common.xcontent.XContentType;
@@ -45,4 +46,27 @@ public void testSerialization() throws IOException {
4546
}
4647
}
4748
}
49+
50+
public void testTopLevelKeys() throws IOException {
51+
String createIndex =
52+
"{\n"
53+
+ " \"FOO_SHOULD_BE_ILLEGAL_HERE\": {\n"
54+
+ " \"BAR_IS_THE_SAME\": 42\n"
55+
+ " },\n"
56+
+ " \"mappings\": {\n"
57+
+ " \"test\": {\n"
58+
+ " \"properties\": {\n"
59+
+ " \"field1\": {\n"
60+
+ " \"type\": \"text\"\n"
61+
+ " }\n"
62+
+ " }\n"
63+
+ " }\n"
64+
+ " }\n"
65+
+ "}";
66+
67+
CreateIndexRequest request = new CreateIndexRequest();
68+
ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class,
69+
() -> {request.source(createIndex, XContentType.JSON);});
70+
assertEquals("unknown key [FOO_SHOULD_BE_ILLEGAL_HERE] for create index", e.getMessage());
71+
}
4872
}

docs/reference/analysis/tokenfilters/compound-word-tokenfilter.asciidoc

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,27 @@ Here is an example:
8686
--------------------------------------------------
8787
PUT /compound_word_example
8888
{
89-
"index": {
90-
"analysis": {
91-
"analyzer": {
92-
"my_analyzer": {
93-
"type": "custom",
94-
"tokenizer": "standard",
95-
"filter": ["dictionary_decompounder", "hyphenation_decompounder"]
96-
}
97-
},
98-
"filter": {
99-
"dictionary_decompounder": {
100-
"type": "dictionary_decompounder",
101-
"word_list": ["one", "two", "three"]
89+
"settings": {
90+
"index": {
91+
"analysis": {
92+
"analyzer": {
93+
"my_analyzer": {
94+
"type": "custom",
95+
"tokenizer": "standard",
96+
"filter": ["dictionary_decompounder", "hyphenation_decompounder"]
97+
}
10298
},
103-
"hyphenation_decompounder": {
104-
"type" : "hyphenation_decompounder",
105-
"word_list_path": "analysis/example_word_list.txt",
106-
"hyphenation_patterns_path": "analysis/hyphenation_patterns.xml",
107-
"max_subword_size": 22
99+
"filter": {
100+
"dictionary_decompounder": {
101+
"type": "dictionary_decompounder",
102+
"word_list": ["one", "two", "three"]
103+
},
104+
"hyphenation_decompounder": {
105+
"type" : "hyphenation_decompounder",
106+
"word_list_path": "analysis/example_word_list.txt",
107+
"hyphenation_patterns_path": "analysis/hyphenation_patterns.xml",
108+
"max_subword_size": 22
109+
}
108110
}
109111
}
110112
}

plugins/store-smb/src/test/resources/rest-api-spec/test/store_smb/15_index_creation.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
indices.create:
44
index: smb-test
55
body:
6-
index:
7-
store.type: smb_mmap_fs
6+
settings:
7+
index:
8+
store.type: smb_mmap_fs
89

910
- do:
1011
index:

rest-api-spec/src/main/resources/rest-api-spec/test/cluster.allocation_explain/10_basic.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
- do:
4848
indices.create:
4949
index: test
50-
body: { "index.number_of_shards": 1, "index.number_of_replicas": 9 }
50+
body: { "settings": { "index.number_of_shards": 1, "index.number_of_replicas": 9 } }
5151

5252
- do:
5353
cluster.state:

0 commit comments

Comments
 (0)