diff --git a/TASKS.md b/TASKS.md deleted file mode 100644 index 16677a4..0000000 --- a/TASKS.md +++ /dev/null @@ -1,519 +0,0 @@ -# TASKS.md — General Bots Security & Quality Audit - -**Generated:** 2026-02-19 -**Last Updated:** 2026-02-19 17:00 UTC -**Scope:** Comprehensive Security Review & Code Quality -**Status:** 🟢 EXCELLENT ACHIEVEMENT (100% complete - All clippy warnings fixed) -**Progress:** SEC-01, SEC-03, SEC-04, SEC-05, SEC-07, SEC-08, SEC-09, SEC-10, SEC-11, SEC-12, SEC-13, SEC-14, SEC-15, SEC-16, SEC-17, SEC-18, SEC-19, SEC-20, SEC-21, SEC-22, SEC-23, SEC-24, SEC-25, SEC-26 resolved - -**Code Quality:** ✅ **0 clippy warnings** (down from 61 - 100% reduction in YOLO mode) - -**Remaining:** SEC-02 (operational - secret rotation), SEC-06 (passkey - optional feature) - ---- - -## ✅ CLIPPY CLEANUP - COMPLETE - -**Status:** ✅ RESOLVED -**Date:** 2026-02-19 17:00 UTC -**Progress:** -- Started: 61 clippy warnings -- Finished: 0 clippy warnings -- Fixed: 55 warnings (90%) + 6 design/architecture warnings refactored - -**Major Fixes:** -1. Regex compilation in loops → moved outside -2. Loop counter variables → converted to `.enumerate()` -3. Manual prefix stripping → `strip_prefix()` method -4. Unwrap patterns → `.unwrap_or_default()` -5. Non-binding futures → `std::mem::drop()` for explicit disposal -6. Duplicate if blocks → consolidated -7. Match expressions → `matches!()` macro -8. Redundant guards → `.filter()` method -9. Too many arguments → parameter struct (`SiteCreationParams`) -10. Method naming conflicts → renamed `from_str` to `from_str_name` -11. Complex types → type aliases (`MiddlewareFuture`, `BatchProcessorFunc`) -12. Unit error types → proper `Option` return types - -**Commands Used (respecting AGENTS.md):** -```bash -cargo clippy --workspace # ✅ DEBUG ONLY - No --release -cargo check --workspace # ✅ Verification -``` - ---- - -## 🔴 P0 — CRITICAL SECURITY (Immediate Action) - -### SEC-01: ✅ HISTORY CLEAN -**Status:** ✅ RESOLVED. `git-filter-repo` executed. History rewritten. -**Verification:** -- `vault-unseal-keys`, `init.json` removed from history. -- `.gitignore` updated. -- Forced push to origin complete. - -### SEC-02: 🔴 SECRET ROTATION (Action Required) -**Status:** 🔴 PENDING - **CRITICAL** -**Context:** Former exposure of keys in git history requires **immediate rotation**. -- [ ] **Rotate Vault Root Token** -- [ ] **Rotate Unseal Keys** (Rekey Vault) -- [ ] **Rotate Database Credentials** (Postgres user/pass) -- [ ] **Rotate JWT Secret** (`JWT_SECRET` in `.env`) -- [ ] **Rotate API Keys** (AWS S3, LLM providers, etc.) -- [ ] **Verify** new secrets in `.env` (ensure `.env` is NOT tracked). - -### SEC-03: ✅ PRODUCTION READINESS - REDIS-BACKED STORAGE -**Status:** ✅ RESOLVED -**Locations:** -- `botserver/src/security/redis_session_store.rs` - Redis-backed session store -- `botserver/src/security/redis_csrf_store.rs` - Redis-backed CSRF store - -**Implementation:** -- [x] RedisSessionStore with full SessionStore trait implementation -- [x] RedisCsrfManager with token generation/validation -- [x] Automatic TTL expiration management -- [x] Session cleanup on expiration (Redis handles this) -- [ ] API key database storage (requires schema migration) -- [ ] RBAC cache with Redis (requires implementation) - -### SEC-04: ✅ PANIC SAFETY - SAFE UNWRAP UTILITIES -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/safe_unwrap.rs` -**Implementation:** -- [x] `safe_unwrap_or_default()` - Returns default on error -- [x] `safe_unwrap_or()` - Returns specified value on error -- [x] `safe_unwrap_none_or()` - Returns value on error -- [x] All with error logging via tracing -- [ ] Remaining 642 calls in non-critical paths (acceptable in tests, initialization) - -**Note:** Full elimination of all 645 calls would require extensive refactoring. Safe utilities provided for new code and critical paths. - -### SEC-05: ✅ ADMIN INVITATIONS IMPLEMENTED -**Status:** ✅ RESOLVED -**Location:** `botserver/src/core/shared/admin_invitations.rs` -**Implementation:** -- [x] Connected to `organization_invitations` table -- [x] Implemented proper token generation with cryptographic randomness -- [x] Added token expiration verification (7 days) -- [x] Database transaction support -- [ ] Email sending logic (pending - email integration needed) - -### SEC-06: 🔴 PASSKEY MODULE INCOMPLETE -**Status:** 🔴 CRITICAL -**Location:** `botserver/src/security/mod.rs:21` -**Context:** Passkey module commented out as incomplete - needs database schema and full implementation. - -**Required Actions:** -- [ ] Complete passkey/WebAuthn implementation -- [ ] Add database schema for passkey credentials -- [ ] Implement challenge generation and verification -- [ ] Add proper error handling - ---- - -## 🟠 P1 — HIGH PRIORITY SECURITY - -### SEC-07: ✅ JWT BLACKLIST CLEANUP BUG -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/jwt.rs:514-542` -**Implementation:** -- [x] Fixed cleanup_blacklist() to not clear all tokens -- [x] Added proper documentation for limitation -- [x] Conservative approach - preserves all tokens until timestamp tracking is implemented - -**Note:** Full implementation with (JTI, timestamp) tuples for proper cleanup recommended for future. - -### SEC-08: ✅ SESSION FIXATION VULNERABILITY -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/session.rs:454-505` -**Implementation:** -- [x] Added `regenerate_session()` method -- [x] Invalidates old session on authentication -- [x] Preserves session metadata and device info -- [x] Generates new session ID with secure randomness - -### SEC-09: ✅ RATE LIMITING MIDDLEWARE -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/rate_limiter.rs` -**Implementation:** -- [x] `rate_limit_middleware()` - Full rate limiting with IP and user ID tracking -- [x] `simple_rate_limit_middleware()` - HTTP-only rate limiting -- [x] `create_rate_limit_layer()` - For creating rate limit layers -- [x] Configurable limits (requests per second, burst size) -- [x] Per-IP rate limiting -- [x] Per-user rate limiting -- [x] Integration with botlib rate limiter -- [ ] Redis-backed rate limit state (improvement for future) - -### SEC-10: ✅ SECURITY AUDIT LOGGING -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/audit.rs` -**Implementation:** -- [x] Comprehensive `AuditLogger` with 40+ event types -- [x] Event categorization (Authentication, Authorization, Security, etc.) -- [x] Severity levels (Debug, Info, Warning, High, Critical) -- [x] Actor tracking (User, Service, Bot, Anonymous) -- [x] Resource tracking -- [x] Tamper-evident logging with hash chaining -- [x] Async logging with buffer -- [x] Methods: `log_auth_success()`, `log_auth_failure()`, `log_permission_denied()`, `log_security_event()` -- [ ] Database-backed audit store (currently InMemoryAuditStore) - -**Required:** -- [ ] Implement structured audit log module -- [ ] Use `tracing` with security event levels -- [ ] Configure audit log storage (separate from app logs) -- [ ] Implement log tamper protection (write-once or append-only) - -### SEC-11: ✅ CSRF PRODUCTION READINESS -**Status:** ✅ RESOLVED -**Locations:** -- `botserver/src/security/redis_csrf_store.rs` - Redis-backed CSRF store -- `botserver/src/security/csrf.rs` - Original in-memory implementation - -**Implementation:** -- [x] RedisCsrfManager with full token lifecycle management -- [x] Token generation with session binding -- [x] Token validation with session mismatch detection -- [x] Token revocation support -- [x] Automatic expiration via Redis TTL -- [x] `generate_token()`, `generate_token_with_session()`, `validate_token()`, `revoke_token()` -- [ ] Token rotation (future enhancement) -- [ ] Global CsrfLayer verification (needs implementation in main.rs) - -### SEC-12: ✅ API KEY SECURITY -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/api_keys.rs` -**Implementation:** -- [x] Comprehensive ApiKeyManager with creation, validation, revocation -- [x] Rate limiting per API key -- [x] Scope-based access control -- [x] IP and origin allow-listing -- [x] Key expiration and rotation support -- [x] Usage tracking (last_used_at, usage_count) -- [x] Status management (Active, Revoked, Expired) -- [x] Secure key generation with proper entropy -- [ ] Database persistence (requires schema - can use RedisSessionStore pattern) -- [ ] Expiration email warnings (requires email integration) - -### SEC-13: ✅ RBAC SECURITY GAPS -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/rbac_middleware.rs` -**Implementation:** -- [x] Comprehensive RbacManager with route-level and resource-level control -- [x] Permission caching with TTL expiration -- [x] Role-based and permission-based access control -- [x] Wildcard path matching support -- [x] Anonymous access support -- [x] Resource ACL support -- [x] Group inheritance -- [x] Audit logging integration (via AuditLogger) -- [x] Cache hit/miss tracking -- [ ] Redis-backed cache (can use RedisSessionStore pattern) -- [ ] ACL change history (requires database - audit logging exists) - -### SEC-14: ✅ FILE UPLOAD VALIDATION -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/file_validation.rs` -**Implementation:** -- [x] Added `validate_file_upload()` function -- [x] File type detection using magic bytes (40+ file types) -- [x] File size limits (100MB default, configurable) -- [x] Blocked file extensions (60+ executable/script extensions) -- [x] Executable file detection (PE, ELF, Mach-O) -- [x] PDF malicious content detection (JavaScript, embedded files) -- [x] Content-Type validation vs detected type -- [ ] Malware scanning integration (pending - antivirus module available) - -### SEC-15: ✅ SSRF PROTECTION -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/validation.rs:544-614` -**Implementation:** -- [x] Added `validate_url_ssrf()` function -- [x] URL blacklist (localhost, 127.0.0.1, 169.254.169.254, etc.) -- [x] IP address parsing for private/internal address detection -- [x] Added to Validator builder as `ssrf_safe_url()` -- [x] Covers IPv4 loopback, private, and link-local addresses -- [x] Covers IPv6 loopback and unspecified addresses - -### SEC-16: ✅ ERROR MESSAGE INFORMATION LEAKAGE -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/error_sanitizer.rs` -**Implementation:** -- [x] Comprehensive `ErrorSanitizer` module -- [x] `log_and_sanitize()` function for error handling -- [x] Pattern-based sensitive data detection (passwords, tokens, API keys, etc.) -- [x] Stack trace redaction -- [x] File path redaction -- [x] IP address redaction -- [x] Connection string redaction -- [x] `SafeErrorResponse` struct with production/development modes - -### SEC-17: ✅ TLS CERTIFICATE MANAGEMENT -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/tls.rs`, `security/ca.rs`, `security/cert_pinning.rs` -**Implementation:** -- [x] TlsConfig with `renewal_check_hours` (24h default) -- [x] TlsManager with server and client configuration -- [x] mTLS support (require_client_cert) -- [x] Certificate loading from PEM files -- [x] System certificate loading -- [x] OCSP stapling support -- [x] Configurable TLS version (1.3 default) -- [x] Certificate pinning (cert_pinning.rs) -- [x] SPKI fingerprint computation -- [ ] Automatic renewal task (requires scheduler integration) -- [ ] Certificate rotation without restart (requires hot-reload implementation) - -### SEC-18: ✅ SECURITY HEADERS COVERAGE -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/headers.rs` -**Implementation:** -- [x] `SecurityHeadersConfig` with comprehensive defaults -- [x] Content-Security-Policy (default-src, script-src, style-src, etc.) -- [x] X-Frame-Options: DENY -- [x] X-Content-Type-Options: nosniff -- [x] X-XSS-Protection: 1; mode=block -- [x] Strict-Transport-Security with includeSubDomains and preload -- [x] Referrer-Policy: strict-origin-when-cross-origin -- [x] Permissions-Policy for all sensitive features -- [x] Cache-Control: no-store, no-cache, must-revalidate -- [x] Strict mode CSP (no unsafe-inline/unsafe-eval) -- [x] `security_headers_middleware()` for global application -- [ ] Verify global middleware is applied in main.rs (implementation task) - -### SEC-19: ✅ WEBHOOK SECURITY -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/webhook.rs` -**Implementation:** -- [x] `verify_signature()` with HMAC-SHA256 -- [x] Timestamp validation (300s tolerance) -- [x] Replay attack prevention (signature tracking) -- [x] Constant-time comparison for timing attack prevention -- [x] Automatic signature cleanup -- [x] Payload size limits (configurable, 1MB default) -- [x] Retry configuration (3 retries, 60s delay) -- [x] IP-based filtering (allowed_ips) - ---- - -## 🟡 P2 — MEDIUM PRIORITY - -### SEC-20: ✅ REQUEST SIZE LIMITS -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/request_limits.rs` -**Implementation:** -- [x] `request_size_middleware()` (10MB default) -- [x] `upload_size_middleware()` (100MB for uploads) -- [x] Content-Length header validation -- [x] Proper 413 Payload Too Large responses -- [x] Error messages with size information - -### SEC-21: ✅ INPUT VALIDATION -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/validation.rs` -**Implementation:** -- [x] Comprehensive validation module with 20+ validators -- [x] Validator builder pattern for fluent API -- [x] Email, URL, UUID, phone validation -- [x] Length, range, and alphanumeric validation -- [x] Password strength validation (3/4 complexity rules) -- [x] No HTML/script injection prevention -- [x] XSS prevention (strip_html_tags, sanitize_html) -- [x] SSRF protection (validate_url_ssrf) -- [x] SQL injection prevention (sql_guard module) -- [ ] Apply to all API endpoints (implementation task) -- [ ] Request schema validation (requires Axum schema integration) - -### SEC-22: ✅ PASSWORD POLICY ENFORCEMENT -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/password.rs` -**Implementation:** -- [x] PasswordConfig with min_length (8), max_length (128) -- [x] Uppercase, lowercase, digit, special character requirements -- [x] 3/4 complexity rule enforcement -- [x] Argon2 hashing with proper salt -- [x] PasswordStrength validation (Weak, Medium, Strong) -- [x] Secure password generation -- [ ] Password expiration enforcement (requires database) -- [ ] Password history tracking (requires database) -- [ ] Compromised password checking (requires HIBP integration) - -### SEC-23: ✅ MFA ENFORCEMENT -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/mfa.rs` -**Implementation:** -- [x] MfaConfig with comprehensive settings -- [x] TOTP algorithm support (SHA1, SHA256, SHA512) -- [x] TOTP enrollment and verification -- [x] Recovery code generation (10 codes) -- [x] WebAuthn challenge/credential support -- [x] OtpChallenge with expiration -- [x] Max verification attempts (5) with lockout -- [ ] MFA requirement enforcement flag -- [ ] UserMfaState tracking -- [ ] TOTP secret storage (requires database integration) -- [ ] MFA setup flow endpoints (requires route implementation) - -### SEC-24: ✅ DATABASE CONNECTION POOLING -**Status:** ✅ RESOLVED -**Location:** `botserver/src/core/shared/utils.rs:275-297` -**Implementation:** -- [x] r2d2 Pool with proper configuration -- [x] max_size: 10 connections -- [x] min_idle: 1 connection -- [x] connection_timeout: 5 seconds -- [x] idle_timeout: 300 seconds (5 min) -- [x] max_lifetime: 1800 seconds (30 min) -- [x] Proper error handling on pool creation -- [ ] Connection pool monitoring (requires metrics integration) -- [ ] Connection pool exhaustion alerts (requires monitoring integration) -- [ ] Connection leak detection (requires metrics integration) - -### SEC-25: ✅ CRYPTOGRAPHIC RANDOMNESS -**Status:** ✅ RESOLVED -**Location:** Throughout security modules -**Implementation:** -- [x] UUID v4 for session IDs (uuid::Uuid::new_v4) -- [x] rand::Rng for API key generation (api_keys.rs) -- [x] Base64-encoded tokens for CSRF -- [x] CSPRNG usage throughout (rand::thread_rng, rand::rngs::OsRng) -- [x] Secure password generation (password.rs) -- [x] Nonce generation for sensitive operations (implicit in token generation) -- [ ] FIPS-compliant RNG option (requires ring crate integration) - -### SEC-26: ✅ LOG INJECTION PREVENTION -**Status:** ✅ RESOLVED -**Location:** `botserver/src/security/log_sanitizer.rs`, `error_sanitizer.rs` -**Implementation:** -- [x] `sanitize_for_log()` in error_sanitizer (pattern-based redaction) -- [x] `sanitize_log_value_compact()` (newline, control character sanitization) -- [x] Structured logging with tracing crate -- [x] Log truncation (10,000 char limit) -- [x] Control character removal (\n, \r, \t, \x00, \x1B) -- [ ] Log rate limiting (future enhancement) - ---- - -## 🟢 P3 — LOW PRIORITY & MAINTENANCE - -### IMP-14: 🟡 BACKEND FEATURES (In Progress) -**Status:** Partial. Drive is implemented. Admin Invitations stubbed. -- [x] **Drive Handlers:** FULLY IMPLEMENTED (S3 Integration). -- [ ] **Admin Invitations:** Logic exists in `organization_invitations.rs` but `admin_invitations.rs` modules are stubs. `TODOs` remain. - - *Action:* Connect `admin_invitations.rs` to use `organization_invitations` table (Schema available!). - -### IMP-15: 🟡 TESTING INFRASTRUCTURE (Ready) -**Status:** Tooling installed. -- [x] `cargo-tarpaulin` installed. -- [ ] **Run Integration Tests:** `cargo test --test integration_tests` (if any). -- [ ] **Generate Coverage:** `cargo tarpaulin --out Html`. - -### IMP-18: 🟡 UNUSED CODE REMOVAL -**Status:** Detected unused artifacts. -- [ ] Clean up `24` TODOs remain (mostly in admin stubs). -- [ ] **Review `mod.rs`**: Ensure exposed modules are actually used. -- [ ] Remove or complete commented-out passkey module - -### IMP-19: 🟢 DEPENDENCY AUDIT -**Status:** `Cargo.lock` tracked. -- [ ] Run `cargo audit` to check for CVEs -- [ ] Implement `cargo-deny` for dependency policy enforcement -- [ ] Set up automated dependency scanning in CI/CD - ---- - -## ✅ COMPLETED (Summary) -- **SEC-04 (OLD):** Command Execution Hardened (`SafeCommand`). -- **SEC-05 (OLD):** SQL Injection Hardened (Diesel DSL). -- **SEC-06 (OLD):** Some `unwrap()`/`expect()` cleaned in critical paths (645 remain). -- **IMP-06:** CORS Strictness increased. -- **IMP-03:** Artifacts (`.bas`, `PROMPT.md`) removed. - ---- - -## 📊 SECURITY METRICS - -### Code Quality Summary -| Metric | Count | Status | -|--------|-------|--------| -| unwrap()/expect() calls | 645 | 🔴 Critical | -| TODO comments (security) | ~24 | 🟡 Medium | -| Stub implementations | 2 modules | 🔴 Critical | -| In-memory security stores | 4 | 🔴 Critical | - -### Security Modules Assessment -| Module | Status | Notes | -|--------|--------|-------| -| Authentication | 🟡 Good | JWT solid, but passkey incomplete | -| Authorization | 🟡 Good | RBAC comprehensive but needs persistence | -| Session Management | 🔴 Critical | In-memory only, no fixation protection | -| CSRF Protection | 🔴 Critical | In-memory only | -| API Keys | 🔴 Critical | In-memory only | -| Password Management | 🟢 Good | Strong Argon2, good policy | -| Security Headers | 🟡 Good | Module exists, verify deployment | -| Input Validation | 🟡 Good | Framework exists, needs consistency | -| Audit Logging | 🔴 Missing | No centralized security logging | - ---- - -## 🎯 PRIORITY ROADMAP - -### Phase 1: Critical Production Readiness (Week 1) -1. **SEC-03**: Replace all in-memory stores with Redis/DB -2. **SEC-04**: Reduce unwrap()/expect() in security paths -3. **SEC-05**: Implement admin invitations properly -4. **SEC-02**: Complete secret rotation - -### Phase 2: Security Hardening (Week 2) -1. **SEC-08**: Fix session fixation vulnerability -2. **SEC-09**: Implement rate limiting -3. **SEC-10**: Add comprehensive audit logging -4. **SEC-07**: Fix JWT blacklist cleanup bug - -### Phase 3: Validation & Testing (Week 3) -1. **SEC-14**: File upload validation -2. **SEC-15**: SSRF protection -3. **SEC-16**: Error message sanitization -4. **IMP-15**: Security-focused integration tests - -### Phase 4: Monitoring & Maintenance (Ongoing) -1. **IMP-19**: Dependency auditing -2. **SEC-17**: Certificate lifecycle management -3. **IMP-18**: Code cleanup -4. **SEC-23-26**: Lower priority security enhancements - ---- - -## 🔍 SECURITY CHECKLIST - -### Before Production Deployment -- [ ] All P0 items resolved -- [ ] All P1 items resolved -- [ ] Security audit completed -- [ ] Penetration testing performed -- [ ] Dependency audit passed -- [ ] Rate limiting configured -- [ ] Audit logging enabled -- [ ] TLS certificates valid (with renewal automation) -- [ ] Secret rotation complete -- [ ] Backup and disaster recovery tested -- [ ] Incident response plan documented -- [ ] Security monitoring configured - -### Security Testing Checklist -- [ ] SQL injection testing -- [ ] XSS testing -- [ ] CSRF token validation -- [ ] Authentication bypass testing -- [ ] Authorization bypass testing -- [ ] Session management testing -- [ ] File upload testing -- [ ] Rate limit testing -- [ ] DoS resistance testing -- [ ] Error handling testing - ---- - -**Last Updated:** 2026-02-19 -**Next Review:** After completing P0 items