From e2eada249816c3d63f4c0435195e1ead849b5cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Mon, 14 Sep 2020 17:13:17 +0200 Subject: [PATCH] Fix disabling `allow_leading_wildcard` (#62300) (#62318) 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 --- .../index/search/QueryStringQueryParser.java | 4 +++- .../index/query/QueryStringQueryBuilderTests.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java index 95f6bae7a45..117e238be2a 100644 --- a/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java @@ -681,7 +681,9 @@ public class QueryStringQueryParser extends XQueryParser { setAnalyzer(forceAnalyzer); return super.getWildcardQuery(currentFieldType.name(), termStr); } - + if (getAllowLeadingWildcard() == false && (termStr.startsWith("*") || termStr.startsWith("?"))) { + throw new ParseException("'*' or '?' not allowed as first character in WildcardQuery"); + } return currentFieldType.wildcardQuery(termStr, getMultiTermRewriteMethod(), context); } catch (RuntimeException e) { if (lenient) { diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java index f7993ae7e16..3e3e0f874be 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java @@ -529,6 +529,20 @@ public class QueryStringQueryBuilderTests extends AbstractQueryTestCase queryStringQuery("*test").field("mapped_string").allowLeadingWildcard(false).toQuery(createShardContext()) + ); + assertEquals("Failed to parse query [*test]", ex.getMessage()); + assertEquals("Cannot parse '*test': '*' or '?' not allowed as first character in WildcardQuery", ex.getCause().getMessage()); + } + public void testToQueryDisMaxQuery() throws Exception { Query query = queryStringQuery("test").field(TEXT_FIELD_NAME, 2.2f) .field(KEYWORD_FIELD_NAME)