0

I'm already using Java 8 and it's Nashorn javascript engine. And In my application I access javaScript script files from Java classess for various purposes. And yet it's possible to access Java classes from javaScript codes as well. But since the JavaScripts in my applications can be written by a third party also, I want to restrict them(JS scripts) from accessing the Java modules. (Specifically prohibit some of the Java classes)

I do not want to restrict all the access to Java classes, just looking for a way to restrict or prohibit some specific java classes.

In Nashorn we can do this by using ClassFilters (overiding 'exposeToScripts()' method) as below.

class MyCF implements ClassFilter {
    @Override
    public boolean exposeToScripts(String s) {
      if (s.compareTo("myPackage.MyClass") == 0) return false;
      return true;
    }
}

But how can we use Java Security Manager to do the same thing, or is the way using a ClassFilter (mentioned above) enough to catch and restrict all unwanted Java class access.

prime
  • 14,464
  • 14
  • 99
  • 131

1 Answers1

1

ClassFilter is not a replacement for security manager! The ClassFilter JEP page -> http://openjdk.java.net/jeps/202 makes this clear in the non goals section:

/[This does not] Make security managers redundant for scripts. Embedding applications should still turn on security management before evaluating scripts from untrusted sources. Class filtering alone will not provide a complete script "sandbox."/

ClassFilter is finer control over and above the security manager. For example, you can avoid thread creation from scripts by preventing access to java.lang.Thread class [and it's subclasses by name].

A. Sundararajan
  • 4,277
  • 1
  • 15
  • 30
  • Thanks for the clarification. So any idea on how to do it using Security Manager ? i.e. restrict accessing specific Java classes for scripts. – prime Jan 03 '17 at 05:05
  • See package.access property in this document -> http://docs.oracle.com/javase/7/docs/technotes/guides/security/smPortGuide.html Note that preventing access to java packages is for all classes . Not just for script generated classes! I mentioned security manager for sandboxing - not necessarily for java access access prevention from scripts. I recommend that you run with security manager on and set ClassFilter for filtering access to specific classes from *scripts*. package.access security properly is usually set in standard security file in your JRE. – A. Sundararajan Jan 03 '17 at 15:24
  • I think you are referring to the java.policy file in the /security folder in java,jre. well I configured a custom Security Manager using the provided securityManager class, well yes as you suggested we can have Nashorn ClassFilter on top of that also. That is what I'm doing currently, I just needed to verify that those Java classes are restricted to any third party access, not just for the JS scripts. – prime Jan 04 '17 at 05:48
  • 1
    Yes, your approach sounds fine to me [using both security manager and Nashorn ClassFilter] – A. Sundararajan Jan 04 '17 at 13:12