d204ce3187
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
3.7 KiB
Go
102 lines
3.7 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/netip"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/chattools"
|
|
)
|
|
|
|
var testPNG = append([]byte("\x89PNG\r\n\x1a\n"), bytes.Repeat([]byte{0}, 32)...)
|
|
|
|
// imageServer is a loopback https host the fixture's fetcher may reach.
|
|
func (f *chatToolFixture) imageServer(t *testing.T) *httptest.Server {
|
|
t.Helper()
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/a.png", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "image/png")
|
|
_, _ = w.Write(testPNG)
|
|
})
|
|
mux.HandleFunc("/page", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
_, _ = w.Write([]byte("<html></html>"))
|
|
})
|
|
mux.HandleFunc("/drawing.svg", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
_, _ = w.Write([]byte("<svg/>"))
|
|
})
|
|
mux.HandleFunc("/fake.png", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "image/png")
|
|
_, _ = w.Write([]byte("<html><script>alert(1)</script></html>"))
|
|
})
|
|
mux.HandleFunc("/big.png", func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "image/png")
|
|
w.Header().Set("Content-Length", strconv.Itoa(chattools.MaxFetchedImageBytes+1))
|
|
_, _ = w.Write(append(testPNG, make([]byte, chattools.MaxFetchedImageBytes+1-len(testPNG))...))
|
|
})
|
|
mux.HandleFunc("/to-http", func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "http://"+r.Host+"/a.png", http.StatusFound)
|
|
})
|
|
mux.HandleFunc("/to-png", func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "/a.png", http.StatusFound)
|
|
})
|
|
server := httptest.NewTLSServer(mux)
|
|
t.Cleanup(server.Close)
|
|
f.service.fetch = shownImageFetcher{
|
|
allowed: func(addr netip.Addr) bool { return addr.IsLoopback() },
|
|
tlsConfig: server.Client().Transport.(*http.Transport).TLSClientConfig,
|
|
}
|
|
return server
|
|
}
|
|
|
|
func TestShowImageFetchesOnlyRealImagesWithinBounds(t *testing.T) {
|
|
f := newChatToolFixture(t)
|
|
token := f.token(t, chatToolSessionScope+"ses_w")
|
|
web := f.imageServer(t)
|
|
if r := f.call(token, "show_image", `{"url_or_path":"`+web.URL+`/to-png"}`); r.IsError {
|
|
t.Fatalf("an https redirect is followed: %v", r)
|
|
}
|
|
for path, want := range map[string]string{
|
|
"/page": "text/html, not a PNG",
|
|
"/drawing.svg": "image/svg+xml, not a PNG",
|
|
"/fake.png": "content is not an image",
|
|
"/big.png": "the limit is 10 MiB",
|
|
"/to-http": "redirects to a non-https address",
|
|
"/missing.png": "answered 404",
|
|
} {
|
|
r := f.call(token, "show_image", `{"url_or_path":"`+web.URL+path+`"}`)
|
|
if !r.IsError || !strings.Contains(r.Text, want) {
|
|
t.Errorf("%s: got %v, want %q", path, r, want)
|
|
}
|
|
}
|
|
if got := f.items(t, "ses_w"); len(got) != 1 {
|
|
t.Fatalf("only the real image is recorded: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestShowImageRefusesPrivateAndLocalHosts(t *testing.T) {
|
|
f := newChatToolFixture(t)
|
|
token := f.token(t, chatToolSessionScope+"ses_s")
|
|
web := f.imageServer(t)
|
|
f.service.fetch.allowed = publicAddress
|
|
r := f.call(token, "show_image", `{"url_or_path":"`+web.URL+`/a.png"}`)
|
|
if !r.IsError || !strings.Contains(r.Text, "private or local address") {
|
|
t.Fatalf("a loopback host must be refused: %v", r)
|
|
}
|
|
for _, refused := range []string{"127.0.0.1", "10.1.2.3", "172.16.0.1", "192.168.1.1", "169.254.169.254", "100.64.0.1", "0.0.0.0", "224.0.0.1", "::1", "fc00::1", "fe80::1", "::ffff:127.0.0.1", "::ffff:10.0.0.1", "2001:db8::1", "198.18.0.1"} {
|
|
if publicAddress(netip.MustParseAddr(refused)) {
|
|
t.Errorf("%s must not be fetched from", refused)
|
|
}
|
|
}
|
|
for _, allowed := range []string{"93.184.216.34", "1.1.1.1", "2606:4700:4700::1111"} {
|
|
if !publicAddress(netip.MustParseAddr(allowed)) {
|
|
t.Errorf("%s is a public address", allowed)
|
|
}
|
|
}
|
|
}
|