From 3f22d12a90d137ee098fe6ca2c1dfc7cdcb1706f Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:24:03 -0500 Subject: [PATCH] fix: pin resolved address and enforce redirect policy for server-side fetchers Co-Authored-By: Claude Opus 5.5 Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- ...xed-18992-pin-server-side-fetch-address.md | 1 + src/docs/security-model.md | 16 ++ src/pkg/dashboard/api_agents.go | 1 + .../dashboard/api_workflows_ingest_sources.go | 2 +- .../dashboard/import_test_transport_test.go | 12 +- src/pkg/dashboard/ssrf_guard.go | 21 ++ .../dashboard/ssrf_pinned_transport_test.go | 83 ++++++ src/pkg/hub/pinned_fetch_test.go | 128 ++++++++++ src/pkg/hub/saas_hives_api.go | 9 + src/pkg/hub/server.go | 16 ++ src/pkg/hub/webhook.go | 7 + src/pkg/knowledge/connector_support_test.go | 1 + src/pkg/knowledge/docsource.go | 28 +- src/pkg/knowledge/docsource_pin_test.go | 88 +++++++ src/pkg/knowledge/docsource_test.go | 1 + src/pkg/netguard/pin.go | 187 ++++++++++++++ src/pkg/netguard/pin_test.go | 239 ++++++++++++++++++ 17 files changed, 836 insertions(+), 4 deletions(-) create mode 100644 changelog.d/fixed-18992-pin-server-side-fetch-address.md create mode 100644 src/pkg/dashboard/ssrf_pinned_transport_test.go create mode 100644 src/pkg/hub/pinned_fetch_test.go create mode 100644 src/pkg/knowledge/docsource_pin_test.go create mode 100644 src/pkg/netguard/pin.go create mode 100644 src/pkg/netguard/pin_test.go diff --git a/changelog.d/fixed-18992-pin-server-side-fetch-address.md b/changelog.d/fixed-18992-pin-server-side-fetch-address.md new file mode 100644 index 0000000000..3880a48e18 --- /dev/null +++ b/changelog.d/fixed-18992-pin-server-side-fetch-address.md @@ -0,0 +1 @@ +- Server-side fetches of user- or spoke-chosen URLs (dashboard agent import and workflow spec URLs, the hub's hive config proxy and webhook relay to a spoke's dashboard URL, knowledge document imports) now resolve each connection once, refuse it when any DNS answer is private, loopback, link-local or otherwise not globally routable, and dial the address that was checked. A name that rebinds between the SSRF check and the dial is refused instead of reached, and every redirect hop is checked at dial time ([#18992](https://github.com/hivecommons/hive/issues/18992), part of [#18986](https://github.com/hivecommons/hive/issues/18986)). diff --git a/src/docs/security-model.md b/src/docs/security-model.md index 6914d12476..757a30b6aa 100644 --- a/src/docs/security-model.md +++ b/src/docs/security-model.md @@ -343,6 +343,22 @@ on the same evidence as a literal IP. Set destination is intended; it lifts the guard for all three legs at once, and the proxy warns at boot when it is set. +**Server-side fetches of user- or spoke-chosen URLs.** The dashboard's agent +import and workflow spec fetches, the hub's hive config proxy and webhook +relay to a spoke's self-reported dashboard URL, and knowledge document imports +check the URL before fetching and then dial through a pinned transport +(`netguard.PinnedDialer`): the name is resolved once per connection, every +answer must pass the same classifier, and the connection goes to an address +that was checked, never to the name. A name that rebinds between the check and +the dial is refused, and because the check runs per connection it covers +every redirect hop as well. Two limits are deliberate. When an +`HTTP(S)_PROXY` applies, the connection is to the operator's proxy and the +proxy resolves the target, the same limit as the agent proxy's chained +upstream leg, so the pre-fetch check and the redirect policy are the controls +there. Operator-configured gateway endpoints and the fixed-host `/api/style` +fetch are not pinned: a gateway may legitimately sit on a private address, and +redirects off either are still checked. + **Hub-to-spoke channel.** Heartbeats carry a per-hive bearer key derived from the hub master secret and are compared in constant time. The payload is telemetry only: health, version, agent and governor summaries and 24-hour diff --git a/src/pkg/dashboard/api_agents.go b/src/pkg/dashboard/api_agents.go index 27bea6d34d..dd5e5c21ff 100644 --- a/src/pkg/dashboard/api_agents.go +++ b/src/pkg/dashboard/api_agents.go @@ -346,6 +346,7 @@ func (s *Server) handleAgentImport(w http.ResponseWriter, r *http.Request) { client := &http.Client{ Timeout: importHTTPTimeoutS * 1e9, // nanoseconds CheckRedirect: noRedirectToPrivate, + Transport: ssrfPinnedTransport, } resp, err := client.Get(body.URL) if err != nil { diff --git a/src/pkg/dashboard/api_workflows_ingest_sources.go b/src/pkg/dashboard/api_workflows_ingest_sources.go index 77fd312a99..122b609220 100644 --- a/src/pkg/dashboard/api_workflows_ingest_sources.go +++ b/src/pkg/dashboard/api_workflows_ingest_sources.go @@ -311,7 +311,7 @@ func (s *Server) loadWorkflowSpecText(ctx context.Context, req workflowIngestSpe } // workflowSpecHTTP is the client for URL specs; a seam for tests. -var workflowSpecHTTP = &http.Client{Timeout: workflowSpecURLTimeout, CheckRedirect: noRedirectToPrivate} +var workflowSpecHTTP = &http.Client{Timeout: workflowSpecURLTimeout, CheckRedirect: noRedirectToPrivate, Transport: ssrfPinnedTransport} func fetchWorkflowSpecURL(ctx context.Context, rawURL string) (string, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) diff --git a/src/pkg/dashboard/import_test_transport_test.go b/src/pkg/dashboard/import_test_transport_test.go index f163f256e3..7540722c32 100644 --- a/src/pkg/dashboard/import_test_transport_test.go +++ b/src/pkg/dashboard/import_test_transport_test.go @@ -37,14 +37,22 @@ func routeImportFetchesToServer(t *testing.T, ts *httptest.Server) { "gist.github.com": {"140.82.121.3"}, }) previous := http.DefaultTransport - http.DefaultTransport = roundTripFunc(func(req *http.Request) (*http.Response, error) { + rewrite := roundTripFunc(func(req *http.Request) (*http.Response, error) { rewritten := req.Clone(req.Context()) rewritten.URL.Scheme = target.Scheme rewritten.URL.Host = target.Host rewritten.Host = req.URL.Host return previous.RoundTrip(rewritten) }) - t.Cleanup(func() { http.DefaultTransport = previous }) + http.DefaultTransport = rewrite + // The import handler dials through the pinned transport (#18992), not + // http.DefaultTransport, so route that seam the same way. + previousPinned := ssrfPinnedTransport + ssrfPinnedTransport = rewrite + t.Cleanup(func() { + http.DefaultTransport = previous + ssrfPinnedTransport = previousPinned + }) } // Guard against the helper above being mistaken for a production bypass: the diff --git a/src/pkg/dashboard/ssrf_guard.go b/src/pkg/dashboard/ssrf_guard.go index e0ac38dcfd..0dfbed9cb3 100644 --- a/src/pkg/dashboard/ssrf_guard.go +++ b/src/pkg/dashboard/ssrf_guard.go @@ -5,10 +5,31 @@ import ( "fmt" "net/http" "strings" + + "github.com/hivecommons/hive/pkg/netguard" ) const ssrfMaxRedirects = 3 +// ssrfPinnedTransport is the transport for every fetch whose URL a user or a +// remote party chooses and that isPrivateURL already checked (#18992). The +// pre-fetch check resolves the name, and a default http.Client resolves it +// again when it dials, so a rebinding name passed the check as public and was +// connected to as private. This transport resolves each dial once through +// privateURLResolver, runs isPrivateURL on every answer (so the dial-time +// check is the pre-fetch check, test exemptions included), and connects to +// an address it checked. It runs for every redirect hop's connection too. +// +// Not used for operator-configured gateway endpoints (governor probes, model +// discovery), which may legitimately be private, nor for /api/style, whose +// host is the fixed raw.githubusercontent.com; a redirect off either is still +// bounded by noRedirectToPrivate. A package var so import tests can route the +// same seam to an httptest server. +var ssrfPinnedTransport http.RoundTripper = netguard.PinnedDialer{ + Resolve: func(ctx context.Context, host string) ([]string, error) { return privateURLResolver(ctx, host) }, + Refuse: func(ctx context.Context, target string) bool { return isPrivateURL(ctx, "http://"+target) }, +}.Transport() + // noRedirectToPrivate returns an http.Client CheckRedirect function that // rejects any redirect whose target resolves to a private/internal host. // It also limits the redirect chain depth to ssrfMaxRedirects. Use this diff --git a/src/pkg/dashboard/ssrf_pinned_transport_test.go b/src/pkg/dashboard/ssrf_pinned_transport_test.go new file mode 100644 index 0000000000..96a1e048e2 --- /dev/null +++ b/src/pkg/dashboard/ssrf_pinned_transport_test.go @@ -0,0 +1,83 @@ +package dashboard + +import ( + "context" + "errors" + "net" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + + "github.com/hivecommons/hive/pkg/netguard" +) + +// stubRebindingImportResolver answers host with a public address on the first +// lookup and loopback afterwards: the pre-fetch guard sees public, a client +// that resolves again sees 127.0.0.1 (#18992). +func stubRebindingImportResolver(t *testing.T, host string) { + t.Helper() + var mu sync.Mutex + n := 0 + orig := privateURLResolver + privateURLResolver = func(_ context.Context, h string) ([]string, error) { + if !strings.EqualFold(h, host) { + return nil, errors.New("no such host") + } + mu.Lock() + defer mu.Unlock() + n++ + if n == 1 { + return []string{"93.184.216.34"}, nil + } + return []string{"127.0.0.1"}, nil + } + t.Cleanup(func() { privateURLResolver = orig }) +} + +func TestSSRFPinnedTransport_RefusesRebindingAfterPreflight(t *testing.T) { + var reached atomic.Bool + srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached.Store(true) })) + defer srv.Close() + _, port, _ := net.SplitHostPort(strings.TrimPrefix(srv.URL, "http://")) + stubRebindingImportResolver(t, "agents.rebind.example") + + target := "http://agents.rebind.example:" + port + "/agent.yaml" + if err := validateImportFetchURL(context.Background(), target); err != nil { + t.Fatalf("preflight should pass on the first (public) answer: %v", err) + } + client := &http.Client{CheckRedirect: noRedirectToPrivate, Transport: ssrfPinnedTransport} + resp, err := client.Get(target) + if err == nil { + _ = resp.Body.Close() + } + if !errors.Is(err, netguard.ErrRefused) || reached.Load() { + t.Fatalf("rebound import fetch: err=%v reached=%v; want netguard.ErrRefused and no fetch", err, reached.Load()) + } +} + +// The pinned transport's dial-time check is isPrivateURL itself, so the +// exact host:port test exemption reaches an httptest server and nothing else. +func TestSSRFPinnedTransport_UsesIsPrivateURLAtDialTime(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusNoContent) })) + defer srv.Close() + client := &http.Client{Transport: ssrfPinnedTransport} + + if resp, err := client.Get(srv.URL); err == nil { + _ = resp.Body.Close() + t.Fatal("pinned transport dialled loopback without an exemption") + } else if !errors.Is(err, netguard.ErrRefused) { + t.Fatalf("err = %v, want netguard.ErrRefused", err) + } + + prev := privateURLTestExemptHostPorts + privateURLTestExemptHostPorts = map[string]struct{}{strings.TrimPrefix(srv.URL, "http://"): {}} + t.Cleanup(func() { privateURLTestExemptHostPorts = prev }) + resp, err := client.Get(srv.URL) + if err != nil { + t.Fatalf("exempted loopback test server: %v", err) + } + _ = resp.Body.Close() +} diff --git a/src/pkg/hub/pinned_fetch_test.go b/src/pkg/hub/pinned_fetch_test.go new file mode 100644 index 0000000000..52cb56b3a9 --- /dev/null +++ b/src/pkg/hub/pinned_fetch_test.go @@ -0,0 +1,128 @@ +package hub + +import ( + "bytes" + "context" + "errors" + "net" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + + "github.com/hivecommons/hive/pkg/netguard" +) + +// stubRebindingResolver answers host with public on the first lookup and +// loopback on every later one, the DNS rebinding shape #18992 closes: the +// SSRF guard's lookup sees a public address, the client's own lookup sees +// 127.0.0.1. It returns a lookup counter for host. +func stubRebindingResolver(t *testing.T, host string) func() int { + t.Helper() + var mu sync.Mutex + n := 0 + orig := privateURLResolver + privateURLResolver = func(_ context.Context, h string) ([]string, error) { + if !strings.EqualFold(h, host) { + return nil, errors.New("no such host") + } + mu.Lock() + defer mu.Unlock() + n++ + if n == 1 { + return []string{publicTestResolvedAddr}, nil + } + return []string{"127.0.0.1"}, nil + } + t.Cleanup(func() { privateURLResolver = orig }) + return func() int { + mu.Lock() + defer mu.Unlock() + return n + } +} + +func loopbackPort(t *testing.T, srv *httptest.Server) string { + t.Helper() + _, port, err := net.SplitHostPort(strings.TrimPrefix(srv.URL, "http://")) + if err != nil { + t.Fatal(err) + } + return port +} + +// The hive config proxy checks the self-reported DashboardURL with the real +// guard and then fetches it. The fetch must resolve through the same seam and +// dial only a checked address, so the rebound loopback answer is refused. +func TestHandleProxyHiveConfigPinsResolvedAddress(t *testing.T) { + var reached atomic.Bool + upstream := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached.Store(true) })) + defer upstream.Close() + lookups := stubRebindingResolver(t, "spoke.rebind.example") + + mux := proxyConfigServer(t, "alice", "http://spoke.rebind.example:"+loopbackPort(t, upstream)) + req := httptest.NewRequest("GET", "/api/saas/hive-config/cfg-hive", nil) + req.AddCookie(testAuthCookie("alice")) + w := httptest.NewRecorder() + mux.ServeHTTP(w, req) + + if reached.Load() { + t.Fatal("hive config proxy reached the rebound loopback address") + } + if w.Code != http.StatusBadGateway { + t.Fatalf("rebound fetch: expected 502, got %d (body: %s)", w.Code, w.Body.String()) + } + if got := lookups(); got < 2 { + t.Fatalf("resolver consulted %d times, want the guard's lookup and the pinned dial's own", got) + } +} + +func TestSpokeWebhookRelayClientPinsResolvedAddress(t *testing.T) { + var reached atomic.Bool + spoke := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached.Store(true) })) + defer spoke.Close() + stubRebindingResolver(t, "relay.rebind.example") + + dashboardURL := "http://relay.rebind.example:" + loopbackPort(t, spoke) + if spokeWebhookRelayGuard(context.Background(), dashboardURL) { + t.Fatal("the guard's lookup should see the first, public answer") + } + req, err := http.NewRequest(http.MethodPost, dashboardURL+spokeWebhookPath, bytes.NewReader([]byte("{}"))) + if err != nil { + t.Fatal(err) + } + resp, err := spokeWebhookRelayClient.Do(req) + if err == nil { + _ = resp.Body.Close() + } + if !errors.Is(err, netguard.ErrRefused) || reached.Load() { + t.Fatalf("relay: err=%v reached=%v; want netguard.ErrRefused and no delivery", err, reached.Load()) + } +} + +// The relay tests swap spokeWebhookRelayGuard to reach an httptest spoke; the +// pinned transport must honour that seam rather than capture the original. +func TestSpokeWebhookRelayClientHonoursGuardSeam(t *testing.T) { + var reached atomic.Bool + spoke := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + reached.Store(true) + w.WriteHeader(http.StatusOK) + })) + defer spoke.Close() + allowLoopbackRelay(t) + + req, err := http.NewRequest(http.MethodPost, spoke.URL+spokeWebhookPath, bytes.NewReader([]byte("{}"))) + if err != nil { + t.Fatal(err) + } + resp, err := spokeWebhookRelayClient.Do(req) + if err != nil { + t.Fatalf("relay with guard overridden: %v", err) + } + _ = resp.Body.Close() + if !reached.Load() { + t.Fatal("relay did not reach the allowed spoke") + } +} diff --git a/src/pkg/hub/saas_hives_api.go b/src/pkg/hub/saas_hives_api.go index f71da247f5..db8f298764 100644 --- a/src/pkg/hub/saas_hives_api.go +++ b/src/pkg/hub/saas_hives_api.go @@ -1,6 +1,7 @@ package hub import ( + "context" _ "embed" "encoding/json" "fmt" @@ -2127,6 +2128,13 @@ func (s *HubServer) pushVisibilityToSpoke(id string, isPublic bool) { // loopback httptest server; production always uses isPrivateURL. var hiveConfigSSRFGuard = isPrivateURL +// hiveConfigTransport dials only an address hiveConfigSSRFGuard passed, so a +// DashboardURL whose name rebinds after the guard ran is refused at dial time +// (#18992). +var hiveConfigTransport = pinnedFetchTransport(func(ctx context.Context, rawURL string) bool { + return hiveConfigSSRFGuard(ctx, rawURL) +}) + func (s *HubServer) handleProxyHiveConfig(w http.ResponseWriter, r *http.Request) { hiveID := r.PathValue("hiveID") caller := s.getAuthUser(r) @@ -2173,6 +2181,7 @@ func (s *HubServer) handleProxyHiveConfig(w http.ResponseWriter, r *http.Request // Do NOT follow redirects — a 30x could send us from a public // DashboardURL to an internal host, re-opening the SSRF the guard closes. CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }, + Transport: hiveConfigTransport, } resp, err := client.Get(strings.TrimRight(dashURL, "/") + "/api/config/download") if err != nil { diff --git a/src/pkg/hub/server.go b/src/pkg/hub/server.go index 4f4cbf8b45..d41192d0a0 100644 --- a/src/pkg/hub/server.go +++ b/src/pkg/hub/server.go @@ -5165,6 +5165,22 @@ func isPrivateURL(ctx context.Context, rawURL string) bool { return netguard.IsPrivateURL(ctx, rawURL, netguard.Resolver(privateURLResolver)) } +// pinnedFetchTransport returns the transport for a fetch whose URL a remote +// party chose and that guard already checked (#18992). The pre-fetch guard +// resolves the name, and a default http.Client resolves it again when it +// dials, so a self-reported DashboardURL whose name rebinds passed the guard +// as public and was connected to as private. This transport resolves each +// dial once through privateURLResolver, runs guard on every answer, and +// connects to an address it checked, for every connection including redirect +// hops. guard is called on each dial, not captured, so a test that swaps the +// guard var behind it reaches its loopback server through the same path. +func pinnedFetchTransport(guard func(ctx context.Context, rawURL string) bool) *http.Transport { + return netguard.PinnedDialer{ + Resolve: func(ctx context.Context, host string) ([]string, error) { return privateURLResolver(ctx, host) }, + Refuse: func(ctx context.Context, target string) bool { return guard(ctx, "http://"+target) }, + }.Transport() +} + func clampInt(v, min, max int) int { if v < min { return min diff --git a/src/pkg/hub/webhook.go b/src/pkg/hub/webhook.go index d177a66280..cef7e20589 100644 --- a/src/pkg/hub/webhook.go +++ b/src/pkg/hub/webhook.go @@ -123,8 +123,15 @@ var spokeWebhookRelayGuard = isPrivateURL // answers 30x to an internal host would otherwise re-open the SSRF the guard // above closes. The spoke receiver is a fixed path, so a redirect is never // legitimate here. +// +// Its transport dials only an address spokeWebhookRelayGuard passed, so a +// DashboardURL whose name rebinds between the guard and the dial is refused +// instead of reached (#18992). var spokeWebhookRelayClient = &http.Client{ CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }, + Transport: pinnedFetchTransport(func(ctx context.Context, rawURL string) bool { + return spokeWebhookRelayGuard(ctx, rawURL) + }), } // relayWebhookToSpoke forwards a verified PR-shaped delivery, byte for byte diff --git a/src/pkg/knowledge/connector_support_test.go b/src/pkg/knowledge/connector_support_test.go index 04b068aa06..63513f6cb9 100644 --- a/src/pkg/knowledge/connector_support_test.go +++ b/src/pkg/knowledge/connector_support_test.go @@ -150,6 +150,7 @@ func TestFetchDocumentFacts_URL(t *testing.T) { _, _ = w.Write([]byte("Page Title

Body text here.

")) })) defer srv.Close() + allowDocFetchTo(t, srv) facts, title, err := FetchDocumentFacts(context.Background(), DocSourceConfig{URL: srv.URL + "/doc"}, "", "", nil) if err != nil { diff --git a/src/pkg/knowledge/docsource.go b/src/pkg/knowledge/docsource.go index 034d7a52cb..0f23bb50dc 100644 --- a/src/pkg/knowledge/docsource.go +++ b/src/pkg/knowledge/docsource.go @@ -12,6 +12,8 @@ import ( "path/filepath" "strings" "time" + + "github.com/hivecommons/hive/pkg/netguard" ) const ( @@ -321,6 +323,30 @@ func knowledgeNoRedirectToPrivate(req *http.Request, via []*http.Request) error return nil } +// docFetchRefuse decides, at dial time, whether a document fetch may connect +// to an already-resolved "ip:port". Nil means the netguard classifier. A +// package var only so tests can reach a loopback httptest server; production +// never sets it. +var docFetchRefuse func(ctx context.Context, target string) bool + +// docFetchTransport pins every document fetch connection to an address it +// checked (#18992). The dashboard's pre-fetch isPrivateURL check and the +// redirect check above each resolve the name, and a default http.Client +// resolves it a further time when it dials, so a name that rebinds between +// those lookups was validated as public and connected to as private. This +// transport resolves each dial once through docRedirectResolver, refuses it +// when any answer is not globally routable, and dials the checked address, +// for the first request and every redirect hop. +var docFetchTransport = netguard.PinnedDialer{ + Resolve: func(ctx context.Context, host string) ([]string, error) { return docRedirectResolver(ctx, host) }, + Refuse: func(ctx context.Context, target string) bool { + if docFetchRefuse != nil { + return docFetchRefuse(ctx, target) + } + return netguard.IsPrivateURL(ctx, "http://"+target, nil) + }, +}.Transport() + func (ds *DocumentSource) fetchURL(ctx context.Context, url string) ([]byte, string, error) { ctx, cancel := context.WithTimeout(ctx, docFetchTimeout) defer cancel() @@ -331,7 +357,7 @@ func (ds *DocumentSource) fetchURL(ctx context.Context, url string) ([]byte, str } req.Header.Set("User-Agent", docUserAgent) - client := &http.Client{CheckRedirect: docNoRedirectToPrivate} + client := &http.Client{CheckRedirect: docNoRedirectToPrivate, Transport: docFetchTransport} resp, err := client.Do(req) if err != nil { return nil, "", fmt.Errorf("HTTP GET: %w", err) diff --git a/src/pkg/knowledge/docsource_pin_test.go b/src/pkg/knowledge/docsource_pin_test.go new file mode 100644 index 0000000000..d5b1e8c6b2 --- /dev/null +++ b/src/pkg/knowledge/docsource_pin_test.go @@ -0,0 +1,88 @@ +package knowledge + +import ( + "context" + "errors" + "net" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + + "github.com/hivecommons/hive/pkg/netguard" +) + +// allowDocFetchTo lets document fetches dial srv's loopback listener (and +// nothing else private) for the duration of a test. The exemption is one +// exact ip:port, so it cannot widen to all of loopback. +func allowDocFetchTo(t *testing.T, srv *httptest.Server) { + t.Helper() + allowed := strings.TrimPrefix(srv.URL, "http://") + orig := docFetchRefuse + docFetchRefuse = func(ctx context.Context, target string) bool { + if target == allowed { + return false + } + return netguard.IsPrivateURL(ctx, "http://"+target, nil) + } + t.Cleanup(func() { docFetchRefuse = orig }) +} + +// A document URL that passed the dashboard's pre-fetch isPrivateURL check +// (first lookup: public) must not be connected to when the name answers +// loopback on the client's own lookup (#18992). Before the pin, http.Client +// resolved the name again and dialled whatever came back. +func TestFetchURL_PinsAddressAcrossRebinding(t *testing.T) { + var hit atomic.Bool + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hit.Store(true) + _, _ = w.Write([]byte("internal secret")) + })) + defer srv.Close() + _, port, _ := net.SplitHostPort(strings.TrimPrefix(srv.URL, "http://")) + + var mu sync.Mutex + lookups := 0 + orig := docRedirectResolver + t.Cleanup(func() { docRedirectResolver = orig }) + docRedirectResolver = func(_ context.Context, host string) ([]string, error) { + if host != "rebind.attacker.example" { + return nil, errors.New("no such host") + } + mu.Lock() + defer mu.Unlock() + lookups++ + if lookups == 1 { + return []string{"93.184.216.34"}, nil + } + return []string{"127.0.0.1"}, nil + } + + target := "http://rebind.attacker.example:" + port + "/doc" + if docRedirectHostIsPrivate(context.Background(), "rebind.attacker.example") { + t.Fatal("first lookup should look public to the pre-fetch check") + } + ds := &DocumentSource{} + _, _, err := ds.fetchURL(context.Background(), target) + if err == nil || hit.Load() { + t.Fatalf("fetchURL reached the rebound loopback address: err=%v hit=%v", err, hit.Load()) + } + if !errors.Is(err, netguard.ErrRefused) { + t.Fatalf("err = %v, want netguard.ErrRefused", err) + } +} + +// The dial-time check uses the netip classifier, so an answer the redirect +// guard's older classifier misses (carrier-grade NAT) is refused too. +func TestFetchURL_RefusesAnyBarredAnswer(t *testing.T) { + stubDocRedirectResolver(t, map[string][]string{ + "mixed.attacker.example": {"93.184.216.34", "100.100.100.200"}, + }) + ds := &DocumentSource{} + _, _, err := ds.fetchURL(context.Background(), "http://mixed.attacker.example/doc") + if !errors.Is(err, netguard.ErrRefused) { + t.Fatalf("err = %v, want netguard.ErrRefused", err) + } +} diff --git a/src/pkg/knowledge/docsource_test.go b/src/pkg/knowledge/docsource_test.go index d87bc37a5a..2788a4df0a 100644 --- a/src/pkg/knowledge/docsource_test.go +++ b/src/pkg/knowledge/docsource_test.go @@ -147,6 +147,7 @@ func TestDocumentSource_ImportURL(t *testing.T) { `) })) defer server.Close() + allowDocFetchTo(t, server) tmpDir := t.TempDir() vaultDir := filepath.Join(tmpDir, "vault") diff --git a/src/pkg/netguard/pin.go b/src/pkg/netguard/pin.go new file mode 100644 index 0000000000..682452398f --- /dev/null +++ b/src/pkg/netguard/pin.go @@ -0,0 +1,187 @@ +package netguard + +import ( + "context" + "errors" + "fmt" + "net" + "net/http" + "net/url" + "strings" + "sync" + "time" +) + +// ErrRefused marks a dial PinnedDialer would not make. The text matches the +// redirect guards' "private/internal host blocked" so callers and logs read +// the same whichever check fired. +var ErrRefused = errors.New("private/internal host blocked") + +// DialFunc opens a connection to addr ("host:port"). +type DialFunc func(ctx context.Context, network, addr string) (net.Conn, error) + +// PinnedDialer closes the rebinding window between "check the URL" and "dial +// the URL" for a server-side fetch (#18992, part of #18986). A pre-fetch +// IsPrivateURL check resolves the name, and http.Client then resolves it again +// when it dials, so a name whose answer changes between the two lookups (DNS +// rebinding, a TTL of zero) is checked as public and connected to as private. +// PinnedDialer resolves the dial target exactly once, refuses the dial when +// any answer is barred (not only the first), and connects to an address it +// checked, never to the name. As a Transport's DialContext it runs for every +// new connection, so every redirect hop is checked at dial time with the same +// classifier, whatever the CheckRedirect policy does. +// +// This is the shape of the agent egress proxy's dial-time guard +// (src/crux/proxy/plain_forward_guard.go) and of inbox.LinkFetcher.dial. +type PinnedDialer struct { + // Resolve looks a name up. Nil means net.DefaultResolver.LookupHost. + // A lookup error or an empty answer refuses the dial (fail closed). + Resolve Resolver + // Refuse reports whether the connection to target, an "ip:port" the + // dialer has already resolved, must not be made. Nil means the address + // is refused when DenyReason names a class for it. Callers with a test + // seam on their pre-fetch guard pass that guard here, so the dial-time + // check and the pre-fetch check are the same function. + Refuse func(ctx context.Context, target string) bool + // Dial connects to a checked "ip:port". Nil means a net.Dialer with a + // 30s timeout. + Dial DialFunc +} + +func (p PinnedDialer) resolve(ctx context.Context, host string) ([]string, error) { + if p.Resolve != nil { + return p.Resolve(ctx, host) + } + return net.DefaultResolver.LookupHost(ctx, host) +} + +func (p PinnedDialer) refuse(ctx context.Context, target string) bool { + if p.Refuse != nil { + return p.Refuse(ctx, target) + } + host, _, err := net.SplitHostPort(target) + if err != nil { + return true + } + addr, ok := ParseHostLiteral(host) + return !ok || Denied(addr) +} + +func (p PinnedDialer) dial(ctx context.Context, network, addr string) (net.Conn, error) { + if p.Dial != nil { + return p.Dial(ctx, network, addr) + } + d := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second} + return d.DialContext(ctx, network, addr) +} + +// Targets resolves addr's host once and returns the "ip:port" targets the +// dialer may connect to, in answer order. It refuses the whole set when any +// answer is barred or does not parse as an address, when the lookup fails, +// or when it returns nothing. +func (p PinnedDialer) Targets(ctx context.Context, addr string) ([]string, error) { + host, port, err := net.SplitHostPort(addr) + if err != nil { + return nil, fmt.Errorf("%w: %v", ErrRefused, err) + } + var answers []string + if lit, ok := ParseHostLiteral(host); ok { + answers = []string{lit.String()} + } else { + answers, err = p.resolve(ctx, host) + if err != nil { + return nil, fmt.Errorf("%w: looking up %s: %v", ErrRefused, host, err) + } + if len(answers) == 0 { + return nil, fmt.Errorf("%w: looking up %s: no addresses", ErrRefused, host) + } + } + targets := make([]string, 0, len(answers)) + for _, a := range answers { + ip, ok := ParseHostLiteral(a) + if !ok { + return nil, fmt.Errorf("%w: %s resolved to unparseable answer %q", ErrRefused, host, a) + } + target := net.JoinHostPort(ip.Unmap().String(), port) + if p.refuse(ctx, target) { + return nil, fmt.Errorf("%w: %s resolves to %s", ErrRefused, host, ip) + } + targets = append(targets, target) + } + return targets, nil +} + +// DialContext resolves addr once, checks every answer, and connects to the +// checked addresses in order until one answers. +func (p PinnedDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) { + targets, err := p.Targets(ctx, addr) + if err != nil { + return nil, err + } + var firstErr error + for _, t := range targets { + conn, err := p.dial(ctx, network, t) + if err == nil { + return conn, nil + } + if firstErr == nil { + firstErr = err + } + if ctx.Err() != nil { + break + } + } + return nil, firstErr +} + +// Transport returns an http.Transport whose every new connection goes +// through p.DialContext. +// +// Chained upstream decision: the transport keeps the standard +// HTTP(S)_PROXY / NO_PROXY behaviour of http.DefaultTransport. When a forward +// proxy applies, the connection is to the operator-configured proxy, which is +// dialled as configured (it is commonly on a private address), and the proxy +// resolves the target name itself. Hive cannot pin that resolution, the same +// documented limit as dialUpstreamRaw in the agent egress proxy, so on a +// proxied fetch the pre-fetch check and the CheckRedirect policy are the +// controls. Without a proxy (the default) every dial is pinned. +func (p PinnedDialer) Transport() *http.Transport { + var proxies sync.Map // canonical "host:port" of every proxy URL handed out + proxy := func(req *http.Request) (*url.URL, error) { + u, err := http.ProxyFromEnvironment(req) + if u != nil && err == nil { + proxies.Store(proxyHostPort(u), struct{}{}) + } + return u, err + } + return &http.Transport{ + Proxy: proxy, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + if _, ok := proxies.Load(strings.ToLower(addr)); ok { + return p.dial(ctx, network, addr) + } + return p.DialContext(ctx, network, addr) + }, + ForceAttemptHTTP2: true, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + } +} + +// proxyHostPort is the address http.Transport dials for proxy u. +func proxyHostPort(u *url.URL) string { + port := u.Port() + if port == "" { + switch strings.ToLower(u.Scheme) { + case "https": + port = "443" + case "socks5", "socks5h": + port = "1080" + default: + port = "80" + } + } + return strings.ToLower(net.JoinHostPort(u.Hostname(), port)) +} diff --git a/src/pkg/netguard/pin_test.go b/src/pkg/netguard/pin_test.go new file mode 100644 index 0000000000..3b77161c1b --- /dev/null +++ b/src/pkg/netguard/pin_test.go @@ -0,0 +1,239 @@ +package netguard + +import ( + "context" + "errors" + "net" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "sync" + "sync/atomic" + "testing" +) + +// rebindResolver answers each lookup of a name with the next entry of its +// script, repeating the last one: a DNS rebinding server in miniature. +type rebindResolver struct { + mu sync.Mutex + script map[string][][]string + lookups map[string]int +} + +func newRebindResolver(script map[string][][]string) *rebindResolver { + return &rebindResolver{script: script, lookups: map[string]int{}} +} + +func (r *rebindResolver) resolve(_ context.Context, host string) ([]string, error) { + r.mu.Lock() + defer r.mu.Unlock() + answers, ok := r.script[host] + if !ok { + return nil, errors.New("no such host") + } + i := r.lookups[host] + r.lookups[host]++ + if i >= len(answers) { + i = len(answers) - 1 + } + return answers[i], nil +} + +func (r *rebindResolver) count(host string) int { + r.mu.Lock() + defer r.mu.Unlock() + return r.lookups[host] +} + +// recordingDial never touches the network: it records the address it was +// asked to dial and fails, so a test can see what would have been connected. +type recordingDial struct { + mu sync.Mutex + addrs []string +} + +func (d *recordingDial) dial(_ context.Context, _, addr string) (net.Conn, error) { + d.mu.Lock() + d.addrs = append(d.addrs, addr) + d.mu.Unlock() + return nil, errors.New("recording dialer: not connecting") +} + +func TestPinnedDialer_DialsTheCheckedAddressNotTheName(t *testing.T) { + res := newRebindResolver(map[string][][]string{ + "docs.example": {{"93.184.216.34"}, {"127.0.0.1"}}, + }) + rec := &recordingDial{} + p := PinnedDialer{Resolve: res.resolve, Dial: rec.dial} + _, _ = p.DialContext(context.Background(), "tcp", "docs.example:443") + if got := res.count("docs.example"); got != 1 { + t.Fatalf("resolved %d times, want exactly once", got) + } + if len(rec.addrs) != 1 || rec.addrs[0] != "93.184.216.34:443" { + t.Fatalf("dialled %v, want the validated 93.184.216.34:443", rec.addrs) + } +} + +func TestPinnedDialer_RefusesWhenAnyAnswerIsBarred(t *testing.T) { + for _, tc := range []struct { + name string + answers []string + }{ + {"metadata second", []string{"93.184.216.34", "169.254.169.254"}}, + {"loopback first", []string{"127.0.0.1", "93.184.216.34"}}, + {"v6 unique local", []string{"2606:2800:220:1::1", "fd00::1"}}, + {"cgnat", []string{"100.64.0.1"}}, + {"nat64 private", []string{"64:ff9b::a00:1"}}, + {"unparseable", []string{"93.184.216.34", "not-an-ip"}}, + } { + t.Run(tc.name, func(t *testing.T) { + res := newRebindResolver(map[string][][]string{"mixed.example": {tc.answers}}) + rec := &recordingDial{} + p := PinnedDialer{Resolve: res.resolve, Dial: rec.dial} + _, err := p.DialContext(context.Background(), "tcp", "mixed.example:80") + if !errors.Is(err, ErrRefused) { + t.Fatalf("err = %v, want ErrRefused", err) + } + if len(rec.addrs) != 0 { + t.Fatalf("dialled %v after a barred answer", rec.addrs) + } + }) + } +} + +func TestPinnedDialer_FailsClosed(t *testing.T) { + rec := &recordingDial{} + empty := func(context.Context, string) ([]string, error) { return nil, nil } + broken := func(context.Context, string) ([]string, error) { return nil, errors.New("SERVFAIL") } + for name, p := range map[string]PinnedDialer{ + "lookup error": {Resolve: broken, Dial: rec.dial}, + "no answers": {Resolve: empty, Dial: rec.dial}, + } { + if _, err := p.DialContext(context.Background(), "tcp", "x.example:443"); !errors.Is(err, ErrRefused) { + t.Errorf("%s: err = %v, want ErrRefused", name, err) + } + } + p := PinnedDialer{Dial: rec.dial} + for _, addr := range []string{"127.0.0.1:80", "[::1]:80", "0x7f.1:80", "2130706433:80", "[fe80::1%25eth0]:80", "no-port"} { + if _, err := p.DialContext(context.Background(), "tcp", addr); !errors.Is(err, ErrRefused) { + t.Errorf("%s: err = %v, want ErrRefused", addr, err) + } + } + if len(rec.addrs) != 0 { + t.Fatalf("dialled %v on a refused target", rec.addrs) + } +} + +func TestPinnedDialer_TriesEachCheckedAnswer(t *testing.T) { + res := newRebindResolver(map[string][][]string{"multi.example": {{"93.184.216.34", "2606:2800:220:1::1"}}}) + rec := &recordingDial{} + p := PinnedDialer{Resolve: res.resolve, Dial: rec.dial} + _, _ = p.DialContext(context.Background(), "tcp", "multi.example:443") + want := []string{"93.184.216.34:443", "[2606:2800:220:1::1]:443"} + if strings.Join(rec.addrs, ",") != strings.Join(want, ",") { + t.Fatalf("dialled %v, want %v", rec.addrs, want) + } +} + +func TestPinnedDialer_RefuseHookSeesResolvedTarget(t *testing.T) { + res := newRebindResolver(map[string][][]string{"spoke.example": {{"93.184.216.34"}}}) + var seen []string + p := PinnedDialer{ + Resolve: res.resolve, + Dial: (&recordingDial{}).dial, + Refuse: func(_ context.Context, target string) bool { + seen = append(seen, target) + return true + }, + } + if _, err := p.DialContext(context.Background(), "tcp", "spoke.example:8443"); !errors.Is(err, ErrRefused) { + t.Fatalf("err = %v, want ErrRefused from the hook", err) + } + if len(seen) != 1 || seen[0] != "93.184.216.34:8443" { + t.Fatalf("hook saw %v, want the resolved ip:port", seen) + } +} + +// The pre-fetch check and the client's own dial each resolve the name. With +// a resolver that answers public, then loopback, the check passes and an +// unpinned client would connect to loopback; the pinned transport refuses. +func TestTransport_RebindingBetweenCheckAndDialIsRefused(t *testing.T) { + var hit atomic.Bool + srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { hit.Store(true) })) + defer srv.Close() + _, port, _ := net.SplitHostPort(strings.TrimPrefix(srv.URL, "http://")) + + res := newRebindResolver(map[string][][]string{ + "rebind.example": {{"93.184.216.34"}, {"127.0.0.1"}}, + }) + target := "http://rebind.example:" + port + "/" + if IsPrivateURL(context.Background(), target, res.resolve) { + t.Fatal("pre-fetch check saw the first (public) answer as private") + } + client := &http.Client{Transport: PinnedDialer{Resolve: res.resolve}.Transport()} + resp, err := client.Get(target) + if err == nil { + _ = resp.Body.Close() + } + if !errors.Is(err, ErrRefused) || hit.Load() { + t.Fatalf("rebound fetch: err = %v, server hit = %v; want ErrRefused and no hit", err, hit.Load()) + } +} + +// The dial hook runs for every connection, so a redirect hop to a name that +// resolves privately is refused at dial time even with no CheckRedirect. +func TestTransport_EveryRedirectHopIsDialChecked(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "http://internal.example/latest/meta-data/", http.StatusFound) + })) + defer srv.Close() + allowed := strings.TrimPrefix(srv.URL, "http://") + res := newRebindResolver(map[string][][]string{"internal.example": {{"169.254.169.254"}}}) + rec := &recordingDial{} + p := PinnedDialer{ + Resolve: res.resolve, + Refuse: func(_ context.Context, target string) bool { + if target == allowed { + return false + } + host, _, _ := net.SplitHostPort(target) + a, _ := ParseHostLiteral(host) + return Denied(a) + }, + Dial: func(ctx context.Context, network, addr string) (net.Conn, error) { + if addr == allowed { + return (&net.Dialer{}).DialContext(ctx, network, addr) + } + return rec.dial(ctx, network, addr) + }, + } + resp, err := (&http.Client{Transport: p.Transport()}).Get(srv.URL) + if err == nil { + _ = resp.Body.Close() + } + if !errors.Is(err, ErrRefused) { + t.Fatalf("redirect hop: err = %v, want ErrRefused", err) + } + if len(rec.addrs) != 0 { + t.Fatalf("dialled %v for the redirect hop", rec.addrs) + } +} + +func TestProxyHostPort(t *testing.T) { + for raw, want := range map[string]string{ + "http://proxy.corp:3128": "proxy.corp:3128", + "http://Proxy.Corp": "proxy.corp:80", + "https://proxy.corp": "proxy.corp:443", + "socks5://10.0.0.9": "10.0.0.9:1080", + "http://[fd00::9]:8080": "[fd00::9]:8080", + } { + u, err := url.Parse(raw) + if err != nil { + t.Fatal(err) + } + if got := proxyHostPort(u); got != want { + t.Errorf("proxyHostPort(%s) = %q, want %q", raw, got, want) + } + } +}