From b548cabcbf023ee41fee79134c9f9aa8225263ac Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 8 Nov 2026 15:52:39 +1100 Subject: [PATCH] refactor(publicnet): one public-address dial guard for webhooks, attachments and web fetch (#1795) --- internal/app/archied/alerts.go | 3 +- internal/infrastructure/gatewayrpc/server.go | 4 +- .../public.go => publicnet/publicnet.go} | 60 ++++++++++--------- .../publicnet_test.go} | 6 +- internal/tools/webfetch/webfetch.go | 27 ++------- 5 files changed, 45 insertions(+), 55 deletions(-) rename internal/infrastructure/{alertwebhook/public.go => publicnet/publicnet.go} (63%) rename internal/infrastructure/{alertwebhook/public_test.go => publicnet/publicnet_test.go} (78%) diff --git a/internal/app/archied/alerts.go b/internal/app/archied/alerts.go index 3c0665ba1..389f20cb8 100644 --- a/internal/app/archied/alerts.go +++ b/internal/app/archied/alerts.go @@ -17,6 +17,7 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" "github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore" "github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog" + "github.com/samcharles93/archie-core/internal/infrastructure/publicnet" "github.com/samcharles93/archie-core/internal/releaseupdate" ) @@ -301,7 +302,7 @@ func (b *boot) recheckAlerts(ctx context.Context) { // as skipped; one that fails is retried by the store on its schedule. func (b *boot) deliverAlerts(store alert.Deliverer) func(context.Context) { client := &http.Client{Timeout: 15 * time.Second} - public := alertwebhook.PublicClient() + public := publicnet.Client(10*time.Second, 15*time.Second) // The system org's webhook may sit on the instance's own network; another // org's reaches the public internet only. clientFor := func(owner org.OrgID) *http.Client { diff --git a/internal/infrastructure/gatewayrpc/server.go b/internal/infrastructure/gatewayrpc/server.go index b9492eb7c..1ecc9cda4 100644 --- a/internal/infrastructure/gatewayrpc/server.go +++ b/internal/infrastructure/gatewayrpc/server.go @@ -16,7 +16,7 @@ import ( "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/messaging" "github.com/samcharles93/archie-core/internal/domain/taskactions" - "github.com/samcharles93/archie-core/internal/infrastructure/alertwebhook" + "github.com/samcharles93/archie-core/internal/infrastructure/publicnet" "github.com/samcharles93/archie-core/internal/taskstate" ) @@ -35,7 +35,7 @@ type server struct { // reload) the dashboard reads over the same contract; a zero Catalog answers // empty lists and an Unavailable reload. func RegisterServer(registrar grpc.ServiceRegistrar, chat messaging.ChatContract, sessions messaging.SessionStore, catalog Catalog) { - pb.RegisterChatServiceServer(registrar, &server{chat: chat, sessions: sessions, catalog: catalog, fetch: alertwebhook.PublicClient()}) + pb.RegisterChatServiceServer(registrar, &server{chat: chat, sessions: sessions, catalog: catalog, fetch: publicnet.Client(10*time.Second, 15*time.Second)}) } func (s *server) sessionStore() (messaging.SessionStore, error) { diff --git a/internal/infrastructure/alertwebhook/public.go b/internal/infrastructure/publicnet/publicnet.go similarity index 63% rename from internal/infrastructure/alertwebhook/public.go rename to internal/infrastructure/publicnet/publicnet.go index f8d758dd8..57e0abd34 100644 --- a/internal/infrastructure/alertwebhook/public.go +++ b/internal/infrastructure/publicnet/publicnet.go @@ -1,4 +1,7 @@ -package alertwebhook +// Package publicnet keeps outbound requests that a user or org aims on the +// public internet. Archie sends them from inside the instance's network, so a +// URL must not reach the instance's own host, its LAN or other infrastructure. +package publicnet import ( "context" @@ -9,33 +12,8 @@ import ( "time" ) -// PublicClient reaches public addresses only and follows no redirect. It -// carries a webhook an org chose: the daemon sends from inside the instance's -// network, so the org must not aim it at the daemon's own host or LAN. -func PublicClient() *http.Client { - dialer := &net.Dialer{ - Timeout: timeout, - Control: func(_, address string, _ syscall.RawConn) error { - host, _, err := net.SplitHostPort(address) - if err != nil { - return err - } - if ip := net.ParseIP(host); ip == nil || !public(ip) { - return fmt.Errorf("webhook address %s is not public", host) - } - return nil - }, - } - return &http.Client{ - Timeout: 15 * time.Second, - Transport: &http.Transport{DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - return dialer.DialContext(ctx, network, addr) - }}, - CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }, - } -} - -func public(ip net.IP) bool { +// Public reports whether ip is on the public internet. +func Public(ip net.IP) bool { switch { case ip.IsLoopback(), ip.IsPrivate(), ip.IsUnspecified(), ip.IsLinkLocalUnicast(), ip.IsLinkLocalMulticast(), @@ -50,6 +28,32 @@ func public(ip net.IP) bool { return true } +// Client reaches public addresses only and follows no redirect. The address +// is checked as the socket connects, after resolution, so a hostname that +// resolves to a private address is refused too. +func Client(dialTimeout, timeout time.Duration) *http.Client { + dialer := &net.Dialer{ + Timeout: dialTimeout, + Control: func(_, address string, _ syscall.RawConn) error { + host, _, err := net.SplitHostPort(address) + if err != nil { + return err + } + if ip := net.ParseIP(host); ip == nil || !Public(ip) { + return fmt.Errorf("address %s is not public", host) + } + return nil + }, + } + return &http.Client{ + Timeout: timeout, + Transport: &http.Transport{DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + return dialer.DialContext(ctx, network, addr) + }}, + CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }, + } +} + // reserved are ranges that are not the public internet: carrier-grade NAT, // benchmarking, IETF protocol assignments and the NAT64 prefix, which can map // to private IPv4 hosts. diff --git a/internal/infrastructure/alertwebhook/public_test.go b/internal/infrastructure/publicnet/publicnet_test.go similarity index 78% rename from internal/infrastructure/alertwebhook/public_test.go rename to internal/infrastructure/publicnet/publicnet_test.go index 3b5630b6a..0f26c29f0 100644 --- a/internal/infrastructure/alertwebhook/public_test.go +++ b/internal/infrastructure/publicnet/publicnet_test.go @@ -1,4 +1,4 @@ -package alertwebhook +package publicnet import ( "net" @@ -25,8 +25,8 @@ func TestPublicRefusesInstanceNetworks(t *testing.T) { } for _, tt := range tests { t.Run(tt.ip, func(t *testing.T) { - if got := public(net.ParseIP(tt.ip)); got != tt.want { - t.Fatalf("public(%s) = %v, want %v", tt.ip, got, tt.want) + if got := Public(net.ParseIP(tt.ip)); got != tt.want { + t.Fatalf("Public(%s) = %v, want %v", tt.ip, got, tt.want) } }) } diff --git a/internal/tools/webfetch/webfetch.go b/internal/tools/webfetch/webfetch.go index 40111a9cc..6bc020094 100644 --- a/internal/tools/webfetch/webfetch.go +++ b/internal/tools/webfetch/webfetch.go @@ -14,6 +14,8 @@ import ( "time" "golang.org/x/net/html" + + "github.com/samcharles93/archie-core/internal/infrastructure/publicnet" ) const ( @@ -38,9 +40,9 @@ type Config struct { // MaxBytes bounds how much of a response body is read. MaxBytes int64 - // AllowPrivateNetworks permits loopback, private, link-local and - // carrier-grade-NAT addresses. Off by default: leaving it off is what - // keeps the daemon's own control surfaces out of reach. + // AllowPrivateNetworks permits addresses off the public internet. Off by + // default: leaving it off is what keeps the daemon's own control surfaces + // out of reach. AllowPrivateNetworks bool } @@ -245,24 +247,7 @@ func (c *Client) addressAllowed(ip net.IP) bool { if c.cfg.AllowPrivateNetworks { return true } - switch { - case ip.IsLoopback(), ip.IsPrivate(), ip.IsUnspecified(), - ip.IsLinkLocalUnicast(), ip.IsLinkLocalMulticast(), - ip.IsInterfaceLocalMulticast(), ip.IsMulticast(): - return false - case isCarrierGradeNAT(ip): - return false - default: - return true - } -} - -// isCarrierGradeNAT reports whether ip is in 100.64.0.0/10, which net.IP has -// no predicate for and which routes to infrastructure rather than the public -// internet. -func isCarrierGradeNAT(ip net.IP) bool { - v4 := ip.To4() - return v4 != nil && v4[0] == 100 && v4[1] >= 64 && v4[1] <= 127 + return publicnet.Public(ip) } // blockedElements hold text that is markup machinery rather than content.