fix(gatewayrpc): refuse an inbound attachment whose bytes contradict its declared type
This commit is contained in:
1 parent
2d9b0734be
commit
e5383b2129
4 files changed
+74
-6
No files matched your search
@@ -3,6 +3,7 @@ package messaging
|
||||
import (
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -67,8 +68,9 @@ type MediaAttachment struct {
|
||||
|
||||
// ValidateInbound refuses an attachment whose declared content type contradicts
|
||||
// its declared attachment type, whose declared size is over the ceiling, or --
|
||||
// when the bytes are here and the attachment claims to be speech -- bytes that
|
||||
// are not in a container audio is recorded in. The declaration decides how the
|
||||
// when the bytes are here and the attachment claims to be an image or speech --
|
||||
// bytes that are not one. Speech is read by container magic, an image by
|
||||
// http.DetectContentType. The declaration decides how the
|
||||
// file is used: a voice note is transcribed, an image is sent to a model. Only
|
||||
// speech is read, because it is the one type where every producer uses one of a
|
||||
// handful of containers and a wrong container means it was never a clip.
|
||||
@@ -87,8 +89,8 @@ func ValidateInbound(att MediaAttachment) error {
|
||||
return fmt.Errorf("media: %s is %d bytes, over the %d byte limit", fileName, size, MaxInboundAttachmentBytes)
|
||||
}
|
||||
}
|
||||
if len(att.Data) > 0 && (att.Type == MediaTypeAudio || att.Type == MediaTypeVoice) && sniffAudio(att.Data) == "" {
|
||||
return fmt.Errorf("media: %s: the bytes are not a recorded clip", fileName)
|
||||
if err := checkBytes(att, fileName); err != nil {
|
||||
return err
|
||||
}
|
||||
if att.MIMEType == "" {
|
||||
// A platform adapter may genuinely not know the content type. A caller
|
||||
@@ -106,6 +108,25 @@ func ValidateInbound(att MediaAttachment) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkBytes refuses downloaded bytes that are not the kind of file the
|
||||
// attachment claims to be.
|
||||
func checkBytes(att MediaAttachment, fileName string) error {
|
||||
if len(att.Data) == 0 {
|
||||
return nil
|
||||
}
|
||||
switch att.Type {
|
||||
case MediaTypeAudio, MediaTypeVoice:
|
||||
if sniffAudio(att.Data) == "" {
|
||||
return fmt.Errorf("media: %s: the bytes are not a recorded clip", fileName)
|
||||
}
|
||||
case MediaTypeImage:
|
||||
if !strings.HasPrefix(http.DetectContentType(att.Data), "image/") {
|
||||
return fmt.Errorf("media: %s: the bytes are not an image", fileName)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// typeCarriesMIME reports whether an attachment of this type can hold a file of
|
||||
// that content type. A document carries anything; the others would be used as
|
||||
// what they claim to be.
|
||||
|
||||
@@ -35,6 +35,9 @@ func TestValidateInbound(t *testing.T) {
|
||||
{"a webm clip", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/webm;codecs=opus", Data: []byte("\x1a\x45\xdf\xa3\x01\x00")}, true, false},
|
||||
{"a safari mp4 clip", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/mp4", Data: []byte("\x00\x00\x00\x20ftypM4A ")}, true, false},
|
||||
{"a wav clip", MediaAttachment{Type: MediaTypeAudio, MIMEType: "audio/wav", Data: []byte("RIFF\x24\x00\x00\x00WAVEfmt ")}, false, false},
|
||||
{"a png", MediaAttachment{Type: MediaTypeImage, MIMEType: "image/png", Data: []byte("\x89PNG\r\n\x1a\n")}, false, false},
|
||||
{"an image declaring png but carrying html", MediaAttachment{Type: MediaTypeImage, MIMEType: "image/png", Data: []byte("<!doctype html><script>")}, false, true},
|
||||
{"an image declaring png but carrying an executable", MediaAttachment{Type: MediaTypeImage, MIMEType: "image/png", Data: []byte("\x7fELF\x02\x01\x01\x00")}, false, true},
|
||||
{"a voice note carrying html", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/ogg", Data: []byte("<!doctype html><script>")}, true, true},
|
||||
{"a voice note carrying a png", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/webm", Data: []byte("\x89PNG\r\n\x1a\n")}, true, true},
|
||||
{"a document is not sniffed", MediaAttachment{Type: MediaTypeDocument, MIMEType: "application/pdf", Data: []byte("%PDF-1.7")}, false, false},
|
||||
|
||||
@@ -71,7 +71,11 @@ func (s *server) RecentTurns(ctx context.Context, r *pb.RecentTurnsRequest) (*pb
|
||||
}
|
||||
|
||||
func (s *server) Route(ctx context.Context, r *pb.RouteRequest) (*pb.RouteResponse, error) {
|
||||
v, err := s.chat.Route(ctx, inboundValue(r.Message))
|
||||
in := inboundValue(r.Message)
|
||||
if err := checkInbound(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v, err := s.chat.Route(ctx, in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -262,7 +266,11 @@ func (s *server) Stream(r *pb.StreamRequest, out grpc.ServerStreamingServer[pb.S
|
||||
return out.Send(v)
|
||||
}
|
||||
ctx := withAsks(out.Context(), r, remoteAsker{pending: &s.asks, send: send})
|
||||
events, err := s.chat.Stream(ctx, inboundValue(r.Message))
|
||||
in := inboundValue(r.Message)
|
||||
if err := checkInbound(in); err != nil {
|
||||
return err
|
||||
}
|
||||
events, err := s.chat.Stream(ctx, in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -284,3 +292,14 @@ func (s *server) Stream(r *pb.StreamRequest, out grpc.ServerStreamingServer[pb.S
|
||||
func (s *server) Answer(_ context.Context, r *pb.AnswerRequest) (*pb.AnswerResponse, error) {
|
||||
return &pb.AnswerResponse{Found: s.asks.answer(r.AskId, r.Answer)}, nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
for _, m := range in.Media {
|
||||
if err := messaging.ValidateInbound(m); err != nil {
|
||||
return status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -3,6 +3,9 @@ package gatewayrpc
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
@@ -15,3 +18,25 @@ func TestChatWireKeepsGatewayInputs(t *testing.T) {
|
||||
t.Fatalf("inbound round trip lost a field: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckInboundRefusesContradictoryMedia pins that a channel extension
|
||||
// cannot deliver an attachment whose declared type its bytes contradict.
|
||||
func TestCheckInboundRefusesContradictoryMedia(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
att messaging.MediaAttachment
|
||||
want codes.Code
|
||||
}{
|
||||
{"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}}))
|
||||
if got != tt.want {
|
||||
t.Fatalf("code = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user