Skip to content

Commit 29e25fd

Browse files
committed
Sandbox filesystem
1 parent 94a9ebb commit 29e25fd

File tree

4 files changed

+76
-35
lines changed

4 files changed

+76
-35
lines changed

JavaScript/application.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ console.log('From application global context');
1010
const fs = require('fs');
1111
console.dir({ fs });
1212

13+
const mkdirp = require('mkdirp');
14+
console.dir({ mkdirp });
15+
16+
mkdirp('/hello/world', (err) => {
17+
if (err) console.error(err);
18+
else console.log('pow!');
19+
});
20+
1321
module.exports = () => {
1422
// Print from the exported function context
1523
console.log('From application exported function');

JavaScript/framework.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,45 @@ const EXECUTION_TIMEOUT = 5000;
1010
// The framework can require core libraries
1111
const fs = require('fs');
1212
const vm = require('vm');
13+
const sfs = require('sandboxed-fs');
1314

1415
// Create a hash and turn it into the sandboxed context which will be
1516
// the global context of an application
1617
const context = {
17-
module: {}, console,
18+
module: {},
19+
console,
1820
require: (name) => {
19-
if (name === 'fs') {
20-
console.log('Module fs is restricted');
21-
return null;
22-
}
23-
return require(name);
21+
if (name === 'fs') return sfs.bind('./');
22+
let exported = execute('./node_modules/' + name + '/index.js');
23+
if (!exported) exported = require(name);
2424
}
2525
};
2626

2727
context.global = context;
2828
const sandbox = vm.createContext(context);
2929

30-
// Read an application source code from the file
31-
const fileName = './application.js';
32-
fs.readFile(fileName, (err, src) => {
33-
// We need to handle errors here
34-
35-
// Run an application in sandboxed context
36-
let script;
37-
try {
38-
script = new vm.Script(src, { timeout: PARSING_TIMEOUT });
39-
} catch (e) {
40-
console.log('Parsing timeout');
41-
process.exit(1);
42-
}
43-
44-
try {
45-
script.runInNewContext(sandbox, { timeout: EXECUTION_TIMEOUT });
46-
const exported = sandbox.module.exports;
47-
console.dir({ exported });
48-
} catch (e) {
49-
console.log('Execution timeout');
50-
process.exit(1);
51-
}
52-
53-
// We can access a link to exported interface from sandbox.module.exports
54-
// to execute, save to the cache, print to console, etc.
55-
});
30+
function execute(fileName) {
31+
console.log(fileName);
32+
fs.readFile(fileName, (err, src) => {
33+
console.log(src);
34+
let script;
35+
try {
36+
script = new vm.Script(src, { timeout: PARSING_TIMEOUT });
37+
console.dir({ script });
38+
} catch (e) {
39+
console.dir(e);
40+
process.exit(1);
41+
}
42+
try {
43+
script.runInNewContext(sandbox, { timeout: EXECUTION_TIMEOUT });
44+
const exported = sandbox.module.exports;
45+
console.dir({ exported });
46+
return exported;
47+
} catch (e) {
48+
console.dir(e);
49+
process.exit(1);
50+
}
51+
});
52+
}
5653

57-
process.on('uncaughtException', (err) => {
58-
console.log('Unhandled exception: ' + err);
59-
});
54+
execute('./application.js');

JavaScript/package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaScript/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "sandboxing",
3+
"version": "0.0.2",
4+
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>",
5+
"license": "MIT",
6+
"main": "./framework.js",
7+
"dependencies": {
8+
"metarhia-common": "^0.0.25",
9+
"mkdirp": "^0.5.1",
10+
"sandboxed-fs": "^0.3.0"
11+
}
12+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy