This repository was archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
295 lines (245 loc) · 7.96 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright (c) 2013, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/*jshint node: true */
var currentCWD = process.cwd();
var block_access = false;
var FILE_ACCESSDIR = process.env.NODEJS_FILE_ACCESSDIR;
var OPEN_BASEDIR = process.env.NODEJS_OPEN_BASEDIR;
var path = require('path');
var config = null;
var configSet = false;
var mainCalled = false;
var allowChange = false;
var getConfig = function() {
return config || {
'file_accessdir': ['/'],
'open_basedir': ['/']
};
};
function normalizePath(full) {
var dir_name = full;
if (dir_name.indexOf('\\\\?\\') === 0) {
dir_name = dir_name.substring(4, dir_name.length);
}
dir_name = path.resolve(dir_name);
dir_name += (dir_name[dir_name.length - 1] === path.sep) ? '' : path.sep;
return dir_name;
}
var allowPath = function(pathname, name) {
if (pathname.indexOf('http:') === 0 || pathname.indexOf('https:') === 0) {
return false;
}
var conf = getConfig(),
paths = conf[name],
i, nextChar, curPath;
pathname = normalizePath(pathname);
for (i = 0; i < paths.length; i++) {
curPath = paths[i];
if (pathname.indexOf(curPath) !== 0) {
continue;
}
nextChar = pathname[curPath.length];
/*istanbul ignore next*/
if (curPath[curPath.length - 1] !== path.sep && nextChar && nextChar !== path.sep) {
continue;
}
return true;
}
return false;
};
var isAccessAllowed = function(file) {
return allowPath(file, 'file_accessdir');
};
function augmentFS(binding) {
var orig = {};
Object.keys(binding).forEach(function(i) {
orig[i] = binding[i];
});
function checkAndPass(name, num) {
return function(path) {
if (!block_access || isAccessAllowed(path)) {
return orig[name].apply(binding, arguments);
} else {
var err = new Error('Access denied (file: ' + path + ')'),
arg = arguments[num - 1];
/*istanbul ignore else*/
if (arg === undefined) {
throw err;
} else if (typeof arg === 'object') {
if (arg.oncomplete) {
arg.oncomplete(err);
} else {
throw err;
}
} else {
arg(err);
}
}
};
}
function checkAndPass2(name, num) {
return function(path1, path2) {
if (!block_access || (isAccessAllowed(path1) && isAccessAllowed(path2))) {
return orig[name].apply(binding, arguments);
} else {
var str = [], err, arg;
if (!isAccessAllowed(path1)) {
str.push(path1);
}
if (!isAccessAllowed(path2)) {
str.push(path2);
}
err = new Error('Access denied (file: ' + str.join(', ') + ')');
arg = arguments[num - 1];
/*istanbul ignore else*/
if (arg === undefined) {
throw err;
} else if (typeof arg === 'object') {
if (arg.oncomplete) {
arg.oncomplete(err);
} else {
throw err;
}
} else {
arg(err);
}
}
};
}
binding.open = checkAndPass('open', 4);
binding.rename = checkAndPass2('rename', 3);
binding.rmdir = checkAndPass('rmdir', 2);
binding.mkdir = checkAndPass('mkdir', 3);
binding.readdir = checkAndPass('readdir', 2);
binding.symlink = checkAndPass2('symlink', 4);
binding.link = checkAndPass2('link', 3);
binding.unlink = checkAndPass('unlink', 2);
binding.chmod = checkAndPass('chmod', 3);
binding.chown = checkAndPass('chown', 4);
binding.readlink = checkAndPass('readlink', 2);
Object.defineProperty(binding, '@fs-lock', { value: true });
//Freeze this object so no one else can override this method
Object.freeze(binding);
return binding;
}
function setupProtectedFS() {
var oldBinding = process.binding,
oldDLopen = process.dlopen,
augmentedFS = augmentFS(oldBinding('fs'));
process.binding = function(mod) {
mod = mod.replace(/\0/g, '');
return ((mod === 'fs') ? augmentedFS : oldBinding(mod));
};
process.dlopen = function(mod, path) {
var real_path = mod.filename || mod;
if (!block_access || isAccessAllowed(real_path)) {
return oldDLopen.apply(process, arguments);
}
throw new Error('Access denied (native module: ' + real_path + ')');
};
}
var resolveList = function(arr, param) {
var tempArr = [],
resArr = [];
if (typeof param === 'string') {
tempArr = param.split(':');
}
if (param instanceof Array) {
tempArr = param;
}
tempArr.forEach(function(v, k) {
if (typeof v === 'string') {
v = v.trim();
if (v.length !== 0) {
resArr[k] = normalizePath(v);
}
}
});
return arr.concat(resArr);
};
function setFromEnv(name, param) {
config = config || {};
if (param) {
config[name] = resolveList(config[name] || [], param.trim());
}
}
setAccess = function(module) {
// wraps module._findPath, adding file access permission check
var trueFindPath = module._findPath;
module._findPath = function(request, paths) {
var filename = trueFindPath(request, paths);
if (!filename) { return filename; }
if (allowPath(filename, 'open_basedir')) {
return filename;
}
throw new Error('Access denied (module: ' + request + ')');
};
//Freeze this object so no one else can override this method
Object.freeze(module);
// load usual config first
getConfig();
// Set from env variables
setFromEnv('file_accessdir', FILE_ACCESSDIR);
setFromEnv('open_basedir', OPEN_BASEDIR);
};
setupProtectedFS();
block_access = true;
var NativeModule = require('module');
setAccess(NativeModule);
var resolveConfig = function(s, config, overwrite) {
config = (overwrite ? {} : config);
['open_basedir', 'file_accessdir'].forEach(function(name) {
config[name] = resolveList(config[name] || [], s[name]);
});
return config;
};
var verifyConfig = function(configSet, allowChange, overrideChange, mainCalled) {
if (configSet && !allowChange) {
if (overrideChange) {
if (mainCalled) {
throw new Error('Constructor already called, can not override allowChange');
}
} else {
throw new Error('Config already set, can not set again');
}
}
if (overrideChange && !mainCalled) {
allowChange = true;
console.error('!! OVERRIDING FS LOCK - THIS SHOULD ONLY BE FOR TESTING');
}
return allowChange;
};
module.exports = function(s, overrideChange) {
allowChange = verifyConfig(configSet, allowChange, overrideChange, mainCalled);
configSet = true;
config = resolveConfig(s, config, allowChange);
mainCalled = true;
};
//Wrapped so they can't be over written from the outside
module.exports.isRequireAllowed = function(module) {
return allowPath(module, 'open_basedir') &&
allowPath(module, 'file_accessdir');
};
module.exports.getAllowedRequire = function() {
var conf = getConfig();
return conf.open_basedir.slice(0);
};
//Exposed only for testing..
module.exports.normalizePath = function(full) {
return normalizePath(full);
};
module.exports.resolveList = function(arr, param) {
return resolveList(arr, param);
};
module.exports.isAccessAllowed = function(file) {
return isAccessAllowed(file);
};
module.exports._resolveConfig = function(s, config) {
return resolveConfig(s, config);
};
module.exports._verifyConfig = function(configSet, allowChange, overrideChange, mainCalled) {
return verifyConfig(configSet, allowChange, overrideChange, mainCalled);
};