media: never project zero image/video dimensions (crashes Telegram Desktop on reactions)

This commit is contained in:
Astra 2026-09-08 16:29:37 +01:00
parent 233a2399e9
commit f26468ef6d
3 changed files with 122 additions and 4 deletions

View file

@ -681,6 +681,64 @@ func TestTGDocumentDoesNotEmitAnimatedAttributeForTGSSticker(t *testing.T) {
}
}
func TestTGDocumentNeverEmitsZeroImageOrVideoDimensions(t *testing.T) {
// A sticker / custom emoji whose stored metadata carries a zero dimension
// must not reach the client as documentAttributeImageSize#0 -- Telegram
// Desktop divides by it and crashes when the emoji is used as a reaction.
sticker := tgDocument(domain.Document{
ID: 100, AccessHash: 1, DCID: 2, MimeType: "application/x-tgsticker",
Attributes: []domain.DocumentAttribute{
{Kind: domain.DocAttrImageSize, W: 0, H: 0},
{Kind: domain.DocAttrCustomEmoji, Alt: "🙂", StickerSetID: 10, StickerSetAccessHash: 20},
},
}).(*tg.Document)
var gotImage bool
for _, attr := range sticker.Attributes {
if a, ok := attr.(*tg.DocumentAttributeImageSize); ok {
gotImage = true
if a.W != 512 || a.H != 512 {
t.Fatalf("sticker imageSize = %dx%d, want 512x512 fallback", a.W, a.H)
}
}
}
if !gotImage {
t.Fatal("sticker lost its imageSize attribute entirely")
}
video := tgDocument(domain.Document{
ID: 101, AccessHash: 1, DCID: 2, MimeType: "video/webm",
Attributes: []domain.DocumentAttribute{
{Kind: domain.DocAttrVideo, W: 0, H: 0, Duration: 3},
{Kind: domain.DocAttrSticker, Alt: "😀", StickerSetID: 10, StickerSetAccessHash: 20},
},
}).(*tg.Document)
for _, attr := range video.Attributes {
if a, ok := attr.(*tg.DocumentAttributeVideo); ok && (a.W <= 0 || a.H <= 0) {
t.Fatalf("video sticker attribute has zero dimension: %+v", a)
}
}
// A malformed plain image drops the attribute rather than emitting a zero.
plain := tgDocument(domain.Document{
ID: 102, AccessHash: 1, DCID: 2, MimeType: "image/jpeg",
Attributes: []domain.DocumentAttribute{{Kind: domain.DocAttrImageSize, W: 0, H: 480}},
}).(*tg.Document)
for _, attr := range plain.Attributes {
if _, ok := attr.(*tg.DocumentAttributeImageSize); ok {
t.Fatalf("plain image kept a malformed imageSize: %+v", attr)
}
}
// A zero-dimension thumbnail is dropped, not emitted.
thumbed := tgDocument(domain.Document{
ID: 103, AccessHash: 1, DCID: 2, MimeType: "image/webp",
Thumbs: []domain.PhotoSize{{Kind: domain.PhotoSizeKindDefault, Type: "s", W: 0, H: 0, Size: 10}},
}).(*tg.Document)
if len(thumbed.Thumbs) != 0 {
t.Fatalf("kept a zero-dimension thumb: %#v", thumbed.Thumbs)
}
}
func TestTGDocumentUsesDomainDocumentID(t *testing.T) {
const documentID int64 = 1382305375846410902