diff --git a/CHANGELOG.md b/CHANGELOG.md index 6451e01dd..f670a43e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/infrastructure/gatewayrpc/server.go b/internal/infrastructure/gatewayrpc/server.go index 5bdb6eaed..b9492eb7c 100644 --- a/internal/infrastructure/gatewayrpc/server.go +++ b/internal/infrastructure/gatewayrpc/server.go @@ -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 +} diff --git a/internal/infrastructure/gatewayrpc/wire_test.go b/internal/infrastructure/gatewayrpc/wire_test.go index 556e67699..79c5abb65 100644 --- a/internal/infrastructure/gatewayrpc/wire_test.go +++ b/internal/infrastructure/gatewayrpc/wire_test.go @@ -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("