-
Notifications
You must be signed in to change notification settings - Fork 3
/
sandboxed-fs.js
178 lines (160 loc) · 4.54 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
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const { URL } = require('url');
const TMP_DIR = os.tmpdir();
const isWindows =
process.platform === 'win32' ||
process.env.OSTYPE === 'cygwin' ||
process.env.OSTYPE === 'msys';
const isUncPath = (location) => /^[\\/]{2,}[^\\/]+[\\/]+[^\\/]+/.test(location);
const makePathSafe = (location, allowTmp) => {
const safePath = path.resolve('/', location);
if (allowTmp && safePath.startsWith(TMP_DIR)) {
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(path.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, location, allowTmp) => {
if (typeof arg === 'string' || Buffer.isBuffer(arg)) {
const safePath = makePathSafe(arg.toString(), allowTmp);
const isTemp = allowTmp && safePath.startsWith(TMP_DIR);
return isTemp ? safePath : path.join(location, safePath);
}
if (arg instanceof URL && arg.protocol === 'file:') {
const safePath = makePathSafe(arg.pathname, allowTmp);
const isTemp = allowTmp && safePath.startsWith(TMP_DIR);
arg.pathname = isTemp ? safePath : path.join(location, safePath);
}
return arg;
};
const stringPathFunctionsWrapper =
(func, location, allowTmp) =>
(name, ...args) =>
func(
typeof name === 'string'
? path.join(location, makePathSafe(name, allowTmp))
: name,
...args,
);
const pathFunctionsWrapper =
(func, location, allowTmp) =>
(name, ...args) =>
func(makeFsArgSafe(name, location, allowTmp), ...args);
const pathFunctionsWithNativeWrapper = (func, location, allowTmp) => {
const f = pathFunctionsWrapper(func, location, allowTmp);
if (func.native) {
f.native = pathFunctionsWrapper(func.native, location, allowTmp);
}
return f;
};
const fileFunctionsWrapper =
(func, location, allowTmp) =>
(file, ...args) =>
func(
typeof file === 'number' ? file : makeFsArgSafe(file, location, allowTmp),
...args,
);
const twoPathFunctionsWrapper =
(func, location, allowTmp) =>
(p1, p2, ...args) =>
func(
makeFsArgSafe(p1, location, allowTmp),
makeFsArgSafe(p2, location, 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,
isPromises: true,
},
stringPathFunctions: {
names: ['mkdtemp'],
wrapper: stringPathFunctionsWrapper,
hasSyncCounterpart: true,
isPromises: true,
},
pathFunctionsWithNative: {
names: ['realpath'],
wrapper: pathFunctionsWithNativeWrapper,
hasSyncCounterpart: true,
isPromises: true,
},
pathNonSyncFunctions: {
names: [
'createReadStream',
'createWriteStream',
'unwatchFile',
'watch',
'watchFile',
],
wrapper: pathFunctionsWrapper,
hasSyncCounterpart: false,
isPromises: false,
},
fileFunctions: {
names: ['appendFile', 'readFile', 'writeFile'],
wrapper: fileFunctionsWrapper,
hasSyncCounterpart: true,
isPromises: true,
},
twoPathFunctions: {
names: ['copyFile', 'link', 'rename', 'symlink'],
wrapper: twoPathFunctionsWrapper,
hasSyncCounterpart: true,
isPromises: true,
},
};
const bind = (location, allowTmp = true) => {
const wrapped = Object.assign({}, fs);
const wrapFunction = (fn, wrapper) => wrapper(fn, location, allowTmp);
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] = wrapFunction(fn, type.wrapper);
if (type.hasSyncCounterpart) {
const syncName = name + 'Sync';
wrapped[syncName] = wrapFunction(fs[syncName], type.wrapper);
}
if (type.isPromises) {
const promisesFn = fs.promises[name];
wrapped.promises[name] = wrapFunction(promisesFn, type.wrapper);
}
}
}
return wrapped;
};
module.exports = { bind };