Skip to content

Commit 40c582c

Browse files
caddyhttp: Fix merging consecutive client_ip or remote_ip matchers (#6350)
1 parent a52917a commit 40c582c

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

caddytest/integration/caddyfile_adapt/matcher_syntax.caddyfiletest

+48
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@
4646

4747
@matcher12 client_ip private_ranges
4848
respond @matcher12 "client_ip matcher with private ranges"
49+
50+
@matcher13 {
51+
remote_ip 1.1.1.1
52+
remote_ip 2.2.2.2
53+
}
54+
respond @matcher13 "remote_ip merged"
55+
56+
@matcher14 {
57+
client_ip 1.1.1.1
58+
client_ip 2.2.2.2
59+
}
60+
respond @matcher14 "client_ip merged"
4961
}
5062
----------
5163
{
@@ -279,6 +291,42 @@
279291
"handler": "static_response"
280292
}
281293
]
294+
},
295+
{
296+
"match": [
297+
{
298+
"remote_ip": {
299+
"ranges": [
300+
"1.1.1.1",
301+
"2.2.2.2"
302+
]
303+
}
304+
}
305+
],
306+
"handle": [
307+
{
308+
"body": "remote_ip merged",
309+
"handler": "static_response"
310+
}
311+
]
312+
},
313+
{
314+
"match": [
315+
{
316+
"client_ip": {
317+
"ranges": [
318+
"1.1.1.1",
319+
"2.2.2.2"
320+
]
321+
}
322+
}
323+
],
324+
"handle": [
325+
{
326+
"body": "client_ip merged",
327+
"handler": "static_response"
328+
}
329+
]
282330
}
283331
]
284332
}

modules/caddyhttp/ip_matchers.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,21 @@ func (MatchRemoteIP) CaddyModule() caddy.ModuleInfo {
7272

7373
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
7474
func (m *MatchRemoteIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
75-
d.Next() // consume matcher name
76-
for d.NextArg() {
77-
if d.Val() == "forwarded" {
78-
return d.Err("the 'forwarded' option is no longer supported; use the 'client_ip' matcher instead")
75+
// iterate to merge multiple matchers into one
76+
for d.Next() {
77+
for d.NextArg() {
78+
if d.Val() == "forwarded" {
79+
return d.Err("the 'forwarded' option is no longer supported; use the 'client_ip' matcher instead")
80+
}
81+
if d.Val() == "private_ranges" {
82+
m.Ranges = append(m.Ranges, PrivateRangesCIDR()...)
83+
continue
84+
}
85+
m.Ranges = append(m.Ranges, d.Val())
7986
}
80-
if d.Val() == "private_ranges" {
81-
m.Ranges = append(m.Ranges, PrivateRangesCIDR()...)
82-
continue
87+
if d.NextBlock(0) {
88+
return d.Err("malformed remote_ip matcher: blocks are not supported")
8389
}
84-
m.Ranges = append(m.Ranges, d.Val())
85-
}
86-
if d.NextBlock(0) {
87-
return d.Err("malformed remote_ip matcher: blocks are not supported")
8890
}
8991
return nil
9092
}
@@ -164,16 +166,18 @@ func (MatchClientIP) CaddyModule() caddy.ModuleInfo {
164166

165167
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
166168
func (m *MatchClientIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
167-
d.Next() // consume matcher name
168-
for d.NextArg() {
169-
if d.Val() == "private_ranges" {
170-
m.Ranges = append(m.Ranges, PrivateRangesCIDR()...)
171-
continue
169+
// iterate to merge multiple matchers into one
170+
for d.Next() {
171+
for d.NextArg() {
172+
if d.Val() == "private_ranges" {
173+
m.Ranges = append(m.Ranges, PrivateRangesCIDR()...)
174+
continue
175+
}
176+
m.Ranges = append(m.Ranges, d.Val())
177+
}
178+
if d.NextBlock(0) {
179+
return d.Err("malformed client_ip matcher: blocks are not supported")
172180
}
173-
m.Ranges = append(m.Ranges, d.Val())
174-
}
175-
if d.NextBlock(0) {
176-
return d.Err("malformed client_ip matcher: blocks are not supported")
177181
}
178182
return nil
179183
}

0 commit comments

Comments
 (0)