Files
ai/object/address_redirect_test.go
zeekay 99f2e88361 A tenant's URL is fetched by outbound's rule, and never read as a path
Every address a request names now reaches the network through one client,
github.com/hanzoai/outbound (proxy.Public), which judges the address at
the dial, on the IP the socket connects to, and re-dials and re-judges
every redirect.

What it replaces failed three ways. A message href was checked by
util.Fetchable and then fetched by a different client: the name was
resolved twice, so a rebinding answer passed the check, and a redirect
to 169.254.169.254 was followed unchecked. The check itself was
net.IP's predicates, which admit NAT64 64:ff9b::a9fe:a9fe, 6to4,
Teredo, 198.18/15, 240/4 and the TEST-NETs; proxy/public.go's routable
admitted the same. And /v1/ai/rag/embed's url and /v1/ai/rag/ingest's
files[].url had no check at all: a loopback or metadata URL was
fetched, and a scheme-less string was read off this machine's disk.

A tenant document now goes through txt.GetParsedTextFromAddress (http
and https only, over proxy.Public); txt.GetParsedTextFromUrl is for
addresses this system stored. The Go crawler fetches every page and
robots.txt through proxy.Public, and the browser engine is handed only
addresses outbound.Host finds public.
2026-10-18 22:21:42 -07:00

67 lines
2.1 KiB
Go

// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
package object
import (
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/hanzoai/outbound"
"github.com/hanzoai/ai/proxy"
)
// admitted stands a loopback server in for a public one: requests to its address
// skip the rule, and every other request — each redirect hop included — goes
// through outbound's own transport. It is a swap of this module's client, never a
// switch inside outbound.
type admitted struct {
host string
next http.RoundTripper
}
func (a admitted) RoundTrip(r *http.Request) (*http.Response, error) {
if r.URL.Host == a.host {
return http.DefaultTransport.RoundTrip(r)
}
return a.next.RoundTrip(r)
}
// A public address that redirects inward is refused at the hop, because the hop is
// a new dial and the dial is where the address is judged.
func TestATenantAddressThatRedirectsInwardIsRefused(t *testing.T) {
srv, reached := inside(t)
front := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, r.URL.Query().Get("to"), http.StatusFound)
}))
t.Cleanup(front.Close)
rule := outbound.Client(outbound.Options{Timeout: 5 * time.Second, Redirects: 5})
prev := proxy.Public
proxy.Public = &http.Client{
Timeout: rule.Timeout,
CheckRedirect: rule.CheckRedirect,
Transport: admitted{host: front.Listener.Addr().String(), next: rule.Transport},
}
t.Cleanup(func() { proxy.Public = prev })
for _, to := range []string{
srv.URL + "/doc.txt",
"http://localhost:" + srv.URL[len("http://127.0.0.1:"):] + "/doc.txt",
"http://169.254.169.254/latest/meta-data/",
"http://[64:ff9b::a9fe:a9fe]/latest/meta-data/",
"file:///etc/passwd",
} {
address := front.URL + "/doc.txt?to=" + url.QueryEscape(to)
if err, got := embedAddress(t, address); !errors.Is(err, outbound.ErrRefused) || got != "" {
t.Errorf("a redirect to %s: err = %v, indexed %q; want outbound.ErrRefused and nothing", to, err, got)
}
}
if n := reached.Load(); n != 0 {
t.Errorf("the server on this network was reached %d times through a redirect", n)
}
}