Nashorn ClassFilter only filters Java.type()? -


i have following 2 code tests.

first: javatypetest() blocks access java.io.file expected.

second: javamethodgetfiletest() not block access when java.io.file object returned bypassing filter.

is not supposed block when java.type() used? or there specific way should adding objects engine?

expected output:

javatypetest success: true javamethodgetfiletest success: true 

actual output:

javatypetest success: true z:\eclipse ws\nashorntests\. javamethodgetfiletest success: false 

the reasoning behind want proxy class has allowed methods return allowed objects have getinstance() method returns dissallowedobject have access instance contained in proxy without exposing nashorn.

public class nashorntest {     class nashornclassfilter implements classfilter     {         public nashornclassfilter()         {         }          @override         public boolean exposetoscripts(string clazz)         {             if (clazz.equals("java.io.file")) return false;             return true;         }     }      public static class allowedclass     {         public allowedclass()         {         }          public file disallowedmethod()         {             return new file(".");         }     }      public static void main(string[] args)     {         nashornscriptenginefactory factory = new nashornscriptenginefactory();          nashornclassfilter filter = new nashorntest().new nashornclassfilter();         nashornscriptengine engine = (nashornscriptengine) factory.getscriptengine(filter);          nashornclassfilter filter1 = new nashorntest().new nashornclassfilter();         nashornscriptengine engine1 = (nashornscriptengine) factory.getscriptengine(filter1);          system.out.println("javatypetest success: " + javatypetest(engine));         system.out.println("javamethodgetfiletest success: " + javamethodgetfiletest(engine1));      }      public static boolean javatypetest(nashornscriptengine engine)     {         try         {             engine.eval(                 "function wrapper(){ "                 + "java.type('java.io.file');"                 + "}");             ((invocable) engine).invokefunction("wrapper");         }         catch (runtimeexception e)         {             if(e.getcause() instanceof classnotfoundexception) return true;             e.printstacktrace();         }         catch(exception e)         {             e.printstacktrace();         }         return false;     }      public static boolean javamethodgetfiletest(nashornscriptengine engine)     {         try         {             engine.put("allowed", new allowedclass());             engine.eval(                 "function wrapper(){ "                         + "var file = allowed.disallowedmethod();"                         + "print(file.getabsolutepath());"                         + "}");             ((invocable) engine).invokefunction("wrapper");         }         catch(runtimeexception e)         {             if(e.getcause() instanceof classnotfoundexception) return true;             e.printstacktrace();         }         catch (exception e)         {             e.printstacktrace();         }         return false;     } } 


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -