From 2965f8354cb3736fb8e6e7380fbfa32448ca761d Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 20:17:30 +1100 Subject: [PATCH] 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. --- internal/domain/messaging/media.go | 54 ++++++++++++++++++++++--- internal/domain/messaging/media_test.go | 7 ++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/internal/domain/messaging/media.go b/internal/domain/messaging/media.go index bc05bf799..d2d9cef6a 100644 --- a/internal/domain/messaging/media.go +++ b/internal/domain/messaging/media.go @@ -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, diff --git a/internal/domain/messaging/media_test.go b/internal/domain/messaging/media_test.go index 62fdc7788..f8eb51f76 100644 --- a/internal/domain/messaging/media_test.go +++ b/internal/domain/messaging/media_test.go @@ -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("