Major security, observability, and configuration improvements:
## Security Hardening
- Implemented configurable CORS (no more wildcards)
- Added comprehensive auth middleware for admin endpoints
- Enhanced webhook HMAC validation
- Added input validation and rate limiting
- Security headers and CSP policies
## Configuration Management
- Made N8N webhook URL configurable (WHOOSH_N8N_BASE_URL)
- Replaced all hardcoded endpoints with environment variables
- Added feature flags for LLM vs heuristic composition
- Gitea fetch hardening with EAGER_FILTER and FULL_RESCAN options
## API Completeness
- Implemented GetCouncilComposition function
- Added GET /api/v1/councils/{id} endpoint
- Council artifacts API (POST/GET /api/v1/councils/{id}/artifacts)
- /admin/health/details endpoint with component status
- Database lookup for repository URLs (no hardcoded fallbacks)
## Observability & Performance
- Added OpenTelemetry distributed tracing with goal/pulse correlation
- Performance optimization database indexes
- Comprehensive health monitoring
- Enhanced logging and error handling
## Infrastructure
- Production-ready P2P discovery (replaces mock implementation)
- Removed unused Redis configuration
- Enhanced Docker Swarm integration
- Added migration files for performance indexes
## Code Quality
- Comprehensive input validation
- Graceful error handling and failsafe fallbacks
- Backwards compatibility maintained
- Following security best practices
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package middleware
|
|
|
|
// Ported from Goji's middleware, source:
|
|
// https://github.com/zenazn/goji/tree/master/web/middleware
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
var trueClientIP = http.CanonicalHeaderKey("True-Client-IP")
|
|
var xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
|
|
var xRealIP = http.CanonicalHeaderKey("X-Real-IP")
|
|
|
|
// RealIP is a middleware that sets a http.Request's RemoteAddr to the results
|
|
// of parsing either the True-Client-IP, X-Real-IP or the X-Forwarded-For headers
|
|
// (in that order).
|
|
//
|
|
// This middleware should be inserted fairly early in the middleware stack to
|
|
// ensure that subsequent layers (e.g., request loggers) which examine the
|
|
// RemoteAddr will see the intended value.
|
|
//
|
|
// You should only use this middleware if you can trust the headers passed to
|
|
// you (in particular, the two headers this middleware uses), for example
|
|
// because you have placed a reverse proxy like HAProxy or nginx in front of
|
|
// chi. If your reverse proxies are configured to pass along arbitrary header
|
|
// values from the client, or if you use this middleware without a reverse
|
|
// proxy, malicious clients will be able to make you very sad (or, depending on
|
|
// how you're using RemoteAddr, vulnerable to an attack of some sort).
|
|
func RealIP(h http.Handler) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
if rip := realIP(r); rip != "" {
|
|
r.RemoteAddr = rip
|
|
}
|
|
h.ServeHTTP(w, r)
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
}
|
|
|
|
func realIP(r *http.Request) string {
|
|
var ip string
|
|
|
|
if tcip := r.Header.Get(trueClientIP); tcip != "" {
|
|
ip = tcip
|
|
} else if xrip := r.Header.Get(xRealIP); xrip != "" {
|
|
ip = xrip
|
|
} else if xff := r.Header.Get(xForwardedFor); xff != "" {
|
|
i := strings.Index(xff, ",")
|
|
if i == -1 {
|
|
i = len(xff)
|
|
}
|
|
ip = xff[:i]
|
|
}
|
|
if ip == "" || net.ParseIP(ip) == nil {
|
|
return ""
|
|
}
|
|
return ip
|
|
}
|