Skip to content

Commit 09552c0

Browse files
committed
Use a more complete list of built-in modules.
It is generated via much hackery, but then checked in for a given release, to avoid how this seems way too fragile to do at runtime. Fixes #33. Inspired greatly by @robrich's work in #34.
1 parent adb3d21 commit 09552c0

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

lib/builtin_modules.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
"_debugger",
3+
"_linklist",
4+
"assert",
5+
"buffer",
6+
"child_process",
7+
"console",
8+
"constants",
9+
"crypto",
10+
"cluster",
11+
"dgram",
12+
"dns",
13+
"domain",
14+
"events",
15+
"freelist",
16+
"fs",
17+
"http",
18+
"https",
19+
"module",
20+
"net",
21+
"os",
22+
"path",
23+
"punycode",
24+
"querystring",
25+
"readline",
26+
"repl",
27+
"stream",
28+
"_stream_readable",
29+
"_stream_writable",
30+
"_stream_duplex",
31+
"_stream_transform",
32+
"_stream_passthrough",
33+
"string_decoder",
34+
"sys",
35+
"timers",
36+
"tls",
37+
"tty",
38+
"url",
39+
"util",
40+
"vm",
41+
"zlib"
42+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Stay in sloppy mode since we need `.caller` :(
2+
var fs = require('fs');
3+
var path = require('path');
4+
5+
// Based on https://gist.github.com/Benvie/1841241
6+
7+
var NativeModule;
8+
9+
// Intercept NativeModule.require's call to process.moduleLoadList.push
10+
process.moduleLoadList.push = function newPush() {
11+
// `NativeModule.require('native_module')` returns NativeModule
12+
NativeModule = newPush.caller('native_module');
13+
14+
// Delete the interceptor and forward normal functionality
15+
delete process.moduleLoadList.push;
16+
return Array.prototype.push.apply(process.moduleLoadList, arguments);
17+
}
18+
19+
// Force an initial call to the interceptor
20+
require('vm');
21+
22+
fs.writeFileSync(path.resolve(__dirname, 'builtin_modules.json'), JSON.stringify(Object.keys(NativeModule._source), undefined, 2));

lib/sandboxed_module.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ var Module = require('module');
44
var fs = require('fs');
55
var vm = require('vm');
66
var path = require('path');
7+
var builtinModules = require('./builtin_modules.json');
78
var parent = module.parent;
89
var globalOptions = {};
9-
var registeredBuiltInSourceTransformers = ['coffee']
10-
11-
var builtinlibs = ['assert', 'buffer', 'child_process', 'cluster',
12-
'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https','net',
13-
'os', 'path', 'punycode', 'querystring', 'readline', 'stream',
14-
'string_decoder','timers', 'tls', 'tty', 'url', 'util', 'vm', 'zlib', 'smalloc'];
10+
var registeredBuiltInSourceTransformers = ['coffee'];
1511

1612
module.exports = SandboxedModule;
1713
function SandboxedModule() {
@@ -198,7 +194,7 @@ SandboxedModule.prototype._createRecursiveRequireProxy = function() {
198194
//the module Module can also be used to require, so need special care
199195
return createFakeModuleModule();
200196
}
201-
if (builtinlibs.indexOf(request) >=0) {
197+
if (builtinModules.indexOf(request) >= 0) {
202198
if (request in cache) return cache[request];
203199
return require(request);
204200
}

test/fixture/builtinModules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('../../lib/builtin_modules.json').forEach(require);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var assert = require('assert');
2+
var SandboxedModule = require('../..');
3+
4+
var foo = SandboxedModule.require('../fixture/builtinModules', {
5+
requires: { './bar': 'fakeBar' }
6+
});
7+
8+
assert.ok(foo);

0 commit comments

Comments
 (0)