Skip to content

Commit 815ab42

Browse files
committed
Reject long regex in query_string (#31136)
This change applies the existing `index.max_regex_length` to regex queries produced by the `query_string` query. Relates #28344
1 parent 2d57604 commit 815ab42

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/search/30_limits.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ setup:
107107
---
108108
"Regexp length limit":
109109
- skip:
110-
version: " - 6.99.99"
111-
reason: "The regex length limit was introduced in 7.0.0"
110+
version: " - 6.3.99"
111+
reason: "The regex length limit was introduced in 6.4.0"
112112

113113
- do:
114114
catch: /The length of regex \[1110\] used in the Regexp Query request has exceeded the allowed maximum of \[1000\]\. This maximum can be set by changing the \[index.max_regex_length\] index level setting\./
@@ -131,3 +131,25 @@ setup:
131131
]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[\\t])*))*\\>(?:(?:\\r\\n)?[ \\t])*)(?:,\\s*( |
132132
\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[\\t]))*\"(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:( |
133133
\\[\"()<>@,;:\\\\\".\\[\\]]))|\"(?:[^\\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[\\t]))*\"(?:(?:\\r\\n)?[\\t"
134+
135+
- do:
136+
catch: /The length of regex \[1110\]/
137+
search:
138+
index: test_1
139+
body:
140+
query:
141+
query_string:
142+
query: "/^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[\\t])*)(?:\\.(?:(?:\\r\\n)?[\\t])*(?:[^()<>@,;:\\\\\" |
143+
.\\[\\]\\000-\\031]+(?:(?:(?:\\r\\n)?[\\t])+|\\Z|(?=[\\[\"()<>@,;:\\\\\".\\[\\]]))|\\[([^\\[\\ |
144+
]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)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\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[\\t]))*\"(?:(?:\\r\\n)?[\\t/"

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.elasticsearch.common.unit.Fuzziness;
4848
import org.elasticsearch.core.internal.io.IOUtils;
4949
import org.elasticsearch.index.mapper.AllFieldMapper;
50+
import org.elasticsearch.index.IndexSettings;
5051
import org.elasticsearch.index.mapper.FieldNamesFieldMapper;
5152
import org.elasticsearch.index.mapper.MappedFieldType;
5253
import org.elasticsearch.index.mapper.MapperService;
@@ -675,6 +676,13 @@ private Query getWildcardQuerySingle(String field, String termStr) throws ParseE
675676

676677
@Override
677678
protected Query getRegexpQuery(String field, String termStr) throws ParseException {
679+
final int maxAllowedRegexLength = context.getIndexSettings().getMaxRegexLength();
680+
if (termStr.length() > maxAllowedRegexLength) {
681+
throw new IllegalArgumentException(
682+
"The length of regex [" + termStr.length() + "] used in the [query_string] has exceeded " +
683+
"the allowed maximum of [" + maxAllowedRegexLength + "]. This maximum can be set by changing the [" +
684+
IndexSettings.MAX_REGEX_LENGTH_SETTING.getKey() + "] index level setting.");
685+
}
678686
Map<String, Float> fields = extractMultiFields(field, false);
679687
if (fields.isEmpty()) {
680688
return newUnmappedFieldQuery(termStr);

0 commit comments

Comments
 (0)