forked from metarhia/sandboxed-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandboxed-fs.js
165 lines (147 loc) · 4.17 KB
/
sandboxed-fs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
const sandboxedFs = {};
module.exports = sandboxedFs;
const fs = require('fs');
const os = require('os');
const pathModule = require('path');
const { URL } = require('url');
const isWindows =
process.platform === 'win32' ||
process.env.OSTYPE === 'cygwin' ||
process.env.OSTYPE === 'msys';
const isUncPath = path => /^[\\/]{2,}[^\\/]+[\\/]+[^\\/]+/.test(path);
const makePathSafe = (path, allowTmp) => {
const safePath = pathModule.resolve('/', path);
if (allowTmp && safePath.startsWith(os.tmpdir())) {
return safePath;
}
// As Windows is the only non-UNIX like platform supported by node
// https://github.com/nodejs/node/blob/master/BUILDING.md#supported-platforms-1
if (isWindows) {
if (isUncPath(safePath)) {
return safePath.substring(pathModule.parse(safePath).root.length);
} else {
// If the path is a non-unc path on windows, the root is fixed to 3
// characters like 'C:\'
return safePath.substring(3);
}
}
return safePath;
};
const makeFsArgSafe = (arg, path, allowTmp) => {
let safePath;
if (typeof arg === 'string' || Buffer.isBuffer(arg)) {
safePath = makePathSafe(arg.toString(), allowTmp);
if (allowTmp && safePath.startsWith(os.tmpdir())) {
arg = safePath;
} else {
arg = pathModule.join(path, safePath);
}
} else if (arg instanceof URL && arg.protocol === 'file:') {
safePath = makePathSafe(arg.pathname, allowTmp);
if (allowTmp && safePath.startsWith(os.tmpdir())) {
arg.pathname = safePath;
} else {
arg.pathname = pathModule.join(path, safePath);
}
}
return arg;
};
const stringPathFunctionsWrapper = (func, path, allowTmp) => (p, ...args) => {
if (typeof p === 'string') {
p = pathModule.join(path, makePathSafe(p, allowTmp));
}
return func(p, ...args);
};
const pathFunctionsWrapper = (func, path, allowTmp) => (p, ...args) =>
func(makeFsArgSafe(p, path, allowTmp), ...args);
const pathFunctionsWithNativeWrapper = (func, path, allowTmp) => {
const f = pathFunctionsWrapper(func, path, allowTmp);
if (func.native) {
f.native = pathFunctionsWrapper(func.native, path, allowTmp);
}
return f;
};
const fileFunctionsWrapper = (func, path, allowTmp) => (file, ...args) => {
if (typeof file === 'number') {
return func(file, ...args);
}
return func(makeFsArgSafe(file, path, allowTmp), ...args);
};
const twoPathFunctionsWrapper = (func, path, allowTmp) => (p1, p2, ...args) =>
func(
makeFsArgSafe(p1, path, allowTmp),
makeFsArgSafe(p2, path, allowTmp),
...args
);
const functionTypes = {
pathFunctions: {
names: [
'access',
'chmod',
'chown',
'exists',
'lchmod',
'lchown',
'lstat',
'mkdir',
'open',
'readdir',
'readlink',
'rmdir',
'stat',
'truncate',
'unlink',
'utimes',
],
wrapper: pathFunctionsWrapper,
hasSyncCounterpart: true,
},
stringPathFunctions: {
names: ['mkdtemp'],
wrapper: stringPathFunctionsWrapper,
hasSyncCounterpart: true,
},
pathFunctionsWithNative: {
names: ['realpath'],
wrapper: pathFunctionsWithNativeWrapper,
hasSyncCounterpart: true,
},
pathNonSyncFunctions: {
names: [
'createReadStream',
'createWriteStream',
'unwatchFile',
'watch',
'watchFile',
],
wrapper: pathFunctionsWrapper,
hasSyncCounterpart: false,
},
fileFunctions: {
names: ['appendFile', 'readFile', 'writeFile'],
wrapper: fileFunctionsWrapper,
hasSyncCounterpart: true,
},
twoPathFunctions: {
names: ['copyFile', 'link', 'rename', 'symlink'],
wrapper: twoPathFunctionsWrapper,
hasSyncCounterpart: true,
},
};
sandboxedFs.bind = (path, allowTmp = true) => {
const wrapped = Object.assign({}, fs);
for (const typeName of Object.keys(functionTypes)) {
const type = functionTypes[typeName];
for (const name of type.names) {
const fn = fs[name];
if (!fn) continue;
wrapped[name] = type.wrapper(fn, path, allowTmp);
if (type.hasSyncCounterpart) {
const syncName = name + 'Sync';
wrapped[syncName] = type.wrapper(fs[syncName], path, allowTmp);
}
}
}
return wrapped;
};