Files
pipelines/internal/server/chat_attachment_image_test.go
alan 8c421952db feat(chat): show sent images as thumbnails with a full-screen viewer
Operator-attached images render as rounded thumbnails above the user
bubble and open a zoomable Material/Cupertino viewer. Inline data: URIs
decode locally; file:// attachment-store URIs load through the new
chat_attachment_image websocket request, which serves only regular image
files directly inside the private attachment store (os.Root, <= 4 MiB,
sniffed raster types). Missing files show an 'Image no longer available'
placeholder; other files keep the file row.
2026-10-30 20:53:03 +00:00

150 lines
4.9 KiB
Go

package server
import (
"context"
"encoding/base64"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// pngHeader is enough for content sniffing to report image/png.
var pngHeader = []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00")
func attachmentImageStore(t *testing.T) string {
t.Helper()
dir := filepath.Join(t.TempDir(), "uploads")
if err := os.Mkdir(dir, 0o700); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
return dir
}
func fileURI(path string) string { return (&url.URL{Scheme: "file", Path: path}).String() }
func requestAttachmentImage(t *testing.T, uri string) protocol.Message {
t.Helper()
var reply protocol.Message
request := protocol.Message{Type: protocol.ChatAttachmentImage, ID: "img-1", ChatAttachmentImageRequest: &protocol.ChatAttachmentImageRequest{URI: uri}}
handled := handleChat(context.Background(), request, &fakeChat{}, nil, nil, "conn-test", func(message protocol.Message) error {
reply = message
return nil
})
if !handled {
t.Fatal("chat_attachment_image was not dispatched")
}
if reply.ID != "img-1" {
t.Fatalf("reply id = %q", reply.ID)
}
return reply
}
func TestChatAttachmentImageServesStoredImage(t *testing.T) {
dir := attachmentImageStore(t)
path := filepath.Join(dir, "up-0123456789abcdef0123456789abcdef-shot one.png")
if err := os.WriteFile(path, pngHeader, 0o600); err != nil {
t.Fatal(err)
}
reply := requestAttachmentImage(t, fileURI(path))
if reply.Type != protocol.ChatAttachmentImage || reply.ChatAttachmentImage == nil {
t.Fatalf("reply = %+v", reply)
}
got := reply.ChatAttachmentImage
data, err := base64.StdEncoding.DecodeString(got.Data)
if err != nil || string(data) != string(pngHeader) {
t.Fatalf("data = %q err=%v", data, err)
}
if got.Mime != "image/png" || got.Size != int64(len(pngHeader)) || got.URI != fileURI(path) {
t.Fatalf("content = %+v", got)
}
}
func TestChatAttachmentImageRefusesOutsideAndUnsafeReferences(t *testing.T) {
dir := attachmentImageStore(t)
outside := filepath.Join(filepath.Dir(dir), "secret.png")
if err := os.WriteFile(outside, pngHeader, 0o600); err != nil {
t.Fatal(err)
}
nested := filepath.Join(dir, "nested")
if err := os.Mkdir(nested, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(nested, "inner.png"), pngHeader, 0o600); err != nil {
t.Fatal(err)
}
link := filepath.Join(dir, "link.png")
if err := os.Symlink(outside, link); err != nil {
t.Fatal(err)
}
text := filepath.Join(dir, "notes.txt")
if err := os.WriteFile(text, []byte("plain text, not an image"), 0o600); err != nil {
t.Fatal(err)
}
cases := map[string]string{
"outside": fileURI(outside),
"traversal": "file://" + dir + "/../secret.png",
"encoded": "file://" + dir + "/%2e%2e/secret.png",
"nested": fileURI(filepath.Join(nested, "inner.png")),
"symlink": fileURI(link),
"directory": fileURI(nested),
"store itself": fileURI(dir),
"relative": "file:secret.png",
"remote host": "file://example.com" + outside,
"http": "https://example.com/a.png",
"data": "data:image/png;base64,AAAA",
"query": fileURI(outside) + "?x=1",
"not an image": fileURI(text),
"plain path": outside,
"fragment": fileURI(outside) + "#top",
}
for name, uri := range cases {
t.Run(name, func(t *testing.T) {
reply := requestAttachmentImage(t, uri)
if reply.Type != protocol.Error || reply.ChatAttachmentImage != nil {
t.Fatalf("%s was served: %+v", uri, reply)
}
})
}
}
func TestChatAttachmentImageMissingAndOversize(t *testing.T) {
dir := attachmentImageStore(t)
missing := requestAttachmentImage(t, fileURI(filepath.Join(dir, "up-gone.png")))
if missing.Type != protocol.Error || !strings.Contains(missing.Error, "no longer available") {
t.Fatalf("missing = %+v", missing)
}
large := filepath.Join(dir, "large.png")
body := append(append([]byte{}, pngHeader...), make([]byte, maxChatAttachmentImageBytes)...)
if err := os.WriteFile(large, body, 0o600); err != nil {
t.Fatal(err)
}
oversize := requestAttachmentImage(t, fileURI(large))
if oversize.Type != protocol.Error || !strings.Contains(oversize.Error, "too large") {
t.Fatalf("oversize = %+v", oversize)
}
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
gone := requestAttachmentImage(t, fileURI(filepath.Join(dir, "up-any.png")))
if gone.Type != protocol.Error || !strings.Contains(gone.Error, "no longer available") {
t.Fatalf("store gone = %+v", gone)
}
}
func TestChatAttachmentImageRequiresRequestBody(t *testing.T) {
attachmentImageStore(t)
var reply protocol.Message
handleChat(context.Background(), protocol.Message{Type: protocol.ChatAttachmentImage, ID: "x"}, &fakeChat{}, nil, nil, "c", func(m protocol.Message) error {
reply = m
return nil
})
if reply.Type != protocol.Error {
t.Fatalf("reply = %+v", reply)
}
}