Giter Site home page Giter Site logo

nashorn-sandbox's Introduction

Nashorn Sandbox

Nashorn运行JavaScript的沙箱。

  • 可设置最大的CPU运行时间(防止无限循环等)
  • 可设置最大的内存使用限制
  • 对可使用的Java类进行过滤

简单用法

做资源限制,但前提是给沙箱设置ExecutorService

StandardNashornSandbox nashornSandbox = new StandardNashornSandbox();
ExecutorService executorService = Executors.newFixedThreadPool(3);
// limit cpu time (10 seconds)
nashornSandbox.setMaxCPUTime(10 * 1000);
// limit memory (10MB)
nashornSandbox.setMaxMemory(10 * 1024 * 1024);
nashornSandbox.setExecutor(executorService);
nashornSandbox.eval("while(true) {  }");

Java

允许脚本中可使用的Java类:

nashornSandbox.allow(Thread.class);

多线程

沙箱中只有一个ScriptEngine实例,多线程下使用eval时,应该创建新的ScriptContext实例。

下面例子中省略了异常处理,如果scriptContextnull,程序运行结果则是无法预料的:

for (int i = 0; i < 30; i++) {
    executorService.execute(() -> {
        ScriptContext scriptContext = nashornSandbox.createScriptContext();
        nashornSandbox.eval("var j = 0; for (var i = 0; i < 100; i++) { j++ } console.log('j=' + j)", scriptContext);
    });
}

编译脚本

CompiledScript compiledScript = nashornSandbox.compile("var j = 0; for (var i = 0; i < 100; i++) { j++ }");
compiledScript.eval();

JavaScript日志

沙箱默认给Bindings提供了console对象,因此可以JavaScript脚本中使用如下api:

console.debug('Hello')
// log同info
console.log('Hello')
console.info('Hello')
console.error('Hello')

console打印的日志使用Java的slf4j-api实现,所以用户需要制定自己的日志框架实现。see SLF4J bindings

脚本编写

由于安全限制,语句体必须包裹在双大括号{}中,即使只有一句

// not allow
if (a > b)
    return a - b

// good
if (a > b) {
    return a - b
}

download

maven:

<!-- 即将到来 -->

nashorn-sandbox's People

Contributors

liangbizhi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.