diff --git a/modules/caddyhttp/app.go b/modules/caddyhttp/app.go index a2c20d91e..b62f93b5e 100644 --- a/modules/caddyhttp/app.go +++ b/modules/caddyhttp/app.go @@ -444,6 +444,11 @@ func (app *App) Validate() error { } } + // zero disables strict parsing; a negative count would silently do the same + if srv.TrustedProxiesStrict < 0 { + return fmt.Errorf("server %s: trusted_proxies_strict must not be negative: %d", srvName, srv.TrustedProxiesStrict) + } + // logger names must not have ports if srv.Logs != nil { for host := range srv.Logs.LoggerNames { diff --git a/modules/caddyhttp/app_test.go b/modules/caddyhttp/app_test.go index b1819d24d..9b4bc82ee 100644 --- a/modules/caddyhttp/app_test.go +++ b/modules/caddyhttp/app_test.go @@ -16,6 +16,7 @@ package caddyhttp import ( "context" + "encoding/json" "fmt" "io" "net/http" @@ -184,3 +185,26 @@ func TestServerErrorLoggerLevels(t *testing.T) { }) } } + +func TestValidateTrustedProxiesStrictCount(t *testing.T) { + for _, tc := range []struct { + count string + wantErr bool + }{ + {"0", false}, + {"2", false}, + {"-1", true}, + } { + t.Run(tc.count, func(t *testing.T) { + var app App + config := `{"servers": {"srv0": {"listen": [":443"], "trusted_proxies_strict": ` + tc.count + `}}}` + if err := json.Unmarshal([]byte(config), &app); err != nil { + t.Fatal(err) + } + err := app.Validate() + if tc.wantErr != (err != nil) { + t.Fatalf("Validate() = %v, want error: %t", err, tc.wantErr) + } + }) + } +}