-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (179 loc) · 4.45 KB
/
index.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
179
180
181
182
183
184
185
186
187
188
189
const fs = require('fs');
const path = require('path');
const fsMethods = {
access: [1],
appendFile: [1],
chmod: [1],
chown: [1],
copyFile: [2],
cp: [2],
createReadStream: [1],
createWriteStream: [1],
exists: [1],
lchmod: [1],
lchown: [1],
lutimes: [-1],
link: [2],
mkdir: [1],
mkdtemp: [1],
open: [1],
openAsBlob: [1],
opendir: [1],
rename: [1, 2],
rmdir: [1],
rm: [1],
truncate: [1],
unlink: [1],
utimes: [1],
writeFile: [1],
accessSync: [1],
appendFileSync: [1],
chmodSync: [1],
chownSync: [1],
copyFileSync: [2],
cpSync: [2],
lchmodSync: [1],
lchownSync: [1],
lutimesSync: [-1],
linkSync: [2],
mkdirSync: [1],
mkdtempSync: [1],
opendirSync: [1],
openSync: [1],
renameSync: [1, 2],
rmdirSync: [1],
rmSync: [1],
symlink: [2],
symlinkSync: [2],
truncateSync: [1],
unlinkSync: [1],
utimesSync: [1],
writeFileSync: [1],
};
const promiseMethods = {
access: [1],
appendFile: [1],
chmod: [1],
chown: [1],
copyFile: [2],
cp: [2],
lchmod: [1],
lchown: [1],
lutimes: [-1],
link: [2],
mkdir: [1],
mkdtemp: [1],
open: [1],
opendir: [1],
rename: [1, 2],
rmdir: [1],
rm: [1],
symlink: [2],
truncate: [1],
unlink: [1],
utimes: [1],
writeFile: [1],
};
// const writeFlags = ['a', 'ax', 'a+', 'ax+', 'as', 'as+', 'r+', 'rs+', 'w', 'wx', 'w+', 'wx+'];
const realMembers = {promises: {}};
for (const methodName of Object.keys(fsMethods)) {
realMembers[methodName] = fs[methodName];
}
for (const methodName of Object.keys(promiseMethods)) {
realMembers.promises[methodName] = fs.promises[methodName];
}
function sandboxFs(...dirs) {
const fakeMembers = {promises: {}};
for (const methodName of Object.keys(fsMethods)) {
fakeMembers[methodName] = getProxy(realMembers, fsMethods, methodName, dirs);
}
for (const methodName of Object.keys(promiseMethods)) {
fakeMembers.promises[methodName] = getProxy(realMembers.promises, promiseMethods, methodName, dirs);
}
assign(fakeMembers);
return unbox;
}
function unbox() {
assign(realMembers);
}
function assign(members) {
for (const methodName of Object.keys(fsMethods)) {
fs[methodName] = members[methodName];
}
for (const methodName of Object.keys(promiseMethods)) {
fs.promises[methodName] = members.promises[methodName];
}
}
function getProxy(realNamespace, methodPaths, methodName, sandboxDirs) {
switch (methodName) {
case 'access':
case 'accessSync': return function (...args) {
if (args[1] & fs.constants.W_OK) {
verify(args[0], sandboxDirs);
}
return realNamespace[methodName](...args);
};
case 'open':
case 'openSync': {
return function (...args) {
// if (writeFlags.includes(args[1])) {
if (/[aw+]/i.test(args[1])) {
verify(args[0], sandboxDirs);
}
return realNamespace[methodName](...args);
};
}
default: {
const indexes = methodPaths[methodName].map(pathIndex => {
const index = Math.abs(pathIndex);
const expectsLink = index !== pathIndex; // negative
return [index - 1, expectsLink];
});
return function (...args) {
for (const [index, expectsLink] of indexes) {
verify(args[index], sandboxDirs, expectsLink);
}
return realNamespace[methodName](...args);
};
}
}
}
function verify(pathToVerify, sandboxDirs, expectsLink) {
if (typeof pathToVerify === 'string') {
if (expectsLink) {
// If this path is expected to be a link,
// only its parent and ancestors need to be real and inside the sandbox.
pathToVerify = path.dirname(pathToVerify);
}
pathToVerify = realExistingPartOfPath(pathToVerify);
if (!sandboxDirs.some(sandboxDir => isInside(pathToVerify, sandboxDir, true))) {
throw {
code: 'OUTSIDE_SANDBOX',
path: pathToVerify,
sandboxes: sandboxDirs,
msg: `${pathToVerify} is outside the sandbox directories (${sandboxDirs.join(', ')})`,
};
}
}
}
function realExistingPartOfPath(pathName) {
try {
return fs.realpathSync.native(pathName);
} catch (e) {
// Unlike the native and promises versions of realpath,
// realpathSync's error provides the first real part of the path that didn't exist,
// so back out one directory and we've got the part that does exist.
return fs.realpathSync(path.dirname(e.path));
}
}
// thanks to: https://github.com/sindresorhus/is-path-inside/blob/v4.0.0/index.js
function isInside(child, parent, inclusive = false) {
const relative = path.relative(parent, child);
return (
(relative || inclusive) &&
relative !== '..' &&
!relative.startsWith(`..${path.sep}`) &&
!path.isAbsolute(relative)
);
}
module.exports = sandboxFs;