You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2013/09/25 13:30:57 UTC

[08/12] git commit: updated refs/heads/1894-feature-experimental-nodejs-couchjs to ca5ecb8

add sandbox.js


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/ac411706
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/ac411706
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/ac411706

Branch: refs/heads/1894-feature-experimental-nodejs-couchjs
Commit: ac411706720a370c945ac756248f399e0152ae99
Parents: 1db3bb6
Author: Jan Lehnardt <ja...@apache.org>
Authored: Sat Sep 21 16:28:47 2013 +0200
Committer: Jan Lehnardt <ja...@apache.org>
Committed: Sat Sep 21 16:28:47 2013 +0200

----------------------------------------------------------------------
 NOTICE                      |  3 +++
 license.skip                |  1 +
 src/couchjs-node/sandbox.js | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/ac411706/NOTICE
----------------------------------------------------------------------
diff --git a/NOTICE b/NOTICE
index 95d4ff6..5ee02d5 100644
--- a/NOTICE
+++ b/NOTICE
@@ -166,3 +166,6 @@ This product also includes the following third-party components:
 
    Copyright (c) 2013 Dave Gandy
 
+ * sandbox.js https://github.com/KlausTrainer/sandbox.js
+
+   (c) 2013 Klaus Trainer

http://git-wip-us.apache.org/repos/asf/couchdb/blob/ac411706/license.skip
----------------------------------------------------------------------
diff --git a/license.skip b/license.skip
index ca5eb6c..5e2b667 100644
--- a/license.skip
+++ b/license.skip
@@ -98,6 +98,7 @@
 ^src/couchdb/priv/couchspawnkillable
 ^src/couchdb/priv/stat_descriptions.cfg
 ^src/couchjs-node/package.json
+^src/couchjs-node/sandbox.js
 ^src/couchjs-node/README.md
 ^src/erlang-oauth/.*
 ^src/couch_dbupdates

http://git-wip-us.apache.org/repos/asf/couchdb/blob/ac411706/src/couchjs-node/sandbox.js
----------------------------------------------------------------------
diff --git a/src/couchjs-node/sandbox.js b/src/couchjs-node/sandbox.js
new file mode 100644
index 0000000..cfdff18
--- /dev/null
+++ b/src/couchjs-node/sandbox.js
@@ -0,0 +1,40 @@
+// from https://github.com/KlausTrainer/sandbox.js
+exports.runInSandbox = function(src, ctx, whitelist) {
+  var vm = require('vm'),
+    sandbox;
+
+  if (ctx && ctx.require) {
+    whitelist = whitelist || [];
+    var insecureRequire = ctx.require,
+      module = require("module"),
+      oldModulePrototype = module.prototype;
+
+    var secureRequire = function(moduleName) {
+      if (whitelist.indexOf(moduleName) == -1) {
+        module.prototype = oldModulePrototype;
+        throw new Error("'" + moduleName + "' is not whitelisted");
+      } else {
+        var requiredModule = insecureRequire(moduleName);
+        module.prototype = oldModulePrototype;
+        return requiredModule;
+      }
+    };
+
+    module.prototype = {
+      require: secureRequire,
+      load: module.prototype.load,
+      _compile: module.prototype._compile
+    };
+
+    module._cache = {};
+
+    ctx.require = secureRequire;
+    sandbox = Object.freeze(vm.createContext(ctx));
+    ctx.require = insecureRequire;
+  } else {
+    sandbox = Object.freeze(vm.createContext(ctx || {}));
+  }
+
+  return vm.createScript('(function() {"use strict"; return ('
+                         + src + ')()}())').runInContext(sandbox);
+};