Allow any public upstream address by explicit opt-in
CI / validate (push) Successful in 33s
CI / gateway-contract (push) Failing after 56s
CI / images (deploy/Dockerfile.api, api) (push) Skipped
CI / images (deploy/Dockerfile.checker, checker) (push) Skipped
CI / images (deploy/Dockerfile.http-gateway, http-gateway) (push) Skipped
CI / images (deploy/Dockerfile.web, web) (push) Skipped

Accept GATEWAY_UPSTREAM_NETWORKS=any while keeping public, own-IP, mixed-answer, destination and client checks.
This commit is contained in:
omar committed 2026-11-27 08:47:23 +03:00
1 parent 3967d14f00
commit 95d5d6d1b3
7 files changed
+111 -33

No files matched your search

+2 -2
View File
@@ -45,6 +45,6 @@ GATEWAY_PROBE_URL=
# Comma-separated exact DNS names and TCP ports, including the probe authority.
# Set "Allow all public websites" in admin Settings, not in the environment.
GATEWAY_ALLOWED_DESTINATIONS=
# 1-32 operator-managed public peer IPs or canonical CIDRs, not exit IP pools.
# Restrict to /32 (IPv4) or /128 (IPv6) where possible; min prefix lengths /24 and /64.
# Set GATEWAY_UPSTREAM_NETWORKS=any to allow public upstream peers from the snapshot.
# For stricter filtering, list 1-32 public peer IPs or canonical CIDRs (/24 IPv4, /64 IPv6, or narrower).
GATEWAY_UPSTREAM_NETWORKS=
+19 -14
View File
@@ -101,17 +101,22 @@ do not send bearer tokens or catalog exports over unprotected LAN or WAN links.
### Provisioning
1. **Enroll only authorized peers (full gateway only).** The gateway considers
1. **Choose an upstream policy (full gateway only).** The gateway considers
every member of the last completed, current checker session across all its
sources. Use only
operator-owned peers or peers whose operator has explicitly authorized
relay use. If the existing snapshot mixes authorized and unauthorized
peers, use a separate checker/database; do not enable the gateway on that
snapshot. In `GATEWAY_UPSTREAM_NETWORKS`, list 1-32 unique public peer IPs or
canonical CIDRs you manage or are authorized to use, IPv4 `/24` or narrower,
IPv6 `/64` or narrower. Prefer individual `/32` or `/128` addresses. These
limits apply to the actual TCP peer, not the exit IP or destination. Do not
add external proxy pools or unrelated addresses to those networks.
sources. Use only operator-owned peers or peers whose operator has explicitly
authorized relay use. If the snapshot mixes authorized and unauthorized peers,
use a separate checker/database; do not enable the gateway on that snapshot.
Set `GATEWAY_UPSTREAM_NETWORKS=any` to allow public upstream addresses from
the published snapshot without maintaining a manual IP list. The gateway
still rejects non-public addresses, its own IP and local interface addresses,
and DNS responses that mix permitted and forbidden addresses. Destination
checks and client authentication are unchanged.
For stricter filtering, set `GATEWAY_UPSTREAM_NETWORKS` to a comma-separated
list of 1-32 unique public peer IPs or canonical CIDRs, IPv4 `/24` or narrower
and IPv6 `/64` or narrower. Prefer individual IPs (`/32` or `/128`). This list
restricts the actual upstream TCP peer, not its exit IP or the destination.
2. **Configure the destination allowlist and probe (full gateway only).** Set
`GATEWAY_ALLOWED_DESTINATIONS` to exact comma-separated DNS names with ports,
for example `origin.example:80,origin.example:443,probe.example:443`. Include
@@ -120,7 +125,7 @@ do not send bearer tokens or catalog exports over unprotected LAN or WAN links.
without credentials, query, or fragment. Keep the configured Cloudflare
probe when changing the destination policy. The endpoint must return exactly
`204` without a redirect, have a trusted TLS certificate, and be reachable
directly from the gateway and through each enrolled peer. HTTP CONNECT and
directly from the gateway and through each permitted peer. HTTP CONNECT and
SOCKS5 CONNECT restrict the TCP destination, not URL paths inside TLS; the
gateway does not decrypt the connection.
3. **Provision a restricted database login.** Start the updated checker to apply
@@ -211,9 +216,9 @@ In **Settings → HTTP gateway**, **Allow all public websites** is off by defaul
only the configured allowlist is permitted. Turning it on also allows public
DNS names and public IP literals via HTTP on port 80, HTTP CONNECT on port
443, or SOCKS5 CONNECT on either port. All DNS answers and IP literals still
pass the same public-address and
local-interface checks; the exact allowlist, upstream enrollment, and proxy
authentication stay in effect. The switch is saved in the database as
pass the same public-address and local-interface checks; the exact allowlist,
configured upstream policy, and proxy authentication remain in effect. The
switch is saved in the database as
`gateway_allow_all_sites`; no environment variable enables or overrides it.
### Catalog and proxy clients
+5 -3
View File
@@ -26,6 +26,7 @@ type Config struct {
ProbeURL *url.URL
AllowedDestinations map[string]struct{} // normalized DNS name and port
UpstreamNetworks []netip.Prefix // operator-owned underlay enrollment
AnyUpstream bool // accept any public upstream, not only enrolled CIDRs
}
// LoadConfig parses the environment without printing any configured values.
@@ -97,9 +98,10 @@ func parseGatewayConfig(getenv func(string) string) (Config, error) {
}
if networks := getenv("GATEWAY_UPSTREAM_NETWORKS"); networks != "" || !cfg.CatalogOnly {
cfg.UpstreamNetworks, ok = parseGatewayUpstreamNetworks(networks)
if !ok {
return Config{}, errors.New("GATEWAY_UPSTREAM_NETWORKS must list 1 to 32 unique operator-managed public IPs or canonical CIDRs (IPv4 /24 or narrower, IPv6 /64 or narrower)")
if strings.EqualFold(strings.TrimSpace(networks), "any") {
cfg.AnyUpstream = true
} else if cfg.UpstreamNetworks, ok = parseGatewayUpstreamNetworks(networks); !ok {
return Config{}, errors.New("GATEWAY_UPSTREAM_NETWORKS must be any or list 1 to 32 unique operator-managed public IPs or canonical CIDRs (IPv4 /24 or narrower, IPv6 /64 or narrower)")
}
}
return cfg, nil
+26
View File
@@ -52,6 +52,9 @@ func TestGatewayLaunchConfig(t *testing.T) {
if !reflect.DeepEqual(cfg.AllowedDestinations, wantDestinations) {
t.Errorf("destination allowlist = %v, want %v", cfg.AllowedDestinations, wantDestinations)
}
if cfg.AnyUpstream {
t.Error("an explicit upstream list must keep strict enrollment")
}
wantNetworks := []netip.Prefix{netip.MustParsePrefix("1.1.1.0/24"), netip.MustParsePrefix("8.8.8.8/32"), netip.MustParsePrefix("2606:4700::/64")}
if !reflect.DeepEqual(cfg.UpstreamNetworks, wantNetworks) {
t.Errorf("enrolled networks = %v, want %v", cfg.UpstreamNetworks, wantNetworks)
@@ -87,6 +90,9 @@ func TestGatewayLaunchConfigRejectsInvalidPolicyAndRedactsValues(t *testing.T) {
{"destination wildcard", "GATEWAY_ALLOWED_DESTINATIONS", "*.example:443"},
{"legacy public web mode", "GATEWAY_ALLOWED_DESTINATIONS", "public-web"},
{"broad upstream", "GATEWAY_UPSTREAM_NETWORKS", "1.1.0.0/16"},
{"any followed by a CIDR", "GATEWAY_UPSTREAM_NETWORKS", "any, 1.1.1.0/24"},
{"any after a listed address", "GATEWAY_UPSTREAM_NETWORKS", "8.8.8.8, any"},
{"any with trailing comma", "GATEWAY_UPSTREAM_NETWORKS", "any,"},
} {
t.Run(tc.name, func(t *testing.T) {
env := identityTestEnv()
@@ -254,6 +260,26 @@ func TestGatewayDestinationAndProbePolicy(t *testing.T) {
}
}
func TestGatewayLaunchConfigAnyUpstream(t *testing.T) {
for _, tc := range []struct{ name, value string }{
{"literal", "any"},
{"padded", " any "},
{"uppercase", "ANY"},
} {
t.Run(tc.name, func(t *testing.T) {
env := identityTestEnv()
env["GATEWAY_UPSTREAM_NETWORKS"] = tc.value
cfg, err := parseGatewayConfig(func(k string) string { return env[k] })
if err != nil {
t.Fatal(err)
}
if !cfg.AnyUpstream || cfg.UpstreamNetworks != nil {
t.Fatalf("AnyUpstream=%t, UpstreamNetworks=%v; want true and nil", cfg.AnyUpstream, cfg.UpstreamNetworks)
}
})
}
}
func TestGatewayUpstreamEnrollmentBounds(t *testing.T) {
addresses := make([]string, 33)
for i := range addresses {
+1 -1
View File
@@ -225,7 +225,7 @@ func wireTruncatedResponse(t *testing.T, client *http.Client, origin *testfixtur
x := origin.Expect(t, peer.Source, testfixture.Exchange{
Method: http.MethodGet, Response: []byte("partial response"), Check: wireOriginHeaders, Truncate: true,
ResponseHeader: http.Header{
"Connection": {"close, X-Wire-Truncated-First", "X-Wire-Truncated-Second"},
"Connection": {"close, X-Wire-Truncated-First", "X-Wire-Truncated-Second"},
"X-Wire-Truncated-First": {"hop-by-hop"},
"X-Wire-Truncated-Second": {"hop-by-hop"},
},
+6 -1
View File
@@ -27,6 +27,7 @@ var (
type RoutePolicy struct {
destinations map[string]struct{}
upstreams []netip.Prefix
anyUpstream bool
probe string
selfName string
selfIP netip.Addr
@@ -46,6 +47,7 @@ func NewRoutePolicy(cfg Config) *RoutePolicy {
p := &RoutePolicy{
destinations: maps.Clone(cfg.AllowedDestinations),
upstreams: slices.Clone(cfg.UpstreamNetworks),
anyUpstream: cfg.AnyUpstream,
openTCP: dialer.DialContext,
localIPs: routeInterfaceIPs,
allowIP: netpolicy.IsPublicIP,
@@ -234,7 +236,7 @@ func (p *RoutePolicy) pin(ctx context.Context, target routeAuthority, upstream b
if target.name != "" && target.name == p.selfName {
return "", denied
}
if upstream && len(p.upstreams) == 0 {
if upstream && !p.anyUpstream && len(p.upstreams) == 0 {
return "", denied
}
ips := []netip.Addr{target.ip}
@@ -266,6 +268,9 @@ func (p *RoutePolicy) pin(ctx context.Context, target routeAuthority, upstream b
}
func (p *RoutePolicy) enrolled(ip netip.Addr) bool {
if p.anyUpstream {
return true
}
for _, prefix := range p.upstreams {
if prefix.Contains(ip) {
return true
+52 -12
View File
@@ -260,17 +260,39 @@ func TestRouteRequestRejectsAnyUnsafeAnswer(t *testing.T) {
}
func TestRouteUpstreamOpensOnlyEnrolledNumericAddress(t *testing.T) {
testRouteUpstreamPinning(t, false)
}
func TestRouteUpstreamAnyPublicAddress(t *testing.T) {
testRouteUpstreamPinning(t, true)
}
func testRouteUpstreamPinning(t *testing.T, anyUpstream bool) {
t.Helper()
cfg := routeTestConfig()
if anyUpstream {
cfg.AnyUpstream = true
cfg.UpstreamNetworks = nil
}
cases := []struct {
name, network, target, lookup, pinned string
answers []string
unenrolled bool
}{
{name: "enrolled IPv4 literal", network: "tcp", target: "8.8.8.7:8443", pinned: "8.8.8.7:8443"},
{name: "enrolled IPv6 literal", network: "tcp", target: "[2606:4700:4700::1001]:443", pinned: "[2606:4700:4700::1001]:443"},
{name: "all enrolled DNS", network: "tcp", target: "Peer.Operator.test:443", lookup: "peer.operator.test.",
pinned: "[2606:4700:4700::1001]:443", answers: []string{"2606:4700:4700::1001", "8.8.8.7"}},
{name: "unenrolled IPv4", network: "tcp", target: "1.1.1.1:443"},
{name: "unenrolled IPv6", network: "tcp", target: "[2001:4860:4860::8888]:443"},
{name: "mixed enrollment", network: "tcp", target: "peer.operator.test:443", lookup: "peer.operator.test.", answers: []string{"8.8.8.7", "1.1.1.1"}},
{name: "unenrolled IPv4", network: "tcp", target: "1.1.1.1:443", pinned: "1.1.1.1:443", unenrolled: true},
{name: "unenrolled IPv6", network: "tcp", target: "[2001:4860:4860::8888]:443", pinned: "[2001:4860:4860::8888]:443", unenrolled: true},
{name: "unenrolled DNS", network: "tcp", target: "Peer.Operator.test:443", lookup: "peer.operator.test.",
pinned: "[2001:4860:4860::8888]:443", answers: []string{"2001:4860:4860::8888", "1.1.1.1"}, unenrolled: true},
{name: "mixed enrollment", network: "tcp", target: "peer.operator.test:443", lookup: "peer.operator.test.",
pinned: "8.8.8.7:443", answers: []string{"8.8.8.7", "1.1.1.1"}, unenrolled: true},
{name: "private literal", network: "tcp", target: "10.0.0.1:443"},
{name: "loopback literal", network: "tcp", target: "127.0.0.1:443"},
{name: "link-local literal", network: "tcp", target: "[fe80::1]:443"},
{name: "special literal", network: "tcp", target: "198.18.1.2:443"},
{name: "mixed private", network: "tcp", target: "peer.operator.test:443", lookup: "peer.operator.test.", answers: []string{"8.8.8.7", "10.0.0.1"}},
{name: "mixed own IP", network: "tcp", target: "peer.operator.test:443", lookup: "peer.operator.test.", answers: []string{"8.8.8.7", "8.8.8.9"}},
{name: "own IP literal", network: "tcp", target: "8.8.8.9:443"},
@@ -282,7 +304,11 @@ func TestRouteUpstreamOpensOnlyEnrolledNumericAddress(t *testing.T) {
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
policy := newRouteTestPolicy(t, routeTestConfig())
policy := newRouteTestPolicy(t, cfg)
wantPinned := tt.pinned
if tt.unenrolled && !anyUpstream {
wantPinned = ""
}
policy.localIPs = func() ([]netip.Addr, error) {
return []netip.Addr{netip.MustParseAddr("8.8.8.9")}, nil
}
@@ -300,8 +326,8 @@ func TestRouteUpstreamOpensOnlyEnrolledNumericAddress(t *testing.T) {
}
policy.openTCP = func(_ context.Context, network, address string) (net.Conn, error) {
opens++
if tt.pinned == "" || network != "tcp" || address != tt.pinned {
t.Fatalf("unexpected open %s %s, want tcp %s", network, address, tt.pinned)
if wantPinned == "" || network != "tcp" || address != wantPinned {
t.Fatalf("unexpected open %s %s, want tcp %s", network, address, wantPinned)
}
client, peer := net.Pipe()
t.Cleanup(func() { _ = peer.Close() })
@@ -314,7 +340,7 @@ func TestRouteUpstreamOpensOnlyEnrolledNumericAddress(t *testing.T) {
if (tt.lookup == "" && lookups != 0) || (tt.lookup != "" && lookups != 1) {
t.Fatalf("lookups = %d", lookups)
}
if tt.pinned == "" {
if wantPinned == "" {
if err != ErrUpstreamDenied || conn != nil || opens != 0 {
t.Fatalf("upstream was not denied before opening: conn=%v, err=%v, opens=%d", conn, err, opens)
}
@@ -404,11 +430,25 @@ func TestRouteFailsClosedWithoutLeakingNetworkErrors(t *testing.T) {
}
func TestRouteAdvertisedIPIsDeniedWithoutInterfaceMatch(t *testing.T) {
cfg := routeTestConfig()
cfg.PublicProxyHost = "8.8.8.9"
policy := newRouteTestPolicy(t, cfg)
if _, err := policy.DialUpstream(t.Context(), "tcp", "8.8.8.9:443"); err != ErrUpstreamDenied {
t.Fatalf("dial to advertised IP returned %v", err)
for _, tc := range []struct {
name string
anyUpstream bool
}{
{"strict enrollment", false},
{"any public upstream", true},
} {
t.Run(tc.name, func(t *testing.T) {
cfg := routeTestConfig()
cfg.PublicProxyHost = "8.8.8.9"
if tc.anyUpstream {
cfg.AnyUpstream = true
cfg.UpstreamNetworks = nil
}
policy := newRouteTestPolicy(t, cfg)
if _, err := policy.DialUpstream(t.Context(), "tcp", "8.8.8.9:443"); err != ErrUpstreamDenied {
t.Fatalf("dial to advertised IP returned %v", err)
}
})
}
}