Skip to content

Commit 953f5cf

Browse files
author
Christoph Büscher
authored
Fix disabling allow_leading_wildcard (#62300) (#62319)
Disabling the `query_string` queries `allow_leading_wildcard` parameter didn't work after a change probably introduced in #60959 because the various field types `wildcardQuery` don't check the leading characters like QueryParserBase#getWildcardQuery does. This PR adds the missing check also before calling the field types wildcard generating method. Closes #62267
1 parent 76916f7 commit 953f5cf

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,9 @@ private Query getWildcardQuerySingle(String field, String termStr) throws ParseE
681681
setAnalyzer(forceAnalyzer);
682682
return super.getWildcardQuery(currentFieldType.name(), termStr);
683683
}
684-
684+
if (getAllowLeadingWildcard() == false && (termStr.startsWith("*") || termStr.startsWith("?"))) {
685+
throw new ParseException("'*' or '?' not allowed as first character in WildcardQuery");
686+
}
685687
return currentFieldType.wildcardQuery(termStr, getMultiTermRewriteMethod(), context);
686688
} catch (RuntimeException e) {
687689
if (lenient) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,20 @@ public void testToQueryFieldsWildcard() throws Exception {
529529
equalTo(new Term(KEYWORD_FIELD_NAME, "test")));
530530
}
531531

532+
/**
533+
* Test that dissalowing leading wildcards causes exception
534+
*/
535+
public void testAllowLeadingWildcard() throws Exception {
536+
Query query = queryStringQuery("*test").field("mapped_string").allowLeadingWildcard(true).toQuery(createShardContext());
537+
assertThat(query, instanceOf(WildcardQuery.class));
538+
QueryShardException ex = expectThrows(
539+
QueryShardException.class,
540+
() -> queryStringQuery("*test").field("mapped_string").allowLeadingWildcard(false).toQuery(createShardContext())
541+
);
542+
assertEquals("Failed to parse query [*test]", ex.getMessage());
543+
assertEquals("Cannot parse '*test': '*' or '?' not allowed as first character in WildcardQuery", ex.getCause().getMessage());
544+
}
545+
532546
public void testToQueryDisMaxQuery() throws Exception {
533547
Query query = queryStringQuery("test").field(TEXT_FIELD_NAME, 2.2f)
534548
.field(KEYWORD_FIELD_NAME)

0 commit comments

Comments
 (0)