From dda9aecde68011b576bc26eb7e140eb8ea041cad Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 18:13:30 +1100 Subject: [PATCH] fix(messaging): say what the inbound media check actually checks The check compares two declarations; it never reads the bytes, so it cannot tell whether a file is what its content type says. The comment and the test row claiming otherwise have gone, and so has an allowance letting a video carry an image type, which no producer emits. A caller that named the attachment type itself -- a browser upload -- must now also state a content type, since the previous empty-type escape made the check optional there. The Telegram declared-size rule is deleted: the shared check runs first with the same ceiling, so only the rule on the bytes actually received can still fire. --- internal/channels/telegram/inboundmedia.go | 7 ++-- internal/domain/messaging/media.go | 33 ++++++++++++------ internal/domain/messaging/media_test.go | 40 +++++++++++++--------- internal/webui/api_chat.go | 2 +- 4 files changed, 50 insertions(+), 32 deletions(-) diff --git a/internal/channels/telegram/inboundmedia.go b/internal/channels/telegram/inboundmedia.go index dcada2fdb..df4de0811 100644 --- a/internal/channels/telegram/inboundmedia.go +++ b/internal/channels/telegram/inboundmedia.go @@ -119,14 +119,13 @@ var errInboundMediaTooLarge = errors.New("file exceeds the bot download limit") // downloadMedia turns a Telegram file id into the file's bytes: getFile // resolves the transfer path, then the file is fetched from the Bot API // server. Called on the turn lane, which is already running the rest of -// the download-and-chat work for this message. +// the download-and-chat work for this message. The size ceiling is the caller's +// check (messaging.ValidateInbound, before the download); what this enforces is +// the bytes Telegram did not declare. func (g *Gateway) downloadMedia(ctx context.Context, b *bot.Bot, att messaging.MediaAttachment) ([]byte, error) { if att.FileID == "" { return nil, fmt.Errorf("media attachment carries no file id") } - if att.FileSize != nil && *att.FileSize > maxInboundDownloadBytes { - return nil, fmt.Errorf("%w: %d bytes", errInboundMediaTooLarge, *att.FileSize) - } file, err := b.GetFile(ctx, &bot.GetFileParams{FileID: att.FileID}) if err != nil { return nil, fmt.Errorf("resolve telegram file: %w", err) diff --git a/internal/domain/messaging/media.go b/internal/domain/messaging/media.go index 35cfbbe62..bc05bf799 100644 --- a/internal/domain/messaging/media.go +++ b/internal/domain/messaging/media.go @@ -65,13 +65,12 @@ type MediaAttachment struct { 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. +// ValidateInbound refuses an attachment whose declared content type contradicts +// its declared attachment type, or whose declared size is over the ceiling. The +// declaration decides how the file is used -- a voice note is transcribed, an +// image is sent to a model -- so two declarations that disagree are refused +// rather than resolved by guessing. It cannot tell whether the bytes are what +// the content type says: nothing here reads them. func ValidateInbound(att MediaAttachment) error { fileName := att.FileName if fileName == "" { @@ -88,6 +87,9 @@ func ValidateInbound(att MediaAttachment) error { } } if att.MIMEType == "" { + // A platform adapter may genuinely not know the content type. A caller + // that chose the attachment type itself must supply one; see + // ValidateAuthoredInbound. return nil } mediaType, _, err := mime.ParseMediaType(att.MIMEType) @@ -112,10 +114,10 @@ func typeCarriesMIME(attachmentType, mediaType string) bool { case MediaTypeImage: return family == "image" case MediaTypeVideo: - return family == "video" || family == "image" // Animated stickers and GIF-style clips arrive as video. + return family == "video" case MediaTypeAudio, MediaTypeVoice: - // Ogg is served as application/ogg, and every voice note a browser or - // platform records is Ogg or WebM. + // Ogg is served as application/ogg, and the voice notes browsers and + // platforms record are Ogg, WebM or (Safari) mp4. return family == "audio" || mediaType == "application/ogg" default: return true @@ -128,3 +130,14 @@ func int64Of(p *int64) int64 { } return *p } + +// ValidateAuthoredInbound is ValidateInbound for an attachment the caller +// described itself, as a browser upload is: its content type is a claim, not an +// observation, so it must be stated. A platform adapter uses ValidateInbound, +// where an unknown content type is not a lie. +func ValidateAuthoredInbound(att MediaAttachment) error { + if att.MIMEType == "" { + return fmt.Errorf("media: %s: no content type", att.Type) + } + return ValidateInbound(att) +} diff --git a/internal/domain/messaging/media_test.go b/internal/domain/messaging/media_test.go index 69d809948..62fdc7788 100644 --- a/internal/domain/messaging/media_test.go +++ b/internal/domain/messaging/media_test.go @@ -12,27 +12,33 @@ import ( func TestValidateInbound(t *testing.T) { oversize := int64(MaxInboundAttachmentBytes) + 1 tests := []struct { - name string - att MediaAttachment - wantErr bool + name string + att MediaAttachment + authored bool + 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}, + {"a photo", MediaAttachment{Type: MediaTypeImage, MIMEType: "image/jpeg"}, false, false}, + {"a voice note", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/ogg"}, true, false}, + {"a browser clip with codec parameters", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/webm;codecs=opus"}, true, false}, + {"ogg served as application/ogg", MediaAttachment{Type: MediaTypeAudio, MIMEType: "application/ogg"}, false, false}, + {"a safari clip", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/mp4"}, true, false}, + {"any document", MediaAttachment{Type: MediaTypeDocument, MIMEType: "application/zip"}, false, false}, + {"a platform that reports no content type is not claiming one", MediaAttachment{Type: MediaTypeDocument}, false, false}, + {"a caller that chose the type must state a content type", MediaAttachment{Type: MediaTypeVoice}, true, true}, + {"a voice note that declares html", MediaAttachment{Type: MediaTypeVoice, MIMEType: "text/html"}, false, true}, + {"an image that declares a document", MediaAttachment{Type: MediaTypeImage, MIMEType: "application/pdf"}, false, true}, + {"a content type that is not one", MediaAttachment{Type: MediaTypeImage, MIMEType: "jpeg"}, false, true}, + {"an attachment type the Gateway does not use", MediaAttachment{Type: "sticker", MIMEType: "image/webp"}, false, true}, + {"declared bytes over the ceiling", MediaAttachment{Type: MediaTypeImage, FileSize: &oversize}, false, true}, + {"downloaded bytes over the ceiling", MediaAttachment{Type: MediaTypeImage, Data: make([]byte, oversize)}, false, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := ValidateInbound(tt.att) + validate := ValidateInbound + if tt.authored { + validate = ValidateAuthoredInbound + } + err := validate(tt.att) if (err != nil) != tt.wantErr { t.Fatalf("ValidateInbound(%+v) = %v, want error %v", tt.att, err, tt.wantErr) } diff --git a/internal/webui/api_chat.go b/internal/webui/api_chat.go index 275432901..cfb010f18 100644 --- a/internal/webui/api_chat.go +++ b/internal/webui/api_chat.go @@ -264,7 +264,7 @@ 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 { + if err := messaging.ValidateAuthoredInbound(media[0]); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return messaging.Inbound{}, false }