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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
bd986cd49f
commit
789ef077eb
6 files changed
+56
No files matched your search
@@ -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_DATABASE_URL=postgres://uptime:uptime@localhost:5432/uptime_test?sslmode=disable
|
||||||
UPTIME_TEST_REDIS_URL=redis://localhost:6379/15
|
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
|
# 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
|
# hourly aggregate 7d and 30d, the daily aggregate up to a year; resolved
|
||||||
# incidents follow the daily aggregate.
|
# incidents follow the daily aggregate.
|
||||||
@@ -31,6 +34,8 @@ UPTIME_TEST_REDIS_URL=redis://localhost:6379/15
|
|||||||
# UPTIME_RETENTION_DAILY=26280h
|
# UPTIME_RETENTION_DAILY=26280h
|
||||||
# Delivered notifications, expired invitations and expired API tokens.
|
# Delivered notifications, expired invitations and expired API tokens.
|
||||||
# UPTIME_RETENTION_HOUSEKEEPING=720h
|
# UPTIME_RETENTION_HOUSEKEEPING=720h
|
||||||
|
# Audit log entries.
|
||||||
|
# UPTIME_RETENTION_AUDIT=8760h
|
||||||
|
|
||||||
# Status page server: hosts serving pages by slug; enable TLS for custom domains.
|
# Status page server: hosts serving pages by slug; enable TLS for custom domains.
|
||||||
UPTIME_STATUSPAGE_URL=http://localhost:8082
|
UPTIME_STATUSPAGE_URL=http://localhost:8082
|
||||||
|
|||||||
@@ -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})
|
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())
|
health := platform.NewHealth(deps.Probes())
|
||||||
r := platform.NewRouter(log)
|
r := platform.NewRouter(log)
|
||||||
|
if err := platform.TrustProxies(r, cfg.TrustedProxies); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
health.Register(r)
|
health.Register(r)
|
||||||
r.StaticFS("/static", web.Static())
|
r.StaticFS("/static", web.Static())
|
||||||
srv.Mount(r)
|
srv.Mount(r)
|
||||||
|
|||||||
@@ -71,6 +71,9 @@ func run(log *slog.Logger, level *slog.LevelVar) error {
|
|||||||
"pages": func(ctx context.Context) error { return pages.Ready(ctx, apiProbe) },
|
"pages": func(ctx context.Context) error { return pages.Ready(ctx, apiProbe) },
|
||||||
})
|
})
|
||||||
r := platform.NewRouter(log)
|
r := platform.NewRouter(log)
|
||||||
|
if err := platform.TrustProxies(r, cfg.TrustedProxies); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
health.Register(r)
|
health.Register(r)
|
||||||
r.StaticFS("/static", web.Static())
|
r.StaticFS("/static", web.Static())
|
||||||
pages.Mount(r)
|
pages.Mount(r)
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ type Common struct {
|
|||||||
Edition Edition `env:"UPTIME_EDITION" envDefault:"selfhosted"`
|
Edition Edition `env:"UPTIME_EDITION" envDefault:"selfhosted"`
|
||||||
LogLevel slog.Level `env:"UPTIME_LOG_LEVEL" envDefault:"info"`
|
LogLevel slog.Level `env:"UPTIME_LOG_LEVEL" envDefault:"info"`
|
||||||
ShutdownTimeout time.Duration `env:"UPTIME_SHUTDOWN_TIMEOUT" envDefault:"20s"`
|
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
|
// 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"`
|
Daily time.Duration `env:"UPTIME_RETENTION_DAILY" envDefault:"26280h"`
|
||||||
// Delivered notifications, expired invitations and expired API tokens.
|
// Delivered notifications, expired invitations and expired API tokens.
|
||||||
Housekeeping time.Duration `env:"UPTIME_RETENTION_HOUSEKEEPING" envDefault:"720h"`
|
Housekeeping time.Duration `env:"UPTIME_RETENTION_HOUSEKEEPING" envDefault:"720h"`
|
||||||
|
// Audit log entries.
|
||||||
|
Audit time.Duration `env:"UPTIME_RETENTION_AUDIT" envDefault:"8760h"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SSOAllowPrivate resolves SSOPrivateNetworks.
|
// SSOAllowPrivate resolves SSOPrivateNetworks.
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,10 +21,17 @@ func NewRouter(log *slog.Logger) *gin.Engine {
|
|||||||
gin.DefaultWriter = io.Discard
|
gin.DefaultWriter = io.Discard
|
||||||
gin.DefaultErrorWriter = io.Discard
|
gin.DefaultErrorWriter = io.Discard
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
|
_ = r.SetTrustedProxies(nil)
|
||||||
r.Use(requestid.Middleware(), accessLog(log), recovery(log))
|
r.Use(requestid.Middleware(), accessLog(log), recovery(log))
|
||||||
return r
|
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 {
|
func accessLog(log *slog.Logger) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|||||||
Reference in new issue
Block a user