mirror of
https://github.com/andwati/zitadel.git
synced 2026-12-02 08:58:52 +00:00
Merge commit from fork
* Add DenyLists parsing * Remove unneeded returned error * Plug global denylist into Command * app creation: apply denylist to backchannel logout URI * Inject denylist to backchannel logout worker * webhook config: validate against blocked URLs * Add notificationsWebhook denylist target * command: Add SMTP endpoint validation against blocklist * command: Add SMS endpoint validation against blocklist * Validate webhook endpoint against denylist on channel notification * Remove unused tests * handle deprecated denylists * remove unintended denylist entry in deprecated list * use single http client * fix tests * update comments * fixes * cleanup * address comments * fix merge --------- Co-authored-by: Livio Spring <[email protected]>
This commit is contained in:
@@ -14,10 +14,7 @@ type AddressChecker interface {
|
||||
func ParseDenyList(inputList []string) ([]AddressChecker, error) {
|
||||
var toReturn []AddressChecker
|
||||
for _, input := range inputList {
|
||||
parsed, parseErr := NewHostChecker(input)
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
parsed := NewHostChecker(input)
|
||||
if parsed != nil {
|
||||
toReturn = append(toReturn, parsed)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package denylist
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseDenyList(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tt := []struct {
|
||||
testName string
|
||||
inputIPList []string
|
||||
expectedAddressCheckers int
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
testName: "when empty list should return no error and no checker list",
|
||||
},
|
||||
{
|
||||
testName: "list with a non-ip should return no error and no checker list",
|
||||
inputIPList: []string{"127.0.0.1", "localhost", "not an ip"},
|
||||
expectedAddressCheckers: 3,
|
||||
},
|
||||
{
|
||||
testName: "parseable IP list should return no error, non-zero hash and non-empty checker list",
|
||||
inputIPList: []string{"127.0.0.1", "localhost", "1.1.1.1"},
|
||||
expectedAddressCheckers: 3,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.testName, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Test
|
||||
checkers, err := ParseDenyList(tc.inputIPList)
|
||||
|
||||
// Verify
|
||||
assert.Len(t, checkers, tc.expectedAddressCheckers)
|
||||
assert.ErrorIs(t, err, tc.expectedError)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package denylist
|
||||
import (
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
internal_net "github.com/zitadel/zitadel/internal/net"
|
||||
)
|
||||
@@ -15,23 +16,30 @@ type HostChecker struct {
|
||||
Domain string
|
||||
}
|
||||
|
||||
func NewHostChecker(entry string) (AddressChecker, error) {
|
||||
// NewHostChecker returns an [AddressChecker].
|
||||
//
|
||||
// If the input string is neither a network, nor an IP address, it is considered a domain and saved as such
|
||||
func NewHostChecker(entry string) AddressChecker {
|
||||
if entry == "" {
|
||||
return nil, nil
|
||||
return nil
|
||||
}
|
||||
_, network, err := net.ParseCIDR(entry)
|
||||
if err == nil {
|
||||
return &HostChecker{Net: network}, nil
|
||||
return &HostChecker{Net: network}
|
||||
}
|
||||
if ip := net.ParseIP(entry); ip != nil {
|
||||
return &HostChecker{IP: ip}, nil
|
||||
return &HostChecker{IP: ip}
|
||||
}
|
||||
return &HostChecker{Domain: entry}, nil
|
||||
return &HostChecker{Domain: entry}
|
||||
}
|
||||
|
||||
// IsDenied checks if the address or one of the IPs is blocked by the [HostChecker].
|
||||
// The address is checked using string comparison.
|
||||
// If either is blocked, an [AddressDeniedError] will be returned.
|
||||
// If either is not blocked, it will return nil.
|
||||
func (c *HostChecker) IsDenied(ips []net.IP, address string) error {
|
||||
// if the address matches the domain, no additional checks as needed
|
||||
if c.Domain == address {
|
||||
if c.Domain != "" && strings.EqualFold(c.Domain, address) {
|
||||
return NewAddressDeniedError(c.Domain)
|
||||
}
|
||||
// otherwise we need to check on ips (incl. the resolved ips of the host)
|
||||
@@ -46,20 +54,9 @@ func (c *HostChecker) IsDenied(ips []net.IP, address string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsHostBlocked checks address against denyList. If a match is found, an [AddressDeniedError] will be returned
|
||||
// Takes an input lookupFunc to convert address to a list of IP addresses.
|
||||
// If lookupFunc is nil, it defaults to [net.LookupIP]
|
||||
func IsHostBlocked(denyList []AddressChecker, address *url.URL, lookupFunc internal_net.IPLookupFunc) error {
|
||||
if lookupFunc == nil {
|
||||
lookupFunc = net.LookupIP
|
||||
}
|
||||
|
||||
host := address.Hostname()
|
||||
ips, err := internal_net.HostnameToIPList(host, lookupFunc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// IsHostBlocked checks address against denyList. If a match is found, an [AddressDeniedError] will be returned.
|
||||
// If no match is found, it will return nil.
|
||||
func IsHostBlocked(denyList []AddressChecker, host string, ips ...net.IP) error {
|
||||
for _, denied := range denyList {
|
||||
if err := denied.IsDenied(ips, host); err != nil {
|
||||
return err
|
||||
@@ -67,3 +64,26 @@ func IsHostBlocked(denyList []AddressChecker, address *url.URL, lookupFunc inter
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsHostnameBlocked checks a hostname against the denyList. If a match is found, an [AddressDeniedError] will be returned
|
||||
// It takes an input lookupFunc to convert the hostname to a list of IP addresses.
|
||||
// If lookupFunc is nil, it defaults to [net.LookupIP]
|
||||
func IsHostnameBlocked(denyList []AddressChecker, hostname string, lookupFunc internal_net.IPLookupFunc) error {
|
||||
if lookupFunc == nil {
|
||||
lookupFunc = net.LookupIP
|
||||
}
|
||||
|
||||
ips, err := internal_net.HostnameToIPList(hostname, lookupFunc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return IsHostBlocked(denyList, hostname, ips...)
|
||||
}
|
||||
|
||||
// IsURLBlocked checks a URL against denyList. If a match is found, an [AddressDeniedError] will be returned
|
||||
// It takes an input lookupFunc to convert the URL's hostname to a list of IP addresses.
|
||||
// If lookupFunc is nil, it defaults to [net.LookupIP]
|
||||
func IsURLBlocked(denyList []AddressChecker, address *url.URL, lookupFunc internal_net.IPLookupFunc) error {
|
||||
return IsHostnameBlocked(denyList, address.Hostname(), lookupFunc)
|
||||
}
|
||||
|
||||
@@ -7,23 +7,25 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
func TestIsHostBlocked(t *testing.T) {
|
||||
func TestIsURLBlocked(t *testing.T) {
|
||||
t.Parallel()
|
||||
var denyList = []AddressChecker{
|
||||
mustNewHostChecker(t, "192.168.5.0/24"),
|
||||
mustNewHostChecker(t, "127.0.0.1"),
|
||||
mustNewHostChecker(t, "test.com"),
|
||||
|
||||
denyList := []AddressChecker{
|
||||
NewHostChecker("192.168.5.0/24"),
|
||||
NewHostChecker("127.0.0.1"),
|
||||
NewHostChecker("test.com"),
|
||||
}
|
||||
|
||||
type fields struct {
|
||||
lookup func(host string) ([]net.IP, error)
|
||||
}
|
||||
type args struct {
|
||||
address *url.URL
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
@@ -31,21 +33,31 @@ func TestIsHostBlocked(t *testing.T) {
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "in range",
|
||||
name: "in range (CIDR match)",
|
||||
fields: fields{
|
||||
lookup: func(host string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("192.168.5.4")}, nil
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
address: mustNewURL(t, "https://192.168.5.4/hodor"),
|
||||
},
|
||||
want: NewAddressDeniedError("192.168.5.0/24"),
|
||||
},
|
||||
{
|
||||
name: "exact ip",
|
||||
name: "exact ip literal match",
|
||||
fields: fields{
|
||||
lookup: func(host string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("127.0.0.1")}, nil
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
address: mustNewURL(t, "http://127.0.0.1:8080/hodor"),
|
||||
},
|
||||
want: NewAddressDeniedError("127.0.0.1"),
|
||||
},
|
||||
{
|
||||
name: "address match",
|
||||
name: "domain string match",
|
||||
fields: fields{
|
||||
lookup: func(host string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("194.264.52.4")}, nil
|
||||
@@ -57,7 +69,7 @@ func TestIsHostBlocked(t *testing.T) {
|
||||
want: NewAddressDeniedError("test.com"),
|
||||
},
|
||||
{
|
||||
name: "address not match",
|
||||
name: "allowed domain string pass",
|
||||
fields: fields{
|
||||
lookup: func(host string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("194.264.52.4")}, nil
|
||||
@@ -69,7 +81,7 @@ func TestIsHostBlocked(t *testing.T) {
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "looked up ip matches",
|
||||
name: "unmatched domain resolves to blocked IP (SSRF pivot attempt)",
|
||||
fields: fields{
|
||||
lookup: func(host string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("127.0.0.1")}, nil
|
||||
@@ -81,7 +93,7 @@ func TestIsHostBlocked(t *testing.T) {
|
||||
want: NewAddressDeniedError("127.0.0.1"),
|
||||
},
|
||||
{
|
||||
name: "lookup failure",
|
||||
name: "dns resolver failures are isolated",
|
||||
fields: fields{
|
||||
lookup: func(host string) ([]net.IP, error) {
|
||||
return nil, io.EOF
|
||||
@@ -93,23 +105,48 @@ func TestIsHostBlocked(t *testing.T) {
|
||||
want: zerrors.ThrowInternal(io.EOF, "NET-4m9s2", "lookup failed"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
got := IsHostBlocked(denyList, tt.args.address, tt.fields.lookup)
|
||||
got := IsURLBlocked(denyList, tt.args.address, tt.fields.lookup)
|
||||
assert.ErrorIs(t, got, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func mustNewHostChecker(t *testing.T, ip string) AddressChecker {
|
||||
t.Helper()
|
||||
checker, err := NewHostChecker(ip)
|
||||
if err != nil {
|
||||
t.Errorf("unable to parse cidr of %q because: %v", ip, err)
|
||||
t.FailNow()
|
||||
func TestIsHostnameBlocked_EdgeCases(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
denyList := []AddressChecker{
|
||||
NewHostChecker("127.0.0.1"),
|
||||
NewHostChecker("blocked.example.com"),
|
||||
}
|
||||
return checker
|
||||
|
||||
t.Run("blocked by host lookup via hostname method", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := IsHostnameBlocked(denyList, "safe.example.com", func(_ string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("127.0.0.1")}, nil
|
||||
})
|
||||
assert.ErrorIs(t, err, NewAddressDeniedError("127.0.0.1"))
|
||||
})
|
||||
|
||||
t.Run("blocked by exact domain string mapping", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := IsHostnameBlocked(denyList, "blocked.example.com", func(_ string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("192.0.2.1")}, nil
|
||||
})
|
||||
assert.ErrorIs(t, err, NewAddressDeniedError("blocked.example.com"))
|
||||
})
|
||||
|
||||
t.Run("ipv6 target check compatibility", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
// URL parsing or raw string handling yields plain IPv6 targets, make sure checker behaves cleanly
|
||||
err := IsHostnameBlocked(denyList, "::1", func(_ string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("::1")}, nil
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func mustNewURL(t *testing.T, raw string) *url.URL {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package denylist
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NewHTTPTransport returns a cloned default transport that enforces denylist checks
|
||||
// right before each TCP dial to avoid DNS rebinding TOCTOU gaps.
|
||||
func NewHTTPTransport(denyList []AddressChecker) http.RoundTripper {
|
||||
base := http.DefaultTransport.(*http.Transport).Clone()
|
||||
if len(denyList) == 0 {
|
||||
return base
|
||||
}
|
||||
dialer := &net.Dialer{
|
||||
Timeout: 5 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
Control: func(network, address string, c syscall.RawConn) error {
|
||||
host, _, err := net.SplitHostPort(address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parsedIP := net.ParseIP(host)
|
||||
if parsedIP == nil { // at this point, it must be an IP so it should never happen
|
||||
return &net.DNSError{Err: "invalid IP address", Name: host}
|
||||
}
|
||||
|
||||
return IsHostBlocked(denyList, host, parsedIP)
|
||||
},
|
||||
}
|
||||
base.DialContext = dialer.DialContext
|
||||
|
||||
return base
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package denylist
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewHTTPTransport_Allowed(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Create a denylist that does NOT block our local test server
|
||||
denyList := []AddressChecker{
|
||||
NewHostChecker("192.168.1.0/24"),
|
||||
NewHostChecker("10.0.0.1"),
|
||||
}
|
||||
|
||||
// Spin up a dummy local test server. It will listen on a loopback address like 127.0.0.1 or ::1
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("success"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Instantiate the transport with the denylist rules
|
||||
transport := NewHTTPTransport(denyList)
|
||||
client := &http.Client{Transport: transport}
|
||||
|
||||
// Execute the request to the allowed local server target
|
||||
resp, err := client.Get(server.URL)
|
||||
assert.NoError(t, err)
|
||||
if resp != nil {
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewHTTPTransport_BlockedByControl(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Spin up a test server so we can dynamically extract its specific local listening loopback IP
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Parse out the target host/ip text from the server URL
|
||||
u, err := net.Dial("tcp", server.Listener.Addr().String())
|
||||
assert.NoError(t, err)
|
||||
localIP, _, err := net.SplitHostPort(u.RemoteAddr().String())
|
||||
assert.NoError(t, err)
|
||||
_ = u.Close()
|
||||
|
||||
// Add the explicitly discovered test server IP to the denylist
|
||||
denyList := []AddressChecker{
|
||||
NewHostChecker(localIP),
|
||||
}
|
||||
|
||||
transport := NewHTTPTransport(denyList)
|
||||
client := &http.Client{Transport: transport}
|
||||
|
||||
// This must fail because the Control hook interceptor throws an address denied error
|
||||
// right before the low-level TCP handshake begins.
|
||||
_, err = client.Get(server.URL) //nolint:bodyclose
|
||||
assert.Error(t, err)
|
||||
|
||||
// Because Go wraps dialer context execution errors inside a generic url.Error,
|
||||
// we verify that the true root cause matches our address denial definition.
|
||||
var addressDeniedErr *AddressDeniedError
|
||||
assert.ErrorAs(t, err, &addressDeniedErr)
|
||||
}
|
||||
|
||||
func TestNewHTTPTransport_EmptyDenylistReturnsDefaultClone(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// An empty list should bypass adding a dialer wrapper entirely for performance
|
||||
transport := NewHTTPTransport([]AddressChecker{})
|
||||
|
||||
assert.NotNil(t, transport)
|
||||
httpTransport, ok := transport.(*http.Transport)
|
||||
assert.True(t, ok)
|
||||
|
||||
// Ensure that DialContext is still populated (inherited cleanly from http.DefaultTransport Clone)
|
||||
assert.NotNil(t, httpTransport.DialContext)
|
||||
}
|
||||
|
||||
func TestNewHTTPTransport_MalformedAddressHandling(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
denyList := []AddressChecker{
|
||||
NewHostChecker("127.0.0.1"),
|
||||
}
|
||||
|
||||
transport := NewHTTPTransport(denyList)
|
||||
httpTransport, ok := transport.(*http.Transport)
|
||||
assert.True(t, ok)
|
||||
|
||||
// Explicitly invoke DialContext with an unparseable address format (missing a valid port boundary)
|
||||
// to test how the underlying Control code deals with structural string parsing failure exceptions.
|
||||
_, err := httpTransport.DialContext(context.Background(), "tcp", "malformed-address-without-port")
|
||||
assert.Error(t, err)
|
||||
|
||||
// Ensure it didn't crash and returns a clean, standard connection framework error context
|
||||
var netErr net.Error
|
||||
assert.True(t, errors.As(err, &netErr))
|
||||
}
|
||||
Reference in New Issue
Block a user