feat: add environment variable validation on startup

- Created validateEnv utility for comprehensive environment validation
  - Validates required variables: JWT_SECRET, COUCHDB_URL, COUCHDB_DB_NAME
  - Validates optional variables with defaults: NODE_ENV, PORT, FRONTEND_URL
  - Enforces JWT_SECRET minimum length of 32 characters for security
  - Validates URL formats for COUCHDB_URL and FRONTEND_URL
  - Validates CouchDB database name format
  - Warns about missing optional services in production

- Integrated validation into server startup
  - Server exits with clear error messages if configuration is invalid
  - Logs environment configuration on startup (masks sensitive values)

- Updated test setup
  - Set proper 32+ character JWT_SECRET for tests
  - Added all required environment variables for validation

Security Benefits:
- Prevents server from starting with weak or missing credentials
- Catches configuration errors early before database connections
- Provides clear guidance on required variables
- Protects against default/example credentials in production

🤖 Generated with AI Assistant

Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
William Valentin
2025-11-03 13:07:26 -08:00
parent b614ca5739
commit 928d9a65fe
3 changed files with 205 additions and 2 deletions

View File

@@ -12,6 +12,17 @@ const { errorHandler } = require("./middleware/errorHandler");
const socketAuth = require("./middleware/socketAuth");
const requestLogger = require("./middleware/requestLogger");
const logger = require("./utils/logger");
const { validateEnv, logEnvConfig } = require("./utils/validateEnv");
// Validate environment variables before starting server
try {
validateEnv();
logEnvConfig();
} catch (error) {
logger.error('Environment validation failed:');
logger.error(error.message);
process.exit(1);
}
const app = express();
const server = http.createServer(app);