fix: prevent path crossing #391

fix: compatibility with node version
This commit is contained in:
yuanqin.wyq 2019-03-18 20:35:14 +08:00
parent a8c9f590fc
commit 9a9c076554
2 changed files with 62 additions and 41 deletions

View File

@ -126,12 +126,12 @@ class Recorder extends events.EventEmitter {
*
*/
updateRecordWsMessage(id, message) {
const cachePath = this.cachePath;
if (id < 0) return;
try {
const recordWsMessageFile = path.join(cachePath, WS_MESSAGE_FILE_PRFIX + id);
this.getCacheFile(WS_MESSAGE_FILE_PRFIX + id, (err, recordWsMessageFile) => {
if (err) return;
fs.appendFile(recordWsMessageFile, wsMessageStingify(message) + ',', () => {});
});
} catch (e) {
console.error(e);
logUtil.error(e.message + e.stack);
@ -172,15 +172,16 @@ class Recorder extends events.EventEmitter {
updateRecordBody(id, info) {
const self = this;
const cachePath = self.cachePath;
if (id === -1) return;
if (!id || typeof info.resBody === 'undefined') return;
//add to body map
//ignore image data
const bodyFile = path.join(cachePath, BODY_FILE_PRFIX + id);
self.getCacheFile(BODY_FILE_PRFIX + id, (err, bodyFile) => {
if (err) return;
fs.writeFile(bodyFile, info.resBody, () => {});
});
}
/**
@ -189,13 +190,16 @@ class Recorder extends events.EventEmitter {
*/
getBody(id, cb) {
const self = this;
const cachePath = self.cachePath;
if (id < 0) {
cb && cb('');
return;
}
self.getCacheFile(BODY_FILE_PRFIX + id, (error, bodyFile) => {
if (error) {
cb && cb(error);
return;
}
const bodyFile = path.join(cachePath, BODY_FILE_PRFIX + id);
fs.access(bodyFile, fs.F_OK || fs.R_OK, (err) => {
if (err) {
cb && cb(err);
@ -203,6 +207,7 @@ class Recorder extends events.EventEmitter {
fs.readFile(bodyFile, cb);
}
});
});
}
getDecodedBody(id, cb) {
@ -270,14 +275,16 @@ class Recorder extends events.EventEmitter {
*
*/
getDecodedWsMessage(id, cb) {
const self = this;
const cachePath = self.cachePath;
if (id < 0) {
cb && cb([]);
return;
}
const wsMessageFile = path.join(cachePath, WS_MESSAGE_FILE_PRFIX + id);
this.getCacheFile(WS_MESSAGE_FILE_PRFIX + id, (outError, wsMessageFile) => {
if (outError) {
cb && cb(outError);
return;
}
fs.access(wsMessageFile, fs.F_OK || fs.R_OK, (err) => {
if (err) {
cb && cb(err);
@ -301,6 +308,7 @@ class Recorder extends events.EventEmitter {
});
}
});
});
}
getSingleRecord(id, cb) {
@ -330,6 +338,19 @@ class Recorder extends events.EventEmitter {
const self = this;
proxyUtil.deleteFolderContentsRecursive(self.cachePath, true);
}
getCacheFile(fileName, cb) {
const self = this;
const cachePath = self.cachePath;
const filepath = path.join(cachePath, fileName);
if (filepath.indexOf(cachePath) !== 0) {
cb && cb(new Error('invalid cache file path'));
} else {
cb && cb(null, filepath);
return filepath;
}
}
}
module.exports = Recorder;

View File

@ -161,7 +161,7 @@ class webInterface extends events.EventEmitter {
}
});
} else {
res.end({});
res.end('');
}
});