fix: SSRF guards still allow 100.64.0.0/10 (CGNAT) destinations (#6256)

Signed-off-by: goingforstudying-ctrl <goingforstudying@gmail.com>
Co-authored-by: goingforstudying-ctrl <258847353+goingforstudying-ctrl@users.noreply.github.com>
Co-authored-by: goingforstudying-ctrl <goingforstudying-ctrl@users.noreply.github.com>
This commit is contained in:
goingforstudying-ctrl
2026-10-12 20:50:43 -04:00
committed by GitHub
parent 2cd3d09391
commit 8120a6cee0
2 changed files with 84 additions and 0 deletions
+52
View File
@@ -634,3 +634,55 @@ func (reader *countingReadCloser) Read(buffer []byte) (int, error) {
func (*countingReadCloser) Close() error {
return nil
}
func TestIsInternalIP(t *testing.T) {
tests := []struct {
name string
ip string
internal bool
}{
{name: "loopback", ip: "127.0.0.1", internal: true},
{name: "private 10", ip: "10.0.0.1", internal: true},
{name: "private 172", ip: "172.16.0.1", internal: true},
{name: "private 192", ip: "192.168.1.1", internal: true},
{name: "link local imds", ip: "169.254.169.254", internal: true},
{name: "unspecified", ip: "0.0.0.0", internal: true},
{name: "cgnat lower bound", ip: "100.64.0.1", internal: true},
{name: "cgnat alibaba metadata", ip: "100.100.100.200", internal: true},
{name: "cgnat upper bound", ip: "100.127.255.255", internal: true},
{name: "mapped ipv6 cgnat", ip: "::ffff:100.100.100.200", internal: true},
{name: "ipv6 loopback", ip: "::1", internal: true},
{name: "below cgnat", ip: "100.63.255.255", internal: false},
{name: "above cgnat", ip: "100.128.0.0", internal: false},
{name: "public dns", ip: "8.8.8.8", internal: false},
{name: "public example", ip: "93.184.216.34", internal: false},
{name: "ipv6 public", ip: "2606:4700:4700::1111", internal: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.internal, isInternalIP(net.ParseIP(test.ip)))
})
}
}
func TestHTMLMetaFetcherGetRejectsResolvedCGNAT(t *testing.T) {
originalLookupIPAddr := lookupIPAddr
originalDialContext := dialContext
t.Cleanup(func() {
lookupIPAddr = originalLookupIPAddr
dialContext = originalDialContext
})
lookupIPAddr = func(context.Context, string) ([]net.IPAddr, error) {
return []net.IPAddr{{IP: net.ParseIP("100.100.100.200")}}, nil
}
var dialed atomic.Bool
dialContext = func(context.Context, string, string) (net.Conn, error) {
dialed.Store(true)
return nil, errors.New("unexpected dial")
}
_, err := NewHTMLMetaFetcher().Get(context.Background(), "http://rebind.example/page")
require.ErrorIs(t, err, ErrInternalIP)
require.False(t, dialed.Load())
}
+32
View File
@@ -308,6 +308,38 @@ func TestPostWithoutSecretSetsNoSignatureHeaders(t *testing.T) {
require.False(t, hasSignatureHeaders, "no signature headers should be set when no secret is configured")
}
func TestIsReservedIP(t *testing.T) {
tests := []struct {
name string
ip string
reserved bool
}{
{name: "loopback", ip: "127.0.0.1", reserved: true},
{name: "private 10", ip: "10.0.0.1", reserved: true},
{name: "private 172", ip: "172.16.0.1", reserved: true},
{name: "private 192", ip: "192.168.1.1", reserved: true},
{name: "link local imds", ip: "169.254.169.254", reserved: true},
{name: "cgnat lower bound", ip: "100.64.0.1", reserved: true},
{name: "cgnat alibaba metadata", ip: "100.100.100.200", reserved: true},
{name: "cgnat upper bound", ip: "100.127.255.255", reserved: true},
{name: "mapped ipv6 cgnat", ip: "::ffff:100.100.100.200", reserved: true},
{name: "ipv6 loopback", ip: "::1", reserved: true},
{name: "below cgnat", ip: "100.63.255.255", reserved: false},
{name: "above cgnat", ip: "100.128.0.0", reserved: false},
{name: "public dns", ip: "8.8.8.8", reserved: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.reserved, isReservedIP(net.ParseIP(test.ip)))
})
}
}
func TestValidateURLRejectsCGNAT(t *testing.T) {
resetPrivateDestinationPolicy(t)
require.Error(t, ValidateURL("http://100.100.100.200/hook"))
}
// TestPostRejectsUnspecifiedAddressDestination pins that a webhook URL pointing at the
// unspecified address is refused at dial time, bcs the kernel treats it as the local host.
func TestPostRejectsUnspecifiedAddressDestination(t *testing.T) {