# Phase 1: Security & Infrastructure **Duration:** 4 weeks **Team:** 1 Backend Developer + 1 DevOps Engineer **Goal:** Fix critical vulnerabilities, establish production-ready infrastructure --- ## Week 1: Critical Security Fixes ### Day 1-2: JWT Secret & Rate Limiting **SEC-1: JWT Secret Hardcoded (CRITICAL)** - [ ] Remove hardcoded JWT secret from source code - [ ] Add JWT_SECRET environment variable to .env - [ ] Update server/src/auth/ to read from env - [ ] Generate strong random secret (64+ chars) - [ ] Document secret rotation procedure - [ ] Test authentication with new secret - [ ] Verify old tokens rejected after rotation **SEC-2: Rate Limiting (CRITICAL)** - [ ] Install tower-governor or similar rate limiting middleware - [ ] Add rate limiting to /api/auth/login (5 attempts/minute) - [ ] Add rate limiting to /api/auth/register (2 attempts/minute) - [ ] Add rate limiting to support code validation (10 attempts/minute) - [ ] Add IP-based tracking - [ ] Test rate limiting with automated requests - [ ] Add rate limit headers (X-RateLimit-Remaining, etc.) ### Day 3: SQL Injection Prevention **SEC-3: SQL Injection in Machine Filters (CRITICAL)** - [ ] Audit all raw SQL queries in server/src/db/ - [ ] Replace string concatenation with sqlx parameterized queries - [ ] Focus on machine_filters.rs (high risk) - [ ] Review user_queries.rs for injection points - [ ] Add input validation for filter parameters - [ ] Test with SQL injection payloads ('; DROP TABLE--, etc.) - [ ] Document safe query patterns for team ### Day 4-5: Agent & Session Security **SEC-4: Agent Connection Validation (CRITICAL)** - [ ] Implement support code validation in relay handler - [ ] Implement API key validation for persistent agents - [ ] Reject connections without valid credentials - [ ] Add connection attempt logging - [ ] Test with invalid codes/keys - [ ] Add IP whitelisting option for agents - [ ] Document agent authentication flow **SEC-5: Session Takeover Prevention (CRITICAL)** - [ ] Add session ownership validation - [ ] Verify JWT user_id matches session creator - [ ] Prevent cross-user session access - [ ] Add session token binding (tie to initial connection) - [ ] Test with stolen session IDs - [ ] Add session hijacking detection (IP change alerts) - [ ] Implement session timeout (4-hour max) --- ## Week 2: High-Priority Security ### Day 1: Logging & HTTPS **SEC-6: Password Logging (HIGH)** - [ ] Audit all logging statements for sensitive data - [ ] Remove password/token logging from auth.rs - [ ] Add [REDACTED] filter for sensitive fields - [ ] Update tracing configuration - [ ] Test logs don't contain credentials - [ ] Document logging security policy **SEC-10: HTTPS Enforcement (HIGH)** - [ ] Add HTTPS redirect middleware - [ ] Configure HSTS headers (max-age=31536000) - [ ] Update NPM to enforce HTTPS - [ ] Test HTTP requests redirect to HTTPS - [ ] Add secure cookie flags (Secure, HttpOnly) - [ ] Update documentation with HTTPS URLs ### Day 2-3: Input Sanitization **SEC-7: XSS Prevention (HIGH)** - [ ] Install validator crate for input sanitization - [ ] Sanitize all user inputs in API endpoints - [ ] Escape HTML in machine names, notes, tags - [ ] Add Content-Security-Policy headers - [ ] Test with XSS payloads (