caddyhttp: Reject a negative trusted_proxies_strict count

Native JSON accepted a negative count, which silently turned strict
parsing off while the Caddyfile rejects the same value. Zero still
disables it.
This commit is contained in:
Jay Hemnani 2026-10-21 18:08:51 +05:30
parent 3004d2dd17
commit 50839bbecd
2 changed files with 29 additions and 0 deletions

View file

@ -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 {

View file

@ -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)
}
})
}
}