@marcel91200 / caddy Mirror · read-only / commits / 40c582ce
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 12:00 UTC

caddyhttp: Fix merging consecutive `client_ip` or `remote_ip` matchers (#6350)

Francis Lavoie committed May 30, 2024 at 09:32 UTC 40c582ce8273bf12b08e8e77df6498bca5f626a6
2 files changed +72 -20
caddytest/integration/caddyfile_adapt/matcher_syntax.caddyfiletest
+48
@@ -46,6 +46,18 @@
46
47 @matcher12 client_ip private_ranges
48 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"
61 }
62 ----------
63 {
@@ -279,6 +291,42 @@
291 "handler": "static_response"
292 }
293 ]
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 + ]
330 }
331 ]
332 }
modules/caddyhttp/ip_matchers.go
+24 -20
@@ -72,19 +72,21 @@ func (MatchRemoteIP) CaddyModule() caddy.ModuleInfo {
72
73 // UnmarshalCaddyfile implements caddyfile.Unmarshaler.
74 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())
86 }
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")
89 }
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")
90 }
91 return nil
92 }
@@ -164,16 +166,18 @@ func (MatchClientIP) CaddyModule() caddy.ModuleInfo {
166
167 // UnmarshalCaddyfile implements caddyfile.Unmarshaler.
168 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")
180 }
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")
181 }
182 return nil
183 }