Files
archie/internal/domain/messaging/media_test.go
T
sam 2965f8354c fix(messaging): read a voice clip's container instead of believing its type
The dashboard upload names its own attachment type, so the check that ran on
it only compared two declarations the sender wrote. A browser can label any
bytes audio/ogg, and the transcriber keys on the attachment type alone, so the
bytes went to the operator's provider regardless.

When the bytes are present the container decides: Ogg, WebM/Matroska, mp4/m4a,
wav, flac and mp3 are the containers a clip is recorded in, and anything else
claimed to be speech is refused before it reaches a transcriber. Magic numbers
rather than http.DetectContentType, which reports application/octet-stream for
the WebM every browser but Safari records.
2026-10-25 20:17:30 +11:00

58 lines
3.5 KiB
Go

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
authored bool
wantErr bool
}{
{"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},
{"an ogg clip", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/ogg", Data: []byte("OggS\x00\x02\x00\x00")}, true, false},
{"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 voice note carrying html", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/ogg", Data: []byte("<!doctype html><script>")}, true, true},
{"a voice note carrying a png", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/webm", Data: []byte("\x89PNG\r\n\x1a\n")}, true, true},
{"a document is not sniffed", MediaAttachment{Type: MediaTypeDocument, MIMEType: "application/pdf", Data: []byte("%PDF-1.7")}, false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
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)
}
if err != nil && !strings.HasPrefix(err.Error(), "media: ") {
t.Fatalf("error %q does not name the attachment", err)
}
})
}
}