From 789ef077eb4cb374762f752427ecbe4e789c97a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 22 Nov 2026 09:59:52 +0100 Subject: [PATCH] feat(platform): trust X-Forwarded-For only from configured proxies gin trusted every proxy by default, so any client could choose the address used for rate limits and the audit log. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- .env.example | 5 +++++ cmd/api/main.go | 3 +++ cmd/statuspage/main.go | 3 +++ internal/config/config.go | 6 ++++++ internal/platform/proxy_test.go | 32 ++++++++++++++++++++++++++++++++ internal/platform/router.go | 7 +++++++ 6 files changed, 56 insertions(+) create mode 100644 internal/platform/proxy_test.go diff --git a/.env.example b/.env.example index 4d6aac4..6535d9a 100644 --- a/.env.example +++ b/.env.example @@ -23,6 +23,9 @@ UPTIME_API_URL=http://localhost:8080 UPTIME_TEST_DATABASE_URL=postgres://uptime:uptime@localhost:5432/uptime_test?sslmode=disable UPTIME_TEST_REDIS_URL=redis://localhost:6379/15 +# Reverse proxies allowed to set X-Forwarded-For (comma-separated IPs/CIDRs). +# UPTIME_TRUSTED_PROXIES=10.0.0.0/8 + # Data retention (0 keeps forever). Raw results feed charts up to 24h, the # hourly aggregate 7d and 30d, the daily aggregate up to a year; resolved # incidents follow the daily aggregate. @@ -31,6 +34,8 @@ UPTIME_TEST_REDIS_URL=redis://localhost:6379/15 # UPTIME_RETENTION_DAILY=26280h # Delivered notifications, expired invitations and expired API tokens. # UPTIME_RETENTION_HOUSEKEEPING=720h +# Audit log entries. +# UPTIME_RETENTION_AUDIT=8760h # Status page server: hosts serving pages by slug; enable TLS for custom domains. UPTIME_STATUSPAGE_URL=http://localhost:8082 diff --git a/cmd/api/main.go b/cmd/api/main.go index c093638..2f66829 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -80,6 +80,9 @@ func run(log *slog.Logger, level *slog.LevelVar, args []string) error { srv := api.New(cfg, api.Deps{Log: log, DB: tenant.New(deps.DB), Redis: deps.Redis, Keys: keys, Catalog: catalog}) health := platform.NewHealth(deps.Probes()) r := platform.NewRouter(log) + if err := platform.TrustProxies(r, cfg.TrustedProxies); err != nil { + return err + } health.Register(r) r.StaticFS("/static", web.Static()) srv.Mount(r) diff --git a/cmd/statuspage/main.go b/cmd/statuspage/main.go index 7ae9ce7..b650ed0 100644 --- a/cmd/statuspage/main.go +++ b/cmd/statuspage/main.go @@ -71,6 +71,9 @@ func run(log *slog.Logger, level *slog.LevelVar) error { "pages": func(ctx context.Context) error { return pages.Ready(ctx, apiProbe) }, }) r := platform.NewRouter(log) + if err := platform.TrustProxies(r, cfg.TrustedProxies); err != nil { + return err + } health.Register(r) r.StaticFS("/static", web.Static()) pages.Mount(r) diff --git a/internal/config/config.go b/internal/config/config.go index 3deaa02..6811d26 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -27,6 +27,10 @@ type Common struct { Edition Edition `env:"UPTIME_EDITION" envDefault:"selfhosted"` LogLevel slog.Level `env:"UPTIME_LOG_LEVEL" envDefault:"info"` ShutdownTimeout time.Duration `env:"UPTIME_SHUTDOWN_TIMEOUT" envDefault:"20s"` + // TrustedProxies are addresses or CIDRs of reverse proxies whose + // X-Forwarded-For is believed. Client addresses feed rate limits and + // the audit log, so the default trusts no proxy. + TrustedProxies []string `env:"UPTIME_TRUSTED_PROXIES" envSeparator:","` } // Storage locates Postgres and Redis. Only the API connects to them; every @@ -100,6 +104,8 @@ type Retention struct { Daily time.Duration `env:"UPTIME_RETENTION_DAILY" envDefault:"26280h"` // Delivered notifications, expired invitations and expired API tokens. Housekeeping time.Duration `env:"UPTIME_RETENTION_HOUSEKEEPING" envDefault:"720h"` + // Audit log entries. + Audit time.Duration `env:"UPTIME_RETENTION_AUDIT" envDefault:"8760h"` } // SSOAllowPrivate resolves SSOPrivateNetworks. diff --git a/internal/platform/proxy_test.go b/internal/platform/proxy_test.go new file mode 100644 index 0000000..7bd46b5 --- /dev/null +++ b/internal/platform/proxy_test.go @@ -0,0 +1,32 @@ +package platform + +import ( + "log/slog" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" +) + +func TestClientIPIgnoresForwardedForUnlessTrusted(t *testing.T) { + ip := func(r *gin.Engine) string { + r.GET("/ip", func(c *gin.Context) { c.String(http.StatusOK, c.ClientIP()) }) + req := httptest.NewRequest(http.MethodGet, "/ip", nil) + req.RemoteAddr = "192.0.2.10:4000" + req.Header.Set("X-Forwarded-For", "203.0.113.9") + w := httptest.NewRecorder() + r.ServeHTTP(w, req) + return w.Body.String() + } + if got := ip(NewRouter(slog.New(slog.DiscardHandler))); got != "192.0.2.10" { + t.Errorf("ClientIP() without trusted proxies = %s, want the peer address", got) + } + r := NewRouter(slog.New(slog.DiscardHandler)) + if err := TrustProxies(r, []string{"192.0.2.0/24"}); err != nil { + t.Fatal(err) + } + if got := ip(r); got != "203.0.113.9" { + t.Errorf("ClientIP() behind a trusted proxy = %s, want the forwarded address", got) + } +} diff --git a/internal/platform/router.go b/internal/platform/router.go index d9fae7c..22977f1 100644 --- a/internal/platform/router.go +++ b/internal/platform/router.go @@ -21,10 +21,17 @@ func NewRouter(log *slog.Logger) *gin.Engine { gin.DefaultWriter = io.Discard gin.DefaultErrorWriter = io.Discard r := gin.New() + _ = r.SetTrustedProxies(nil) r.Use(requestid.Middleware(), accessLog(log), recovery(log)) return r } +// TrustProxies makes r take client addresses from X-Forwarded-For only when +// the request comes from one of proxies. +func TrustProxies(r *gin.Engine, proxies []string) error { + return r.SetTrustedProxies(proxies) +} + func accessLog(log *slog.Logger) gin.HandlerFunc { return func(c *gin.Context) { start := time.Now()