用途

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. Could not open Selected VM debug port (8700) (转)

    Could not open Selected VM debug port (8700) 2014年11月14日 ⁄ 综合 ⁄ 共 446字 ⁄ 字号 小 中 大 ⁄ 评论关闭   在运行项目的时候, ...

  2. android用户界面之Gallery3D学习资料汇总

    一.Gallery之根蒂根基教程1.Android Gallery与衍生BaseAdapter容器 http://www.apkbus.com/android-6249-1-1.html 2.Andr ...

  3. 简单的后台json,前台解析 操作

    后台: List<PageData> KeyWords=plantDefDetailCSAService.findKeyWords(pd); JSONArray array = new J ...

  4. oracle 安装

    一 : 建议安装在64位机器上,程序下载地址 http://download.oracle.com/otn/nt/oracle11g/112010/win64_11gR2_database_1of2. ...

  5. a 标签 启用或禁用点击事件

    <a href="#" id="btnAuthCode" class="authCode_btn">获取验证码</a> ...

  6. 获取移除指定Url参数(原创)

    /// <summary> /// 移除指定的Url参数 /// 来自:http://www.cnblogs.com/cielwater /// </summary> /// ...

  7. 【iCore3 双核心板_FPGA】实验二十六:SDRAM读写测试实验

    实验指导书及代码包下载: http://pan.baidu.com/s/1c1VRibY iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  8. Sublime P4语法高亮设置

    Github插件链接:p4-syntax-highlighter 首先安装Package Control. 进入Package界面,我的目录: /Users/wasdns/Library/Applic ...

  9. vi 技巧和诀窍~转IBM

    复合搜索 1 #!/bin/ksh 2 # 3 echo "Starting" 4 file=${1} 5 6 echo ${file} 7 8 if [[ ${file} = 1 ...

  10. ansible 安装

    1.简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署. ...