Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/8.x' into backport_117917
Browse files Browse the repository at this point in the history
  • Loading branch information
jimczi committed Dec 4, 2024
2 parents 18d4078 + 570a8cb commit 4528d9b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/117898.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 117898
summary: Limit size of query
area: ES|QL
type: bug
issues: []
4 changes: 2 additions & 2 deletions docs/reference/search/retriever.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -765,11 +765,11 @@ clauses in a <<query-dsl-bool-query, boolean query>>.
[[retriever-restrictions]]
==== Restrictions on search parameters when specifying a retriever

When a retriever is specified as part of a search, the following elements are not allowed at the top-level.
Instead they are only allowed as elements of specific retrievers:
When a retriever is specified as part of a search, the following elements are not allowed at the top-level:

* <<request-body-search-query, `query`>>
* <<search-api-knn, `knn`>>
* <<search-after, `search_after`>>
* <<request-body-search-terminate-after, `terminate_after`>>
* <<search-sort-param, `sort`>>
* <<rescore, `rescore`>>
3 changes: 0 additions & 3 deletions muted-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,3 @@ tests:
issue: https://github.com/elastic/elasticsearch/issues/117805
- class: org.elasticsearch.xpack.security.authc.ldap.UserAttributeGroupsResolverTests
issue: https://github.com/elastic/elasticsearch/issues/116537
- class: org.elasticsearch.xpack.core.ml.search.SparseVectorQueryBuilderTests
method: testToQuery
issue: https://github.com/elastic/elasticsearch/issues/117998
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ private void assertCircuitBreaks(ThrowingRunnable r) throws IOException {
);
}

private void assertParseFailure(ThrowingRunnable r) throws IOException {
ResponseException e = expectThrows(ResponseException.class, r);
Map<?, ?> map = responseAsMap(e.getResponse());
logger.info("expected parse failure {}", map);
assertMap(map, matchesMap().entry("status", 400).entry("error", matchesMap().extraOk().entry("type", "parsing_exception")));
}

private Response sortByManyLongs(int count) throws IOException {
logger.info("sorting by {} longs", count);
return query(makeSortByManyLongs(count).toString(), null);
Expand Down Expand Up @@ -318,6 +325,13 @@ public void testManyConcatFromRow() throws IOException {
assertManyStrings(resp, strings);
}

/**
* Fails to parse a huge huge query.
*/
public void testHugeHugeManyConcatFromRow() throws IOException {
assertParseFailure(() -> manyConcat("ROW a=9999, b=9999, c=9999, d=9999, e=9999", 50000));
}

/**
* Tests that generate many moderately long strings.
*/
Expand Down Expand Up @@ -378,6 +392,13 @@ public void testManyRepeatFromRow() throws IOException {
assertManyStrings(resp, strings);
}

/**
* Fails to parse a huge huge query.
*/
public void testHugeHugeManyRepeatFromRow() throws IOException {
assertParseFailure(() -> manyRepeat("ROW a = 99", 100000));
}

/**
* Tests that generate many moderately long strings.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ public void testToQuery() throws IOException {

private void testDoToQuery(SparseVectorQueryBuilder queryBuilder, SearchExecutionContext context) throws IOException {
Query query = queryBuilder.doToQuery(context);

// test query builder can randomly have no vectors, which rewrites to a MatchNoneQuery - nothing more to do in this case.
if (query instanceof MatchNoDocsQuery) {
return;
}

assertTrue(query instanceof SparseVectorQueryWrapper);
var sparseQuery = (SparseVectorQueryWrapper) query;
if (queryBuilder.shouldPruneTokens()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public class EsqlParser {

private static final Logger log = LogManager.getLogger(EsqlParser.class);

/**
* Maximum number of characters in an ESQL query. Antlr may parse the entire
* query into tokens to make the choices, buffering the world. There's a lot we
* can do in the grammar to prevent that, but let's be paranoid and assume we'll
* fail at preventing antlr from slurping in the world. Instead, let's make sure
* that the world just isn't that big.
*/
public static final int MAX_LENGTH = 1_000_000;

private EsqlConfig config = new EsqlConfig();

public EsqlConfig config() {
Expand Down Expand Up @@ -60,8 +69,14 @@ private <T> T invokeParser(
Function<EsqlBaseParser, ParserRuleContext> parseFunction,
BiFunction<AstBuilder, ParserRuleContext, T> result
) {
if (query.length() > MAX_LENGTH) {
throw new org.elasticsearch.xpack.esql.core.ParsingException(
"ESQL statement is too large [{} characters > {}]",
query.length(),
MAX_LENGTH
);
}
try {
// new CaseChangingCharStream()
EsqlBaseLexer lexer = new EsqlBaseLexer(CharStreams.fromString(query));

lexer.removeErrorListeners();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public void testInlineCast() throws IOException {
logger.info("Wrote to file: {}", file);
}

public void testTooBigQuery() {
StringBuilder query = new StringBuilder("FROM foo | EVAL a = a");
while (query.length() < EsqlParser.MAX_LENGTH) {
query.append(", a = CONCAT(a, a)");
}
assertEquals("-1:0: ESQL statement is too large [1000011 characters > 1000000]", error(query.toString()));
}

private String functionName(EsqlFunctionRegistry registry, Expression functionCall) {
for (FunctionDefinition def : registry.listFunctions()) {
if (functionCall.getClass().equals(def.clazz())) {
Expand Down

0 comments on commit 4528d9b

Please sign in to comment.