mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	caddytls,caddyhttp: Placeholders for some TLS and HTTP matchers (#6480)
	
		
			
	
		
	
	
		
	
		
			Some checks are pending
		
		
	
	
		
			
				
	
				Tests / test (./cmd/caddy/caddy, ~1.21.0, macos-14, 0, 1.21, mac) (push) Waiting to run
				
			
		
			
				
	
				Tests / test (./cmd/caddy/caddy, ~1.21.0, ubuntu-latest, 0, 1.21, linux) (push) Waiting to run
				
			
		
			
				
	
				Tests / test (./cmd/caddy/caddy, ~1.22.3, macos-14, 0, 1.22, mac) (push) Waiting to run
				
			
		
			
				
	
				Tests / test (./cmd/caddy/caddy, ~1.22.3, ubuntu-latest, 0, 1.22, linux) (push) Waiting to run
				
			
		
			
				
	
				Tests / test (./cmd/caddy/caddy.exe, ~1.21.0, windows-latest, True, 1.21, windows) (push) Waiting to run
				
			
		
			
				
	
				Tests / test (./cmd/caddy/caddy.exe, ~1.22.3, windows-latest, True, 1.22, windows) (push) Waiting to run
				
			
		
			
				
	
				Tests / test (s390x on IBM Z) (push) Waiting to run
				
			
		
			
				
	
				Tests / goreleaser-check (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, aix) (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, darwin) (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, dragonfly) (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, freebsd) (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, illumos) (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, linux) (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, netbsd) (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, openbsd) (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, solaris) (push) Waiting to run
				
			
		
			
				
	
				Cross-Build / build (~1.22.3, 1.22, windows) (push) Waiting to run
				
			
		
			
				
	
				Lint / lint (macos-14, mac) (push) Waiting to run
				
			
		
			
				
	
				Lint / lint (ubuntu-latest, linux) (push) Waiting to run
				
			
		
			
				
	
				Lint / lint (windows-latest, windows) (push) Waiting to run
				
			
		
			
				
	
				Lint / govulncheck (push) Waiting to run
				
			
		
		
	
	
				
					
				
			
		
			Some checks are pending
		
		
	
	Tests / test (./cmd/caddy/caddy, ~1.21.0, macos-14, 0, 1.21, mac) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy, ~1.21.0, ubuntu-latest, 0, 1.21, linux) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy, ~1.22.3, macos-14, 0, 1.22, mac) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy, ~1.22.3, ubuntu-latest, 0, 1.22, linux) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy.exe, ~1.21.0, windows-latest, True, 1.21, windows) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy.exe, ~1.22.3, windows-latest, True, 1.22, windows) (push) Waiting to run
				
			Tests / test (s390x on IBM Z) (push) Waiting to run
				
			Tests / goreleaser-check (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, aix) (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, darwin) (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, dragonfly) (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, freebsd) (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, illumos) (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, linux) (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, netbsd) (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, openbsd) (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, solaris) (push) Waiting to run
				
			Cross-Build / build (~1.22.3, 1.22, windows) (push) Waiting to run
				
			Lint / lint (macos-14, mac) (push) Waiting to run
				
			Lint / lint (ubuntu-latest, linux) (push) Waiting to run
				
			Lint / lint (windows-latest, windows) (push) Waiting to run
				
			Lint / govulncheck (push) Waiting to run
				
			* Runtime placeholders for caddytls matchers (1/3): - remove IPs validation in UnmarshalCaddyfile * Runtime placeholders for caddytls matchers (2/3): - add placeholder replacement for IPs in Provision * Runtime placeholders for caddytls matchers (3/3): - add placeholder replacement for other strings * Runtime placeholders for caddyhttp matchers (1/1): - add placeholder replacement for IPs in Provision * Runtime placeholders for caddyhttp/caddytls matchers: - move PrivateRandesCIDR under internal
This commit is contained in:
		
							parent
							
								
									a8b0dfa8da
								
							
						
					
					
						commit
						59cbb2c83a
					
				
							
								
								
									
										14
									
								
								internal/ranges.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								internal/ranges.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
			
		||||
package internal
 | 
			
		||||
 | 
			
		||||
// PrivateRangesCIDR returns a list of private CIDR range
 | 
			
		||||
// strings, which can be used as a configuration shortcut.
 | 
			
		||||
func PrivateRangesCIDR() []string {
 | 
			
		||||
	return []string{
 | 
			
		||||
		"192.168.0.0/16",
 | 
			
		||||
		"172.16.0.0/12",
 | 
			
		||||
		"10.0.0.0/8",
 | 
			
		||||
		"127.0.0.1/8",
 | 
			
		||||
		"fd00::/8",
 | 
			
		||||
		"::1",
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -29,6 +29,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"github.com/caddyserver/caddy/v2"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/internal"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// MatchRemoteIP matches requests by the remote IP address,
 | 
			
		||||
@ -79,7 +80,7 @@ func (m *MatchRemoteIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
 | 
			
		||||
				return d.Err("the 'forwarded' option is no longer supported; use the 'client_ip' matcher instead")
 | 
			
		||||
			}
 | 
			
		||||
			if d.Val() == "private_ranges" {
 | 
			
		||||
				m.Ranges = append(m.Ranges, PrivateRangesCIDR()...)
 | 
			
		||||
				m.Ranges = append(m.Ranges, internal.PrivateRangesCIDR()...)
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			m.Ranges = append(m.Ranges, d.Val())
 | 
			
		||||
@ -173,7 +174,7 @@ func (m *MatchClientIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
 | 
			
		||||
	for d.Next() {
 | 
			
		||||
		for d.NextArg() {
 | 
			
		||||
			if d.Val() == "private_ranges" {
 | 
			
		||||
				m.Ranges = append(m.Ranges, PrivateRangesCIDR()...)
 | 
			
		||||
				m.Ranges = append(m.Ranges, internal.PrivateRangesCIDR()...)
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			m.Ranges = append(m.Ranges, d.Val())
 | 
			
		||||
@ -250,7 +251,9 @@ func (m MatchClientIP) Match(r *http.Request) bool {
 | 
			
		||||
func provisionCidrsZonesFromRanges(ranges []string) ([]*netip.Prefix, []string, error) {
 | 
			
		||||
	cidrs := []*netip.Prefix{}
 | 
			
		||||
	zones := []string{}
 | 
			
		||||
	repl := caddy.NewReplacer()
 | 
			
		||||
	for _, str := range ranges {
 | 
			
		||||
		str = repl.ReplaceAll(str, "")
 | 
			
		||||
		// Exclude the zone_id from the IP
 | 
			
		||||
		if strings.Contains(str, "%") {
 | 
			
		||||
			split := strings.Split(str, "%")
 | 
			
		||||
 | 
			
		||||
@ -22,6 +22,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"github.com/caddyserver/caddy/v2"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/internal"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
@ -92,7 +93,7 @@ func (m *StaticIPRange) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
 | 
			
		||||
	}
 | 
			
		||||
	for d.NextArg() {
 | 
			
		||||
		if d.Val() == "private_ranges" {
 | 
			
		||||
			m.Ranges = append(m.Ranges, PrivateRangesCIDR()...)
 | 
			
		||||
			m.Ranges = append(m.Ranges, internal.PrivateRangesCIDR()...)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		m.Ranges = append(m.Ranges, d.Val())
 | 
			
		||||
@ -121,19 +122,6 @@ func CIDRExpressionToPrefix(expr string) (netip.Prefix, error) {
 | 
			
		||||
	return prefix, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PrivateRangesCIDR returns a list of private CIDR range
 | 
			
		||||
// strings, which can be used as a configuration shortcut.
 | 
			
		||||
func PrivateRangesCIDR() []string {
 | 
			
		||||
	return []string{
 | 
			
		||||
		"192.168.0.0/16",
 | 
			
		||||
		"172.16.0.0/12",
 | 
			
		||||
		"10.0.0.0/8",
 | 
			
		||||
		"127.0.0.1/8",
 | 
			
		||||
		"fd00::/8",
 | 
			
		||||
		"::1",
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Interface guards
 | 
			
		||||
var (
 | 
			
		||||
	_ caddy.Provisioner     = (*StaticIPRange)(nil)
 | 
			
		||||
 | 
			
		||||
@ -28,6 +28,7 @@ import (
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/caddyconfig"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/internal"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/modules/caddyhttp"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/modules/caddyhttp/headers"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
 | 
			
		||||
@ -688,7 +689,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
 | 
			
		||||
		case "trusted_proxies":
 | 
			
		||||
			for d.NextArg() {
 | 
			
		||||
				if d.Val() == "private_ranges" {
 | 
			
		||||
					h.TrustedProxies = append(h.TrustedProxies, caddyhttp.PrivateRangesCIDR()...)
 | 
			
		||||
					h.TrustedProxies = append(h.TrustedProxies, internal.PrivateRangesCIDR()...)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				h.TrustedProxies = append(h.TrustedProxies, d.Val())
 | 
			
		||||
 | 
			
		||||
@ -26,6 +26,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"github.com/caddyserver/caddy/v2"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/internal"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
@ -49,8 +50,17 @@ func (MatchServerName) CaddyModule() caddy.ModuleInfo {
 | 
			
		||||
 | 
			
		||||
// Match matches hello based on SNI.
 | 
			
		||||
func (m MatchServerName) Match(hello *tls.ClientHelloInfo) bool {
 | 
			
		||||
	// caddytls.TestServerNameMatcher calls this function without any context
 | 
			
		||||
	var repl *caddy.Replacer
 | 
			
		||||
	if ctx := hello.Context(); ctx != nil {
 | 
			
		||||
		repl = ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
 | 
			
		||||
	} else {
 | 
			
		||||
		repl = caddy.NewReplacer()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, name := range m {
 | 
			
		||||
		if certmagic.MatchWildcard(hello.ServerName, name) {
 | 
			
		||||
		rs := repl.ReplaceAll(name, "")
 | 
			
		||||
		if certmagic.MatchWildcard(hello.ServerName, rs) {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@ -107,16 +117,19 @@ func (MatchRemoteIP) CaddyModule() caddy.ModuleInfo {
 | 
			
		||||
 | 
			
		||||
// Provision parses m's IP ranges, either from IP or CIDR expressions.
 | 
			
		||||
func (m *MatchRemoteIP) Provision(ctx caddy.Context) error {
 | 
			
		||||
	repl := caddy.NewReplacer()
 | 
			
		||||
	m.logger = ctx.Logger()
 | 
			
		||||
	for _, str := range m.Ranges {
 | 
			
		||||
		cidrs, err := m.parseIPRange(str)
 | 
			
		||||
		rs := repl.ReplaceAll(str, "")
 | 
			
		||||
		cidrs, err := m.parseIPRange(rs)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		m.cidrs = append(m.cidrs, cidrs...)
 | 
			
		||||
	}
 | 
			
		||||
	for _, str := range m.NotRanges {
 | 
			
		||||
		cidrs, err := m.parseIPRange(str)
 | 
			
		||||
		rs := repl.ReplaceAll(str, "")
 | 
			
		||||
		cidrs, err := m.parseIPRange(rs)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
@ -185,22 +198,18 @@ func (m *MatchRemoteIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
 | 
			
		||||
 | 
			
		||||
		for d.NextArg() {
 | 
			
		||||
			val := d.Val()
 | 
			
		||||
			var exclamation bool
 | 
			
		||||
			if len(val) > 1 && val[0] == '!' {
 | 
			
		||||
				prefixes, err := m.parseIPRange(val[1:])
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return err
 | 
			
		||||
				exclamation, val = true, val[1:]
 | 
			
		||||
			}
 | 
			
		||||
				for _, prefix := range prefixes {
 | 
			
		||||
					m.NotRanges = append(m.NotRanges, prefix.String())
 | 
			
		||||
			ranges := []string{val}
 | 
			
		||||
			if val == "private_ranges" {
 | 
			
		||||
				ranges = internal.PrivateRangesCIDR()
 | 
			
		||||
			}
 | 
			
		||||
			if exclamation {
 | 
			
		||||
				m.NotRanges = append(m.NotRanges, ranges...)
 | 
			
		||||
			} else {
 | 
			
		||||
				prefixes, err := m.parseIPRange(val)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return err
 | 
			
		||||
				}
 | 
			
		||||
				for _, prefix := range prefixes {
 | 
			
		||||
					m.Ranges = append(m.Ranges, prefix.String())
 | 
			
		||||
				}
 | 
			
		||||
				m.Ranges = append(m.Ranges, ranges...)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -233,9 +242,11 @@ func (MatchLocalIP) CaddyModule() caddy.ModuleInfo {
 | 
			
		||||
 | 
			
		||||
// Provision parses m's IP ranges, either from IP or CIDR expressions.
 | 
			
		||||
func (m *MatchLocalIP) Provision(ctx caddy.Context) error {
 | 
			
		||||
	repl := caddy.NewReplacer()
 | 
			
		||||
	m.logger = ctx.Logger()
 | 
			
		||||
	for _, str := range m.Ranges {
 | 
			
		||||
		cidrs, err := m.parseIPRange(str)
 | 
			
		||||
		rs := repl.ReplaceAll(str, "")
 | 
			
		||||
		cidrs, err := m.parseIPRange(rs)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
@ -300,13 +311,12 @@ func (m *MatchLocalIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for d.NextArg() {
 | 
			
		||||
			prefixes, err := m.parseIPRange(d.Val())
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			for _, prefix := range prefixes {
 | 
			
		||||
				m.Ranges = append(m.Ranges, prefix.String())
 | 
			
		||||
			val := d.Val()
 | 
			
		||||
			if val == "private_ranges" {
 | 
			
		||||
				m.Ranges = append(m.Ranges, internal.PrivateRangesCIDR()...)
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			m.Ranges = append(m.Ranges, val)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// No blocks are supported
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user