1
0
Fork 0
forked from svrjs/svrjs

fix: fix crashes with SVR.JS Core

This commit is contained in:
Dorian Niemiec 2024-11-10 16:09:35 +01:00
parent 50465e4a6c
commit e0536620d6
3 changed files with 69 additions and 2 deletions

View file

@ -13,7 +13,6 @@ const statusCodes = require("./res/statusCodes.js");
const middleware = [ const middleware = [
require("./middleware/urlSanitizer.js"), require("./middleware/urlSanitizer.js"),
require("./middleware/rewriteURL.js"),
require("./middleware/redirectTrailingSlashes.js"), require("./middleware/redirectTrailingSlashes.js"),
require("./middleware/defaultHandlerChecks.js"), require("./middleware/defaultHandlerChecks.js"),
require("./middleware/staticFileServingAndDirectoryListings.js") require("./middleware/staticFileServingAndDirectoryListings.js")
@ -591,6 +590,9 @@ function requestHandler(req, res, next) {
? config.domain ? config.domain
: "unknown.invalid") : "unknown.invalid")
); );
// req.originalParsedURL fallback
req.originalParsedURL = req.parsedURL;
} catch (err) { } catch (err) {
res.error(400, err); res.error(400, err);
return; return;
@ -626,6 +628,43 @@ function requestHandler(req, res, next) {
function init(config) { function init(config) {
if (config) coreConfig = config; if (config) coreConfig = config;
if (coreConfig.users === undefined) coreConfig.users = [];
if (coreConfig.page404 === undefined) coreConfig.page404 = "404.html";
if (coreConfig.enableCompression === undefined)
coreConfig.enableCompression = true;
if (coreConfig.customHeaders === undefined) coreConfig.customHeaders = {};
if (coreConfig.enableDirectoryListing === undefined)
coreConfig.enableDirectoryListing = true;
if (coreConfig.enableDirectoryListingWithDefaultHead === undefined)
coreConfig.enableDirectoryListingWithDefaultHead = false;
if (coreConfig.serverAdministratorEmail === undefined)
coreConfig.serverAdministratorEmail = "[no contact information]";
if (coreConfig.stackHidden === undefined) coreConfig.stackHidden = false;
if (coreConfig.exposeServerVersion === undefined)
coreConfig.exposeServerVersion = true;
if (coreConfig.dontCompress === undefined)
coreConfig.dontCompress = [
"/.*\\.ipxe$/",
"/.*\\.(?:jpe?g|png|bmp|tiff|jfif|gif|webp)$/",
"/.*\\.(?:[id]mg|iso|flp)$/",
"/.*\\.(?:zip|rar|bz2|[gb7x]z|lzma|tar)$/",
"/.*\\.(?:mp[34]|mov|wm[av]|avi|webm|og[gv]|mk[va])$/"
];
if (coreConfig.enableIPSpoofing === undefined)
coreConfig.enableIPSpoofing = false;
if (coreConfig.enableETag === undefined) coreConfig.enableETag = true;
if (coreConfig.rewriteDirtyURLs === undefined)
coreConfig.rewriteDirtyURLs = false;
if (coreConfig.errorPages === undefined) coreConfig.errorPages = [];
if (coreConfig.disableTrailingSlashRedirects === undefined)
coreConfig.disableTrailingSlashRedirects = false;
if (coreConfig.allowDoubleSlashes === undefined)
coreConfig.allowDoubleSlashes = false;
// You wouldn't use SVR.JS mods in SVR.JS Core
coreConfig.exposeModsInErrorPages = false;
return requestHandler; return requestHandler;
} }

View file

@ -706,6 +706,9 @@ function requestHandler(req, res) {
? config.domain ? config.domain
: "unknown.invalid") : "unknown.invalid")
); );
// req.originalParsedURL fallback
req.originalParsedURL = req.parsedURL;
} catch (err) { } catch (err) {
res.error(400, err); res.error(400, err);
return; return;

View file

@ -980,7 +980,32 @@ module.exports = (req, res, logFacilities, config, next) => {
} else if (dirImagesMissing) { } else if (dirImagesMissing) {
fs.stat(readFrom, (e, s) => { fs.stat(readFrom, (e, s) => {
if (e || !s.isFile()) { if (e || !s.isFile()) {
properDirectoryListingAndStaticFileServe(); if (err.code == "ENOENT") {
res.error(404);
logFacilities.errmessage("Resource not found.");
return;
} else if (err.code == "ENOTDIR") {
res.error(404); // Assume that file doesn't exist.
logFacilities.errmessage("Resource not found.");
return;
} else if (err.code == "EACCES") {
res.error(403);
logFacilities.errmessage("Access denied.");
return;
} else if (err.code == "ENAMETOOLONG") {
res.error(414);
return;
} else if (err.code == "EMFILE") {
res.error(503);
return;
} else if (err.code == "ELOOP") {
res.error(508); // The symbolic link loop is detected during file system operations.
logFacilities.errmessage("Symbolic link loop detected.");
return;
} else {
res.error(500, err);
return;
}
} else { } else {
stats = s; stats = s;
properDirectoryListingAndStaticFileServe(); properDirectoryListingAndStaticFileServe();