javadelight/delight-nashorn-sandbox

Error on evaluating code starting with "do"

lgomezm opened this issue · 2 comments

When a sandbox instance is created and it disallows no-braces, it throws an exception when the JS code to evaluate starts with any of { function, for, while, do }. Basically, the exception's stacktrace says:

Exception in thread "main" delight.nashornsandbox.exceptions.BracesException: No block braces after function|for|while|do
	at delight.nashornsandbox.internal.JsSanitizer.checkBraces(JsSanitizer.java:177)
	at delight.nashornsandbox.internal.JsSanitizer.secureJs(JsSanitizer.java:220)
	at delight.nashornsandbox.internal.NashornSandboxImpl.eval(NashornSandboxImpl.java:129)
	at delight.nashornsandbox.internal.NashornSandboxImpl.eval(NashornSandboxImpl.java:104)
        ...

It looks like the block braces pattern matching makes it fail whenever the text starts with do. This code can reproduce the issue:

public static void main(String[] args) throws ScriptException {
        NashornSandbox sandbox = NashornSandboxes.create();
        sandbox.setMaxCPUTime(100);
        sandbox.setMaxMemory(1000 * 1000);
        sandbox.allowNoBraces(false);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        sandbox.setExecutor(executor);
        Boolean done = (Boolean) sandbox.eval("done = false;");
        System.out.println(done);
}
mxro commented

Thank you for reporting this issue!

It is fixed in version 0.1.9 which should be available in Maven Central shortly.

Please let me know if this fixes it for you or if there are any other issues!

I can confirm it now works. Thanks, Max.