1
0
Fork 0
forked from svrjs/svrjs

refactor: use config.wwwroot instead of current working directory as a webroot in internal code

This commit is contained in:
Dorian Niemiec 2024-11-10 12:14:55 +01:00
parent 847fb46f07
commit 1174a348b6
8 changed files with 105 additions and 68 deletions

View file

@ -12,6 +12,9 @@ let serverconsole = {};
function clientErrorHandler(err, socket) {
const config = deepClone(process.serverConfig);
// Determine the webroot from the current working directory if it is not configured
if (config.wwwroot === undefined) config.wwwroot = process.cwd();
config.generateServerString = () =>
generateServerString(config.exposeServerVersion);
@ -182,14 +185,14 @@ function clientErrorHandler(err, socket) {
fs.access(config.page404, fs.constants.F_OK, (err) => {
if (err) {
fs.access(
"." + errorCode.toString(),
config.wwwroot + "/." + errorCode.toString(),
fs.constants.F_OK,
(err) => {
try {
if (err) {
callback(errorCode.toString() + ".html");
} else {
callback("." + errorCode.toString());
callback(config.wwwroot + "/." + errorCode.toString());
}
} catch (err2) {
callServerError(500, err2);
@ -205,17 +208,21 @@ function clientErrorHandler(err, socket) {
}
});
} else {
fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => {
try {
if (err) {
callback(errorCode.toString() + ".html");
} else {
callback("." + errorCode.toString());
fs.access(
config.wwwroot + "/." + errorCode.toString(),
fs.constants.F_OK,
(err) => {
try {
if (err) {
callback(errorCode.toString() + ".html");
} else {
callback(config.wwwroot + "/." + errorCode.toString());
}
} catch (err2) {
callServerError(500, err2);
}
} catch (err2) {
callServerError(500, err2);
}
});
);
}
}
};
@ -457,15 +464,15 @@ function clientErrorHandler(err, socket) {
} sent invalid request.`
);
try {
head = fs.existsSync("./.head")
? fs.readFileSync("./.head").toString()
: fs.existsSync("./head.html")
? fs.readFileSync("./head.html").toString()
head = fs.existsSync(`${config.wwwroot}/.head`)
? fs.readFileSync(`${config.wwwroot}/.head`).toString()
: fs.existsSync(`${config.wwwroot}/head.html`)
? fs.readFileSync(`${config.wwwroot}/head.html`).toString()
: ""; // header
foot = fs.existsSync("./.foot")
? fs.readFileSync("./.foot").toString()
: fs.existsSync("./foot.html")
? fs.readFileSync("./foot.html").toString()
foot = fs.existsSync(`${config.wwwroot}/.foot`)
? fs.readFileSync(`${config.wwwroot}/.foot`).toString()
: fs.existsSync(`${config.wwwroot}/foot.html`)
? fs.readFileSync(`${config.wwwroot}/foot.html`).toString()
: ""; // footer
if (

View file

@ -32,6 +32,9 @@ function proxyHandler(req, socket, head) {
// SVR.JS configuration object (modified)
const config = deepClone(process.serverConfig);
// Determine the webroot from the current working directory if it is not configured
if (config.wwwroot === undefined) config.wwwroot = process.cwd();
config.generateServerString = () =>
generateServerString(config.exposeServerVersion);

View file

@ -37,6 +37,9 @@ function requestHandler(req, res) {
config.generateServerString = () =>
generateServerString(config.exposeServerVersion);
// Determine the webroot from the current working directory if it is not configured
if (config.wwwroot === undefined) config.wwwroot = process.cwd();
// getCustomHeaders() in SVR.JS 3.x
config.getCustomHeaders = () => {
let ph = Object.assign({}, config.customHeaders);
@ -345,14 +348,14 @@ function requestHandler(req, res) {
fs.access(config.page404, fs.constants.F_OK, (err) => {
if (err) {
fs.access(
"." + errorCode.toString(),
config.wwwroot + "/." + errorCode.toString(),
fs.constants.F_OK,
(err) => {
try {
if (err) {
callback(errorCode.toString() + ".html");
} else {
callback("." + errorCode.toString());
callback(config.wwwroot + "/." + errorCode.toString());
}
} catch (err2) {
res.error(500, err2);
@ -368,17 +371,21 @@ function requestHandler(req, res) {
}
});
} else {
fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => {
try {
if (err) {
callback(errorCode.toString() + ".html");
} else {
callback("." + errorCode.toString());
fs.access(
config.wwwroot + "/." + errorCode.toString(),
fs.constants.F_OK,
(err) => {
try {
if (err) {
callback(errorCode.toString() + ".html");
} else {
callback(config.wwwroot + "/." + errorCode.toString());
}
} catch (err2) {
res.error(500, err2);
}
} catch (err2) {
res.error(500, err2);
}
});
);
}
}
};
@ -633,15 +640,15 @@ function requestHandler(req, res) {
};
try {
res.head = fs.existsSync("./.head")
? fs.readFileSync("./.head").toString()
: fs.existsSync("./head.html")
? fs.readFileSync("./head.html").toString()
res.head = fs.existsSync(`${config.wwwroot}/.head`)
? fs.readFileSync(`${config.wwwroot}/.head`).toString()
: fs.existsSync(`${config.wwwroot}/head.html`)
? fs.readFileSync(`${config.wwwroot}/head.html`).toString()
: ""; // header
res.foot = fs.existsSync("./.foot")
? fs.readFileSync("./.foot").toString()
: fs.existsSync("./foot.html")
? fs.readFileSync("./foot.html").toString()
res.foot = fs.existsSync(`${config.wwwroot}/.foot`)
? fs.readFileSync(`${config.wwwroot}/.foot`).toString()
: fs.existsSync(`${config.wwwroot}/foot.html`)
? fs.readFileSync(`${config.wwwroot}/foot.html`).toString()
: ""; // footer
} catch (err) {
res.error(500, err);

View file

@ -812,7 +812,11 @@ if (!disableMods) {
}
// Determine path of server-side script file
let SSJSPath = "./serverSideScript.js";
let SSJSPath = `${
process.serverConfig.wwwroot != undefined
? process.serverConfig.wwwroot
: process.dirname
}/serverSideScript.js`;
if (!process.serverConfig.useWebRootServerSideScript)
SSJSPath = process.dirname + "/serverSideScript.js";

View file

@ -8,8 +8,12 @@ const {
} = require("../utils/forbiddenPaths.js");
const svrjsInfo = require("../../svrjs.json");
const { name } = svrjsInfo;
const wwwroot =
process.serverConfig && process.serverConfig.wwwroot !== undefined
? process.serverConfig.wwwroot
: ".";
forbiddenPaths.config = getInitializePath("./config.json");
forbiddenPaths.config = getInitializePath(`${wwwroot}/config.json`);
forbiddenPaths.certificates = [];
if (process.serverConfig.secure) {
forbiddenPaths.certificates.push(
@ -26,7 +30,8 @@ if (process.serverConfig.secure) {
});
}
forbiddenPaths.svrjs = getInitializePath(
"./" +
wwwroot +
"/" +
(process.dirname[process.dirname.length - 1] != "/"
? process.filename.replace(process.dirname + "/", "")
: process.filename.replace(process.dirname, ""))
@ -36,16 +41,18 @@ if (process.serverConfig.useWebRootServerSideScript) {
forbiddenPaths.serverSideScripts.push("/serverSideScript.js");
} else {
forbiddenPaths.serverSideScripts.push(
getInitializePath("./serverSideScript.js")
getInitializePath(`${wwwroot}/serverSideScript.js`)
);
}
forbiddenPaths.serverSideScriptDirectories = [];
forbiddenPaths.serverSideScriptDirectories.push(
getInitializePath("./node_modules")
getInitializePath(`${wwwroot}/node_modules`)
);
forbiddenPaths.serverSideScriptDirectories.push(getInitializePath("./mods"));
forbiddenPaths.temp = getInitializePath("./temp");
forbiddenPaths.log = getInitializePath("./log");
forbiddenPaths.serverSideScriptDirectories.push(
getInitializePath(wwwroot + "/mods")
);
forbiddenPaths.temp = getInitializePath(`${wwwroot}/temp`);
forbiddenPaths.log = getInitializePath(`${wwwroot}/log`);
module.exports = (req, res, logFacilities, config, next) => {
let decodedHrefWithoutDuplicateSlashes = "";

View file

@ -9,22 +9,25 @@ module.exports = (req, res, logFacilities, config, next) => {
req.originalParsedURL.pathname[req.originalParsedURL.pathname.length - 1] !=
"/"
) {
fs.stat("." + decodeURIComponent(req.parsedURL.pathname), (err, stats) => {
if (err || !stats.isDirectory()) {
try {
next();
} catch (err) {
res.error(500, err);
fs.stat(
config.wwwroot + decodeURIComponent(req.parsedURL.pathname),
(err, stats) => {
if (err || !stats.isDirectory()) {
try {
next();
} catch (err) {
res.error(500, err);
}
} else {
res.redirect(
req.originalParsedURL.pathname +
"/" +
(req.parsedURL.search ? req.parsedURL.search : "") +
(req.parsedURL.hash ? req.parsedURL.hash : "")
);
}
} else {
res.redirect(
req.originalParsedURL.pathname +
"/" +
(req.parsedURL.search ? req.parsedURL.search : "") +
(req.parsedURL.hash ? req.parsedURL.hash : "")
);
}
});
);
} else {
next();
}

View file

@ -28,7 +28,7 @@ module.exports = (req, res, logFacilities, config, next) => {
!_fileState
) {
fs.stat(
"." + decodeURIComponent(req.parsedURL.pathname),
config.wwwroot + decodeURIComponent(req.parsedURL.pathname),
(err, stats) => {
let _fileState = 3;
if (err) {

View file

@ -86,7 +86,7 @@ module.exports = (req, res, logFacilities, config, next) => {
res.error(400);
return;
}
let readFrom = "." + dHref;
let readFrom = config.wwwroot + dHref;
let dirImagesMissing = false;
fs.stat(readFrom, (err, stats) => {
if (err) {
@ -502,13 +502,16 @@ module.exports = (req, res, logFacilities, config, next) => {
const getCustomDirListingHeader = (callback) => {
fs.readFile(
("." + dHref + "/.dirhead").replace(/\/+/g, "/"),
(config.wwwroot + dHref + "/.dirhead").replace(/\/+/g, "/"),
(err, data) => {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
if (os.platform != "win32" || href != "/") {
fs.readFile(
("." + dHref + "/HEAD.html").replace(/\/+/g, "/"),
(config.wwwroot + dHref + "/HEAD.html").replace(
/\/+/g,
"/"
),
(err, data) => {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
@ -538,13 +541,16 @@ module.exports = (req, res, logFacilities, config, next) => {
const getCustomDirListingFooter = (callback) => {
fs.readFile(
("." + dHref + "/.dirfoot").replace(/\/+/g, "/"),
(config.wwwroot + dHref + "/.dirfoot").replace(/\/+/g, "/"),
(err, data) => {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
if (os.platform != "win32" || href != "/") {
fs.readFile(
("." + dHref + "/FOOT.html").replace(/\/+/g, "/"),
(config.wwwroot + dHref + "/FOOT.html").replace(
/\/+/g,
"/"
),
(err, data) => {
if (err) {
if (err.code == "ENOENT" || err.code == "EISDIR") {
@ -646,7 +652,7 @@ module.exports = (req, res, logFacilities, config, next) => {
if (
fs.existsSync(
"." +
config.wwwroot +
decodeURIComponent(href) +
"/.maindesc".replace(/\/+/g, "/")
)
@ -654,7 +660,7 @@ module.exports = (req, res, logFacilities, config, next) => {
htmlFoot =
"</table><hr/>" +
fs.readFileSync(
"." +
config.wwwroot +
decodeURIComponent(href) +
"/.maindesc".replace(/\/+/g, "/")
) +