fix(gatewayrpc): sniff the head of URL-only attachments (#1672)
deploy / gate (push) Successful in 4m41s
Quality / check (push) Successful in 7m2s
deploy / archied-image (push) Successful in 3m37s
deploy / agent-image (push) Successful in 12s
deploy / dist-zip (push) Skipped

This commit is contained in:
sam committed 2026-11-08 05:40:27 +11:00
1 parent 70251ff3ce
commit cc8428643a
3 files changed
+81 -6

No files matched your search

+4
View File
@@ -5,6 +5,10 @@ release's per-component sections are labelled beneath its heading.
## [Unreleased]
### archie-gateway
- An image or voice attachment that arrives as a link is now checked against what the link serves, the same as an uploaded one. A link whose content is not the declared type, or that cannot be read, is refused.
## [1.61.0] - 2026-11-07
### archied
+55 -5
View File
@@ -2,7 +2,11 @@ package gatewayrpc
import (
"context"
"fmt"
"io"
"net/http"
"sync"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@@ -12,6 +16,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/domain/taskactions"
"github.com/samcharles93/archie-core/internal/infrastructure/alertwebhook"
"github.com/samcharles93/archie-core/internal/taskstate"
)
@@ -21,6 +26,8 @@ type server struct {
sessions messaging.SessionStore
catalog Catalog
asks pendingAsks
// fetch reads the head of a URL-only attachment; it reaches public hosts only.
fetch *http.Client
}
// RegisterServer serves the Gateway chat contract. catalog supplies the
@@ -28,7 +35,7 @@ type server struct {
// reload) the dashboard reads over the same contract; a zero Catalog answers
// empty lists and an Unavailable reload.
func RegisterServer(registrar grpc.ServiceRegistrar, chat messaging.ChatContract, sessions messaging.SessionStore, catalog Catalog) {
pb.RegisterChatServiceServer(registrar, &server{chat: chat, sessions: sessions, catalog: catalog})
pb.RegisterChatServiceServer(registrar, &server{chat: chat, sessions: sessions, catalog: catalog, fetch: alertwebhook.PublicClient()})
}
func (s *server) sessionStore() (messaging.SessionStore, error) {
@@ -72,7 +79,7 @@ func (s *server) RecentTurns(ctx context.Context, r *pb.RecentTurnsRequest) (*pb
func (s *server) Route(ctx context.Context, r *pb.RouteRequest) (*pb.RouteResponse, error) {
in := inboundValue(r.Message)
if err := checkInbound(in); err != nil {
if err := checkInbound(ctx, s.fetch, in); err != nil {
return nil, err
}
v, err := s.chat.Route(ctx, in)
@@ -267,7 +274,7 @@ func (s *server) Stream(r *pb.StreamRequest, out grpc.ServerStreamingServer[pb.S
}
ctx := withAsks(out.Context(), r, remoteAsker{pending: &s.asks, send: send})
in := inboundValue(r.Message)
if err := checkInbound(in); err != nil {
if err := checkInbound(ctx, s.fetch, in); err != nil {
return err
}
events, err := s.chat.Stream(ctx, in)
@@ -295,11 +302,54 @@ func (s *server) Answer(_ context.Context, r *pb.AnswerRequest) (*pb.AnswerRespo
// checkInbound refuses a self-contradicting attachment at the one seam every
// channel, in-tree or extension, crosses before the Gateway acts on it.
func checkInbound(in messaging.Inbound) error {
func checkInbound(ctx context.Context, fetch *http.Client, in messaging.Inbound) error {
for _, m := range in.Media {
if err := messaging.ValidateInbound(m); err != nil {
m, err := withHead(ctx, fetch, m)
if err == nil {
err = messaging.ValidateInbound(m)
}
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
}
return nil
}
// sniffBytes is enough to recognise an image or a clip container.
const sniffBytes = 512
// withHead gives a URL-only attachment of a sniffed type the first bytes of what
// its URL serves, so the type check sees the content rather than the claim. An
// unreadable URL is an error: the model provider would fetch it unchecked. The
// copy is for validation only; the attachment keeps no bytes.
func withHead(ctx context.Context, fetch *http.Client, m messaging.MediaAttachment) (messaging.MediaAttachment, error) {
switch m.Type {
case messaging.MediaTypeImage, messaging.MediaTypeAudio, messaging.MediaTypeVoice:
default:
return m, nil
}
if len(m.Data) > 0 || m.URL == "" {
return m, nil
}
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.URL, nil)
if err != nil {
return m, fmt.Errorf("%s attachment URL: %w", m.Type, err)
}
req.Header.Set("Range", fmt.Sprintf("bytes=0-%d", sniffBytes-1))
resp, err := fetch.Do(req)
if err != nil {
return m, fmt.Errorf("%s attachment URL is unreadable: %w", m.Type, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
return m, fmt.Errorf("%s attachment URL answered %s", m.Type, resp.Status)
}
head, err := io.ReadAll(io.LimitReader(resp.Body, sniffBytes))
if err != nil || len(head) == 0 {
return m, fmt.Errorf("%s attachment URL served no content", m.Type)
}
m.Data = head
return m, nil
}
@@ -1,6 +1,9 @@
package gatewayrpc
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"google.golang.org/grpc/codes"
@@ -22,18 +25,36 @@ func TestChatWireKeepsGatewayInputs(t *testing.T) {
// TestCheckInboundRefusesContradictoryMedia pins that a channel extension
// cannot deliver an attachment whose declared type its bytes contradict.
func TestCheckInboundRefusesContradictoryMedia(t *testing.T) {
// The URL-only rows are served from a local host, which the production
// client refuses, so the test client is a plain one.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/png":
_, _ = w.Write([]byte("\x89PNG\r\n\x1a\n"))
case "/html":
_, _ = w.Write([]byte("<!doctype html><script>"))
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
tests := []struct {
name string
att messaging.MediaAttachment
want codes.Code
}{
{"a url-only png", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", URL: srv.URL + "/png"}, codes.OK},
{"a url-only png that serves html", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", URL: srv.URL + "/html"}, codes.InvalidArgument},
{"a url-only voice note that serves html", messaging.MediaAttachment{Type: messaging.MediaTypeVoice, MIMEType: "audio/ogg", URL: srv.URL + "/html"}, codes.InvalidArgument},
{"a url-only image that cannot be read", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", URL: srv.URL + "/gone"}, codes.InvalidArgument},
{"a url-only document is not fetched", messaging.MediaAttachment{Type: messaging.MediaTypeDocument, MIMEType: "application/pdf", URL: srv.URL + "/html"}, codes.OK},
{"a png", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", Data: []byte("\x89PNG\r\n\x1a\n")}, codes.OK},
{"html declared as png", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", Data: []byte("<!doctype html><script>")}, codes.InvalidArgument},
{"an executable declared as png", messaging.MediaAttachment{Type: messaging.MediaTypeImage, MIMEType: "image/png", Data: []byte("\x7fELF\x02\x01\x01\x00")}, codes.InvalidArgument},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := status.Code(checkInbound(messaging.Inbound{Media: []messaging.MediaAttachment{tt.att}}))
got := status.Code(checkInbound(context.Background(), srv.Client(), messaging.Inbound{Media: []messaging.MediaAttachment{tt.att}}))
if got != tt.want {
t.Fatalf("code = %v, want %v", got, tt.want)
}