From 5f827b6e19c8d638be8ddea8d1dd09f26f97fa78 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 18:05:34 +1100 Subject: [PATCH] fix(messaging): refuse an inbound attachment whose type contradicts its content A declared MIME type decides what the Gateway does with a file: a voice note is transcribed, an image goes to a model. Both acceptance points took that claim from the caller unchecked -- the dashboard's voice upload and every Telegram attachment -- so a file could be used as something it is not. ValidateInbound refuses a type outside the shared vocabulary, content over the size ceiling, and a MIME type that contradicts the attachment type, with an error the caller sees: a dashboard upload is a 400, a Telegram file gets a chat reply instead of a silent drop. --- internal/channels/telegram/telegram.go | 8 +++ internal/domain/messaging/media.go | 70 +++++++++++++++++++++++++ internal/domain/messaging/media_test.go | 44 ++++++++++++++++ internal/webui/api_chat.go | 4 ++ 4 files changed, 126 insertions(+) create mode 100644 internal/domain/messaging/media_test.go diff --git a/internal/channels/telegram/telegram.go b/internal/channels/telegram/telegram.go index efec6e8b7..ca18e4256 100644 --- a/internal/channels/telegram/telegram.go +++ b/internal/channels/telegram/telegram.go @@ -686,6 +686,14 @@ func (g *Gateway) fetchTurnMedia(ctx context.Context, b *bot.Bot, gm *messaging. if len(gm.Media) == 0 || gm.Media[0].Data != nil { return true } + // Before the download, so a file whose declared type contradicts itself is + // refused rather than fetched and acted on. + if err := messaging.ValidateInbound(gm.Media[0]); err != nil { + g.log.Warn("inbound media refused", "media_type", gm.Media[0].Type, "error", err) + g.sendMessage(ctx, b, chatID, threadID, + mediaReplyLabel(gm.Media[0])+"but I can't use that file — try sending it again.") + return false + } data, err := g.downloadMedia(ctx, b, gm.Media[0]) if err != nil { g.log.Warn("inbound media download failed", "media_type", gm.Media[0].Type, "error", err) diff --git a/internal/domain/messaging/media.go b/internal/domain/messaging/media.go index fcf46677a..35cfbbe62 100644 --- a/internal/domain/messaging/media.go +++ b/internal/domain/messaging/media.go @@ -1,5 +1,11 @@ package messaging +import ( + "fmt" + "mime" + "strings" +) + // MaxInboundAttachmentBytes is the largest attachment carried to the Gateway. const MaxInboundAttachmentBytes = 20 << 20 @@ -58,3 +64,67 @@ type MediaAttachment struct { // persisted. Data []byte `json:"data,omitempty"` } + +// ValidateInbound refuses an attachment that cannot be what it says it is: a +// type outside the shared vocabulary, content over the size ceiling, or a MIME +// type that contradicts the type. The declared MIME type decides how the file +// is used -- a voice note is transcribed, an image is sent to a model -- so a +// caller that can name its own type must have it checked before it is believed. +// An empty MIME type is unknown rather than wrong and is accepted; a platform +// that records no content type is not claiming one. +func ValidateInbound(att MediaAttachment) error { + fileName := att.FileName + if fileName == "" { + fileName = att.Type + } + switch att.Type { + case MediaTypeImage, MediaTypeVideo, MediaTypeAudio, MediaTypeVoice, MediaTypeDocument: + default: + return fmt.Errorf("media: %s: unknown attachment type %q", fileName, att.Type) + } + for _, size := range []int64{int64(len(att.Data)), int64Of(att.FileSize)} { + if size > MaxInboundAttachmentBytes { + return fmt.Errorf("media: %s is %d bytes, over the %d byte limit", fileName, size, MaxInboundAttachmentBytes) + } + } + if att.MIMEType == "" { + return nil + } + mediaType, _, err := mime.ParseMediaType(att.MIMEType) + if err != nil { + return fmt.Errorf("media: %s: %q is not a content type", fileName, att.MIMEType) + } + if !typeCarriesMIME(att.Type, mediaType) { + return fmt.Errorf("media: %s: %q cannot be a %s", fileName, mediaType, att.Type) + } + 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. +func typeCarriesMIME(attachmentType, mediaType string) bool { + family := mediaType + if slash := strings.IndexByte(family, '/'); slash >= 0 { + family = family[:slash] + } + switch attachmentType { + case MediaTypeImage: + return family == "image" + case MediaTypeVideo: + return family == "video" || family == "image" // Animated stickers and GIF-style clips arrive as video. + case MediaTypeAudio, MediaTypeVoice: + // Ogg is served as application/ogg, and every voice note a browser or + // platform records is Ogg or WebM. + return family == "audio" || mediaType == "application/ogg" + default: + return true + } +} + +func int64Of(p *int64) int64 { + if p == nil { + return 0 + } + return *p +} diff --git a/internal/domain/messaging/media_test.go b/internal/domain/messaging/media_test.go new file mode 100644 index 000000000..69d809948 --- /dev/null +++ b/internal/domain/messaging/media_test.go @@ -0,0 +1,44 @@ +package messaging + +import ( + "strings" + "testing" +) + +// TestValidateInbound pins that an attachment the Gateway will act on cannot +// claim a type its content contradicts, and that ordinary platform media is not +// refused: a rejected voice note never gets transcribed, a rejected image never +// reaches a model. +func TestValidateInbound(t *testing.T) { + oversize := int64(MaxInboundAttachmentBytes) + 1 + tests := []struct { + name string + att MediaAttachment + wantErr bool + }{ + {"a photo", MediaAttachment{Type: MediaTypeImage, MIMEType: "image/jpeg"}, false}, + {"a voice note", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/ogg"}, false}, + {"a browser clip with codec parameters", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/webm;codecs=opus"}, false}, + {"ogg served as application/ogg", MediaAttachment{Type: MediaTypeAudio, MIMEType: "application/ogg"}, false}, + {"a gif-style clip", MediaAttachment{Type: MediaTypeVideo, MIMEType: "image/gif"}, false}, + {"any document", MediaAttachment{Type: MediaTypeDocument, MIMEType: "application/zip"}, false}, + {"an unknown content type is not a claim", MediaAttachment{Type: MediaTypeDocument}, false}, + {"a voice note that is really html", MediaAttachment{Type: MediaTypeVoice, MIMEType: "text/html"}, true}, + {"an image that is really a document", MediaAttachment{Type: MediaTypeImage, MIMEType: "application/pdf"}, true}, + {"a content type that is not one", MediaAttachment{Type: MediaTypeImage, MIMEType: "jpeg"}, true}, + {"an attachment type the Gateway does not use", MediaAttachment{Type: "sticker", MIMEType: "image/webp"}, true}, + {"declared bytes over the ceiling", MediaAttachment{Type: MediaTypeImage, FileSize: &oversize}, true}, + {"downloaded bytes over the ceiling", MediaAttachment{Type: MediaTypeImage, Data: make([]byte, oversize)}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := ValidateInbound(tt.att) + if (err != nil) != tt.wantErr { + t.Fatalf("ValidateInbound(%+v) = %v, want error %v", tt.att, err, tt.wantErr) + } + if err != nil && !strings.HasPrefix(err.Error(), "media: ") { + t.Fatalf("error %q does not name the attachment", err) + } + }) + } +} diff --git a/internal/webui/api_chat.go b/internal/webui/api_chat.go index f6da38bb1..275432901 100644 --- a/internal/webui/api_chat.go +++ b/internal/webui/api_chat.go @@ -264,6 +264,10 @@ func (s *Server) decodeChatMessage(w http.ResponseWriter, r *http.Request) (mess return messaging.Inbound{}, false } media = append(media, messaging.MediaAttachment{Type: messaging.MediaTypeVoice, MIMEType: req.Voice.MIMEType, Data: req.Voice.Data}) + if err := messaging.ValidateInbound(media[0]); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return messaging.Inbound{}, false + } if req.Text == "" { req.Text = "[voice message]" }