From f488c019c19c93b18b1a8fdc8aaf8a3cdc65aa61 Mon Sep 17 00:00:00 2001 From: Jack Conradson Date: Wed, 21 Aug 2019 11:16:08 -0700 Subject: [PATCH] Move regex error to node --- .../java/org/elasticsearch/painless/antlr/Walker.java | 5 ----- .../java/org/elasticsearch/painless/node/ERegex.java | 10 +++++++++- .../elasticsearch/painless/WhenThingsGoWrongTests.java | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/Walker.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/Walker.java index 1567856044342..9d550ce5f5dd9 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/Walker.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/Walker.java @@ -817,11 +817,6 @@ public ANode visitString(StringContext ctx) { @Override public ANode visitRegex(RegexContext ctx) { - if (false == settings.areRegexesEnabled()) { - throw location(ctx).createError(new IllegalStateException("Regexes are disabled. Set [script.painless.regex.enabled] to [true] " - + "in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep " - + "recursion and long loops.")); - } String text = ctx.REGEX().getText(); int lastSlash = text.lastIndexOf('/'); String pattern = text.substring(1, lastSlash); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ERegex.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ERegex.java index 356c1958c6e25..106549c1c1373 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ERegex.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ERegex.java @@ -40,6 +40,8 @@ public final class ERegex extends AExpression { private final int flags; private Constant constant; + private CompilerSettings settings; + public ERegex(Location location, String pattern, String flagsString) { super(location); @@ -56,7 +58,7 @@ public ERegex(Location location, String pattern, String flagsString) { @Override void storeSettings(CompilerSettings settings) { - // do nothing + this.settings = settings; } @Override @@ -66,6 +68,12 @@ void extractVariables(Set variables) { @Override void analyze(Locals locals) { + if (false == settings.areRegexesEnabled()) { + throw createError(new IllegalStateException("Regexes are disabled. Set [script.painless.regex.enabled] to [true] " + + "in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep " + + "recursion and long loops.")); + } + if (!read) { throw createError(new IllegalArgumentException("Regex constant may only be read [" + pattern + "].")); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java index d1db6606c86aa..1dac5ae25735f 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java @@ -262,7 +262,7 @@ public void testStackOverflowError() { } public void testRegexDisabledByDefault() { - IllegalStateException e = expectThrows(IllegalStateException.class, () -> exec("return 'foo' ==~ /foo/")); + IllegalStateException e = expectScriptThrows(IllegalStateException.class, () -> exec("return 'foo' ==~ /foo/")); assertEquals("Regexes are disabled. Set [script.painless.regex.enabled] to [true] in elasticsearch.yaml to allow them. " + "Be careful though, regexes break out of Painless's protection against deep recursion and long loops.", e.getMessage()); }