plugin/proxyproto: Apply an explicitly configured default policy evenwhen no allow list is present. (#8278)

* plugin/proxyproto: Apply an explicitly configured default policy even when no allow list is present.

This PR fix the issue where explicitly configured default reject policy
is ignoreed when no allo list is present

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

* golint fix

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

---------

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang
2026-07-13 17:34:30 -07:00
committed by GitHub
parent e16e829181
commit 5131b8f944
2 changed files with 37 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ func setup(c *caddy.Controller) error {
var ( var (
allowedIPNets []*net.IPNet allowedIPNets []*net.IPNet
policy = proxyproto.IGNORE policy = proxyproto.IGNORE
defaultSet bool
sessionTrackingTTL time.Duration sessionTrackingTTL time.Duration
sessionTrackingMaxSessions int sessionTrackingMaxSessions int
) )
@@ -44,6 +45,7 @@ func setup(c *caddy.Controller) error {
allowedIPNets = append(allowedIPNets, ipnet) allowedIPNets = append(allowedIPNets, ipnet)
} }
case "default": case "default":
defaultSet = true
v := c.RemainingArgs() v := c.RemainingArgs()
if len(v) != 1 { if len(v) != 1 {
return plugin.Error("proxyproto", c.ArgErr()) return plugin.Error("proxyproto", c.ArgErr())
@@ -84,6 +86,9 @@ func setup(c *caddy.Controller) error {
} }
config.ProxyProtoConnPolicy = func(connPolicyOptions proxyproto.ConnPolicyOptions) (proxyproto.Policy, error) { config.ProxyProtoConnPolicy = func(connPolicyOptions proxyproto.ConnPolicyOptions) (proxyproto.Policy, error) {
if len(allowedIPNets) == 0 { if len(allowedIPNets) == 0 {
if defaultSet {
return policy, nil
}
return proxyproto.USE, nil return proxyproto.USE, nil
} }
h, _, _ := net.SplitHostPort(connPolicyOptions.Upstream.String()) h, _, _ := net.SplitHostPort(connPolicyOptions.Upstream.String())

View File

@@ -1,12 +1,15 @@
package proxyproto package proxyproto
import ( import (
"net"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/coredns/caddy" "github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
proxyprotoLib "github.com/pires/go-proxyproto"
) )
func TestSetup(t *testing.T) { func TestSetup(t *testing.T) {
@@ -84,3 +87,32 @@ func TestSetup(t *testing.T) {
} }
} }
} }
func TestDefaultPolicyWithoutAllow(t *testing.T) {
c := caddy.NewTestController(
"dns",
"proxyproto {\ndefault reject\n}",
)
if err := setup(c); err != nil {
t.Fatal(err)
}
cfg := dnsserver.GetConfig(c)
policy, err := cfg.ProxyProtoConnPolicy(
proxyprotoLib.ConnPolicyOptions{
Upstream: &net.TCPAddr{
IP: net.ParseIP("192.0.2.1"),
Port: 53,
},
},
)
if err != nil {
t.Fatal(err)
}
if policy != proxyprotoLib.REJECT {
t.Fatalf("Expected REJECT, got %v", policy)
}
}