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