Skip to content

Commit 998747a

Browse files
cseufertfrancislavoie
authored andcommitted
Refactored trusted_proxies_unix to boolean
Added .caddyfiletest case for trusted_proxies_unix
1 parent 124beff commit 998747a

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
@@ -48,7 +48,7 @@ type serverOptions struct {
4848
StrictSNIHost *bool
4949
TrustedProxiesRaw json.RawMessage
5050
TrustedProxiesStrict int
51-
TrustedProxiesUnix int
51+
TrustedProxiesUnix bool
5252
ClientIPHeaders []string
5353
ShouldLogCredentials bool
5454
Metrics *caddyhttp.Metrics
@@ -232,7 +232,7 @@ func unmarshalCaddyfileServerOptions(d *caddyfile.Dispenser) (any, error) {
232232
if d.NextArg() {
233233
return nil, d.ArgErr()
234234
}
235-
serverOpts.TrustedProxiesUnix = 1
235+
serverOpts.TrustedProxiesUnix = true
236236

237237
case "client_ip_headers":
238238
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
@@ -192,7 +192,7 @@ type Server struct {
192192
//
193193
// This option is disabled by default.
194194

195-
TrustedProxiesUnix int `json:"trusted_proxies_unix,omitempty"`
195+
TrustedProxiesUnix bool `json:"trusted_proxies_unix,omitempty"`
196196

197197
// Enables access logging and configures how access logs are handled
198198
// in this server. To minimally enable access logs, simply set this
@@ -933,7 +933,7 @@ func determineTrustedProxy(r *http.Request, s *Server) (bool, string) {
933933
return false, ""
934934
}
935935

936-
if s.TrustedProxiesUnix > 0 && r.RemoteAddr == "@" {
936+
if s.TrustedProxiesUnix && r.RemoteAddr == "@" {
937937
if s.TrustedProxiesStrict > 0 {
938938
ipRanges := []netip.Prefix{}
939939
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)