A continuation of HomeBox the inventory and organization system built for the Home User
fix: middleware didn't check trustProxy config during IP extraction.
Matthew Kilgore2f302f0bc785
diff --git a/backend/app/api/app.go b/backend/app/api/app.goindex 0479b310..3d0bdea9 100644--- a/backend/app/api/app.go+++ b/backend/app/api/app.go@@ -36,7 +36,7 @@ func new(conf *config.Config) *app {}s.authLimiter = newAuthRateLimiter(s.conf.Auth.RateLimit)- s.notifierTestLimiter = newSimpleRateLimiter(10, time.Minute) // 10 requests per minute+ s.notifierTestLimiter = newSimpleRateLimiter(10, time.Minute, s.conf.Options.TrustProxy) // 10 requests per minutereturn s}diff --git a/backend/app/api/middleware.go b/backend/app/api/middleware.goindex 35d33e42..81913923 100644--- a/backend/app/api/middleware.go+++ b/backend/app/api/middleware.go@@ -308,7 +308,7 @@ func (a *app) mwAuthRateLimit(next errchain.Handler) errchain.Handler {return next.ServeHTTP(w, r)}- key := limiter.keyForRequest(r)+ key := limiter.keyForRequest(r, a.conf.Options.TrustProxy)now := limiter.nowFn()if retryAfter, allowed := limiter.shouldAllow(key, now); !allowed {@@ -389,22 +389,19 @@ func (l *authRateLimiter) record(key string, now time.Time, success bool) {}}-// keyForRequest returns a unique key for the given request.-func (l *authRateLimiter) keyForRequest(r *http.Request) string {- return l.clientIP(r) + "|" + r.URL.Path-}--// clientIP returns the client IP address for the given request.-func (l *authRateLimiter) clientIP(r *http.Request) string {- if ip := r.Header.Get("X-Real-IP"); ip != "" {- return ip- }+// extractClientIP extracts the client IP from the request.+// It only uses proxy headers (X-Real-IP, X-Forwarded-For) if trustProxy is enabled.+func extractClientIP(r *http.Request, trustProxy bool) string {+ if trustProxy {+ if ip := r.Header.Get("X-Real-IP"); ip != "" {+ return ip+ }- if ip := r.Header.Get("X-Forwarded-For"); ip != "" {- parts := strings.Split(ip, ",")- return strings.TrimSpace(parts[0])+ if ip := r.Header.Get("X-Forwarded-For"); ip != "" {+ parts := strings.Split(ip, ",")+ return strings.TrimSpace(parts[0])+ }}-host, _, err := net.SplitHostPort(r.RemoteAddr)if err == nil {return host@@ -413,12 +410,24 @@ func (l *authRateLimiter) clientIP(r *http.Request) string {return r.RemoteAddr}+// keyForRequest returns a unique key for the given request.+func (l *authRateLimiter) keyForRequest(r *http.Request, trustProxy bool) string {+ return extractClientIP(r, trustProxy) + "|" + r.URL.Path+}++// clientIP returns the client IP address for the given request.+// It only uses proxy headers (X-Real-IP, X-Forwarded-For) if TrustProxy is enabled.+func (l *authRateLimiter) clientIP(r *http.Request, trustProxy bool) string {+ return extractClientIP(r, trustProxy)+}+// simpleRateLimiter provides token bucket rate limiting per client IP.type simpleRateLimiter struct {mu sync.Mutexlimiters map[string]*rateLimiterEntryrate int // requests allowedwindow time.Duration // time window+ trustProxy bool // whether to trust proxy headersstopCleanup chan struct{}stopOnce sync.Once}@@ -429,11 +438,12 @@ type rateLimiterEntry struct {}// newSimpleRateLimiter creates a new rate limiter with the specified rate and window.-func newSimpleRateLimiter(rate int, window time.Duration) *simpleRateLimiter {+func newSimpleRateLimiter(rate int, window time.Duration, trustProxy bool) *simpleRateLimiter {rl := &simpleRateLimiter{limiters: make(map[string]*rateLimiterEntry),rate: rate,window: window,+ trustProxy: trustProxy,stopCleanup: make(chan struct{}),}@@ -523,28 +533,15 @@ func (rl *simpleRateLimiter) allow(clientIP string) bool {}// getClientIP extracts the client IP from the request.-func (rl *simpleRateLimiter) getClientIP(r *http.Request) string {- if ip := r.Header.Get("X-Real-IP"); ip != "" {- return ip- }-- if ip := r.Header.Get("X-Forwarded-For"); ip != "" {- parts := strings.Split(ip, ",")- return strings.TrimSpace(parts[0])- }-- host, _, err := net.SplitHostPort(r.RemoteAddr)- if err == nil {- return host- }-- return r.RemoteAddr+// It only uses proxy headers (X-Real-IP, X-Forwarded-For) if trustProxy is enabled.+func (rl *simpleRateLimiter) getClientIP(r *http.Request, trustProxy bool) string {+ return extractClientIP(r, trustProxy)}// middleware wraps the rate limiter as an errchain middleware.func (rl *simpleRateLimiter) middleware(next errchain.Handler) errchain.Handler {return errchain.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {- clientIP := rl.getClientIP(r)+ clientIP := rl.getClientIP(r, rl.trustProxy)if !rl.allow(clientIP) {w.Header().Set("Retry-After", strconv.Itoa(int(rl.window.Seconds())))