Skip to content

Commit f9199c7

Browse files
cseufertfrancislavoie
authored andcommitted
Refactored trusted_proxies_unix to boolean
Added .caddyfiletest case for trusted_proxies_unix
1 parent ed1a74a commit f9199c7

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

caddyconfig/httpcaddyfile/serveroptions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type serverOptions struct {
5151
StrictSNIHost *bool
5252
TrustedProxiesRaw json.RawMessage
5353
TrustedProxiesStrict int
54-
TrustedProxiesUnix int
54+
TrustedProxiesUnix bool
5555
ClientIPHeaders []string
5656
ShouldLogCredentials bool
5757
Metrics *caddyhttp.Metrics
@@ -256,7 +256,7 @@ func unmarshalCaddyfileServerOptions(d *caddyfile.Dispenser) (any, error) {
256256
if d.NextArg() {
257257
return nil, d.ArgErr()
258258
}
259-
serverOpts.TrustedProxiesUnix = 1
259+
serverOpts.TrustedProxiesUnix = true
260260

261261
case "client_ip_headers":
262262
headers := d.RemainingArgs()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
servers {
3+
trusted_proxies_unix
4+
}
5+
}
6+
7+
example.com {
8+
reverse_proxy https://local:8080
9+
}
10+
----------
11+
{
12+
"apps": {
13+
"http": {
14+
"servers": {
15+
"srv0": {
16+
"listen": [
17+
":443"
18+
],
19+
"routes": [
20+
{
21+
"match": [
22+
{
23+
"host": [
24+
"example.com"
25+
]
26+
}
27+
],
28+
"handle": [
29+
{
30+
"handler": "subroute",
31+
"routes": [
32+
{
33+
"handle": [
34+
{
35+
"handler": "reverse_proxy",
36+
"transport": {
37+
"protocol": "http",
38+
"tls": {}
39+
},
40+
"upstreams": [
41+
{
42+
"dial": "local:8080"
43+
}
44+
]
45+
}
46+
]
47+
}
48+
]
49+
}
50+
],
51+
"terminal": true
52+
}
53+
],
54+
"trusted_proxies_unix": true
55+
}
56+
}
57+
}
58+
}
59+
}

modules/caddyhttp/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ type Server struct {
208208
//
209209
// This option is disabled by default.
210210

211-
TrustedProxiesUnix int `json:"trusted_proxies_unix,omitempty"`
211+
TrustedProxiesUnix bool `json:"trusted_proxies_unix,omitempty"`
212212

213213
// Enables access logging and configures how access logs are handled
214214
// in this server. To minimally enable access logs, simply set this
@@ -949,7 +949,7 @@ func determineTrustedProxy(r *http.Request, s *Server) (bool, string) {
949949
return false, ""
950950
}
951951

952-
if s.TrustedProxiesUnix > 0 && r.RemoteAddr == "@" {
952+
if s.TrustedProxiesUnix && r.RemoteAddr == "@" {
953953
if s.TrustedProxiesStrict > 0 {
954954
ipRanges := []netip.Prefix{}
955955
if s.trustedProxies != nil {

modules/caddyhttp/server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func TestServer_DetermineTrustedProxy_TrustedLoopback(t *testing.T) {
300300
func TestServer_DetermineTrustedProxy_UnixSocket(t *testing.T) {
301301
server := &Server{
302302
ClientIPHeaders: []string{"X-Forwarded-For"},
303-
TrustedProxiesUnix: 1,
303+
TrustedProxiesUnix: true,
304304
}
305305

306306
req := httptest.NewRequest("GET", "/", nil)
@@ -316,7 +316,7 @@ func TestServer_DetermineTrustedProxy_UnixSocket(t *testing.T) {
316316
func TestServer_DetermineTrustedProxy_UnixSocketStrict(t *testing.T) {
317317
server := &Server{
318318
ClientIPHeaders: []string{"X-Forwarded-For"},
319-
TrustedProxiesUnix: 1,
319+
TrustedProxiesUnix: true,
320320
TrustedProxiesStrict: 1,
321321
}
322322

0 commit comments

Comments
 (0)