1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Fileserver: Check for valid file paths

This commit is contained in:
jeremy@jermolene.com 2021-08-28 13:16:54 +01:00
parent 124b49456a
commit a67b1b8bb5

View File

@ -21,21 +21,28 @@ exports.handler = function(request,response,state) {
fs = require("fs"), fs = require("fs"),
util = require("util"), util = require("util"),
suppliedFilename = decodeURIComponent(state.params[0]), suppliedFilename = decodeURIComponent(state.params[0]),
filename = path.resolve(state.boot.wikiPath,"files",suppliedFilename), baseFilename = path.resolve(state.boot.wikiPath,"files"),
filename = path.resolve(baseFilename,suppliedFilename),
extension = path.extname(filename); extension = path.extname(filename);
fs.readFile(filename,function(err,content) { // Check that the filename is inside the wiki files folder
var status,content,type = "text/plain"; if(path.relative(baseFilename,filename).indexOf("..") !== 0) {
if(err) { // Send the file
console.log("Error accessing file " + filename + ": " + err.toString()); fs.readFile(filename,function(err,content) {
status = 404; var status,content,type = "text/plain";
content = "File '" + suppliedFilename + "' not found"; if(err) {
} else { console.log("Error accessing file " + filename + ": " + err.toString());
status = 200; status = 404;
content = content; content = "File '" + suppliedFilename + "' not found";
type = ($tw.config.fileExtensionInfo[extension] ? $tw.config.fileExtensionInfo[extension].type : "application/octet-stream"); } else {
} status = 200;
state.sendResponse(status,{"Content-Type": type},content); content = content;
}); type = ($tw.config.fileExtensionInfo[extension] ? $tw.config.fileExtensionInfo[extension].type : "application/octet-stream");
}
state.sendResponse(status,{"Content-Type": type},content);
});
} else {
state.sendResponse(404,{"Content-Type": "text/plain"},"File '" + suppliedFilename + "' not found");
}
}; };
}()); }());