Skip to content

Commit 0c97d1b

Browse files
committed
Search: Validate script query is run with a single script (#29304)
The parsing code for script query currently silently skips by any tokens it does not know about within its parsing loop. The only token it does not catch is an array, which means pasing multiple scripts in via an array will cause the last script to be parsed and one, silently dropping the others. This commit adds validation that arrays are not seen while parsing.
1 parent eff9d63 commit 0c97d1b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ public static ScriptQueryBuilder fromXContent(XContentParser parser) throws IOEx
113113
} else {
114114
throw new ParsingException(parser.getTokenLocation(), "[script] query does not support [" + currentFieldName + "]");
115115
}
116+
} else {
117+
if (token != XContentParser.Token.START_ARRAY) {
118+
throw new AssertionError("Impossible token received: " + token.name());
119+
}
120+
throw new ParsingException(parser.getTokenLocation(),
121+
"[script] query does not support an array of scripts. Use a bool query with a clause per script instead.");
116122
}
117123
}
118124

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.elasticsearch.index.query;
2121

2222
import org.apache.lucene.search.Query;
23-
import org.elasticsearch.index.query.ScriptQueryBuilder.ScriptQuery;
23+
import org.elasticsearch.common.ParsingException;
2424
import org.elasticsearch.script.MockScriptEngine;
2525
import org.elasticsearch.script.Script;
2626
import org.elasticsearch.script.ScriptType;
@@ -32,6 +32,7 @@
3232
import java.util.Map;
3333
import java.util.Set;
3434

35+
import static org.hamcrest.Matchers.containsString;
3536
import static org.hamcrest.Matchers.instanceOf;
3637

3738
public class ScriptQueryBuilderTests extends AbstractQueryTestCase<ScriptQueryBuilder> {
@@ -89,6 +90,25 @@ public void testFromJson() throws IOException {
8990
assertEquals(json, "5", parsed.script().getIdOrCode());
9091
}
9192

93+
public void testArrayOfScriptsException() {
94+
String json =
95+
"{\n" +
96+
" \"script\" : {\n" +
97+
" \"script\" : [ {\n" +
98+
" \"source\" : \"5\",\n" +
99+
" \"lang\" : \"mockscript\"\n" +
100+
" },\n" +
101+
" {\n" +
102+
" \"source\" : \"6\",\n" +
103+
" \"lang\" : \"mockscript\"\n" +
104+
" }\n ]" +
105+
" }\n" +
106+
"}";
107+
108+
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
109+
assertThat(e.getMessage(), containsString("does not support an array of scripts"));
110+
}
111+
92112
@Override
93113
protected Set<String> getObjectsHoldingArbitraryContent() {
94114
//script_score.script.params can contain arbitrary parameters. no error is expected when

0 commit comments

Comments
 (0)