mirror of
https://github.com/usememos/memos.git
synced 2026-11-19 10:29:11 +01:00
fix: block CGNAT SSRF and restore cross-database CI coverage
This commit is contained in:
@@ -66,7 +66,7 @@ jobs:
|
||||
case "${{ matrix.test-group }}" in
|
||||
store)
|
||||
# Run store tests for all drivers (sqlite, mysql, postgres)
|
||||
go test -v -coverprofile=coverage.out -covermode=atomic ./store/...
|
||||
DRIVER= go test -v -coverprofile=coverage.out -covermode=atomic ./store/...
|
||||
;;
|
||||
server)
|
||||
# Includes ./server/test, which boots the real server via
|
||||
@@ -82,7 +82,7 @@ jobs:
|
||||
;;
|
||||
esac
|
||||
env:
|
||||
DRIVER: ${{ matrix.test-group == 'store' && '' || 'sqlite' }}
|
||||
DRIVER: sqlite
|
||||
|
||||
- name: Upload coverage
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||
|
||||
@@ -113,7 +113,13 @@ func resolveAllowedIPs(ctx context.Context, host string) ([]net.IP, error) {
|
||||
}
|
||||
|
||||
func isInternalIP(ip net.IP) bool {
|
||||
return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsUnspecified()
|
||||
if ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsUnspecified() {
|
||||
return true
|
||||
}
|
||||
// IsPrivate excludes RFC 6598 shared address space (100.64.0.0/10).
|
||||
// To4 also covers IPv4-mapped IPv6 addresses.
|
||||
ipv4 := ip.To4()
|
||||
return ipv4 != nil && ipv4[0] == 100 && ipv4[1] >= 64 && ipv4[1] <= 127
|
||||
}
|
||||
|
||||
func validateURL(urlStr string) error {
|
||||
|
||||
@@ -465,6 +465,37 @@ func TestHTMLMetaFetcherRejectsUnsafeURLsAndRedirects(t *testing.T) {
|
||||
require.ErrorIs(t, err, ErrInternalIP)
|
||||
}
|
||||
|
||||
func TestValidateURLCGNAT(t *testing.T) {
|
||||
tests := []struct {
|
||||
ip string
|
||||
blocked bool
|
||||
}{
|
||||
{"100.63.255.255", false},
|
||||
{"100.64.0.0", true},
|
||||
{"100.100.100.200", true},
|
||||
{"100.127.255.255", true},
|
||||
{"100.128.0.0", false},
|
||||
{"::ffff:100.64.0.0", true},
|
||||
{"::ffff:100.100.100.200", true},
|
||||
{"::ffff:100.127.255.255", true},
|
||||
{"::ffff:100.128.0.0", false},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.ip, func(t *testing.T) {
|
||||
target := "http://" + net.JoinHostPort(tc.ip, "80") + "/page"
|
||||
err := validateURL(target)
|
||||
if tc.blocked {
|
||||
require.ErrorIs(t, err, ErrInternalIP)
|
||||
redirect, err := http.NewRequest(http.MethodGet, target, nil)
|
||||
require.NoError(t, err)
|
||||
require.ErrorIs(t, newHTTPClient().CheckRedirect(redirect, nil), ErrInternalIP)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecureDialContextRejectsResolvedInternalIP(t *testing.T) {
|
||||
originalLookupIPAddr := lookupIPAddr
|
||||
originalDialContext := dialContext
|
||||
@@ -473,16 +504,21 @@ func TestSecureDialContextRejectsResolvedInternalIP(t *testing.T) {
|
||||
dialContext = originalDialContext
|
||||
})
|
||||
|
||||
lookupIPAddr = func(context.Context, string) ([]net.IPAddr, error) {
|
||||
return []net.IPAddr{{IP: net.ParseIP("127.0.0.1")}}, nil
|
||||
for _, ip := range []string{"127.0.0.1", "100.100.100.200", "::ffff:100.100.100.200"} {
|
||||
t.Run(ip, func(t *testing.T) {
|
||||
dialContext = func(context.Context, string, string) (net.Conn, error) {
|
||||
t.Fatal("internal IP should be rejected before dialing")
|
||||
return nil, nil
|
||||
}
|
||||
lookupIPAddr = func(context.Context, string) ([]net.IPAddr, error) {
|
||||
return []net.IPAddr{{IP: net.ParseIP(ip)}}, nil
|
||||
}
|
||||
_, err := secureDialContext(context.Background(), "tcp", "rebind.example:80")
|
||||
require.ErrorIs(t, err, ErrInternalIP)
|
||||
_, err = secureDialContext(context.Background(), "tcp", net.JoinHostPort(ip, "80"))
|
||||
require.ErrorIs(t, err, ErrInternalIP)
|
||||
})
|
||||
}
|
||||
dialContext = func(context.Context, string, string) (net.Conn, error) {
|
||||
t.Fatal("internal IP should be rejected before dialing")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
_, err := secureDialContext(context.Background(), "tcp", "rebind.example:80")
|
||||
require.ErrorIs(t, err, ErrInternalIP)
|
||||
}
|
||||
|
||||
func TestSecureDialContextDialsResolvedIP(t *testing.T) {
|
||||
|
||||
@@ -15,14 +15,15 @@ import (
|
||||
// reservedNetworks lists IP ranges blocked by default for outbound webhook requests.
|
||||
// Explicit allowlist entries or the deprecated blanket override may permit them. The
|
||||
// ranges cover "this network" (the kernel treats it as the local host), loopback,
|
||||
// RFC-1918 private, link-local (including cloud IMDS at 169.254.169.254), and their
|
||||
// IPv6 equivalents.
|
||||
// RFC-1918 private, RFC-6598 shared, link-local (including cloud IMDS at
|
||||
// 169.254.169.254), and their IPv6 equivalents.
|
||||
var reservedNetworks = []netip.Prefix{
|
||||
netip.MustParsePrefix("0.0.0.0/8"), // "this network" (RFC 791), dials the local host
|
||||
netip.MustParsePrefix("127.0.0.0/8"), // IPv4 loopback
|
||||
netip.MustParsePrefix("10.0.0.0/8"), // RFC-1918 class A
|
||||
netip.MustParsePrefix("172.16.0.0/12"), // RFC-1918 class B
|
||||
netip.MustParsePrefix("192.168.0.0/16"), // RFC-1918 class C
|
||||
netip.MustParsePrefix("100.64.0.0/10"), // RFC-6598 shared address space / CGNAT
|
||||
netip.MustParsePrefix("169.254.0.0/16"), // Link-local / cloud IMDS
|
||||
netip.MustParsePrefix("::/128"), // IPv6 unspecified address, dials the local host
|
||||
netip.MustParsePrefix("::1/128"), // IPv6 loopback
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package webhook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
@@ -41,6 +42,14 @@ func TestConfigurePrivateDestinationAllowlist(t *testing.T) {
|
||||
require.Error(t, ValidateURL("http://127.0.0.43/hook"))
|
||||
})
|
||||
|
||||
t.Run("CGNAT requires an explicit allowlist entry", func(t *testing.T) {
|
||||
resetPrivateDestinationPolicy(t)
|
||||
require.Error(t, ValidateURL("http://100.100.100.200/hook"))
|
||||
require.NoError(t, ConfigurePrivateDestinationAllowlist([]string{"100.100.100.200"}))
|
||||
require.NoError(t, ValidateURL("http://100.100.100.200/hook"))
|
||||
require.Error(t, ValidateURL("http://100.100.100.201/hook"))
|
||||
})
|
||||
|
||||
t.Run("CIDR is normalized and allowed", func(t *testing.T) {
|
||||
resetPrivateDestinationPolicy(t)
|
||||
require.NoError(t, ConfigurePrivateDestinationAllowlist([]string{"127.0.0.99/24"}))
|
||||
@@ -330,3 +339,37 @@ func TestValidateURLRejectsUnspecifiedAddress(t *testing.T) {
|
||||
require.Error(t, ValidateURL("http://0.0.0.0:8080/hook"))
|
||||
require.Error(t, ValidateURL("http://[::]:8080/hook"))
|
||||
}
|
||||
|
||||
func TestValidateURLCGNAT(t *testing.T) {
|
||||
resetPrivateDestinationPolicy(t)
|
||||
tests := []struct {
|
||||
ip string
|
||||
blocked bool
|
||||
}{
|
||||
{"100.63.255.255", false},
|
||||
{"100.64.0.0", true},
|
||||
{"100.100.100.200", true},
|
||||
{"100.127.255.255", true},
|
||||
{"100.128.0.0", false},
|
||||
{"::ffff:100.64.0.0", true},
|
||||
{"::ffff:100.100.100.200", true},
|
||||
{"::ffff:100.127.255.255", true},
|
||||
{"::ffff:100.128.0.0", false},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.ip, func(t *testing.T) {
|
||||
address := net.JoinHostPort(tc.ip, "80")
|
||||
err := ValidateURL("http://" + address + "/hook")
|
||||
if tc.blocked {
|
||||
require.ErrorContains(t, err, "must not resolve to a reserved or private IP address")
|
||||
// Cancellation prevents any network connection if the guard regresses.
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, err = safeDialContext(ctx, "tcp", address)
|
||||
require.ErrorContains(t, err, "connection to reserved/private IP address is not allowed")
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user