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 1369adfb44..2ca84a5d44 100644 --- a/src/docs/security-model.md +++ b/src/docs/security-model.md @@ -364,6 +364,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("
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) {