Files
Kelx/outbound_addr_guard_test.go
T
2026-11-30 16:32:37 +00:00

138 lines
6.2 KiB
Go

package main
// adpanel:outbound-cgnat - the daemon's outbound fetch filter (safeURL and the
// safeDialer DialContext) used to rely on netip's IsPrivate/IsGlobalUnicast,
// which both silently allow the carrier-grade NAT range 100.64.0.0/10. The
// panel-side SSRF guards (index.js isPrivateIPv4/isPrivateIPv6,
// utils/node-ssrf-guard.js) explicitly refuse it, and the panel relays user
// URL uploads to the daemon's /v1/fs/uploadUrl where the daemon does the
// fetching - so a range blocked only panel-side stayed reachable through the
// relay (a full SSRF read primitive into Tailscale tailnets and other 100.64/10
// internal services, blunted only by the 80/443 port allowlist). These tests
// pin the daemon's filter to the same classification as the panel guards,
// including the ranges netip cannot see: 0.0.0.0/8, the documentation and
// benchmarking blocks, 240/4, the deprecated IPv6 site-local range, and the
// transition prefixes (6to4/Teredo/NAT64) whose payload is an encoded IPv4
// address.
import (
"net/netip"
"testing"
)
func TestIsBlockedOutboundAddrBlocksSpecialPurposeRanges(t *testing.T) {
cases := []struct {
ip string
reason string
}{
// The audit finding: carrier-grade NAT 100.64.0.0/10 - both edges and
// the Tailscale MagicDNS resolver address in the middle.
{"100.64.0.0", "first address of CGNAT 100.64.0.0/10"},
{"100.64.0.1", "inside CGNAT 100.64.0.0/10"},
{"100.100.100.100", "Tailscale MagicDNS resolver (CGNAT range)"},
{"100.127.255.255", "last address of CGNAT 100.64.0.0/10"},
// IPv4-mapped spelling of the same target (::ffff:100.64.0.1) - the
// Unmap must fold it into the IPv4 classification.
{"::ffff:100.64.0.1", "IPv4-mapped CGNAT address"},
{"::ffff:10.0.0.1", "IPv4-mapped RFC1918 address"},
// 0.0.0.0/8 "this network": IsUnspecified covers only 0.0.0.0 itself.
{"0.0.0.0", "unspecified address"},
{"0.1.2.3", "0.0.0.0/8 this-network range"},
// Documentation and benchmarking blocks (also refused panel-side).
{"192.0.2.1", "TEST-NET-1 documentation range"},
{"198.51.100.7", "TEST-NET-2 documentation range"},
{"203.0.113.9", "TEST-NET-3 documentation range"},
{"198.18.0.1", "benchmarking range 198.18.0.0/15"},
{"198.19.255.255", "benchmarking range 198.18.0.0/15"},
{"240.0.0.1", "reserved 240.0.0.0/4"},
{"255.255.255.255", "limited broadcast"},
// netip regression guard: the ranges the stdlib does classify must keep
// being refused.
{"10.1.2.3", "RFC1918 10.0.0.0/8"},
{"172.16.0.1", "RFC1918 172.16.0.0/12"},
{"192.168.1.1", "RFC1918 192.168.0.0/16"},
{"127.0.0.1", "IPv4 loopback"},
{"169.254.169.254", "link-local cloud metadata"},
{"224.0.0.1", "IPv4 multicast"},
{"::", "IPv6 unspecified"},
{"::1", "IPv6 loopback"},
{"fe80::1", "IPv6 link-local"},
{"fd00::1", "IPv6 unique-local fc00::/7"},
{"ff02::1", "IPv6 multicast"},
// Deprecated site-local fec0::/10 is neither link-local (fe80::/10) nor
// ULA (fc00::/7) in netip's eyes, so it must be refused explicitly.
{"fec0::1", "deprecated IPv6 site-local fec0::/10"},
{"2001:db8::1", "IPv6 documentation 2001:db8::/32"},
// Transition prefixes: their only payload is an encoded IPv4 address
// (here 10.0.0.1), refused wholesale - see the isBlockedOutboundIPv6
// rationale for why this is deliberately stricter than the panel's
// embedded-IPv4 classification.
{"2002:0a00:0001::", "6to4 wrapping 10.0.0.1"},
{"2001:0::fefe:0a0a", "Teredo 2001:0::/32"},
{"64:ff9b::0a00:0001", "NAT64 well-known prefix wrapping 10.0.0.1"},
{"64:ff9b:1::", "NAT64 local-use 64:ff9b:1::/48"},
// Deprecated IPv4-compatible ::/96 wrapping 10.0.0.1.
{"::0a00:0001", "IPv4-compatible ::/96 wrapping 10.0.0.1"},
// The transition ranges are refused WHOLESALE - deliberately stricter
// than node-ssrf-guard.js, which would allow these with a PUBLIC
// embedded IPv4. Pin that divergence: no legitimate DNS answer lands in
// deprecated 6to4/Teredo/::/96, so embedded 8.8.8.8 must not make them
// fetchable.
{"2002:0808:0808::", "6to4 wrapping public 8.8.8.8 (wholesale refusal)"},
{"::0808:0808", "IPv4-compatible ::/96 wrapping public 8.8.8.8 (wholesale refusal)"},
// fec0::/10 far edge: byte 1 = 0xff must classify as site-local.
{"fec0:ffff::1", "last /16 of deprecated site-local fec0::/10"},
// Mapped spelling of a documentation address.
{"::ffff:192.0.2.1", "IPv4-mapped TEST-NET-1 address"},
}
for _, tc := range cases {
addr, err := netip.ParseAddr(tc.ip)
if err != nil {
t.Fatalf("parse %s: %v", tc.ip, err)
}
if !isBlockedOutboundAddr(addr) {
t.Errorf("%s (%s) must be blocked", tc.ip, tc.reason)
}
}
}
// The over-blocking guard: everything the panel guards consider public must
// stay fetchable by the daemon, especially the unicast addresses hugging the
// newly blocked ranges (a boundary off-by-one here silently breaks legitimate
// downloads from hosts whose addresses sit next to 100.64/10 or 198.18/15).
func TestIsBlockedOutboundAddrKeepsPublicAddressesFetchable(t *testing.T) {
cases := []struct {
ip string
reason string
}{
{"8.8.8.8", "public resolver"},
{"1.1.1.1", "public resolver"},
{"100.63.255.255", "public unicast just below CGNAT 100.64.0.0/10"},
{"100.128.0.1", "public unicast just above CGNAT 100.64.0.0/10"},
{"198.20.0.1", "public unicast just above benchmarking 198.18.0.0/15"},
{"203.0.114.1", "public unicast just above TEST-NET-3"},
{"::ffff:8.8.8.8", "IPv4-mapped public address stays fetchable"},
{"2606:4700:4700::1111", "public IPv6 (Cloudflare)"},
{"2001:4860:4860::8888", "public IPv6 (Google) - 2001: but outside db8::/32 and Teredo"},
{"2331::1", "public IPv6 outside every refused prefix"},
}
for _, tc := range cases {
addr, err := netip.ParseAddr(tc.ip)
if err != nil {
t.Fatalf("parse %s: %v", tc.ip, err)
}
if isBlockedOutboundAddr(addr) {
t.Errorf("%s (%s) must stay fetchable", tc.ip, tc.reason)
}
}
}
// 239/4 is multicast (IsMulticast), so the "public unicast" table above cannot
// contain it - assert it separately so the boundary byte before the reserved
// 240/4 range is pinned without muddying the fetchable table.
func TestIsBlockedOutboundAddrMulticastBoundary(t *testing.T) {
if !isBlockedOutboundAddr(netip.MustParseAddr("239.255.255.254")) {
t.Error("239.255.255.254 (IPv4 multicast) must be blocked")
}
}