2017-12-19 17:57:03 +00:00
|
|
|
const parseJson = require('parse-json');
|
2018-10-09 20:39:45 +00:00
|
|
|
const os = require('os');
|
|
|
|
let path = require('path');
|
2017-03-29 17:50:05 +00:00
|
|
|
let fs = require('fs-extra');
|
|
|
|
let utils = require('../utils/utils.js');
|
2017-12-19 17:57:03 +00:00
|
|
|
require('colors');
|
2017-02-19 16:37:54 +00:00
|
|
|
|
2018-10-10 19:26:01 +00:00
|
|
|
const pathConfigs = {
|
|
|
|
DAPP_PATH: process.env.DAPP_PATH,
|
|
|
|
EMBARK_PATH: process.env.EMBARK_PATH
|
|
|
|
};
|
|
|
|
|
2018-10-09 20:39:45 +00:00
|
|
|
function restrictPath(receiver, binding, count, args) {
|
|
|
|
const dapp = dappPath();
|
2018-10-09 20:53:58 +00:00
|
|
|
const allowedRoots = [
|
|
|
|
dapp,
|
|
|
|
os.tmpdir()
|
|
|
|
];
|
|
|
|
|
2018-10-09 20:39:45 +00:00
|
|
|
let allInsideRestricted = true;
|
|
|
|
|
|
|
|
for(let i = 0; i < count; i++) {
|
|
|
|
let resolved = path.resolve(dapp, args[i]);
|
2018-10-09 20:53:58 +00:00
|
|
|
allInsideRestricted = allowedRoots.some(p => { return resolved.indexOf(p) === 0; });
|
2018-10-09 20:39:45 +00:00
|
|
|
if(!allInsideRestricted) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(allInsideRestricted) return receiver.apply(binding, args);
|
|
|
|
throw new Error('EPERM: Operation not permitted');
|
|
|
|
}
|
|
|
|
|
2017-02-19 16:37:54 +00:00
|
|
|
function mkdirpSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.mkdirpSync, fs.mkdirpSync, 1, arguments);
|
2017-02-19 16:37:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 20:34:37 +00:00
|
|
|
function mkdirp() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.mkdirp, fs.mkdirp, 1, arguments);
|
2018-04-17 20:34:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 18:30:15 +00:00
|
|
|
function readdir() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.readdir, fs.readdir, 1, arguments);
|
2018-09-13 18:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function stat() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.stat, fs.stat, 1, arguments);
|
2018-09-13 18:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function remove() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.remove, fs.remove, 1, arguments);
|
2018-09-13 18:30:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 13:02:46 +00:00
|
|
|
function copy() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.copy, fs.copy, 2, arguments);
|
2018-05-08 13:02:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 16:37:54 +00:00
|
|
|
function copySync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.copySync, fs.copySync, 2, arguments);
|
2017-02-19 16:37:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 05:15:34 +00:00
|
|
|
function move(){
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.move, fs.move, 2, arguments);
|
2018-05-22 05:15:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 19:08:37 +00:00
|
|
|
function moveSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.moveSync, fs.moveSync, 2, arguments);
|
2018-08-12 19:08:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 18:45:56 +00:00
|
|
|
function appendFileSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.appendFileSync, fs.writeFileSync, 1, arguments);
|
2018-03-10 18:45:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 19:48:01 +00:00
|
|
|
function writeFile() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.writeFile, fs.writeFileSync, 1, arguments);
|
2018-05-07 19:48:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 17:51:32 +00:00
|
|
|
function writeFileSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.writeFileSync, fs.writeFileSync, 1, arguments);
|
2017-02-19 17:51:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 16:09:42 +00:00
|
|
|
function readFile() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.readFile, fs.readFile, 1, arguments);
|
2018-04-18 16:09:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 16:37:54 +00:00
|
|
|
function readFileSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.readFileSync, fs.readFileSync, 1, arguments);
|
2017-02-19 16:37:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:13:37 +00:00
|
|
|
function readdirSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.readdirSync, fs.readdirSync, 1, arguments);
|
2018-08-30 12:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function statSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.statSync, fs.statSync, 1, arguments);
|
2018-08-30 12:13:37 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 16:37:54 +00:00
|
|
|
function readJSONSync() {
|
2017-12-19 17:57:03 +00:00
|
|
|
let content = readFileSync.apply(readFileSync, arguments);
|
|
|
|
try {
|
|
|
|
return parseJson(content);
|
|
|
|
} catch(e) {
|
|
|
|
console.error("error: ".red + arguments[0].green.underline + " " + e.message.green);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2017-02-19 16:37:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function writeJSONSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.writeJSONSync, fs.writeJSONSync, 1, arguments);
|
2017-02-19 16:37:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
function writeJson() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.writeJson, fs.writeJson, 1, arguments);
|
2018-05-08 13:36:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
function existsSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.existsSync, fs.existsSync, 1, arguments);
|
2017-02-21 20:45:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 12:54:10 +00:00
|
|
|
function access() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.access, fs.access, 1, arguments);
|
2018-05-08 12:54:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 14:22:58 +00:00
|
|
|
function removeSync() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.removeSync, fs.removeSync, 1, arguments);
|
2018-01-11 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 22:55:39 +00:00
|
|
|
function anchoredPath(envAnchor, ...args) {
|
2018-10-10 19:26:01 +00:00
|
|
|
let anchor = pathConfigs[envAnchor];
|
|
|
|
if (!pathConfigs[envAnchor]) {
|
2018-08-17 22:55:39 +00:00
|
|
|
console.error(`process.env.${envAnchor} was not set`.bold.red);
|
2018-08-17 17:37:49 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-08-17 22:55:39 +00:00
|
|
|
return utils.joinPath(anchor, ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
function embarkPath() {
|
|
|
|
return anchoredPath('EMBARK_PATH', ...arguments);
|
2017-02-20 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 19:30:16 +00:00
|
|
|
function dappPath() {
|
2018-08-17 22:55:39 +00:00
|
|
|
return anchoredPath('DAPP_PATH', ...arguments);
|
2017-07-05 12:35:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 06:53:12 +00:00
|
|
|
function pkgPath() {
|
|
|
|
return anchoredPath('PKG_PATH', ...arguments);
|
|
|
|
}
|
|
|
|
|
2018-04-17 20:34:37 +00:00
|
|
|
function createWriteStream() {
|
2018-10-09 20:39:45 +00:00
|
|
|
return restrictPath(fs.createWriteStream, fs.createWriteStream, 1, arguments);
|
2018-04-17 20:34:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 08:38:09 +00:00
|
|
|
function tmpDir() {
|
|
|
|
let os = require('os');
|
|
|
|
return utils.joinPath(os.tmpdir(), ...arguments);
|
|
|
|
}
|
|
|
|
|
2018-10-03 18:51:51 +00:00
|
|
|
function copyPreserve(sourceFilePath, targetFilePath) {
|
|
|
|
const path = require('path');
|
|
|
|
let ext = 1;
|
|
|
|
let preserved = targetFilePath;
|
|
|
|
while (fs.existsSync(preserved)) {
|
2018-10-03 19:54:53 +00:00
|
|
|
let extname = path.extname(targetFilePath);
|
2018-10-03 18:51:51 +00:00
|
|
|
preserved = utils.joinPath(
|
|
|
|
path.dirname(targetFilePath),
|
2018-10-03 19:54:53 +00:00
|
|
|
`${path.basename(targetFilePath, extname)}.${ext}${extname}`
|
2018-10-03 18:51:51 +00:00
|
|
|
);
|
|
|
|
ext++;
|
|
|
|
}
|
|
|
|
if (preserved !== targetFilePath) {
|
|
|
|
fs.copySync(targetFilePath, preserved);
|
|
|
|
}
|
|
|
|
fs.copySync(sourceFilePath, targetFilePath);
|
|
|
|
}
|
|
|
|
|
2018-10-04 12:22:41 +00:00
|
|
|
function outputFileSync(){
|
|
|
|
return fs.outputFileSync.apply(fs.outputFile, arguments);
|
|
|
|
}
|
|
|
|
|
2017-02-19 16:37:54 +00:00
|
|
|
module.exports = {
|
2018-10-09 20:39:45 +00:00
|
|
|
access,
|
|
|
|
appendFileSync,
|
2018-05-08 13:02:46 +00:00
|
|
|
copy,
|
2018-05-07 19:48:01 +00:00
|
|
|
copySync,
|
2018-10-09 20:39:45 +00:00
|
|
|
createWriteStream,
|
|
|
|
embarkPath,
|
|
|
|
existsSync,
|
|
|
|
mkdirp,
|
|
|
|
mkdirpSync,
|
2018-05-22 05:15:34 +00:00
|
|
|
move,
|
2018-08-12 19:08:37 +00:00
|
|
|
moveSync,
|
2018-04-18 16:09:42 +00:00
|
|
|
readFile,
|
2018-05-07 19:48:01 +00:00
|
|
|
readFileSync,
|
2018-10-09 20:39:45 +00:00
|
|
|
readJSONSync,
|
|
|
|
readdir,
|
2018-08-30 12:13:37 +00:00
|
|
|
readdirSync,
|
2018-10-09 20:39:45 +00:00
|
|
|
remove,
|
|
|
|
removeSync,
|
|
|
|
stat,
|
2018-08-30 12:13:37 +00:00
|
|
|
statSync,
|
2018-10-09 20:39:45 +00:00
|
|
|
tmpDir,
|
2018-05-07 19:48:01 +00:00
|
|
|
writeFile,
|
|
|
|
writeFileSync,
|
|
|
|
writeJSONSync,
|
2018-10-09 20:39:45 +00:00
|
|
|
copyPreserve
|
2018-05-07 19:48:01 +00:00
|
|
|
dappPath,
|
2018-09-10 06:53:12 +00:00
|
|
|
pkgPath,
|
2018-10-09 20:39:45 +00:00
|
|
|
outputFileSync,
|
|
|
|
writeJson
|
2017-02-19 16:37:54 +00:00
|
|
|
};
|