mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-21 02:36:49 +00:00
A message reaching a vision model was scanned for anything URL-shaped ending in an image extension. The pattern's \S+ accepts braces, so a Leaflet tile template in a builder's code — https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png — was an address, and the GET that followed decided the completion: a failed fetch was returned to the caller and the answer never came. Images are now read from the src of an <img> tag, which is the marker the pipeline writes when a message carries one (controllers/message_util.go, and the generation paths in model). A URL in prose or in a code block is text the model should read, and it now reaches the model whole rather than being cut out of the message. Structured image parts never took this path at all: a request carrying them is proxied upstream before any of this runs. Narrowing the pattern is not what makes it safe, though, because a prompt full of code keeps producing URL-shaped strings. The enrichment is now optional in the type: building vision messages returns no error, so a skipped image is a smaller prompt and nothing else. The extension is read from the URL's path, so a query string stays out of the media type it names. The fetch takes an address chosen by whoever wrote the prompt, and it runs where in-namespace service names resolve and 169.254.169.254 hands out credentials. So it goes through proxy.Public, which checks the address in the dialer and dials the address it checked — a hostname checked earlier is resolved again by the transport, and redirects re-enter the same dialer. The predicate is an allowlist of globally routable unicast, following apps/crawl. The body is read under a cap and one message can cause at most eight fetches. Co-authored-by: Hanzo Dev <[email protected]>
134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package proxy
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestRoutableRefusesEveryInternalAddress(t *testing.T) {
|
|
for _, addr := range []string{
|
|
"127.0.0.1", // loopback
|
|
"127.9.9.9", // the rest of 127/8
|
|
"::1", // loopback, v6
|
|
"::ffff:127.0.0.1", // loopback smuggled in a v4-mapped v6 address
|
|
"10.1.2.3", // RFC1918
|
|
"172.16.5.4", // RFC1918
|
|
"172.31.255.255", // RFC1918, top of the range
|
|
"192.168.0.1", // RFC1918
|
|
"169.254.169.254", // the cloud metadata endpoint
|
|
"169.254.1.1", // the rest of link-local
|
|
"fe80::1", // link-local, v6
|
|
"fd00::1", // unique-local, v6
|
|
"fc00::1", // unique-local, v6
|
|
"0.0.0.0", // unspecified
|
|
"0.1.2.3", // 0.0.0.0/8
|
|
"::", // unspecified, v6
|
|
"224.0.0.1", // multicast
|
|
"ff02::1", // multicast, v6
|
|
"100.64.0.1", // carrier-grade NAT
|
|
"100.127.255.255", // carrier-grade NAT, top of the range
|
|
"::ffff:169.254.1.1", // link-local smuggled in a v4-mapped v6 address
|
|
} {
|
|
ip := net.ParseIP(addr)
|
|
if ip == nil {
|
|
t.Fatalf("%q is not an address", addr)
|
|
}
|
|
if routable(ip) {
|
|
t.Errorf("%s would be dialled", addr)
|
|
}
|
|
}
|
|
if routable(nil) {
|
|
t.Error("a missing address would be dialled")
|
|
}
|
|
}
|
|
|
|
func TestRoutableAllowsAddressesOnTheInternet(t *testing.T) {
|
|
for _, addr := range []string{
|
|
"8.8.8.8",
|
|
"1.1.1.1",
|
|
"93.184.216.34",
|
|
"172.32.0.1", // just above RFC1918
|
|
"172.15.255.255", // just below RFC1918
|
|
"100.63.255.255", // just below carrier-grade NAT
|
|
"100.128.0.0", // just above carrier-grade NAT
|
|
"2606:4700::1111", // v6
|
|
} {
|
|
ip := net.ParseIP(addr)
|
|
if ip == nil {
|
|
t.Fatalf("%q is not an address", addr)
|
|
}
|
|
if !routable(ip) {
|
|
t.Errorf("%s would be refused", addr)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A live server on a loopback address stands in for an in-cluster service. The
|
|
// point is that it is never reached: the check happens when the connection is
|
|
// made, so it holds for a hostname that resolves inward as well as a literal.
|
|
func TestPublicRefusesALocalServer(t *testing.T) {
|
|
reached := false
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
reached = true
|
|
}))
|
|
defer s.Close()
|
|
|
|
resp, err := Public.Get(s.URL)
|
|
if err == nil {
|
|
resp.Body.Close()
|
|
t.Fatal("the fetch succeeded")
|
|
}
|
|
if !errors.Is(err, ErrBlocked) {
|
|
t.Fatalf("error is %v, want ErrBlocked", err)
|
|
}
|
|
if reached {
|
|
t.Fatal("the server was reached")
|
|
}
|
|
}
|
|
|
|
func TestPublicRefusesARedirectAwayFromHTTP(t *testing.T) {
|
|
u, err := url.Parse("file:///etc/passwd")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = Public.CheckRedirect(&http.Request{URL: u}, nil)
|
|
|
|
if !errors.Is(err, ErrBlocked) {
|
|
t.Fatalf("error is %v, want ErrBlocked", err)
|
|
}
|
|
}
|
|
|
|
func TestPublicStopsFollowingRedirects(t *testing.T) {
|
|
u, err := url.Parse("https://example.com/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := &http.Request{URL: u}
|
|
|
|
if err := Public.CheckRedirect(req, make([]*http.Request, maxRedirects-1)); err != nil {
|
|
t.Fatalf("refused hop %d: %v", maxRedirects-1, err)
|
|
}
|
|
if err := Public.CheckRedirect(req, make([]*http.Request, maxRedirects)); err == nil {
|
|
t.Fatalf("followed more than %d redirects", maxRedirects)
|
|
}
|
|
}
|