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.
This commit is contained in:
sam committed 2026-10-25 18:13:30 +11:00
1 parent e3ec4f01d6
commit dda9aecde6
4 files changed
+50 -32

No files matched your search

+3 -4
View File
@@ -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)
+23 -10
View File
@@ -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)
}
+23 -17
View File
@@ -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)
}
+1 -1
View File
@@ -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
}