diff --git a/internal/domain/messaging/media.go b/internal/domain/messaging/media.go index d2d9cef6a..16f89a5e5 100644 --- a/internal/domain/messaging/media.go +++ b/internal/domain/messaging/media.go @@ -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. diff --git a/internal/domain/messaging/media_test.go b/internal/domain/messaging/media_test.go index f8eb51f76..08131c009 100644 --- a/internal/domain/messaging/media_test.go +++ b/internal/domain/messaging/media_test.go @@ -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("