Skip to content

Commit 8c2a72a

Browse files
matchers: Drop forwarded option from remote_ip matcher (#6085)
1 parent bde4621 commit 8c2a72a

File tree

2 files changed

+3
-43
lines changed

2 files changed

+3
-43
lines changed

modules/caddyhttp/celmatcher_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -373,22 +373,6 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
373373
urlTarget: "https://example.com/foo",
374374
wantResult: true,
375375
},
376-
{
377-
name: "remote_ip forwarded (MatchRemoteIP)",
378-
expression: &MatchExpression{
379-
Expr: `remote_ip('forwarded', '192.0.2.1')`,
380-
},
381-
urlTarget: "https://example.com/foo",
382-
wantResult: true,
383-
},
384-
{
385-
name: "remote_ip forwarded not first (MatchRemoteIP)",
386-
expression: &MatchExpression{
387-
Expr: `remote_ip('192.0.2.1', 'forwarded')`,
388-
},
389-
urlTarget: "https://example.com/foo",
390-
wantErr: true,
391-
},
392376
}
393377
)
394378

modules/caddyhttp/ip_matchers.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ type MatchRemoteIP struct {
3737
// The IPs or CIDR ranges to match.
3838
Ranges []string `json:"ranges,omitempty"`
3939

40-
// If true, prefer the first IP in the request's X-Forwarded-For
41-
// header, if present, rather than the immediate peer's IP, as
42-
// the reference IP against which to match. Note that it is easy
43-
// to spoof request headers. Default: false
44-
// DEPRECATED: This is insecure, MatchClientIP should be used instead.
45-
Forwarded bool `json:"forwarded,omitempty"`
46-
4740
// cidrs and zones vars should aligned always in the same
4841
// length and indexes for matching later
4942
cidrs []*netip.Prefix
@@ -82,11 +75,7 @@ func (m *MatchRemoteIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
8275
d.Next() // consume matcher name
8376
for d.NextArg() {
8477
if d.Val() == "forwarded" {
85-
if len(m.Ranges) > 0 {
86-
return d.Err("if used, 'forwarded' must be first argument")
87-
}
88-
m.Forwarded = true
89-
continue
78+
return d.Err("the 'forwarded' option is no longer supported; use the 'client_ip' matcher instead")
9079
}
9180
if d.Val() == "private_ranges" {
9281
m.Ranges = append(m.Ranges, PrivateRangesCIDR()...)
@@ -105,7 +94,7 @@ func (m *MatchRemoteIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
10594
//
10695
// Example:
10796
//
108-
// expression remote_ip('forwarded', '192.168.0.0/16', '172.16.0.0/12', '10.0.0.0/8')
97+
// expression remote_ip('192.168.0.0/16', '172.16.0.0/12', '10.0.0.0/8')
10998
func (MatchRemoteIP) CELLibrary(ctx caddy.Context) (cel.Library, error) {
11099
return CELMatcherImpl(
111100
// name of the macro, this is the function name that users see when writing expressions.
@@ -126,11 +115,7 @@ func (MatchRemoteIP) CELLibrary(ctx caddy.Context) (cel.Library, error) {
126115

127116
for _, input := range strList.([]string) {
128117
if input == "forwarded" {
129-
if len(m.Ranges) > 0 {
130-
return nil, errors.New("if used, 'forwarded' must be first argument")
131-
}
132-
m.Forwarded = true
133-
continue
118+
return nil, errors.New("the 'forwarded' option is no longer supported; use the 'client_ip' matcher instead")
134119
}
135120
m.Ranges = append(m.Ranges, input)
136121
}
@@ -151,21 +136,12 @@ func (m *MatchRemoteIP) Provision(ctx caddy.Context) error {
151136
m.cidrs = cidrs
152137
m.zones = zones
153138

154-
if m.Forwarded {
155-
m.logger.Warn("remote_ip's forwarded mode is deprecated; use the 'client_ip' matcher instead")
156-
}
157-
158139
return nil
159140
}
160141

161142
// Match returns true if r matches m.
162143
func (m MatchRemoteIP) Match(r *http.Request) bool {
163144
address := r.RemoteAddr
164-
if m.Forwarded {
165-
if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" {
166-
address = strings.TrimSpace(strings.Split(fwdFor, ",")[0])
167-
}
168-
}
169145
clientIP, zoneID, err := parseIPZoneFromString(address)
170146
if err != nil {
171147
m.logger.Error("getting remote IP", zap.Error(err))

0 commit comments

Comments
 (0)