Skip to content

Commit fa99721

Browse files
committed
Drop stored scripts with the old style-id (#48078)
This PR fixes (#47593). Stored scripts with the old-style id of lang#id are saved through the upgrade process but are no longer accessible in recent versions. This fix will drop those scripts altogether since there is no way for a user to access them.
1 parent ac1ed6e commit fa99721

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

server/src/main/java/org/elasticsearch/script/ScriptMetaData.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,12 @@ public static ScriptMetaData fromXContent(XContentParser parser) throws IOExcept
249249
source = StoredScriptSource.fromXContent(parser, true);
250250

251251
if (exists == null) {
252-
scripts.put(id, source);
252+
// due to a bug (https://github.com/elastic/elasticsearch/issues/47593)
253+
// scripts may have been retained during upgrade that include the old-style
254+
// id of lang#id; these scripts are unreachable after 7.0, so they are dropped
255+
if (id.contains("#") == false) {
256+
scripts.put(id, source);
257+
}
253258
} else if (exists.getLang().equals(source.getLang()) == false) {
254259
throw new IllegalArgumentException("illegal stored script, id [" + id + "] used for multiple scripts with " +
255260
"different languages [" + exists.getLang() + "] and [" + source.getLang() + "]; scripts using the old " +

server/src/test/java/org/elasticsearch/script/ScriptMetaDataTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.io.IOException;
3434
import java.io.UncheckedIOException;
35+
import java.util.Collections;
3536

3637
public class ScriptMetaDataTests extends AbstractSerializingTestCase<ScriptMetaData> {
3738

@@ -168,6 +169,42 @@ public void testLoadEmptyScripts() throws IOException {
168169
assertWarnings("empty templates should no longer be used");
169170
}
170171

172+
public void testOldStyleDropped() throws IOException {
173+
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
174+
175+
builder.startObject();
176+
{
177+
builder.startObject("painless#test");
178+
{
179+
builder.field("lang", "painless");
180+
builder.field("source", "code");
181+
}
182+
builder.endObject();
183+
builder.startObject("lang#test");
184+
{
185+
builder.field("lang", "test");
186+
builder.field("source", "code");
187+
}
188+
builder.endObject();
189+
builder.startObject("test");
190+
{
191+
builder.field("lang", "painless");
192+
builder.field("source", "code");
193+
}
194+
builder.endObject();
195+
}
196+
builder.endObject();
197+
198+
XContentParser parser = XContentType.JSON.xContent()
199+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
200+
BytesReference.bytes(builder).streamInput());
201+
ScriptMetaData smd = ScriptMetaData.fromXContent(parser);
202+
assertNull(smd.getStoredScript("painless#test"));
203+
assertNull(smd.getStoredScript("lang#test"));
204+
assertEquals(new StoredScriptSource("painless", "code", Collections.emptyMap()), smd.getStoredScript("test"));
205+
assertEquals(1, smd.getStoredScripts().size());
206+
}
207+
171208
@Override
172209
protected boolean enableWarningsCheck() {
173210
return true;

0 commit comments

Comments
 (0)