Skip to content

Commit 4c7f6bb

Browse files
committed
Null check field names in QueryStringQuerybuilder
Signed-off-by: Daniel Widdis <[email protected]>
1 parent 99ac67e commit 4c7f6bb

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3333
- Remove package org.opensearch.transport.grpc and replace with org.opensearch.plugin.transport.grpc ([#18031](https://github.com/opensearch-project/OpenSearch/pull/18031))
3434
- Fix the native plugin installation error cause by the pgp public key change ([#18147](https://github.com/opensearch-project/OpenSearch/pull/18147))
3535
- Fix object field exists query ([#17843](https://github.com/opensearch-project/OpenSearch/pull/17843))
36+
- Null check field names in QueryStringQuerybuilder ([#18194](https://github.com/opensearch-project/OpenSearch/pull/18194))
3637

3738
### Security
3839

rest-api-spec/src/main/resources/rest-api-spec/test/search/50_multi_match.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,18 @@
3333
- match: { hits.total.value: 3 }
3434
- match: { hits.hits.0._id: "2" }
3535
- gt: { hits.hits.2._score: 0.0 }
36+
37+
# Test that query with null field fails appropriately
38+
- do:
39+
catch: bad_request
40+
search:
41+
index: test
42+
body:
43+
query:
44+
query_string:
45+
query: "red"
46+
fields: ["color", null, "shape"]
47+
48+
- match: { status: 400 }
49+
- match: { error.type: parsing_exception }
50+
- match: { error.reason: "[query_string] field name in [fields] cannot be null" }

server/src/main/java/org/opensearch/index/query/QueryStringQueryBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,12 @@ public static QueryStringQueryBuilder fromXContent(XContentParser parser) throws
685685
if (FIELDS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
686686
List<String> fields = new ArrayList<>();
687687
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
688+
if (parser.currentToken() == XContentParser.Token.VALUE_NULL) {
689+
throw new ParsingException(
690+
parser.getTokenLocation(),
691+
"[" + QueryStringQueryBuilder.NAME + "] field name in [" + currentFieldName + "] cannot be null"
692+
);
693+
}
688694
fields.add(parser.text());
689695
}
690696
fieldsAndWeights = QueryParserHelper.parseFieldsAndWeights(fields);

server/src/test/java/org/opensearch/index/query/QueryStringQueryBuilderTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import org.opensearch.common.settings.Settings;
7272
import org.opensearch.common.unit.Fuzziness;
7373
import org.opensearch.common.xcontent.json.JsonXContent;
74+
import org.opensearch.core.common.ParsingException;
7475
import org.opensearch.core.xcontent.XContentBuilder;
7576
import org.opensearch.index.mapper.FieldNamesFieldMapper;
7677
import org.opensearch.index.mapper.MapperService;
@@ -1013,6 +1014,18 @@ public void testToQueryWildcardNonExistingFields() throws IOException {
10131014
assertThat(expectedQuery, equalTo(query));
10141015
}
10151016

1017+
public void testNullFieldInFieldsArray() throws IOException {
1018+
String queryAsString = "{\n"
1019+
+ " \"query_string\" : {\n"
1020+
+ " \"query\" : \"test\",\n"
1021+
+ " \"fields\" : [ \"field1\", null, \"field2\" ]\n"
1022+
+ " }\n"
1023+
+ "}";
1024+
1025+
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(queryAsString));
1026+
assertEquals("[query_string] field name in [fields] cannot be null", e.getMessage());
1027+
}
1028+
10161029
public void testToQueryTextParsing() throws IOException {
10171030
{
10181031
QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder("foo bar").field(TEXT_FIELD_NAME).field(KEYWORD_FIELD_NAME);

0 commit comments

Comments
 (0)