diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerExternal.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerExternal.java index 32ab8f43c9e..5edd4e09c4e 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerExternal.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerExternal.java @@ -448,7 +448,12 @@ class AnalyzerExternal { throw new IllegalArgumentException(AnalyzerUtility.error(ctx) + "Unknown variable [" + id + "]."); } + // special cases: reserved words if ("_score".equals(id)) { + // read-only: don't allow stores + if (parentemd.storeExpr != null) { + throw new IllegalArgumentException(AnalyzerUtility.error(ctx) + "Variable [" + id + "] is read-only."); + } metadata.scoreValueUsed = true; } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ReservedWordTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ReservedWordTests.java new file mode 100644 index 00000000000..c68a158207b --- /dev/null +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ReservedWordTests.java @@ -0,0 +1,40 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.painless; + +/** Tests for special reserved words such as _score */ +public class ReservedWordTests extends ScriptTestCase { + + /** check that we can't declare a variable of _score, its really reserved! */ + public void testScoreVar() { + IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> { + exec("int _score = 5; return _score;"); + }); + assertTrue(expected.getMessage().contains("Variable name [_score] already defined")); + } + + /** check that we can't write to _score, its read-only! */ + public void testScoreStore() { + IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> { + exec("_score = 5; return _score;"); + }); + assertTrue(expected.getMessage().contains("Variable [_score] is read-only")); + } +}