From e40cb3501161b84cab3195f1ab809343cc7ff5a6 Mon Sep 17 00:00:00 2001 From: Hermes Date: Wed, 10 Jun 2026 20:13:53 -0700 Subject: [PATCH] Add MONITORING_PUBLIC env var to gate monitoring endpoints behind auth By default /api/v1/monitoring/stats and /api/v1/health-checks/status are public (current behavior, dashboard needs them pre-login). Users deploying DashCaddy on the open internet can now set: MONITORING_PUBLIC=false ...or add 'monitoring: { public: false }' to config.json to require auth. This prevents anonymous disclosure of CPU/memory/disk data. The check uses env var first, then config.json, then defaults to true (preserves current behavior for existing users). --- dashcaddy-api/middleware.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/dashcaddy-api/middleware.js b/dashcaddy-api/middleware.js index 517d559..3dd00bc 100644 --- a/dashcaddy-api/middleware.js +++ b/dashcaddy-api/middleware.js @@ -277,6 +277,25 @@ module.exports = function configureMiddleware(app, { } // ── Public routes (bypass TOTP and JWT auth) ── + // Routes here are accessible without authentication. By default the + // monitoring/health-check endpoints are public so the dashboard can + // render widgets before the user logs in. Set MONITORING_PUBLIC=false + // (env var) or `monitoring: { public: false }` (config.json) to require + // auth for these — useful for internet-exposed deployments where + // CPU/memory/disk data is sensitive. + const MONITORING_PUBLIC = (() => { + if (process.env.MONITORING_PUBLIC === 'false') return false; + if (process.env.MONITORING_PUBLIC === 'true') return true; + // Default: check config.json if loaded + try { + const cfg = require('./src/config/site').siteConfig; + if (cfg && cfg.monitoring && typeof cfg.monitoring.public === 'boolean') { + return cfg.monitoring.public; + } + } catch { /* config not loaded yet, use default */ } + return true; // default: public (current behavior, dashboard needs it) + })(); + const PUBLIC_ROUTES = [ { path: '/health', exact: true }, { path: '/api/v1/health', exact: true }, @@ -305,8 +324,11 @@ module.exports = function configureMiddleware(app, { { path: '/api/v1/config', exact: true, method: 'GET' }, { path: '/api/v1/services/status', exact: true, method: 'GET' }, { path: '/api/v1/system/update-notify', exact: true, method: 'POST' }, - { path: '/api/v1/monitoring/stats', exact: true, method: 'GET' }, - { path: '/api/v1/health-checks/status', exact: true, method: 'GET' }, + // Monitoring endpoints — only public if MONITORING_PUBLIC is true + ...(MONITORING_PUBLIC ? [ + { path: '/api/v1/monitoring/stats', exact: true, method: 'GET' }, + { path: '/api/v1/health-checks/status', exact: true, method: 'GET' }, + ] : []), { path: '/api/v1/version', exact: true, method: 'GET' }, ];