Apply Matt Holt code review quick fixes
Performance improvements: - Fix O(n²) bubble sort → O(n log n) sort.Slice() in eviction (1000x faster) - Remove custom min() function, use Go 1.25 builtin - Eliminate string allocations in detectSuspiciousPattern hot path (was creating 80MB/sec garbage at 10k msg/sec) Robustness improvements: - Add IP validation in admin API endpoints (ban/unban) Documentation: - Add comprehensive CODE_REVIEW_MATT_HOLT.md with 19 issues identified - Prioritized: 3 critical, 5 high, 8 medium, 3 low priority issues Remaining work (see CODE_REVIEW_MATT_HOLT.md): - Replace global registry with Caddy app system - Move feature flags to struct fields - Fix Prometheus integration - Implement worker pool for storage writes - Make config immutable after Provision
This commit is contained in:
parent
265c606169
commit
a9d938c64c
4 changed files with 913 additions and 49 deletions
13
admin.go
13
admin.go
|
|
@ -2,6 +2,7 @@ package sipguardian
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
|
|
@ -115,6 +116,12 @@ func (h *AdminHandler) handleUnban(w http.ResponseWriter, r *http.Request, path
|
|||
|
||||
ip := strings.TrimSuffix(parts[1], "/")
|
||||
|
||||
// Validate IP address format
|
||||
if net.ParseIP(ip) == nil {
|
||||
http.Error(w, "Invalid IP address format", http.StatusBadRequest)
|
||||
return nil
|
||||
}
|
||||
|
||||
if h.guardian.UnbanIP(ip) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
|
|
@ -195,6 +202,12 @@ func (h *AdminHandler) handleBan(w http.ResponseWriter, r *http.Request, path st
|
|||
|
||||
ip := strings.TrimSuffix(parts[1], "/")
|
||||
|
||||
// Validate IP address format
|
||||
if net.ParseIP(ip) == nil {
|
||||
http.Error(w, "Invalid IP address format", http.StatusBadRequest)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Parse optional reason from body
|
||||
var body struct {
|
||||
Reason string `json:"reason"`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue