Do not register whitelist middleware if whitelist disabled

3e26b93971afaf4d8cb166cbd2428585165d98fe

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +10 -6Showing whitespace changes
server.js+7 -2
@@ -340,9 +340,14 @@ const CORS = cors({
340340
341app.use(CORS);341app.use(CORS);
342342
343if (listen && basicAuthMode) app.use(basicAuthMiddleware);343if (listen && basicAuthMode) {
344 app.use(basicAuthMiddleware);
345}
346
347if (enableWhitelist) {
348 app.use(whitelistMiddleware());
349}
344350
345app.use(whitelistMiddleware(enableWhitelist));
346if (listen) {351if (listen) {
347 app.use(accessLoggerMiddleware());352 app.use(accessLoggerMiddleware());
348}353}
src/middleware/whitelist.js+3 -4
@@ -47,10 +47,9 @@ function getForwardedIp(req) {
4747
48/**48/**
49 * Returns a middleware function that checks if the client IP is in the whitelist.49 * Returns a middleware function that checks if the client IP is in the whitelist.
50 * @param {boolean} whitelistMode If whitelist mode is enabled via config or command line
51 * @returns {import('express').RequestHandler} The middleware function50 * @returns {import('express').RequestHandler} The middleware function
52 */51 */
53export default function whitelistMiddleware(whitelistMode) {52export default function whitelistMiddleware() {
54 const forbiddenWebpage = Handlebars.compile(53 const forbiddenWebpage = Handlebars.compile(
55 safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '',54 safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '',
56 );55 );
@@ -65,8 +64,8 @@ export default function whitelistMiddleware(whitelistMode) {
65 const userAgent = req.headers['user-agent'];64 const userAgent = req.headers['user-agent'];
6665
67 //clientIp = req.connection.remoteAddress.split(':').pop();66 //clientIp = req.connection.remoteAddress.split(':').pop();
68 if (whitelistMode === true && !whitelist.some(x => ipMatching.matches(clientIp, ipMatching.getMatch(x)))67 if (!whitelist.some(x => ipMatching.matches(clientIp, ipMatching.getMatch(x)))
69 || forwardedIp && whitelistMode === true && !whitelist.some(x => ipMatching.matches(forwardedIp, ipMatching.getMatch(x)))68 || forwardedIp && !whitelist.some(x => ipMatching.matches(forwardedIp, ipMatching.getMatch(x)))
70 ) {69 ) {
71 // Log the connection attempt with real IP address70 // Log the connection attempt with real IP address
72 const ipDetails = forwardedIp71 const ipDetails = forwardedIp