refactor(publicnet): one public-address dial guard for webhooks, attachments and web fetch (#1795)
deploy / gate (push) Failing after 8m13s
deploy / archied-image (push) Skipped
deploy / agent-image (push) Skipped
deploy / dist-zip (push) Skipped

This commit is contained in:
sam committed 2026-11-08 15:52:39 +11:00
1 parent 8f37cc79cd
commit b548cabcbf
5 files changed
+45 -55

No files matched your search

+2 -1
View File
@@ -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 {
+2 -2
View File
@@ -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) {
@@ -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.
@@ -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)
}
})
}
+6 -21
View File
@@ -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.