Browse Source

Fix Nashorn Global PropertyMap leak on JS script release

pull/15964/head
Nikita Mazurenko 4 months ago
parent
commit
43851cd3f0
  1. 85
      application/src/test/java/org/thingsboard/server/service/script/NashornJsInvokeServiceTest.java
  2. 16
      common/script/script-api/src/main/java/org/thingsboard/script/api/js/NashornJsInvokeService.java

85
application/src/test/java/org/thingsboard/server/service/script/NashornJsInvokeServiceTest.java

@ -17,13 +17,17 @@ package org.thingsboard.server.service.script;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import delight.nashornsandbox.NashornSandbox;
import delight.nashornsandbox.NashornSandboxes;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.util.ReflectionTestUtils;
import org.thingsboard.common.util.TbStopWatch;
import org.thingsboard.script.api.RuleNodeScriptFactory;
import org.thingsboard.script.api.ScriptType;
import org.thingsboard.script.api.TbScriptException;
import org.thingsboard.script.api.js.NashornJsInvokeService;
@ -31,6 +35,7 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.controller.AbstractControllerTest;
import org.thingsboard.server.dao.service.DaoSqlTest;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.ArrayList;
import java.util.List;
@ -222,6 +227,86 @@ class NashornJsInvokeServiceTest extends AbstractControllerTest {
});
}
@Test
void whenScriptIsReleased_thenGlobalPropertyIsRemoved() throws Exception {
UUID scriptId = evalScript("return msg;");
String functionName = "invokeInternal_" + scriptId.toString().replace('-', '_');
assertThat(evalInEngine("this.hasOwnProperty('" + functionName + "')")).hasToString("true");
invokeService.release(scriptId).get();
// the property must be deleted, not just set to undefined: an abandoned binding keeps
assertThat(evalInEngine("this.hasOwnProperty('" + functionName + "')")).hasToString("false");
}
@Test
void givenIifeWrappedEval_whenInvoking_thenResultIsUnchanged() throws Exception {
UUID scriptId = evalScript("return {doubled: msg.temperature * 2};");
assertThat(invokeScript(scriptId, "{\"temperature\":21}")).contains("\"doubled\":42");
}
@Test
void givenIifeWrappedEval_thenImplicitGlobalStateIsStillSharedBetweenScripts() throws Exception {
UUID writer = evalScript("compatSharedState = 777; return msg;");
UUID reader = evalScript("return {seen: (typeof compatSharedState === 'undefined') ? null : compatSharedState};");
invokeScript(writer, "{}");
assertThat(invokeScript(reader, "{}")).contains("\"seen\":777");
}
@Test
void givenIifeWrappedEval_thenCompilationErrorUnchanged() {
String badScript = "var a = 1;\nvar b = 2;\nreturn msg.temperature?.value;";
String legacyScript = RuleNodeScriptFactory.generateRuleNodeScript("invokeInternal_legacy", badScript,
"msg", "metadata", "msgType");
NashornSandbox legacySandbox = NashornSandboxes.create();
legacySandbox.allowNoBraces(false);
legacySandbox.allowLoadFunctions(true);
legacySandbox.setMaxPreparedStatements(30);
int legacyErrorLine = -1;
try {
legacySandbox.eval(legacyScript);
} catch (ScriptException e) {
legacyErrorLine = e.getLineNumber();
}
assertThat(legacyErrorLine).isGreaterThan(0);
final int expectedLine = legacyErrorLine;
assertThatThrownBy(() -> evalScript(badScript))
.isInstanceOf(ExecutionException.class)
.cause()
.isInstanceOf(TbScriptException.class)
.asInstanceOf(type(TbScriptException.class))
.satisfies(ex -> {
assertThat(ex.getErrorCode()).isEqualTo(TbScriptException.ErrorCode.COMPILATION);
assertThat(ex.getBody()).contains(badScript);
assertThat(ex.getCause()).isInstanceOf(ScriptException.class);
// The IIFE wrapper shifts sandbox-reported line numbers by a constant +2
// (the beautifier splits the wrapper prefix onto its own lines). Reported
// lines never matched the user's source anyway (the sandbox instrumentation
// already offsets them), so only the small constant shift is tolerated here.
assertThat(((ScriptException) ex.getCause()).getLineNumber()).isBetween(expectedLine, expectedLine + 2);
});
}
@Test
void whenInvokingAfterRelease_thenFailsWithNoCompiledScriptFound() throws Exception {
UUID scriptId = evalScript("return msg;");
invokeScript(scriptId, "{}");
invokeService.release(scriptId).get();
assertThatThrownBy(() -> invokeScript(scriptId, "{}"))
.isInstanceOf(ExecutionException.class)
.hasMessageContaining("No compiled script found");
}
private Object evalInEngine(String expression) throws Exception {
Object sandbox = ReflectionTestUtils.getField(invokeService, "sandbox");
if (sandbox != null) {
return ((NashornSandbox) sandbox).eval(expression);
}
return ((ScriptEngine) ReflectionTestUtils.getField(invokeService, "engine")).eval(expression);
}
private void assertThatScriptIsBlocked(UUID scriptId) {
assertThatThrownBy(() -> {
invokeScript(scriptId, "{}");

16
common/script/script-api/src/main/java/org/thingsboard/script/api/js/NashornJsInvokeService.java

@ -140,14 +140,21 @@ public class NashornJsInvokeService extends AbstractJsInvokeService {
@Override
protected ListenableFuture<UUID> doEval(UUID scriptId, JsScriptInfo scriptInfo, String jsScript) {
// A top-level function declaration creates a non-configurable property on the Nashorn Global
// that can never be deleted, so every eval/release cycle would grow the Global's PropertyMap
// shape history forever. Declaring the function inside an IIFE and assigning it to a global
// property keeps the property configurable, allowing doRelease() to actually delete it.
// The prefix stays on the first line so that script line numbers in errors are unchanged.
String wrappedScript = "this['" + scriptInfo.getFunctionName() + "'] = (function() { " + jsScript
+ "\nreturn " + scriptInfo.getFunctionName() + ";\n})();";
return jsExecutor.submit(() -> {
try {
evalLock.lock();
try {
if (useJsSandbox) {
sandbox.eval(jsScript);
sandbox.eval(wrappedScript);
} else {
engine.eval(jsScript);
engine.eval(wrappedScript);
}
} finally {
evalLock.unlock();
@ -182,10 +189,11 @@ public class NashornJsInvokeService extends AbstractJsInvokeService {
}
protected void doRelease(UUID scriptId, JsScriptInfo scriptInfo) throws ScriptException {
String deleteScript = "delete this['" + scriptInfo.getFunctionName() + "'];";
if (useJsSandbox) {
sandbox.eval(scriptInfo.getFunctionName() + " = undefined;");
sandbox.eval(deleteScript);
} else {
engine.eval(scriptInfo.getFunctionName() + " = undefined;");
engine.eval(deleteScript);
}
}

Loading…
Cancel
Save