From e94391f1978c08b31ede290068b0ed38f02fc1e5 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sat, 24 Aug 2024 20:45:17 +0200 Subject: [PATCH] Lint out the codebase. --- src/middleware/redirectTrailingSlashes.js | 53 ++++++++++++----------- src/middleware/rewriteURL.js | 3 +- src/middleware/urlSanitizer.js | 3 +- src/middleware/webRootPostfixes.js | 4 +- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/middleware/redirectTrailingSlashes.js b/src/middleware/redirectTrailingSlashes.js index 74848b5..e36b0cc 100644 --- a/src/middleware/redirectTrailingSlashes.js +++ b/src/middleware/redirectTrailingSlashes.js @@ -2,30 +2,33 @@ const fs = require("fs"); module.exports = (req, res, logFacilities, config, next) => { // Trailing slash redirection - if ( - !req.isProxy && - !config.disableTrailingSlashRedirects && - req.parsedURL.pathname[req.parsedURL.pathname.length - 1] != "/" && - req.originalParsedURL.pathname[ - req.originalParsedURL.pathname.length - 1 - ] != "/" - ) { - fs.stat( - "." + decodeURIComponent(req.parsedURL.pathname), - function (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.hash); + if ( + !req.isProxy && + !config.disableTrailingSlashRedirects && + req.parsedURL.pathname[req.parsedURL.pathname.length - 1] != "/" && + req.originalParsedURL.pathname[req.originalParsedURL.pathname.length - 1] != + "/" + ) { + fs.stat( + "." + decodeURIComponent(req.parsedURL.pathname), + function (err, stats) { + if (err || !stats.isDirectory()) { + try { + next(); + } catch (err) { + res.error(500, err); } - }, - ); - } else { - next(); - } - + } else { + res.redirect( + req.originalParsedURL.pathname + + "/" + + req.parsedURL.search + + req.parsedURL.hash, + ); + } + }, + ); + } else { + next(); + } }; diff --git a/src/middleware/rewriteURL.js b/src/middleware/rewriteURL.js index 3d3ff08..c2bad5f 100644 --- a/src/middleware/rewriteURL.js +++ b/src/middleware/rewriteURL.js @@ -125,7 +125,8 @@ module.exports = (req, res, logFacilities, config, next) => { logFacilities.errmessage("Content blocked."); return; } else if (sHref != req.parsedURL.pathname) { - var rewrittenAgainURL = sHref + req.parsedURL.search + req.parsedURL.hash; + var rewrittenAgainURL = + sHref + req.parsedURL.search + req.parsedURL.hash; logFacilities.resmessage( "URL sanitized: " + req.url + " => " + rewrittenAgainURL, ); diff --git a/src/middleware/urlSanitizer.js b/src/middleware/urlSanitizer.js index 09cef4d..3acb72e 100644 --- a/src/middleware/urlSanitizer.js +++ b/src/middleware/urlSanitizer.js @@ -11,7 +11,8 @@ module.exports = (req, res, logFacilities, config, next) => { // Check if URL is "dirty" if (req.parsedURL.pathname != sanitizedHref && !req.isProxy) { - let sanitizedURL = sanitizedHref + req.parsedURL.search + req.parsedURL.hash; + let sanitizedURL = + sanitizedHref + req.parsedURL.search + req.parsedURL.hash; logFacilities.resmessage( "URL sanitized: " + req.url + " => " + sanitizedURL, ); diff --git a/src/middleware/webRootPostfixes.js b/src/middleware/webRootPostfixes.js index 592e7dd..7031a23 100644 --- a/src/middleware/webRootPostfixes.js +++ b/src/middleware/webRootPostfixes.js @@ -1,4 +1,3 @@ -const url = require("url"); const createRegex = require("../utils/createRegex.js"); const ipMatch = require("../utils/ipMatch.js"); const sanitizeURL = require("../utils/urlSanitizer.js"); @@ -113,7 +112,8 @@ module.exports = (req, res, logFacilities, config, next) => { logFacilities.errmessage("Content blocked."); return; } else if (sHref != req.parsedURL.pathname) { - let rewrittenAgainURL = sHref + req.parsedURL.search + req.parsedURL.hash; + let rewrittenAgainURL = + sHref + req.parsedURL.search + req.parsedURL.hash; logFacilities.resmessage( "URL sanitized: " + req.url + " => " + rewrittenAgainURL, );