Throw exception if an additional field was placed inside the "query" body

Currently the parser accepts queries like

```
"query" : {
     "any_query": {
         ...
     },
     "any_field_name":...
}
```

The "any_field_name" is silently ignored. However, this also causes the parser
not to move to the next closing bracket which in turn can lead to additional query
paremters being ignored such as "fields", "highlight",...
This was the case in issue #4895

closes issue #4895
This commit is contained in:
Britta Weber 2014-01-27 14:51:43 +01:00
parent c7bb784b08
commit 8076a31ac1
2 changed files with 46 additions and 2 deletions

View File

@ -0,0 +1,36 @@
---
setup:
- do:
indices.create:
index: test
- do:
index:
index: test
type: test
id: 1
body:
user : foo
amount : 35
data : some
- do:
indices.refresh:
index: [test]
---
"Test with _local preference placed in query body - should fail":
- do:
catch: request
search:
index: test
type: test
body:
query:
term:
data: some
preference: _local
fields: [user,amount]

View File

@ -617,6 +617,10 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
try { try {
parser = XContentFactory.xContent(source).createParser(source); parser = XContentFactory.xContent(source).createParser(source);
XContentParser.Token token; XContentParser.Token token;
token = parser.nextToken();
if (token != XContentParser.Token.START_OBJECT) {
throw new ElasticsearchParseException("Expected START_OBJECT but got " + token.name() + " " + parser.currentName());
}
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
String fieldName = parser.currentName(); String fieldName = parser.currentName();
@ -626,8 +630,12 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
throw new SearchParseException(context, "No parser for element [" + fieldName + "]"); throw new SearchParseException(context, "No parser for element [" + fieldName + "]");
} }
element.parse(parser, context); element.parse(parser, context);
} else if (token == null) { } else {
break; if (token == null) {
throw new ElasticsearchParseException("End of query source reached but query is not complete.");
} else {
throw new ElasticsearchParseException("Expected field name but got " + token.name() + " \"" + parser.currentName() + "\"");
}
} }
} }
} catch (Throwable e) { } catch (Throwable e) {