用途

https://github.com/gf3/sandbox

  • Can be used to execute untrusted code.
  • Support for timeouts (e.g. prevent infinite loops)
  • Support for memory errors (and memory errors)
  • Handles errors gracefully
  • Restricted code (cannot access node.js methods)
  • Supports console.log and print utility methods
  • Supports interprocess messaging with the sandboxed code

例子

https://github.com/gf3/sandbox/blob/master/example/example.js

var Sandbox = require("../lib/sandbox")
  , s = new Sandbox()

// Example 1 - Standard JS
s.run( "1 + 1", function( output ) {
  console.log( "Example 1: " + output.result + "\n" )
})

通信

箱内和向外相互通信。

Sandboxed code:

onmessage = function(message){
if (message === 'hello from outside') {
postMessage('hello from inside');
}
};

Sandbox:

var sandbox = new Sandbox();
sandbox.run(sandboxed_code);
sandbox.on('message', function(message){
// Handle message sent from the inside
// In this example message will be 'hello from inside'
});
sandbox.postMessage('hello from outside');
// Example 11 - IPC Messaging
s.run( "onmessage = function(message){ if (message === 'hello from outside') { postMessage('hello from inside'); };", function(output){ })
s.on('message', function(message){
console.log("Example 11: received message sent from inside the sandbox '" + message + "'\n")
});
var test_message = "hello from outside";
console.log("Example 11: sending message into the sandbox '" + test_message + "'");
s.postMessage(test_message);

javascript sandbox的更多相关文章

  1. JavaScript SandBox沙箱设计模式

    沙箱模式常见于YUI3 core,它是一种采用同一构造器(Constructor)生成彼此独立且互不干扰(self-contained)的实例对象,而从避免污染全局对象的方法. 命名空间 JavaSc ...

  2. 初涉JavaScript模式 (12) : 沙箱模式

    引子 上一篇说了模块模式,而对于其中的命名空间模式其实也是有着一些问题,比如每添加一个模块或则深入叠加都会导致长命名,并且对于多个库的不同版本同时运行,一不小心就会污染全局标识,而这两天也发现了JSe ...

  3. web应用安全防御100技 好书再次阅读, 变的只是表象,被概念迷惑的时候还是静下心来回顾本质

    如何进行web应用安全防御,是每个web安全从业者都会被问到的问题,非常不好回答,容易过于肤浅或流于理论,要阐明清楚,答案就是一本书的长度.而本文要介绍一本能很好回答这个问题的优秀书籍——<we ...

  4. What technical details should a programmer of a web application consider before making the site public?

    What things should a programmer implementing the technical details of a web application consider bef ...

  5. Cheatsheet: 2016 05.01 ~ 05.31

    Other Awesome Go - A curated list of awesome Go frameworks, libraries and software Visual Studio Cod ...

  6. JS中跨域和沙箱的解析

    先来直接分析源码,如下: <!DOCTYPE HTML><html><head> <meta charset="UTF-8"/> & ...

  7. Cucumber

    http://www.ibm.com/developerworks/library/a-automating-ria/ Cucumber is a testing framework that hel ...

  8. 做BS开发,你应该知道的一些东西

    界面和用户体验(Interface and User Experience) 知道各大浏览器执行Web标准的情况,保证你的站点在主要浏览器上都能正常运行.你至少要测试以下引擎:Gecko(用于Fire ...

  9. selenium docs

    Note to the Reader - Docs Being Revised for Selenium 2.0! Introduction Test Automation for Web Appli ...

随机推荐

  1. error C2144: 语法错误:“int”的前面应有“;”

    百度网上都说是中文输入的问题. 但我的错误是函数声明时后面忘了加 :真是要死......

  2. sd卡脱机烧写系统的方法(测试成功)

    一.sd卡烧写系统的基本思路: (1)把uboot.bin烧写到sd卡 (2)把image整个文件夹复制到sd卡 (3)开发板从sd卡启动,就开始自动烧写到nandflash中了. 二.烧写uboot ...

  3. 怎样上传网页到ftp中

    1.下载filezilla软件软件并安装 打开刚刚装好的FileZilla,点击菜单中的"文件"             2.点击站点管理器 3.点击新站点

  4. 跟我一起学JQuery插件开发

    http://www.cnblogs.com/Leo_wl/archive/2012/04/06/2435511.html 以前一直比较好奇,jquery插件是怎么开发的,怎么写属于自己的插件? 昨天 ...

  5. 踩坑事件:windows操作系统下的eclipse中编写SparkSQL不能从本地读取或者保存parquet文件

    这个大坑... .... 如题,在Windows的eclipse中编写SparkSQL代码时,编写如下代码时,一运行就抛出一堆空指针异常: // 首先还是创建SparkConf SparkConf c ...

  6. Java的序列化ID的作用

    Java的序列化ID的作用 简单来说,Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的.在进行反序列化时,JVM会把传来的字节流中的serialVersio ...

  7. xp系统重绘边框线不显示(首次加载没有触发paint事件)

    同样是,重绘边框事件,win7系统显示正常,而xp系统却不显示,这是什么原因造成的呢? 于是,小编开始百度,不停的查找原因,通过一番查找,小编也意外的收获了一些内容: 例如:窗口的拖动,放大,缩小,等 ...

  8. Objective-O Runtime 运行时初体验

    Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理.这种动态语言的优势在于:我们写代码时更具灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一 ...

  9. Android课程---关于数据存储的学习(2)

    手机外部存储的学习 activity_data2.xml <?xml version="1.0" encoding="utf-8"?> <Li ...

  10. Elasticsearch 运维实战之1 -- 集群规划

    规划一个可用于生产环境的elasticsearch集群. 集群节点划分 整个集群的节点分为以下三种主要类型 Master nodes -- 负责维护集群状态,不保存index数据, 硬件要求: 一般性 ...