Skip to content

Commit 379e464

Browse files
dbwiddisandrross
andauthored
Null check field names in QueryStringQueryBuilder (#18194)
* Null check field names in QueryStringQuerybuilder Signed-off-by: Daniel Widdis <[email protected]> * Restrict test to run on >=3.1.0 Signed-off-by: Daniel Widdis <[email protected]> --------- Signed-off-by: Daniel Widdis <[email protected]> Signed-off-by: Andrew Ross <[email protected]> Co-authored-by: Andrew Ross <[email protected]>
1 parent 012357a commit 379e464

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3535
- Fix the native plugin installation error cause by the pgp public key change ([#18147](https://github.com/opensearch-project/OpenSearch/pull/18147))
3636
- Fix object field exists query ([#17843](https://github.com/opensearch-project/OpenSearch/pull/17843))
3737
- Use Bad Request status for InputCoercionEcception ([#18161](https://github.com/opensearch-project/OpenSearch/pull/18161))
38+
- Null check field names in QueryStringQueryBuilder ([#18194](https://github.com/opensearch-project/OpenSearch/pull/18194))
3839

3940
### Security
4041

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"Cross fields do not return negative scores":
2-
- skip:
3-
version: " - 2.99.99"
4-
reason: "This fix is in 2.15. Until we do the BWC dance, we need to skip all pre-3.0, though."
1+
setup:
52
- do:
63
index:
74
index: test
@@ -19,6 +16,12 @@
1916
body: { "color" : "orange red yellow purple" }
2017
- do:
2118
indices.refresh: { }
19+
20+
---
21+
"Cross fields do not return negative scores":
22+
- skip:
23+
version: " - 2.14.99"
24+
reason: "This fix is in 2.15.0"
2225
- do:
2326
search:
2427
index: test
@@ -33,3 +36,22 @@
3336
- match: { hits.total.value: 3 }
3437
- match: { hits.hits.0._id: "2" }
3538
- gt: { hits.hits.2._score: 0.0 }
39+
40+
---
41+
"Query string with null field throws 400":
42+
- skip:
43+
version: " - 3.0.99"
44+
reason: "This fix is in 3.1.0"
45+
- do:
46+
catch: bad_request
47+
search:
48+
index: test
49+
body:
50+
query:
51+
query_string:
52+
query: "red"
53+
fields: ["color", null, "shape"]
54+
55+
- match: { status: 400 }
56+
- match: { error.type: parsing_exception }
57+
- 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)