Security with Java Scripting (JRuby, Jython, Groovy, BeanShell, etc)

后端 未结 3 2028
野的像风
野的像风 2021-02-01 09:16

I\'m looking to run some un-verified scripts (written in a yet-to-be-determined language, but needs to be Java-based, so JRuby, Groovy, Jython, BeanShell, etc are all candidates

相关标签:
3条回答
  • 2021-02-01 09:56

    In Groovy, you can do exactly what you mentioned. Actually very easy. You can easily limit permissions of untrusted scripts running in a trusted environment, allow usage of your own api, which in turn can do untrusted things.

    http://www.chrismoos.com/2010/03/24/groovy-scripts-and-jvm-security/

    0 讨论(0)
  • 2021-02-01 10:08

    Disclaimer: I am not an expert on Java Security APIs, so there may be a better way to do this.

    I work for Alfresco, Java-based Open Source Enterprise CMS, and we implemented something similar to what you describe. We wanted to allow scripting, but only to expose a subset of our Java APIs to the scripting engine.

    We chose Rhino Engine for JavaScript scripting. It allows you to control which APIs are exposed to JavaScript, which allows us to choose which classes are available, and which are not. The overhead, according to our engineers, is on the order of 10%- not too bad.

    In addition to this, and this may be relevant to you as well, on the Java side, we use Acegi (now Spring Security), and use AOP to give role-based control over which methods a certain user can call. That works pretty well for authorization. So in effect, a user accessing our app through JavaScript first has a restricted API available to him in the first place, and then that API can be restricted even further based on authorization. So you could use the AOP techniques to further restrict which methods can be called, thus allowing to expose this in other scripting languages, such as Groovy, etc. We are in the process of adding those as well, having the confidence that our underlying Java APIs protect users from unauthorized access.

    0 讨论(0)
  • 2021-02-01 10:21

    You may be able to use a custom class loader that does vets linking to classes before delegating to its parent.

    You can create your own permissions, check for those in your security sensitive APIs and then use AccessController.doPrivileged to restore appropriate privileges whilst calling the underlying API.

    You need to make sure that the scripting engine itself is secure. The version of Rhino in the Sun JDK should be okay, but no guarantees. Obviously you need to make sure everything available to the script is secure.

    0 讨论(0)
提交回复
热议问题