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.
This commit is contained in:
4 files changed
+126
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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]"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user