Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
fix: block CGNAT SSRF and restore cross-database CI coverage
bluedbirdf4d97fafcdab
diff --git a/.github/workflows/backend-tests.yml b/.github/workflows/backend-tests.ymlindex a1eca1e56..06a714c5a 100644--- a/.github/workflows/backend-tests.yml+++ b/.github/workflows/backend-tests.yml@@ -66,7 +66,7 @@ jobs:case "${{ matrix.test-group }}" instore)# 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:;;esacenv:- DRIVER: ${{ matrix.test-group == 'store' && '' || 'sqlite' }}+ DRIVER: sqlite- name: Upload coverageif: github.event_name == 'push' && github.ref == 'refs/heads/main'diff --git a/internal/linkmeta/html_meta.go b/internal/linkmeta/html_meta.goindex 3947476bb..e2f2c2ad0 100644--- a/internal/linkmeta/html_meta.go+++ b/internal/linkmeta/html_meta.go@@ -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 {diff --git a/internal/linkmeta/html_meta_test.go b/internal/linkmeta/html_meta_test.goindex 252c46963..c1aadb3dd 100644--- a/internal/linkmeta/html_meta_test.go+++ b/internal/linkmeta/html_meta_test.go@@ -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 := lookupIPAddroriginalDialContext := 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- }- dialContext = func(context.Context, string, string) (net.Conn, error) {- t.Fatal("internal IP should be rejected before dialing")- return nil, 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)+ })}-- _, err := secureDialContext(context.Background(), "tcp", "rebind.example:80")- require.ErrorIs(t, err, ErrInternalIP)}func TestSecureDialContextDialsResolvedIP(t *testing.T) {diff --git a/internal/webhook/validate.go b/internal/webhook/validate.goindex fda5aea9d..439de00e7 100644--- a/internal/webhook/validate.go+++ b/internal/webhook/validate.go@@ -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 hostnetip.MustParsePrefix("127.0.0.0/8"), // IPv4 loopbacknetip.MustParsePrefix("10.0.0.0/8"), // RFC-1918 class Anetip.MustParsePrefix("172.16.0.0/12"), // RFC-1918 class Bnetip.MustParsePrefix("192.168.0.0/16"), // RFC-1918 class C+ netip.MustParsePrefix("100.64.0.0/10"), // RFC-6598 shared address space / CGNATnetip.MustParsePrefix("169.254.0.0/16"), // Link-local / cloud IMDSnetip.MustParsePrefix("::/128"), // IPv6 unspecified address, dials the local hostnetip.MustParsePrefix("::1/128"), // IPv6 loopbackdiff --git a/internal/webhook/webhook_test.go b/internal/webhook/webhook_test.goindex 0d1896fb1..6b1607330 100644--- a/internal/webhook/webhook_test.go+++ b/internal/webhook/webhook_test.go@@ -1,6 +1,7 @@package webhookimport (+ "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)+ }+ })+ }+}