Browse Source

Merge pull request #8733 from thingsboard/fix_bug_switch_tbel

[FIX_BUG][3.5.1] switch: add validation to tbel
pull/8749/head
Andrew Shvayka 3 years ago
committed by GitHub
parent
commit
405548d44a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java
  2. 10
      ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js

8
common/script/script-api/src/main/java/org/thingsboard/script/api/tbel/DefaultTbelInvokeService.java

@ -25,6 +25,7 @@ import com.google.common.util.concurrent.MoreExecutors;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.mvel2.CompileException;
import org.mvel2.ExecutionContext;
import org.mvel2.MVEL;
import org.mvel2.ParserContext;
@ -65,6 +66,8 @@ public class DefaultTbelInvokeService extends AbstractScriptInvokeService implem
protected final Map<UUID, String> scriptIdToHash = new ConcurrentHashMap<>();
protected final Map<String, TbelScript> scriptMap = new ConcurrentHashMap<>();
private final String tbelSwitch = "switch";
private final String tbelSwitchErrorMsg = "TBEL does not support the 'switch'.";
protected Cache<String, Serializable> compiledScriptsCache;
private SandboxedParserConfiguration parserConfig;
@ -180,6 +183,11 @@ public class DefaultTbelInvokeService extends AbstractScriptInvokeService implem
lock.unlock();
}
return scriptId;
} catch (CompileException ce) {
if ( ce.getExpr() != null && new String(ce.getExpr()).contains(tbelSwitch)) {
ce = new CompileException(tbelSwitchErrorMsg, ce.getExpr(), ce.getCursor(), ce.getCause());
}
throw new TbScriptException(scriptId, TbScriptException.ErrorCode.COMPILATION, scriptBody, ce);
} catch (Exception e) {
throw new TbScriptException(scriptId, TbScriptException.ErrorCode.COMPILATION, scriptBody, e);
}

10
ui-ngx/src/app/shared/models/ace/tbel/worker-tbel.js

@ -5229,6 +5229,10 @@ var JSHINT = (function() {
var a = [], p;
while (!state.tokens.next.reach && state.tokens.next.id !== "(end)") {
if (state.tokens.next.value === "switch") {
warning("E067", state.tokens.next, "switch");
break;
}
if (state.tokens.next.id === ";") {
p = peek();
@ -9215,7 +9219,7 @@ var JSHINT = (function() {
statements(0);
}
if (state.tokens.next.id !== "(end)") {
if (state.tokens.next.id !== "(end)"&& state.tokens.next.value !== "switch") {
quit("E041", state.tokens.curr);
}
@ -11266,7 +11270,8 @@ var errors = {
E064: "Super call may only be used within class method bodies.",
E065: "Functions defined outside of strict mode with non-simple parameter lists may not " +
"enable strict mode.",
E066: "Asynchronous iteration is only available with for-of loops."
E066: "Asynchronous iteration is only available with for-of loops.",
E067: "Expected an 'if/else' and instead saw 'switch'. TBEL does not support the 'switch' statement."
};
var warnings = {
@ -11364,7 +11369,6 @@ var warnings = {
W086: "Expected a 'break' statement before '{a}'.",
W087: "Forgotten 'debugger' statement?",
W088: "Creating global 'for' variable. Should be 'for (var {a} ...'.",
// W288: "The syntax of function '{a}' is specific to TBEL, and is not supported by JS executor.",
W089: "The body of a for in should be wrapped in an if statement to filter " +
"unwanted properties from the prototype.",
W090: "'{a}' is not a statement label.",

Loading…
Cancel
Save