0

I am creating a sandbox program with the nashorn js interpreter, and using a minimalist security manager to restrict what it can do.
The problem is when I call engine.eval(reader) I get a null pointer exception.
I know the reader works because I was able to print the entire file, letter-by-letter. I know this is a permissions issue because if I add AllPermissions then everything works.
So what permissions do I need to run a script in nashorn?

Apparently I messed up my initial tests, it's not just with the file reader, it also happens when I hard-code a string.

Thomas
  • 750
  • 2
  • 6
  • 19

2 Answers2

0

I found the problem. Apparently I need this permission to run nashorn:

java.io.FilePermission, "/usr/lib/jvm/java-8-openjdk/jre/lib/ext/nashorn.jar", "read"

Thomas
  • 750
  • 2
  • 6
  • 19
0

Actually nashorn.jar is an extension jar. With the default security policy, it gets AllPermission. If you replace it with your own policy, I'd think that you need to give AllPermission - nashorn.jar needs to be able to create fresh classloader, access to restricted packages and so on.

A. Sundararajan
  • 4,161
  • 1
  • 11
  • 28
  • There are certain things I don't want Nashorn to have access to such as reflection so AllPermission is not an option. – Thomas Oct 22 '14 at 22:43
  • Nashorn requires ClassLoader creation permission. If a code can create ClassLoader and load classes, it effectively gets AllPermission. If you're worried by scripts getting those, you need not. Compiled script classes get only minimal sandbox permissions unless given more by policy file (by script URL) – A. Sundararajan Oct 24 '14 at 02:15
  • http://stackoverflow.com/questions/20793089/secure-nashorn-js-execution It will have reflection if RuntimePermission("nashorn.JavaReflection") is set. I've got a working nashorn running with very minimal permissions which works. – Thomas Oct 25 '14 at 16:44
  • surprising indeed. With minimal permission, nashorn would not be able to load your compiled JS scripts - as that requires ClassLoader creation permission. You may want to check your policy and Java command line etc. – A. Sundararajan Oct 26 '14 at 15:15
  • Also, I meant AllPermission for nashorn.jar and that does not imply RuntimePermission("nashorn.JavaReflection") for compiled script classes still. Compiled scripts won't still have reflection permission even when nashorn.jar has AllPermission. – A. Sundararajan Oct 26 '14 at 15:16