Do not register whitelist middleware if whitelist disabled
| @@ -340,9 +340,14 @@ const CORS = cors({ | ||
| 340 | 340 | |
| 341 | 341 | app.use(CORS); |
| 342 | 342 | |
| 343 | 343 | if (listen && basicAuthMode) app.use(basicAuthMiddleware);{ |
| 344 | + app.use(basicAuthMiddleware); | |
| 345 | +} | |
| 346 | + | |
| 347 | +if (enableWhitelist) { | |
| 348 | + app.use(whitelistMiddleware()); | |
| 349 | +} | |
| 344 | 350 | |
| 345 | -app.use(whitelistMiddleware(enableWhitelist)); | |
| 346 | 351 | if (listen) { |
| 347 | 352 | app.use(accessLoggerMiddleware()); |
| 348 | 353 | } |
| @@ -47,10 +47,9 @@ function getForwardedIp(req) { | ||
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | 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 | 50 | * @returns {import('express').RequestHandler} The middleware function |
| 52 | 51 | */ |
| 53 | 52 | export default function whitelistMiddleware(whitelistMode) { |
| 54 | 53 | const forbiddenWebpage = Handlebars.compile( |
| 55 | 54 | safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '', |
| 56 | 55 | ); |
| @@ -65,8 +64,8 @@ export default function whitelistMiddleware(whitelistMode) { | ||
| 65 | 64 | const userAgent = req.headers['user-agent']; |
| 66 | 65 | |
| 67 | 66 | //clientIp = req.connection.remoteAddress.split(':').pop(); |
| 68 | 67 | if (whitelistMode === true && !whitelist.some(x => ipMatching.matches(clientIp, ipMatching.getMatch(x))) |
| 69 | 68 | || forwardedIp && whitelistMode === true && !whitelist.some(x => ipMatching.matches(forwardedIp, ipMatching.getMatch(x))) |
| 70 | 69 | ) { |
| 71 | 70 | // Log the connection attempt with real IP address |
| 72 | 71 | const ipDetails = forwardedIp |