towerops/lib/towerops_web/controllers/api/v1
Graham McIntire 6ef6b3d61d fix: comprehensive security audit fixes (#108)
This commit addresses multiple CRITICAL, HIGH, and MEDIUM severity security vulnerabilities identified in the security audit:

CRITICAL FIXES:
- Fix weak RNG for recovery codes - replaced Enum.random() with :crypto.strong_rand_bytes/1 for cryptographically secure token generation
- Fix subscription limit race conditions - moved free org and device quota checks inside transactions with FOR UPDATE locks to prevent concurrent bypass
- Fix default organization race condition - moved is_default check inside transaction to prevent multiple defaults per user

HIGH SEVERITY FIXES:
- Fix agent token deletion race condition - moved PubSub broadcast inside transaction to ensure agents only receive notification after successful deletion

MEDIUM SEVERITY FIXES:
- Fix LIKE wildcard injection in search - applied sanitize_like() to all user-facing search queries in devices.ex, sites.ex, and gaiia.ex to prevent enumeration attacks
- Fix Jason.decode! DoS - replaced with safe Jason.decode/1 with error handling in device_live/index.ex
- Fix SSRF vulnerability - added URL validation in HTTP executor to block requests to private/internal IP ranges (127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 169.254.0.0/16)
- Fix error information leakage - replaced inspect() in API responses with generic error messages, logging details server-side only
- Fix atom table pollution - HTTP method normalization now uses whitelist mapping instead of String.to_atom()

SECURITY IMPROVEMENTS:
- All quota checks now use pessimistic locking (SELECT FOR UPDATE) to prevent TOCTOU race conditions
- Private IP validation prevents cloud metadata service access (169.254.169.254)
- DNS resolution performed before HTTP requests to detect IP spoofing
- Error details logged server-side but not exposed to clients

Files changed:
- lib/towerops/accounts/user_recovery_code.ex
- lib/towerops/organizations.ex
- lib/towerops/devices.ex
- lib/towerops/sites.ex
- lib/towerops/gaiia.ex
- lib/towerops/agents.ex
- lib/towerops/monitoring/executors/http_executor.ex
- lib/towerops_web/live/device_live/index.ex
- lib/towerops_web/controllers/api/v1/mib_controller.ex
- lib/towerops_web/controllers/api/v1/agent_release_webhook_controller.ex
- lib/towerops_web/controllers/api/v1/geoip_controller.ex

Reviewed-on: graham/towerops-web#108
2026-03-22 10:10:27 -05:00
..
activity_controller.ex feat: expand REST API with alerts, agents, org settings, members, integrations, metrics, activity endpoints 2026-02-14 11:28:57 -06:00
agent_release_webhook_controller.ex fix: comprehensive security audit fixes (#108) 2026-03-22 10:10:27 -05:00
agents_controller.ex fix: resolve compilation errors, test failures, and credo issues 2026-02-14 12:23:10 -06:00
alerts_controller.ex fix: resolve compilation errors, test failures, and credo issues 2026-02-14 12:23:10 -06:00
check_results_controller.ex fix: resolve compilation errors, test failures, and credo issues 2026-02-14 12:23:10 -06:00
checks_controller.ex add query_helpers tests and fix all credo issues (#56) 2026-03-17 10:43:57 -05:00
devices_controller.ex fix: input validation, SSRF, API hardening, and cookie security 2026-03-14 14:48:59 -05:00
escalation_policies_controller.ex feat: add REST API and GraphQL for on-call schedules and escalation policies 2026-03-11 14:01:15 -05:00
gaiia_webhook_controller.ex Fix tests, credo issues, and Gaiia sync bugs 2026-02-15 11:55:49 -06:00
geoip_controller.ex fix: comprehensive security audit fixes (#108) 2026-03-22 10:10:27 -05:00
integrations_controller.ex feat: show human-readable sync status messages for integrations 2026-02-14 14:11:02 -06:00
invitations_controller.ex fix: resolve compilation errors, test failures, and credo issues 2026-02-14 12:23:10 -06:00
maintenance_windows_controller.ex add maintenance windows REST API and GraphQL support 2026-03-11 14:33:25 -05:00
members_controller.ex fix: resolve compilation errors, test failures, and credo issues 2026-02-14 12:23:10 -06:00
mib_controller.ex fix: comprehensive security audit fixes (#108) 2026-03-22 10:10:27 -05:00
organization_controller.ex fix: use correct auth check for API organization updates 2026-03-10 13:41:38 -05:00
pagerduty_webhook_controller.ex Security fixes: mask credentials in logs/API, fix cookie/CSP/LIKE injection/webhooks 2026-02-15 09:09:04 -06:00
schedules_controller.ex feat: add REST API and GraphQL for on-call schedules and escalation policies 2026-03-11 14:01:15 -05:00
sites_controller.ex Refactor duplicate site API logic 2026-03-10 16:27:35 -05:00
stripe_webhook_controller.ex stripe and email after signup 2026-03-06 10:07:27 -06:00