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.
This commit is contained in:
sam committed 2026-10-25 20:17:30 +11:00
1 parent d7fc6d22b2
commit 1e1a055f98
2 files changed
+56 -5

No files matched your search

+49 -5
View File
@@ -66,11 +66,12 @@ type MediaAttachment struct {
}
// 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.
// 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
// 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.
func ValidateInbound(att MediaAttachment) error {
fileName := att.FileName
if fileName == "" {
@@ -86,6 +87,9 @@ 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 att.MIMEType == "" {
// A platform adapter may genuinely not know the content type. A caller
// that chose the attachment type itself must supply one; see
@@ -131,6 +135,46 @@ func int64Of(p *int64) int64 {
return *p
}
// audioMagic are the leading bytes of the containers a clip is recorded in.
var audioMagic = []struct {
at int
magic string
kind string
}{
{0, "OggS", "audio/ogg"},
{0, "\x1a\x45\xdf\xa3", "audio/webm"},
{0, "fLaC", "audio/flac"},
{0, "ID3", "audio/mpeg"},
// An ISO base media file, opened with any of the boxes a writer may lead
// with rather than ftyp first.
{4, "ftyp", "audio/mp4"},
{4, "moov", "audio/mp4"},
{4, "mdat", "audio/mp4"},
{4, "free", "audio/mp4"},
{4, "skip", "audio/mp4"},
{4, "wide", "audio/mp4"},
}
// sniffAudio reports the content type of the container data starts with, or ""
// when the bytes are not in one a voice clip is recorded in. Magic numbers are
// read rather than http.DetectContentType, which answers application/octet-stream
// for the WebM every browser but Safari records.
func sniffAudio(data []byte) string {
for _, m := range audioMagic {
if len(data) >= m.at+len(m.magic) && string(data[m.at:m.at+len(m.magic)]) == m.magic {
return m.kind
}
}
if len(data) >= 12 && string(data[:4]) == "RIFF" && string(data[8:12]) == "WAVE" {
return "audio/wav"
}
// An mp3 with its tag stripped starts on a frame sync.
if len(data) >= 2 && data[0] == 0xFF && data[1]&0xE0 == 0xE0 {
return "audio/mpeg"
}
return ""
}
// 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,
+7
View File
@@ -31,6 +31,13 @@ func TestValidateInbound(t *testing.T) {
{"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) {