Harden API security: input validation, safe auth extraction, new tests, and deploy config
Comprehensive security hardening from audit findings: - Add validation tags to all DTO request structs (max lengths, ranges, enums) - Replace unsafe type assertions with MustGetAuthUser helper across all handlers - Remove query-param token auth from admin middleware (prevents URL token leakage) - Add request validation calls in handlers that were missing c.Validate() - Remove goroutines in handlers (timezone update now synchronous) - Add sanitize middleware and path traversal protection (path_utils) - Stop resetting admin passwords on migration restart - Warn on well-known default SECRET_KEY - Add ~30 new test files covering security regressions, auth safety, repos, and services - Add deploy/ config, audit digests, and AUDIT_FINDINGS documentation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -52,14 +52,18 @@ func (c *Collector) Collect() SystemStats {
|
||||
// CPU stats
|
||||
c.collectCPU(&stats)
|
||||
|
||||
// Read Go runtime memory stats once (used by both memory and runtime collectors)
|
||||
var memStats runtime.MemStats
|
||||
runtime.ReadMemStats(&memStats)
|
||||
|
||||
// Memory stats (system + Go runtime)
|
||||
c.collectMemory(&stats)
|
||||
c.collectMemory(&stats, &memStats)
|
||||
|
||||
// Disk stats
|
||||
c.collectDisk(&stats)
|
||||
|
||||
// Go runtime stats
|
||||
c.collectRuntime(&stats)
|
||||
c.collectRuntime(&stats, &memStats)
|
||||
|
||||
// HTTP stats (API only)
|
||||
if c.httpCollector != nil {
|
||||
@@ -77,9 +81,9 @@ func (c *Collector) Collect() SystemStats {
|
||||
}
|
||||
|
||||
func (c *Collector) collectCPU(stats *SystemStats) {
|
||||
// Get CPU usage percentage (blocks for 1 second to get accurate sample)
|
||||
// Shorter intervals can give inaccurate readings
|
||||
if cpuPercent, err := cpu.Percent(time.Second, false); err == nil && len(cpuPercent) > 0 {
|
||||
// Get CPU usage percentage (blocks for 200ms to sample)
|
||||
// This is called periodically, so a shorter window is acceptable
|
||||
if cpuPercent, err := cpu.Percent(200*time.Millisecond, false); err == nil && len(cpuPercent) > 0 {
|
||||
stats.CPU.UsagePercent = cpuPercent[0]
|
||||
}
|
||||
|
||||
@@ -93,7 +97,7 @@ func (c *Collector) collectCPU(stats *SystemStats) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) collectMemory(stats *SystemStats) {
|
||||
func (c *Collector) collectMemory(stats *SystemStats, m *runtime.MemStats) {
|
||||
// System memory
|
||||
if vmem, err := mem.VirtualMemory(); err == nil {
|
||||
stats.Memory.UsedBytes = vmem.Used
|
||||
@@ -101,9 +105,7 @@ func (c *Collector) collectMemory(stats *SystemStats) {
|
||||
stats.Memory.UsagePercent = vmem.UsedPercent
|
||||
}
|
||||
|
||||
// Go runtime memory
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
// Go runtime memory (reuses pre-read MemStats)
|
||||
stats.Memory.HeapAlloc = m.HeapAlloc
|
||||
stats.Memory.HeapSys = m.HeapSys
|
||||
stats.Memory.HeapInuse = m.HeapInuse
|
||||
@@ -119,10 +121,7 @@ func (c *Collector) collectDisk(stats *SystemStats) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Collector) collectRuntime(stats *SystemStats) {
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
|
||||
func (c *Collector) collectRuntime(stats *SystemStats, m *runtime.MemStats) {
|
||||
stats.Runtime.Goroutines = runtime.NumGoroutine()
|
||||
stats.Runtime.NumGC = m.NumGC
|
||||
if m.NumGC > 0 {
|
||||
|
||||
Reference in New Issue
Block a user