From 51b676e1eaf8062297120bc8b0fd74510fc0e746 Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Tue, 10 Nov 2026 22:29:39 +0200 Subject: [PATCH 1/7] Add sticker pack media, bounded archives and sticker sync operations --- appstate.go | 17 +++- appstate/sticker.go | 32 +++++++ appstate/sticker_test.go | 32 +++++++ client.go | 5 +- download.go | 14 ++- message.go | 5 +- send.go | 2 + sticker.go | 37 ++++++++ sticker_test.go | 75 +++++++++++++++ stickerpack/archive.go | 183 ++++++++++++++++++++++++++++++++++++ stickerpack/archive_test.go | 55 +++++++++++ types/events/appstate.go | 18 ++++ types/events/sticker.go | 11 +++ 13 files changed, 477 insertions(+), 9 deletions(-) create mode 100644 appstate/sticker.go create mode 100644 appstate/sticker_test.go create mode 100644 sticker.go create mode 100644 sticker_test.go create mode 100644 stickerpack/archive.go create mode 100644 stickerpack/archive_test.go create mode 100644 types/events/sticker.go diff --git a/appstate.go b/appstate.go index a8bfd4f08..0e72e0c0b 100644 --- a/appstate.go +++ b/appstate.go @@ -74,7 +74,7 @@ func (cli *Client) fetchAppState(ctx context.Context, name appstate.WAPatchName, wantSnapshot := fullSync var eventsToDispatch []any eventsToDispatchPtr := &eventsToDispatch - if fullSync && !cli.EmitAppStateEventsOnFullSync && !cli.EmitLabelEventsOnFullSync && !cli.EmitQuickReplyEventsOnFullSync { + if fullSync && !cli.EmitAppStateEventsOnFullSync && !cli.EmitLabelEventsOnFullSync && !cli.EmitQuickReplyEventsOnFullSync && !cli.EmitStickerEventsOnFullSync { eventsToDispatchPtr = nil } for hasMore { @@ -118,7 +118,7 @@ func (cli *Client) handleAppStateRecovery( } var eventsToDispatch []any eventsToDispatchPtr := &eventsToDispatch - if !cli.EmitAppStateEventsOnFullSync && !cli.EmitLabelEventsOnFullSync && !cli.EmitQuickReplyEventsOnFullSync { + if !cli.EmitAppStateEventsOnFullSync && !cli.EmitLabelEventsOnFullSync && !cli.EmitQuickReplyEventsOnFullSync && !cli.EmitStickerEventsOnFullSync { eventsToDispatchPtr = nil } snapshot, err := appstate.ParseRecovery(result[0].GetSyncdSnapshotFatalRecoveryResponse()) @@ -215,6 +215,9 @@ func (cli *Client) shouldEmitFullSyncMutation(index []string) bool { if len(index) == 0 { return false } + if cli.EmitStickerEventsOnFullSync && (index[0] == appstate.IndexFavoriteSticker || index[0] == appstate.IndexRemoveRecentSticker) { + return true + } if cli.EmitQuickReplyEventsOnFullSync && index[0] == appstate.IndexQuickReply { return true } @@ -304,6 +307,16 @@ func (cli *Client) dispatchAppState(ctx context.Context, name appstate.WAPatchNa var storeUpdateError error switch mutation.Index[0] { + case appstate.IndexFavoriteSticker: + if len(mutation.Index) != 2 || mutation.Index[1] == "" || (mutation.Action.GetStickerAction() == nil || mutation.Action.GetStickerAction().IsFavorite == nil) { + return + } + eventToDispatch = &events.FavoriteSticker{FileSHA256: mutation.Index[1], Timestamp: ts, Action: mutation.Action.GetStickerAction(), FromFullSync: fullSync} + case appstate.IndexRemoveRecentSticker: + if len(mutation.Index) != 2 || mutation.Index[1] == "" || mutation.Action.GetRemoveRecentStickerAction() == nil { + return + } + eventToDispatch = &events.RemoveRecentSticker{FileSHA256: mutation.Index[1], Timestamp: ts, Action: mutation.Action.GetRemoveRecentStickerAction(), FromFullSync: fullSync} case appstate.IndexMute: act := mutation.Action.GetMuteAction() eventToDispatch = &events.Mute{JID: jid, Timestamp: ts, Action: act, FromFullSync: fullSync} diff --git a/appstate/sticker.go b/appstate/sticker.go new file mode 100644 index 000000000..0607f5988 --- /dev/null +++ b/appstate/sticker.go @@ -0,0 +1,32 @@ +package appstate + +import ( + "encoding/base64" + "fmt" + + "github.com/polymorfa/hypermeow/proto/waSyncAction" + "google.golang.org/protobuf/proto" +) + +// BuildFavoriteSticker builds a regular_low v7 favorite/unfavorite mutation. +// The hash is the plaintext SHA-256. Favoriting requires downloadable media details. +// Evidence: WAWebStickersFavoriteSyncAction, whatsapp-1053201649, lines 20-25, 81-99. +func BuildFavoriteSticker(hash []byte, action *waSyncAction.StickerAction) (PatchInfo, error) { + if len(hash) != 32 || action == nil || action.IsFavorite == nil { + return PatchInfo{}, fmt.Errorf("sticker hash and favorite state are required") + } + if action.GetIsFavorite() && (len(action.MediaKey) != 32 || len(action.FileEncSHA256) != 32 || action.GetDirectPath() == "" || action.GetMimetype() == "") { + return PatchInfo{}, fmt.Errorf("favorite sticker requires media details") + } + return PatchInfo{Type: WAPatchRegularLow, Mutations: []MutationInfo{{Index: []string{IndexFavoriteSticker, base64.StdEncoding.EncodeToString(hash)}, Version: 7, Value: &waSyncAction.SyncActionValue{StickerAction: proto.Clone(action).(*waSyncAction.StickerAction)}}}}, nil +} + +// BuildRemoveRecentSticker removes a recent sticker up to lastSentTimestampMS. +// The timestamp guard preserves a sticker sent again after the removal was made. +// Evidence: WAWebStickersRemoveRecentSyncAction, whatsapp-1053201649, lines 16-21, 35-38, 46-55. +func BuildRemoveRecentSticker(hash []byte, lastSentTimestampMS int64) (PatchInfo, error) { + if len(hash) != 32 || lastSentTimestampMS < 0 { + return PatchInfo{}, fmt.Errorf("valid sticker hash and last sent timestamp are required") + } + return PatchInfo{Type: WAPatchRegularLow, Mutations: []MutationInfo{{Index: []string{IndexRemoveRecentSticker, base64.StdEncoding.EncodeToString(hash)}, Version: 7, Value: &waSyncAction.SyncActionValue{RemoveRecentStickerAction: &waSyncAction.RemoveRecentStickerAction{LastStickerSentTS: proto.Int64(lastSentTimestampMS)}}}}}, nil +} diff --git a/appstate/sticker_test.go b/appstate/sticker_test.go new file mode 100644 index 000000000..05fd2473f --- /dev/null +++ b/appstate/sticker_test.go @@ -0,0 +1,32 @@ +package appstate + +import ( + "bytes" + "github.com/polymorfa/hypermeow/proto/waSyncAction" + "google.golang.org/protobuf/proto" + "testing" +) + +func TestStickerPatches(t *testing.T) { + hash := bytes.Repeat([]byte{1}, 32) + p, err := BuildFavoriteSticker(hash, &waSyncAction.StickerAction{IsFavorite: proto.Bool(false)}) + if err != nil { + t.Fatal(err) + } + if p.Type != WAPatchRegularLow || p.Mutations[0].Version != 7 || p.Mutations[0].Index[0] != IndexFavoriteSticker { + t.Fatal("wrong favorite patch") + } + if _, err = BuildFavoriteSticker(hash, &waSyncAction.StickerAction{IsFavorite: proto.Bool(true)}); err == nil { + t.Fatal("favorite without media accepted") + } + p, err = BuildRemoveRecentSticker(hash, 123) + if err != nil { + t.Fatal(err) + } + if p.Mutations[0].Value.GetRemoveRecentStickerAction().GetLastStickerSentTS() != 123 || p.Mutations[0].Version != 7 { + t.Fatal("missing timestamp") + } + if _, err = BuildRemoveRecentSticker(nil, 123); err == nil { + t.Fatal("missing hash accepted") + } +} diff --git a/client.go b/client.go index 11cf3b5ea..08d9a6e52 100644 --- a/client.go +++ b/client.go @@ -129,7 +129,10 @@ type Client struct { EmitAppStateEventsOnFullSync bool EmitLabelEventsOnFullSync bool EmitQuickReplyEventsOnFullSync bool - AppStateDebugLogs bool + + // EmitStickerEventsOnFullSync emits favorite and recent-removal snapshot events. + EmitStickerEventsOnFullSync bool + AppStateDebugLogs bool AutomaticMessageRerequestFromPhone bool pendingPhoneRerequests map[types.MessageID]context.CancelFunc diff --git a/download.go b/download.go index 1122dc6d4..09387b84e 100644 --- a/download.go +++ b/download.go @@ -47,8 +47,9 @@ const ( MediaHistory MediaType = "WhatsApp History Keys" MediaAppState MediaType = "WhatsApp App State Keys" - MediaStickerPack MediaType = "WhatsApp Sticker Pack Keys" - MediaLinkThumbnail MediaType = "WhatsApp Link Thumbnail Keys" + MediaStickerPackThumbnail MediaType = "WhatsApp Sticker Pack Thumbnail Keys" + MediaStickerPack MediaType = "WhatsApp Sticker Pack Keys" + MediaLinkThumbnail MediaType = "WhatsApp Link Thumbnail Keys" ) // DownloadableMessage represents a protobuf message that contains attachment info. @@ -89,6 +90,7 @@ var ( _ DownloadableMessage = (*waE2E.HistorySyncNotification)(nil) _ DownloadableMessage = (*waServerSync.ExternalBlobReference)(nil) _ DownloadableThumbnail = (*waE2E.ExtendedTextMessage)(nil) + _ DownloadableThumbnail = (*waE2E.StickerPackMessage)(nil) _ DownloadableMessage = (*types.StickerPackItem)(nil) ) @@ -107,6 +109,7 @@ var classToMediaType = map[protoreflect.Name]MediaType{ var classToThumbnailMediaType = map[protoreflect.Name]MediaType{ "ExtendedTextMessage": MediaLinkThumbnail, + "StickerPackMessage": MediaStickerPackThumbnail, } var mediaTypeToMMSType = map[MediaType]string{ @@ -117,8 +120,9 @@ var mediaTypeToMMSType = map[MediaType]string{ MediaHistory: "md-msg-hist", MediaAppState: "md-app-state", - MediaStickerPack: "sticker-pack", - MediaLinkThumbnail: "thumbnail-link", + MediaStickerPack: "sticker-pack", + MediaLinkThumbnail: "thumbnail-link", + MediaStickerPackThumbnail: "thumbnail-sticker-pack", } // DownloadAny loops through the downloadable parts of the given message and downloads the first non-nil item. @@ -139,6 +143,8 @@ func (cli *Client) DownloadAny(ctx context.Context, msg *waE2E.Message) (data [] return cli.Download(ctx, msg.DocumentMessage) case msg.StickerMessage != nil: return cli.Download(ctx, msg.StickerMessage) + case msg.StickerPackMessage != nil: + return cli.Download(ctx, msg.StickerPackMessage) default: return nil, ErrNothingDownloadableFound } diff --git a/message.go b/message.go index 53a93d9b1..1cd8461dc 100644 --- a/message.go +++ b/message.go @@ -1044,9 +1044,10 @@ func (cli *Client) handleProtocolMessage(ctx context.Context, info *types.Messag cli.storeLIDSyncMessage(ctx, protoMsg.GetLidMigrationMappingSyncMessage().GetEncodedMappingPayload()) } - if info.Sender.Device == 0 { - peerResp := protoMsg.GetPeerDataOperationRequestResponseMessage() + if peerResp := protoMsg.GetPeerDataOperationRequestResponseMessage(); info.Sender.Device == 0 && peerResp != nil { switch peerResp.GetPeerDataOperationRequestType() { + case waE2E.PeerDataOperationRequestType_UPLOAD_STICKER: + ok = !cli.dispatchEvent(&events.StickerReupload{RequestID: peerResp.GetStanzaID(), Results: peerResp.GetPeerDataOperationResult()}) && ok case waE2E.PeerDataOperationRequestType_PLACEHOLDER_MESSAGE_RESEND: ok = cli.handlePlaceholderResendResponse(peerResp) && ok case waE2E.PeerDataOperationRequestType_COMPANION_SYNCD_SNAPSHOT_FATAL_RECOVERY: diff --git a/send.go b/send.go index 76a3aaaba..7eba22e43 100644 --- a/send.go +++ b/send.go @@ -983,6 +983,8 @@ func getMediaTypeFromMessage(msg *waE2E.Message) string { return "image" case msg.StickerMessage != nil: return "sticker" + case msg.StickerPackMessage != nil: + return "sticker_pack" case msg.DocumentMessage != nil: return "document" case msg.AudioMessage != nil: diff --git a/sticker.go b/sticker.go new file mode 100644 index 000000000..3a45023b9 --- /dev/null +++ b/sticker.go @@ -0,0 +1,37 @@ +package whatsmeow + +import ( + "encoding/base64" + "fmt" + + "github.com/polymorfa/hypermeow/proto/waE2E" + "google.golang.org/protobuf/proto" +) + +// BuildStickerReuploadRequest requests fresh media details from the primary device. +// Send it with SendPeerMessage. Acceptance of the request does not mean the phone +// found or uploaded the stickers; results arrive in events.StickerReupload. +func (cli *Client) BuildStickerReuploadRequest(hashes [][]byte) (*waE2E.Message, error) { + if len(hashes) == 0 { + return nil, fmt.Errorf("at least one sticker hash is required") + } + req := &waE2E.PeerDataOperationRequestMessage{PeerDataOperationRequestType: waE2E.PeerDataOperationRequestType_UPLOAD_STICKER.Enum()} + seen := make(map[string]bool) + for _, hash := range hashes { + if len(hash) != 32 { + return nil, fmt.Errorf("sticker hashes must contain 32 bytes") + } + key := base64.StdEncoding.EncodeToString(hash) + if !seen[key] { + req.RequestStickerReupload = append(req.RequestStickerReupload, &waE2E.PeerDataOperationRequestMessage_RequestStickerReupload{FileSHA256: proto.String(key)}) + seen[key] = true + } + } + return &waE2E.Message{ProtocolMessage: &waE2E.ProtocolMessage{Type: waE2E.ProtocolMessage_PEER_DATA_OPERATION_REQUEST_MESSAGE.Enum(), PeerDataOperationRequestMessage: req}}, nil +} + +// BuildRecentStickerBootstrapRequest requests recents in a later history sync. +// Send with SendPeerMessage. The primary device must be available to respond. +func (cli *Client) BuildRecentStickerBootstrapRequest() *waE2E.Message { + return &waE2E.Message{ProtocolMessage: &waE2E.ProtocolMessage{Type: waE2E.ProtocolMessage_PEER_DATA_OPERATION_REQUEST_MESSAGE.Enum(), PeerDataOperationRequestMessage: &waE2E.PeerDataOperationRequestMessage{PeerDataOperationRequestType: waE2E.PeerDataOperationRequestType_SEND_RECENT_STICKER_BOOTSTRAP.Enum()}}} +} diff --git a/sticker_test.go b/sticker_test.go new file mode 100644 index 000000000..c28371e5c --- /dev/null +++ b/sticker_test.go @@ -0,0 +1,75 @@ +package whatsmeow + +import ( + "bytes" + "context" + "github.com/polymorfa/hypermeow/appstate" + "github.com/polymorfa/hypermeow/proto/waE2E" + "github.com/polymorfa/hypermeow/proto/waServerSync" + "github.com/polymorfa/hypermeow/proto/waSyncAction" + "github.com/polymorfa/hypermeow/types/events" + "google.golang.org/protobuf/proto" + "testing" +) + +func TestStickerPackMedia(t *testing.T) { + // whatsapp-1053201649: WAWebBackendJobsCommon.js:105 and WAWebCryptoMediaTypeInfo.js:14-15. + msg := &waE2E.Message{StickerPackMessage: &waE2E.StickerPackMessage{}} + if getMediaTypeFromMessage(msg) != "sticker_pack" { + t.Fatal("missing pack media type") + } + if getMediaTypeFromMessage(&waE2E.Message{EphemeralMessage: &waE2E.FutureProofMessage{Message: msg}}) != "sticker_pack" { + t.Fatal("wrapper loses media type") + } + if classToThumbnailMediaType["StickerPackMessage"] != MediaStickerPackThumbnail || mediaTypeToMMSType[MediaStickerPackThumbnail] != "thumbnail-sticker-pack" { + t.Fatal("wrong thumbnail keys/path") + } +} +func TestStickerPeerRequests(t *testing.T) { + cli := &Client{} + h := bytes.Repeat([]byte{1}, 32) + m, err := cli.BuildStickerReuploadRequest([][]byte{h, h}) + if err != nil { + t.Fatal(err) + } + req := m.GetProtocolMessage().GetPeerDataOperationRequestMessage() + if req.GetPeerDataOperationRequestType() != waE2E.PeerDataOperationRequestType_UPLOAD_STICKER || len(req.GetRequestStickerReupload()) != 1 { + t.Fatal("wrong request") + } + if _, err = cli.BuildStickerReuploadRequest([][]byte{{1}}); err == nil { + t.Fatal("bad hash accepted") + } + if cli.BuildRecentStickerBootstrapRequest().GetProtocolMessage().GetPeerDataOperationRequestMessage().GetPeerDataOperationRequestType() != waE2E.PeerDataOperationRequestType_SEND_RECENT_STICKER_BOOTSTRAP { + t.Fatal("wrong bootstrap") + } +} +func TestStickerAppStateEvents(t *testing.T) { + cli := &Client{} + m := appstate.Mutation{Operation: waServerSync.SyncdMutation_SET, Index: []string{appstate.IndexFavoriteSticker, "hash"}, Action: &waSyncAction.SyncActionValue{StickerAction: &waSyncAction.StickerAction{IsFavorite: proto.Bool(false)}}} + e, ok := cli.dispatchAppState(context.Background(), appstate.WAPatchRegularLow, m, true).(*events.FavoriteSticker) + if !ok || e.FileSHA256 != "hash" || !e.FromFullSync || e.Action.GetIsFavorite() { + t.Fatal("wrong favorite event") + } + m.Action = &waSyncAction.SyncActionValue{} + if cli.dispatchAppState(context.Background(), appstate.WAPatchRegularLow, m, true) != nil { + t.Fatal("malformed action emitted") + } + m.Index = []string{appstate.IndexRemoveRecentSticker, "hash"} + m.Action.RemoveRecentStickerAction = &waSyncAction.RemoveRecentStickerAction{LastStickerSentTS: proto.Int64(123)} + r, ok := cli.dispatchAppState(context.Background(), appstate.WAPatchRegularLow, m, false).(*events.RemoveRecentSticker) + if !ok || r.Action.GetLastStickerSentTS() != 123 { + t.Fatal("wrong removal") + } +} + +func TestStickerFullSyncSelection(t *testing.T) { + cli := &Client{EmitStickerEventsOnFullSync: true} + for _, index := range []string{appstate.IndexFavoriteSticker, appstate.IndexRemoveRecentSticker} { + if !cli.shouldEmitFullSyncMutation([]string{index, "hash"}) { + t.Fatal("sticker snapshot suppressed") + } + } + if cli.shouldEmitFullSyncMutation([]string{appstate.IndexMute}) { + t.Fatal("unrelated snapshot emitted") + } +} diff --git a/stickerpack/archive.go b/stickerpack/archive.go new file mode 100644 index 000000000..545025dc8 --- /dev/null +++ b/stickerpack/archive.go @@ -0,0 +1,183 @@ +// Package stickerpack reads and builds sticker-pack message ZIP media. +// Metadata is carried in StickerPackMessage, not in a manifest inside the ZIP. +// Evidence: whatsapp-1053201649 WAWebStickerPackUtils.js:13-40. Phone acceptance +// of newly authored archives must be verified separately from this file format. +package stickerpack + +import ( + "archive/zip" + "bytes" + "fmt" + "io" + "io/fs" + "strings" + + "github.com/polymorfa/hypermeow/proto/waE2E" + "google.golang.org/protobuf/proto" +) + +// Limits bound untrusted archives. These are library safety limits, not claimed +// WhatsApp service limits. Zero values use DefaultLimits. +type Limits struct { + MaxEntries int + MaxFileBytes, MaxTotalBytes int64 +} + +var DefaultLimits = Limits{MaxEntries: 256, MaxFileBytes: 2 << 20, MaxTotalBytes: 64 << 20} + +func (l Limits) normalized() (Limits, error) { + if l.MaxEntries == 0 { + l.MaxEntries = DefaultLimits.MaxEntries + } + if l.MaxFileBytes == 0 { + l.MaxFileBytes = DefaultLimits.MaxFileBytes + } + if l.MaxTotalBytes == 0 { + l.MaxTotalBytes = DefaultLimits.MaxTotalBytes + } + if l.MaxEntries < 1 || l.MaxFileBytes < 1 || l.MaxTotalBytes < 1 { + return l, fmt.Errorf("archive limits must be positive") + } + return l, nil +} + +// File pairs a message's item metadata with the corresponding unencrypted bytes. +type File struct { + Metadata *waE2E.StickerPackMessage_Sticker + Data []byte +} + +func validName(name string) bool { + return fs.ValidPath(name) && name != "." && !strings.ContainsAny(name, "\\:\x00") +} + +// Build creates a ZIP containing the sticker files and optional tray icon. +// The returned item metadata is cloned and uses the supplied file names. +func Build(files []File, trayName string, tray []byte, limits Limits) ([]byte, []*waE2E.StickerPackMessage_Sticker, error) { + l, err := limits.normalized() + if err != nil { + return nil, nil, err + } + if len(files) == 0 { + return nil, nil, fmt.Errorf("pack contains no stickers") + } + count := len(files) + if trayName != "" { + count++ + } + if count > l.MaxEntries || (trayName == "") != (len(tray) == 0) { + return nil, nil, fmt.Errorf("invalid pack entry count or tray icon") + } + var buf bytes.Buffer + w := zip.NewWriter(&buf) + seen := map[string]bool{} + var total int64 + add := func(name string, data []byte) error { + if !validName(name) || seen[name] { + return fmt.Errorf("invalid or duplicate archive name") + } + if len(data) == 0 || int64(len(data)) > l.MaxFileBytes || int64(len(data)) > l.MaxTotalBytes-total { + return fmt.Errorf("pack data exceeds limits or is empty") + } + seen[name] = true + total += int64(len(data)) + entry, err := w.CreateHeader(&zip.FileHeader{Name: name, Method: zip.Store}) + if err != nil { + return err + } + _, err = entry.Write(data) + return err + } + metadata := make([]*waE2E.StickerPackMessage_Sticker, 0, len(files)) + for _, file := range files { + if file.Metadata == nil { + return nil, nil, fmt.Errorf("sticker metadata is required") + } + if err = add(file.Metadata.GetFileName(), file.Data); err != nil { + return nil, nil, err + } + metadata = append(metadata, proto.Clone(file.Metadata).(*waE2E.StickerPackMessage_Sticker)) + } + if trayName != "" { + if err = add(trayName, tray); err != nil { + return nil, nil, err + } + } + if err = w.Close(); err != nil { + return nil, nil, err + } + return buf.Bytes(), metadata, nil +} + +// Parse returns listed stickers in message order, skipping the tray icon. +// It never writes to disk. Every entry is validated, including unlisted files. +func Parse(data []byte, metadata []*waE2E.StickerPackMessage_Sticker, trayName string, limits Limits) ([]File, error) { + l, err := limits.normalized() + if err != nil { + return nil, err + } + if len(metadata) == 0 || len(metadata) > l.MaxEntries || int64(len(data)) > l.MaxTotalBytes+int64(l.MaxEntries)*1024 { + return nil, fmt.Errorf("invalid pack size or sticker count") + } + r, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) + if err != nil { + return nil, fmt.Errorf("invalid pack ZIP: %w", err) + } + if len(r.File) > l.MaxEntries { + return nil, fmt.Errorf("pack contains too many entries") + } + entries := make(map[string][]byte, len(r.File)) + seen := map[string]bool{} + var total int64 + for _, file := range r.File { + name := strings.TrimSuffix(file.Name, "/") + if !validName(name) || seen[name] || file.Mode()&fs.ModeSymlink != 0 { + return nil, fmt.Errorf("invalid or duplicate archive entry") + } + seen[name] = true + if file.FileInfo().IsDir() { + continue + } + if file.UncompressedSize64 > uint64(l.MaxFileBytes) || file.UncompressedSize64 > uint64(l.MaxTotalBytes-total) { + return nil, fmt.Errorf("pack exceeds decompressed size limits") + } + reader, err := file.Open() + if err != nil { + return nil, err + } + content, readErr := io.ReadAll(io.LimitReader(reader, min(l.MaxFileBytes, l.MaxTotalBytes-total)+1)) + closeErr := reader.Close() + if readErr != nil { + return nil, fmt.Errorf("invalid pack entry: %w", readErr) + } + if closeErr != nil { + return nil, closeErr + } + if len(content) == 0 || int64(len(content)) > l.MaxFileBytes || int64(len(content)) > l.MaxTotalBytes-total { + return nil, fmt.Errorf("pack exceeds size limits or contains empty files") + } + total += int64(len(content)) + entries[file.Name] = content + } + out := make([]File, 0, len(metadata)) + listed := map[string]bool{} + for _, item := range metadata { + if item == nil || !validName(item.GetFileName()) || listed[item.GetFileName()] { + return nil, fmt.Errorf("invalid or duplicate sticker metadata") + } + name := item.GetFileName() + listed[name] = true + if name == trayName { + continue + } + content, ok := entries[name] + if !ok { + return nil, fmt.Errorf("sticker file is missing from archive") + } + out = append(out, File{Metadata: proto.Clone(item).(*waE2E.StickerPackMessage_Sticker), Data: content}) + } + if len(out) == 0 { + return nil, fmt.Errorf("pack contains no sticker files") + } + return out, nil +} diff --git a/stickerpack/archive_test.go b/stickerpack/archive_test.go new file mode 100644 index 000000000..2d2fbb100 --- /dev/null +++ b/stickerpack/archive_test.go @@ -0,0 +1,55 @@ +package stickerpack + +import ( + "archive/zip" + "bytes" + "github.com/polymorfa/hypermeow/proto/waE2E" + "google.golang.org/protobuf/proto" + "testing" +) + +func TestRoundTrip(t *testing.T) { + files := []File{{Metadata: &waE2E.StickerPackMessage_Sticker{FileName: proto.String("b.webp"), Emojis: []string{"cat"}}, Data: []byte("second")}, {Metadata: &waE2E.StickerPackMessage_Sticker{FileName: proto.String("a.webp")}, Data: []byte("first")}} + data, meta, err := Build(files, "tray.png", []byte("tray"), Limits{}) + if err != nil { + t.Fatal(err) + } + got, err := Parse(data, meta, "tray.png", Limits{}) + if err != nil { + t.Fatal(err) + } + if len(got) != 2 || got[0].Metadata.GetFileName() != "b.webp" || string(got[1].Data) != "first" { + t.Fatalf("wrong entries: %+v", got) + } + got[0].Metadata.Emojis[0] = "changed" + if meta[0].Emojis[0] != "cat" { + t.Fatal("metadata alias") + } + if _, err = Parse(data, meta, "tray.png", Limits{MaxFileBytes: 3}); err == nil { + t.Fatal("accepted oversized entry") + } +} +func TestMalformedArchives(t *testing.T) { + for _, names := range [][]string{{"../a.webp"}, {"/a.webp"}, {"a.webp", "a.webp"}, {"a\\b.webp"}, {"other.webp"}} { + var b bytes.Buffer + w := zip.NewWriter(&b) + for _, name := range names { + f, _ := w.Create(name) + f.Write([]byte("content")) + } + w.Close() + if _, err := Parse(b.Bytes(), []*waE2E.StickerPackMessage_Sticker{{FileName: proto.String("a.webp")}}, "", Limits{}); err == nil { + t.Fatalf("accepted %v", names) + } + } + data, meta, err := Build([]File{{Metadata: &waE2E.StickerPackMessage_Sticker{FileName: proto.String("a.webp")}, Data: []byte("test")}}, "", nil, Limits{}) + if err != nil { + t.Fatal(err) + } + if _, err = Parse(data, append(meta, meta...), "", Limits{}); err == nil { + t.Fatal("duplicate metadata accepted") + } + if _, err = Parse(data[:10], meta, "", Limits{}); err == nil { + t.Fatal("truncated ZIP accepted") + } +} diff --git a/types/events/appstate.go b/types/events/appstate.go index b345067d1..3b2ceabc3 100644 --- a/types/events/appstate.go +++ b/types/events/appstate.go @@ -212,3 +212,21 @@ type AppStateSyncError struct { Error error FullSync bool } + +// FavoriteSticker is emitted for a favorite/unfavorite mutation, including full snapshots. +// FileSHA256 is the base64-encoded plaintext sticker hash, not a chat JID. +type FavoriteSticker struct { + FileSHA256 string + Timestamp time.Time + Action *waSyncAction.StickerAction + FromFullSync bool +} + +// RemoveRecentSticker removes only recents no newer than Action.LastStickerSentTS. +// This does not delete the sticker from favorites. +type RemoveRecentSticker struct { + FileSHA256 string + Timestamp time.Time + Action *waSyncAction.RemoveRecentStickerAction + FromFullSync bool +} diff --git a/types/events/sticker.go b/types/events/sticker.go new file mode 100644 index 000000000..556c9be47 --- /dev/null +++ b/types/events/sticker.go @@ -0,0 +1,11 @@ +package events + +import "github.com/polymorfa/hypermeow/proto/waE2E" + +// StickerReupload contains every primary-device result, including NOT_FOUND and +// errors. Consumers must inspect MediaUploadResult before using StickerMessage. +// RequestID correlates this asynchronous response with SendPeerMessage's ID. +type StickerReupload struct { + RequestID string + Results []*waE2E.PeerDataOperationRequestResponseMessage_PeerDataOperationResult +} From 38a4337e53f3033a652790479008c08c41c95bbf Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Tue, 10 Nov 2026 22:31:56 +0200 Subject: [PATCH 2/7] Upload stickers on their MMS path with image keys --- upload.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/upload.go b/upload.go index 0b4ab6a72..a2313354f 100644 --- a/upload.go +++ b/upload.go @@ -67,7 +67,18 @@ type UploadResponse struct { // // handle error again // // The same applies to the other message types like DocumentMessage, just replace the struct type and Message field name. -func (cli *Client) Upload(ctx context.Context, plaintext []byte, appInfo MediaType) (resp UploadResponse, err error) { +func (cli *Client) Upload(ctx context.Context, plaintext []byte, appInfo MediaType) (UploadResponse, error) { + return cli.upload(ctx, plaintext, appInfo, "") +} + +// UploadSticker uploads a sticker using image encryption keys and the sticker MMS path. +// Evidence: WAWebCryptoMediaTypeInfo.js:11 and WAWebMmsMediaTypes.js:17, +// whatsapp-1053201649. The caller validates/converts the sticker before uploading. +func (cli *Client) UploadSticker(ctx context.Context, plaintext []byte) (UploadResponse, error) { + return cli.upload(ctx, plaintext, MediaImage, "sticker") +} + +func (cli *Client) upload(ctx context.Context, plaintext []byte, appInfo MediaType, mmsType string) (resp UploadResponse, err error) { resp.FileLength = uint64(len(plaintext)) resp.MediaKey = random.Bytes(32) @@ -91,7 +102,7 @@ func (cli *Client) Upload(ctx context.Context, plaintext []byte, appInfo MediaTy dataHash := sha256.Sum256(dataToUpload) resp.FileEncSHA256 = dataHash[:] - err = cli.rawUpload(ctx, bytes.NewReader(dataToUpload), uint64(len(dataToUpload)), resp.FileEncSHA256, appInfo, false, &resp) + err = cli.rawUpload(ctx, bytes.NewReader(dataToUpload), uint64(len(dataToUpload)), resp.FileEncSHA256, appInfo, false, &resp, mmsType) return } @@ -200,7 +211,7 @@ func (cli *Client) UploadNewsletterReader(ctx context.Context, data io.ReadSeeke return } -func (cli *Client) rawUpload(ctx context.Context, dataToUpload io.Reader, uploadSize uint64, fileHash []byte, appInfo MediaType, newsletter bool, resp *UploadResponse) error { +func (cli *Client) rawUpload(ctx context.Context, dataToUpload io.Reader, uploadSize uint64, fileHash []byte, appInfo MediaType, newsletter bool, resp *UploadResponse, mmsTypeOverride ...string) error { mediaConn, err := cli.refreshMediaConn(ctx, false) if err != nil { return fmt.Errorf("failed to refresh media connections: %w", err) @@ -215,6 +226,9 @@ func (cli *Client) rawUpload(ctx context.Context, dataToUpload io.Reader, upload "token": []string{token}, } mmsType := mediaTypeToMMSType[appInfo] + if len(mmsTypeOverride) > 0 && mmsTypeOverride[0] != "" { + mmsType = mmsTypeOverride[0] + } uploadPrefix := "mms" if cli.MessengerConfig != nil { uploadPrefix = "wa-msgr/mms" From 8c420139c065ffb07a8fccc1bb16d64da1b33cd3 Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Tue, 10 Nov 2026 22:41:15 +0200 Subject: [PATCH 3/7] Upload pack thumbnails under the parent master key --- upload.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/upload.go b/upload.go index a2313354f..ca686d552 100644 --- a/upload.go +++ b/upload.go @@ -68,19 +68,31 @@ type UploadResponse struct { // // The same applies to the other message types like DocumentMessage, just replace the struct type and Message field name. func (cli *Client) Upload(ctx context.Context, plaintext []byte, appInfo MediaType) (UploadResponse, error) { - return cli.upload(ctx, plaintext, appInfo, "") + return cli.upload(ctx, plaintext, appInfo, "", nil) } // UploadSticker uploads a sticker using image encryption keys and the sticker MMS path. // Evidence: WAWebCryptoMediaTypeInfo.js:11 and WAWebMmsMediaTypes.js:17, // whatsapp-1053201649. The caller validates/converts the sticker before uploading. func (cli *Client) UploadSticker(ctx context.Context, plaintext []byte) (UploadResponse, error) { - return cli.upload(ctx, plaintext, MediaImage, "sticker") + return cli.upload(ctx, plaintext, MediaImage, "sticker", nil) } -func (cli *Client) upload(ctx context.Context, plaintext []byte, appInfo MediaType, mmsType string) (resp UploadResponse, err error) { +// UploadStickerPackThumbnail uses the parent pack's master media key with the +// distinct thumbnail key domain. StickerPackMessage has no second media key. +func (cli *Client) UploadStickerPackThumbnail(ctx context.Context, plaintext, packMediaKey []byte) (UploadResponse, error) { + if len(packMediaKey) != 32 { + return UploadResponse{}, fmt.Errorf("pack media key must contain 32 bytes") + } + return cli.upload(ctx, plaintext, MediaStickerPackThumbnail, "", packMediaKey) +} + +func (cli *Client) upload(ctx context.Context, plaintext []byte, appInfo MediaType, mmsType string, mediaKey []byte) (resp UploadResponse, err error) { resp.FileLength = uint64(len(plaintext)) - resp.MediaKey = random.Bytes(32) + resp.MediaKey = bytes.Clone(mediaKey) + if len(resp.MediaKey) == 0 { + resp.MediaKey = random.Bytes(32) + } plaintextSHA256 := sha256.Sum256(plaintext) resp.FileSHA256 = plaintextSHA256[:] From 114743da13475be4ec22ac26e05dfe7df5eb67b4 Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Wed, 11 Nov 2026 16:07:14 +0200 Subject: [PATCH 4/7] Preserve sticker result handler failures through decryption --- appstate/sticker.go | 3 +- appstate/sticker_test.go | 6 ++- message.go | 26 +++++++-- proto/waE2E/WAWebProtobufsE2E.pb.go | 82 ++++++++++++++--------------- sticker.go | 3 +- sticker_delivery_test.go | 48 +++++++++++++++++ sticker_test.go | 6 ++- stickerpack/archive.go | 3 +- stickerpack/archive_test.go | 6 ++- 9 files changed, 129 insertions(+), 54 deletions(-) create mode 100644 sticker_delivery_test.go diff --git a/appstate/sticker.go b/appstate/sticker.go index 0607f5988..c29367655 100644 --- a/appstate/sticker.go +++ b/appstate/sticker.go @@ -4,8 +4,9 @@ import ( "encoding/base64" "fmt" - "github.com/polymorfa/hypermeow/proto/waSyncAction" "google.golang.org/protobuf/proto" + + "github.com/polymorfa/hypermeow/proto/waSyncAction" ) // BuildFavoriteSticker builds a regular_low v7 favorite/unfavorite mutation. diff --git a/appstate/sticker_test.go b/appstate/sticker_test.go index 05fd2473f..61b8b2445 100644 --- a/appstate/sticker_test.go +++ b/appstate/sticker_test.go @@ -2,9 +2,11 @@ package appstate import ( "bytes" - "github.com/polymorfa/hypermeow/proto/waSyncAction" - "google.golang.org/protobuf/proto" "testing" + + "google.golang.org/protobuf/proto" + + "github.com/polymorfa/hypermeow/proto/waSyncAction" ) func TestStickerPatches(t *testing.T) { diff --git a/message.go b/message.go index 1cd8461dc..b95a28e72 100644 --- a/message.go +++ b/message.go @@ -1016,6 +1016,13 @@ func (cli *Client) handlePlaceholderResendResponse(msg *waE2E.PeerDataOperationR } func (cli *Client) handleProtocolMessage(ctx context.Context, info *types.MessageInfo, msg *waE2E.Message) (ok bool) { + ok, _ = handleProtocolMessageResult(cli, ctx, info, msg) + return +} + +// Keep the legacy internal hook's bool result while the decrypt path carries +// handler failure independently from ordinary Message-event suppression. +func handleProtocolMessageResult(cli *Client, ctx context.Context, info *types.MessageInfo, msg *waE2E.Message) (ok, handlerFailed bool) { ok = true protoMsg := msg.GetProtocolMessage() @@ -1047,7 +1054,8 @@ func (cli *Client) handleProtocolMessage(ctx context.Context, info *types.Messag if peerResp := protoMsg.GetPeerDataOperationRequestResponseMessage(); info.Sender.Device == 0 && peerResp != nil { switch peerResp.GetPeerDataOperationRequestType() { case waE2E.PeerDataOperationRequestType_UPLOAD_STICKER: - ok = !cli.dispatchEvent(&events.StickerReupload{RequestID: peerResp.GetStanzaID(), Results: peerResp.GetPeerDataOperationResult()}) && ok + handlerFailed = cli.dispatchEvent(&events.StickerReupload{RequestID: peerResp.GetStanzaID(), Results: peerResp.GetPeerDataOperationResult()}) + ok = false case waE2E.PeerDataOperationRequestType_PLACEHOLDER_MESSAGE_RESEND: ok = cli.handlePlaceholderResendResponse(peerResp) && ok case waE2E.PeerDataOperationRequestType_COMPANION_SYNCD_SNAPSHOT_FATAL_RECOVERY: @@ -1059,7 +1067,7 @@ func (cli *Client) handleProtocolMessage(ctx context.Context, info *types.Messag go cli.handleAppStateSyncKeyShare(context.WithoutCancel(ctx), protoMsg.AppStateSyncKeyShare) } - if info.Category == "peer" { + if info.Category == "peer" && !handlerFailed { go func() { err := cli.SendProtocolMessageReceipt(ctx, info.ID, types.ReceiptTypePeerMsg) if err != nil { @@ -1087,6 +1095,11 @@ func (cli *Client) shouldStoreHistorySyncNonce() bool { } func (cli *Client) processProtocolParts(ctx context.Context, info *types.MessageInfo, msg *waE2E.Message) (ok bool) { + ok, _ = processProtocolPartsResult(cli, ctx, info, msg) + return +} + +func processProtocolPartsResult(cli *Client, ctx context.Context, info *types.MessageInfo, msg *waE2E.Message) (ok, handlerFailed bool) { ok = true cli.storeMessageSecret(ctx, info, msg) // Hopefully sender key distribution messages and protocol messages can't be inside ephemeral messages @@ -1107,7 +1120,9 @@ func (cli *Client) processProtocolParts(ctx context.Context, info *types.Message // N.B. Edits are protocol messages, but they're also wrapped inside EditedMessage, // which is only unwrapped after processProtocolParts, so this won't trigger for edits. if msg.GetProtocolMessage() != nil { - ok = cli.handleProtocolMessage(ctx, info, msg) && ok + protocolOK, failed := handleProtocolMessageResult(cli, ctx, info, msg) + ok = protocolOK && ok + handlerFailed = failed } return } @@ -1339,7 +1354,10 @@ func (cli *Client) storeHistoricalPNLIDMappings(ctx context.Context, mappings [] } func (cli *Client) handleDecryptedMessage(ctx context.Context, info *types.MessageInfo, msg *waE2E.Message, retryCount int) (handlerFailed bool) { - ok := cli.processProtocolParts(ctx, info, msg) + ok, failed := processProtocolPartsResult(cli, ctx, info, msg) + if failed { + return true + } if !ok { return false } diff --git a/proto/waE2E/WAWebProtobufsE2E.pb.go b/proto/waE2E/WAWebProtobufsE2E.pb.go index b7a608dd3..dbbf52265 100644 --- a/proto/waE2E/WAWebProtobufsE2E.pb.go +++ b/proto/waE2E/WAWebProtobufsE2E.pb.go @@ -27710,47 +27710,47 @@ var file_waE2E_WAWebProtobufsE2E_proto_goTypes = []any{ (*ContextInfo_PartiallySelectedContent)(nil), // 294: WAWebProtobufsE2E.ContextInfo.PartiallySelectedContent (*ContextInfo_FeatureEligibilities)(nil), // 295: WAWebProtobufsE2E.ContextInfo.FeatureEligibilities (*ContextInfo_QuestionReplyQuotedMessage)(nil), // 296: WAWebProtobufsE2E.ContextInfo.QuestionReplyQuotedMessage - (*ContextInfo_UTMInfo)(nil), // 297: WAWebProtobufsE2E.ContextInfo.UTMInfo - (*ContextInfo_BusinessMessageForwardInfo)(nil), // 298: WAWebProtobufsE2E.ContextInfo.BusinessMessageForwardInfo - (*ContextInfo_BusinessInteractionPills_UnauthenticatedBusinessMetadata)(nil), // 299: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.UnauthenticatedBusinessMetadata - (*ContextInfo_BusinessInteractionPills_SignedPayload)(nil), // 300: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.SignedPayload - (*ContextInfo_BusinessInteractionPills_Pill)(nil), // 301: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.Pill - (*ContextInfo_DataSharingContext_Parameters)(nil), // 302: WAWebProtobufsE2E.ContextInfo.DataSharingContext.Parameters - (*HydratedTemplateButton_HydratedURLButton)(nil), // 303: WAWebProtobufsE2E.HydratedTemplateButton.HydratedURLButton - (*HydratedTemplateButton_HydratedCallButton)(nil), // 304: WAWebProtobufsE2E.HydratedTemplateButton.HydratedCallButton - (*HydratedTemplateButton_HydratedQuickReplyButton)(nil), // 305: WAWebProtobufsE2E.HydratedTemplateButton.HydratedQuickReplyButton - (*PaymentBackground_MediaData)(nil), // 306: WAWebProtobufsE2E.PaymentBackground.MediaData - (*PollResultSnapshotMessage_PollVote)(nil), // 307: WAWebProtobufsE2E.PollResultSnapshotMessage.PollVote - (*PollCreationMessage_Option)(nil), // 308: WAWebProtobufsE2E.PollCreationMessage.Option - (*ProductMessage_ProductSnapshot)(nil), // 309: WAWebProtobufsE2E.ProductMessage.ProductSnapshot - (*ProductMessage_CatalogSnapshot)(nil), // 310: WAWebProtobufsE2E.ProductMessage.CatalogSnapshot - (*TemplateMessage_HydratedFourRowTemplate)(nil), // 311: WAWebProtobufsE2E.TemplateMessage.HydratedFourRowTemplate - (*TemplateMessage_FourRowTemplate)(nil), // 312: WAWebProtobufsE2E.TemplateMessage.FourRowTemplate - (*TemplateButton_CallButton)(nil), // 313: WAWebProtobufsE2E.TemplateButton.CallButton - (*TemplateButton_URLButton)(nil), // 314: WAWebProtobufsE2E.TemplateButton.URLButton - (*TemplateButton_QuickReplyButton)(nil), // 315: WAWebProtobufsE2E.TemplateButton.QuickReplyButton - (*UrlTrackingMap_UrlTrackingMapElement)(nil), // 316: WAWebProtobufsE2E.UrlTrackingMap.UrlTrackingMapElement - (*waCommon.MessageKey)(nil), // 317: WACommon.MessageKey - (*waAICommon.BotAgentMetadata)(nil), // 318: WAWebProtobufsAICommon.BotAgentMetadata - (*waAICommon.BotFeedbackMessage)(nil), // 319: WAWebProtobufsAICommon.BotFeedbackMessage - (*waCommon.LimitSharing)(nil), // 320: WACommon.LimitSharing - (*waAICommon.AIMediaCollectionMessage)(nil), // 321: WAWebProtobufsAICommon.AIMediaCollectionMessage - (*waAICommon.AIMetadataOperation)(nil), // 322: WAWebProtobufsAICommon.AIMetadataOperation - (*waServerSync.CoexStateSync)(nil), // 323: WAWebProtobufsServerSync.CoexStateSync - (*waCommon.ACP2Setting)(nil), // 324: WACommon.ACP2Setting - (*waAICommon.ForwardedAIBotMessageInfo)(nil), // 325: WAWebProtobufsAICommon.ForwardedAIBotMessageInfo - (*waStatusAttributions.StatusAttribution)(nil), // 326: WAStatusAttributions.StatusAttribution - (*waAICommon.BotMessageSharingInfo)(nil), // 327: WAWebProtobufsAICommon.BotMessageSharingInfo - (*waAICommon.AIProvenance)(nil), // 328: WAWebProtobufsAICommon.AIProvenance - (*waAICommon.BotMetadata)(nil), // 329: WAWebProtobufsAICommon.BotMetadata - (*waAea.NonE2EEAttestation)(nil), // 330: WAWebProtobufsAea.NonE2EEAttestation - (waAdv.ADVEncryptionType)(0), // 331: WAAdv.ADVEncryptionType - (waAICommonDeprecated.AIRichResponseMessageType)(0), // 332: WAAICommonDeprecated.AIRichResponseMessageType - (*waAICommonDeprecated.AIRichResponseSubMessage)(nil), // 333: WAAICommonDeprecated.AIRichResponseSubMessage - (*waAICommon.AIRichResponseUnifiedResponse)(nil), // 334: WAWebProtobufsAICommon.AIRichResponseUnifiedResponse - (waMmsRetry.MediaRetryNotification_ResultType)(0), // 335: WAMmsRetry.MediaRetryNotification.ResultType - (*waCompanionReg.DeviceProps_HistorySyncConfig)(nil), // 336: WACompanionReg.DeviceProps.HistorySyncConfig - (*waAICommon.BotSignatureVerificationMetadata)(nil), // 337: WAWebProtobufsAICommon.BotSignatureVerificationMetadata + (*ContextInfo_UTMInfo)(nil), // 297: WAWebProtobufsE2E.ContextInfo.UTMInfo + (*ContextInfo_BusinessMessageForwardInfo)(nil), // 298: WAWebProtobufsE2E.ContextInfo.BusinessMessageForwardInfo + (*ContextInfo_BusinessInteractionPills_UnauthenticatedBusinessMetadata)(nil), // 299: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.UnauthenticatedBusinessMetadata + (*ContextInfo_BusinessInteractionPills_SignedPayload)(nil), // 300: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.SignedPayload + (*ContextInfo_BusinessInteractionPills_Pill)(nil), // 301: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.Pill + (*ContextInfo_DataSharingContext_Parameters)(nil), // 302: WAWebProtobufsE2E.ContextInfo.DataSharingContext.Parameters + (*HydratedTemplateButton_HydratedURLButton)(nil), // 303: WAWebProtobufsE2E.HydratedTemplateButton.HydratedURLButton + (*HydratedTemplateButton_HydratedCallButton)(nil), // 304: WAWebProtobufsE2E.HydratedTemplateButton.HydratedCallButton + (*HydratedTemplateButton_HydratedQuickReplyButton)(nil), // 305: WAWebProtobufsE2E.HydratedTemplateButton.HydratedQuickReplyButton + (*PaymentBackground_MediaData)(nil), // 306: WAWebProtobufsE2E.PaymentBackground.MediaData + (*PollResultSnapshotMessage_PollVote)(nil), // 307: WAWebProtobufsE2E.PollResultSnapshotMessage.PollVote + (*PollCreationMessage_Option)(nil), // 308: WAWebProtobufsE2E.PollCreationMessage.Option + (*ProductMessage_ProductSnapshot)(nil), // 309: WAWebProtobufsE2E.ProductMessage.ProductSnapshot + (*ProductMessage_CatalogSnapshot)(nil), // 310: WAWebProtobufsE2E.ProductMessage.CatalogSnapshot + (*TemplateMessage_HydratedFourRowTemplate)(nil), // 311: WAWebProtobufsE2E.TemplateMessage.HydratedFourRowTemplate + (*TemplateMessage_FourRowTemplate)(nil), // 312: WAWebProtobufsE2E.TemplateMessage.FourRowTemplate + (*TemplateButton_CallButton)(nil), // 313: WAWebProtobufsE2E.TemplateButton.CallButton + (*TemplateButton_URLButton)(nil), // 314: WAWebProtobufsE2E.TemplateButton.URLButton + (*TemplateButton_QuickReplyButton)(nil), // 315: WAWebProtobufsE2E.TemplateButton.QuickReplyButton + (*UrlTrackingMap_UrlTrackingMapElement)(nil), // 316: WAWebProtobufsE2E.UrlTrackingMap.UrlTrackingMapElement + (*waCommon.MessageKey)(nil), // 317: WACommon.MessageKey + (*waAICommon.BotAgentMetadata)(nil), // 318: WAWebProtobufsAICommon.BotAgentMetadata + (*waAICommon.BotFeedbackMessage)(nil), // 319: WAWebProtobufsAICommon.BotFeedbackMessage + (*waCommon.LimitSharing)(nil), // 320: WACommon.LimitSharing + (*waAICommon.AIMediaCollectionMessage)(nil), // 321: WAWebProtobufsAICommon.AIMediaCollectionMessage + (*waAICommon.AIMetadataOperation)(nil), // 322: WAWebProtobufsAICommon.AIMetadataOperation + (*waServerSync.CoexStateSync)(nil), // 323: WAWebProtobufsServerSync.CoexStateSync + (*waCommon.ACP2Setting)(nil), // 324: WACommon.ACP2Setting + (*waAICommon.ForwardedAIBotMessageInfo)(nil), // 325: WAWebProtobufsAICommon.ForwardedAIBotMessageInfo + (*waStatusAttributions.StatusAttribution)(nil), // 326: WAStatusAttributions.StatusAttribution + (*waAICommon.BotMessageSharingInfo)(nil), // 327: WAWebProtobufsAICommon.BotMessageSharingInfo + (*waAICommon.AIProvenance)(nil), // 328: WAWebProtobufsAICommon.AIProvenance + (*waAICommon.BotMetadata)(nil), // 329: WAWebProtobufsAICommon.BotMetadata + (*waAea.NonE2EEAttestation)(nil), // 330: WAWebProtobufsAea.NonE2EEAttestation + (waAdv.ADVEncryptionType)(0), // 331: WAAdv.ADVEncryptionType + (waAICommonDeprecated.AIRichResponseMessageType)(0), // 332: WAAICommonDeprecated.AIRichResponseMessageType + (*waAICommonDeprecated.AIRichResponseSubMessage)(nil), // 333: WAAICommonDeprecated.AIRichResponseSubMessage + (*waAICommon.AIRichResponseUnifiedResponse)(nil), // 334: WAWebProtobufsAICommon.AIRichResponseUnifiedResponse + (waMmsRetry.MediaRetryNotification_ResultType)(0), // 335: WAMmsRetry.MediaRetryNotification.ResultType + (*waCompanionReg.DeviceProps_HistorySyncConfig)(nil), // 336: WACompanionReg.DeviceProps.HistorySyncConfig + (*waAICommon.BotSignatureVerificationMetadata)(nil), // 337: WAWebProtobufsAICommon.BotSignatureVerificationMetadata } var file_waE2E_WAWebProtobufsE2E_proto_depIdxs = []int32{ 8, // 0: WAWebProtobufsE2E.StatusLinkPreviewMetadata.style:type_name -> WAWebProtobufsE2E.StatusLinkPreviewMetadata.Style diff --git a/sticker.go b/sticker.go index 3a45023b9..e001d042a 100644 --- a/sticker.go +++ b/sticker.go @@ -4,8 +4,9 @@ import ( "encoding/base64" "fmt" - "github.com/polymorfa/hypermeow/proto/waE2E" "google.golang.org/protobuf/proto" + + "github.com/polymorfa/hypermeow/proto/waE2E" ) // BuildStickerReuploadRequest requests fresh media details from the primary device. diff --git a/sticker_delivery_test.go b/sticker_delivery_test.go new file mode 100644 index 000000000..c892ffc88 --- /dev/null +++ b/sticker_delivery_test.go @@ -0,0 +1,48 @@ +package whatsmeow + +import ( + "context" + "testing" + + "google.golang.org/protobuf/proto" + + "github.com/polymorfa/hypermeow/proto/waE2E" + "github.com/polymorfa/hypermeow/store" + "github.com/polymorfa/hypermeow/types" + "github.com/polymorfa/hypermeow/types/events" + waLog "github.com/polymorfa/hypermeow/util/log" +) + +func TestStickerResultPreservesHandlerFailureAndSuppressesMessage(t *testing.T) { + for _, wrapped := range []bool{false, true} { + for _, success := range []bool{false, true} { + cli := NewClient(&store.Device{}, waLog.Noop) + results, messages := 0, 0 + cli.AddEventHandlerWithSuccessStatus(func(event any) bool { + switch e := event.(type) { + case *events.StickerReupload: + results++ + if e.RequestID != "request" { + t.Errorf("request identity lost: %s", e.RequestID) + } + return success + case *events.Message: + messages++ + } + return true + }) + msg := &waE2E.Message{ProtocolMessage: &waE2E.ProtocolMessage{PeerDataOperationRequestResponseMessage: &waE2E.PeerDataOperationRequestResponseMessage{PeerDataOperationRequestType: waE2E.PeerDataOperationRequestType_UPLOAD_STICKER.Enum(), StanzaID: proto.String("request")}}} + if wrapped { + msg = &waE2E.Message{DeviceSentMessage: &waE2E.DeviceSentMessage{Message: msg}} + } + info := &types.MessageInfo{MessageSource: types.MessageSource{IsFromMe: true, Sender: types.JID{User: "fixture", Server: types.DefaultUserServer}}} + failed := cli.handleDecryptedMessage(context.Background(), info, msg, 0) + if failed == success { + t.Errorf("wrapped=%t success=%t: handler failure=%t", wrapped, success, failed) + } + if results != 1 || messages != 0 { + t.Errorf("wrapped=%t success=%t: results=%d ordinary messages=%d", wrapped, success, results, messages) + } + } + } +} diff --git a/sticker_test.go b/sticker_test.go index c28371e5c..92092f3e0 100644 --- a/sticker_test.go +++ b/sticker_test.go @@ -3,13 +3,15 @@ package whatsmeow import ( "bytes" "context" + "testing" + + "google.golang.org/protobuf/proto" + "github.com/polymorfa/hypermeow/appstate" "github.com/polymorfa/hypermeow/proto/waE2E" "github.com/polymorfa/hypermeow/proto/waServerSync" "github.com/polymorfa/hypermeow/proto/waSyncAction" "github.com/polymorfa/hypermeow/types/events" - "google.golang.org/protobuf/proto" - "testing" ) func TestStickerPackMedia(t *testing.T) { diff --git a/stickerpack/archive.go b/stickerpack/archive.go index 545025dc8..646c80c2c 100644 --- a/stickerpack/archive.go +++ b/stickerpack/archive.go @@ -12,8 +12,9 @@ import ( "io/fs" "strings" - "github.com/polymorfa/hypermeow/proto/waE2E" "google.golang.org/protobuf/proto" + + "github.com/polymorfa/hypermeow/proto/waE2E" ) // Limits bound untrusted archives. These are library safety limits, not claimed diff --git a/stickerpack/archive_test.go b/stickerpack/archive_test.go index 2d2fbb100..b63922259 100644 --- a/stickerpack/archive_test.go +++ b/stickerpack/archive_test.go @@ -3,9 +3,11 @@ package stickerpack import ( "archive/zip" "bytes" - "github.com/polymorfa/hypermeow/proto/waE2E" - "google.golang.org/protobuf/proto" "testing" + + "google.golang.org/protobuf/proto" + + "github.com/polymorfa/hypermeow/proto/waE2E" ) func TestRoundTrip(t *testing.T) { From d583a2df11a847a7a9bf745fa8500b09ae07ba0c Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Wed, 11 Nov 2026 17:19:05 +0200 Subject: [PATCH 5/7] style(proto): match CI goimports formatting --- proto/waE2E/WAWebProtobufsE2E.pb.go | 82 ++++++++++++++--------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/proto/waE2E/WAWebProtobufsE2E.pb.go b/proto/waE2E/WAWebProtobufsE2E.pb.go index dbbf52265..b7a608dd3 100644 --- a/proto/waE2E/WAWebProtobufsE2E.pb.go +++ b/proto/waE2E/WAWebProtobufsE2E.pb.go @@ -27710,47 +27710,47 @@ var file_waE2E_WAWebProtobufsE2E_proto_goTypes = []any{ (*ContextInfo_PartiallySelectedContent)(nil), // 294: WAWebProtobufsE2E.ContextInfo.PartiallySelectedContent (*ContextInfo_FeatureEligibilities)(nil), // 295: WAWebProtobufsE2E.ContextInfo.FeatureEligibilities (*ContextInfo_QuestionReplyQuotedMessage)(nil), // 296: WAWebProtobufsE2E.ContextInfo.QuestionReplyQuotedMessage - (*ContextInfo_UTMInfo)(nil), // 297: WAWebProtobufsE2E.ContextInfo.UTMInfo - (*ContextInfo_BusinessMessageForwardInfo)(nil), // 298: WAWebProtobufsE2E.ContextInfo.BusinessMessageForwardInfo - (*ContextInfo_BusinessInteractionPills_UnauthenticatedBusinessMetadata)(nil), // 299: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.UnauthenticatedBusinessMetadata - (*ContextInfo_BusinessInteractionPills_SignedPayload)(nil), // 300: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.SignedPayload - (*ContextInfo_BusinessInteractionPills_Pill)(nil), // 301: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.Pill - (*ContextInfo_DataSharingContext_Parameters)(nil), // 302: WAWebProtobufsE2E.ContextInfo.DataSharingContext.Parameters - (*HydratedTemplateButton_HydratedURLButton)(nil), // 303: WAWebProtobufsE2E.HydratedTemplateButton.HydratedURLButton - (*HydratedTemplateButton_HydratedCallButton)(nil), // 304: WAWebProtobufsE2E.HydratedTemplateButton.HydratedCallButton - (*HydratedTemplateButton_HydratedQuickReplyButton)(nil), // 305: WAWebProtobufsE2E.HydratedTemplateButton.HydratedQuickReplyButton - (*PaymentBackground_MediaData)(nil), // 306: WAWebProtobufsE2E.PaymentBackground.MediaData - (*PollResultSnapshotMessage_PollVote)(nil), // 307: WAWebProtobufsE2E.PollResultSnapshotMessage.PollVote - (*PollCreationMessage_Option)(nil), // 308: WAWebProtobufsE2E.PollCreationMessage.Option - (*ProductMessage_ProductSnapshot)(nil), // 309: WAWebProtobufsE2E.ProductMessage.ProductSnapshot - (*ProductMessage_CatalogSnapshot)(nil), // 310: WAWebProtobufsE2E.ProductMessage.CatalogSnapshot - (*TemplateMessage_HydratedFourRowTemplate)(nil), // 311: WAWebProtobufsE2E.TemplateMessage.HydratedFourRowTemplate - (*TemplateMessage_FourRowTemplate)(nil), // 312: WAWebProtobufsE2E.TemplateMessage.FourRowTemplate - (*TemplateButton_CallButton)(nil), // 313: WAWebProtobufsE2E.TemplateButton.CallButton - (*TemplateButton_URLButton)(nil), // 314: WAWebProtobufsE2E.TemplateButton.URLButton - (*TemplateButton_QuickReplyButton)(nil), // 315: WAWebProtobufsE2E.TemplateButton.QuickReplyButton - (*UrlTrackingMap_UrlTrackingMapElement)(nil), // 316: WAWebProtobufsE2E.UrlTrackingMap.UrlTrackingMapElement - (*waCommon.MessageKey)(nil), // 317: WACommon.MessageKey - (*waAICommon.BotAgentMetadata)(nil), // 318: WAWebProtobufsAICommon.BotAgentMetadata - (*waAICommon.BotFeedbackMessage)(nil), // 319: WAWebProtobufsAICommon.BotFeedbackMessage - (*waCommon.LimitSharing)(nil), // 320: WACommon.LimitSharing - (*waAICommon.AIMediaCollectionMessage)(nil), // 321: WAWebProtobufsAICommon.AIMediaCollectionMessage - (*waAICommon.AIMetadataOperation)(nil), // 322: WAWebProtobufsAICommon.AIMetadataOperation - (*waServerSync.CoexStateSync)(nil), // 323: WAWebProtobufsServerSync.CoexStateSync - (*waCommon.ACP2Setting)(nil), // 324: WACommon.ACP2Setting - (*waAICommon.ForwardedAIBotMessageInfo)(nil), // 325: WAWebProtobufsAICommon.ForwardedAIBotMessageInfo - (*waStatusAttributions.StatusAttribution)(nil), // 326: WAStatusAttributions.StatusAttribution - (*waAICommon.BotMessageSharingInfo)(nil), // 327: WAWebProtobufsAICommon.BotMessageSharingInfo - (*waAICommon.AIProvenance)(nil), // 328: WAWebProtobufsAICommon.AIProvenance - (*waAICommon.BotMetadata)(nil), // 329: WAWebProtobufsAICommon.BotMetadata - (*waAea.NonE2EEAttestation)(nil), // 330: WAWebProtobufsAea.NonE2EEAttestation - (waAdv.ADVEncryptionType)(0), // 331: WAAdv.ADVEncryptionType - (waAICommonDeprecated.AIRichResponseMessageType)(0), // 332: WAAICommonDeprecated.AIRichResponseMessageType - (*waAICommonDeprecated.AIRichResponseSubMessage)(nil), // 333: WAAICommonDeprecated.AIRichResponseSubMessage - (*waAICommon.AIRichResponseUnifiedResponse)(nil), // 334: WAWebProtobufsAICommon.AIRichResponseUnifiedResponse - (waMmsRetry.MediaRetryNotification_ResultType)(0), // 335: WAMmsRetry.MediaRetryNotification.ResultType - (*waCompanionReg.DeviceProps_HistorySyncConfig)(nil), // 336: WACompanionReg.DeviceProps.HistorySyncConfig - (*waAICommon.BotSignatureVerificationMetadata)(nil), // 337: WAWebProtobufsAICommon.BotSignatureVerificationMetadata + (*ContextInfo_UTMInfo)(nil), // 297: WAWebProtobufsE2E.ContextInfo.UTMInfo + (*ContextInfo_BusinessMessageForwardInfo)(nil), // 298: WAWebProtobufsE2E.ContextInfo.BusinessMessageForwardInfo + (*ContextInfo_BusinessInteractionPills_UnauthenticatedBusinessMetadata)(nil), // 299: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.UnauthenticatedBusinessMetadata + (*ContextInfo_BusinessInteractionPills_SignedPayload)(nil), // 300: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.SignedPayload + (*ContextInfo_BusinessInteractionPills_Pill)(nil), // 301: WAWebProtobufsE2E.ContextInfo.BusinessInteractionPills.Pill + (*ContextInfo_DataSharingContext_Parameters)(nil), // 302: WAWebProtobufsE2E.ContextInfo.DataSharingContext.Parameters + (*HydratedTemplateButton_HydratedURLButton)(nil), // 303: WAWebProtobufsE2E.HydratedTemplateButton.HydratedURLButton + (*HydratedTemplateButton_HydratedCallButton)(nil), // 304: WAWebProtobufsE2E.HydratedTemplateButton.HydratedCallButton + (*HydratedTemplateButton_HydratedQuickReplyButton)(nil), // 305: WAWebProtobufsE2E.HydratedTemplateButton.HydratedQuickReplyButton + (*PaymentBackground_MediaData)(nil), // 306: WAWebProtobufsE2E.PaymentBackground.MediaData + (*PollResultSnapshotMessage_PollVote)(nil), // 307: WAWebProtobufsE2E.PollResultSnapshotMessage.PollVote + (*PollCreationMessage_Option)(nil), // 308: WAWebProtobufsE2E.PollCreationMessage.Option + (*ProductMessage_ProductSnapshot)(nil), // 309: WAWebProtobufsE2E.ProductMessage.ProductSnapshot + (*ProductMessage_CatalogSnapshot)(nil), // 310: WAWebProtobufsE2E.ProductMessage.CatalogSnapshot + (*TemplateMessage_HydratedFourRowTemplate)(nil), // 311: WAWebProtobufsE2E.TemplateMessage.HydratedFourRowTemplate + (*TemplateMessage_FourRowTemplate)(nil), // 312: WAWebProtobufsE2E.TemplateMessage.FourRowTemplate + (*TemplateButton_CallButton)(nil), // 313: WAWebProtobufsE2E.TemplateButton.CallButton + (*TemplateButton_URLButton)(nil), // 314: WAWebProtobufsE2E.TemplateButton.URLButton + (*TemplateButton_QuickReplyButton)(nil), // 315: WAWebProtobufsE2E.TemplateButton.QuickReplyButton + (*UrlTrackingMap_UrlTrackingMapElement)(nil), // 316: WAWebProtobufsE2E.UrlTrackingMap.UrlTrackingMapElement + (*waCommon.MessageKey)(nil), // 317: WACommon.MessageKey + (*waAICommon.BotAgentMetadata)(nil), // 318: WAWebProtobufsAICommon.BotAgentMetadata + (*waAICommon.BotFeedbackMessage)(nil), // 319: WAWebProtobufsAICommon.BotFeedbackMessage + (*waCommon.LimitSharing)(nil), // 320: WACommon.LimitSharing + (*waAICommon.AIMediaCollectionMessage)(nil), // 321: WAWebProtobufsAICommon.AIMediaCollectionMessage + (*waAICommon.AIMetadataOperation)(nil), // 322: WAWebProtobufsAICommon.AIMetadataOperation + (*waServerSync.CoexStateSync)(nil), // 323: WAWebProtobufsServerSync.CoexStateSync + (*waCommon.ACP2Setting)(nil), // 324: WACommon.ACP2Setting + (*waAICommon.ForwardedAIBotMessageInfo)(nil), // 325: WAWebProtobufsAICommon.ForwardedAIBotMessageInfo + (*waStatusAttributions.StatusAttribution)(nil), // 326: WAStatusAttributions.StatusAttribution + (*waAICommon.BotMessageSharingInfo)(nil), // 327: WAWebProtobufsAICommon.BotMessageSharingInfo + (*waAICommon.AIProvenance)(nil), // 328: WAWebProtobufsAICommon.AIProvenance + (*waAICommon.BotMetadata)(nil), // 329: WAWebProtobufsAICommon.BotMetadata + (*waAea.NonE2EEAttestation)(nil), // 330: WAWebProtobufsAea.NonE2EEAttestation + (waAdv.ADVEncryptionType)(0), // 331: WAAdv.ADVEncryptionType + (waAICommonDeprecated.AIRichResponseMessageType)(0), // 332: WAAICommonDeprecated.AIRichResponseMessageType + (*waAICommonDeprecated.AIRichResponseSubMessage)(nil), // 333: WAAICommonDeprecated.AIRichResponseSubMessage + (*waAICommon.AIRichResponseUnifiedResponse)(nil), // 334: WAWebProtobufsAICommon.AIRichResponseUnifiedResponse + (waMmsRetry.MediaRetryNotification_ResultType)(0), // 335: WAMmsRetry.MediaRetryNotification.ResultType + (*waCompanionReg.DeviceProps_HistorySyncConfig)(nil), // 336: WACompanionReg.DeviceProps.HistorySyncConfig + (*waAICommon.BotSignatureVerificationMetadata)(nil), // 337: WAWebProtobufsAICommon.BotSignatureVerificationMetadata } var file_waE2E_WAWebProtobufsE2E_proto_depIdxs = []int32{ 8, // 0: WAWebProtobufsE2E.StatusLinkPreviewMetadata.style:type_name -> WAWebProtobufsE2E.StatusLinkPreviewMetadata.Style From 5c0eb0db450c124122daa71c0588bdd5ff441427 Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Wed, 11 Nov 2026 17:37:34 +0200 Subject: [PATCH 6/7] fix(stickerpack): bound ZIP directory admission and serialized output --- stickerpack/archive.go | 32 +++++++- stickerpack/archive_test.go | 151 +++++++++++++++++++++++++++++++++++ stickerpack/zip_preflight.go | 93 +++++++++++++++++++++ 3 files changed, 272 insertions(+), 4 deletions(-) create mode 100644 stickerpack/zip_preflight.go diff --git a/stickerpack/archive.go b/stickerpack/archive.go index 646c80c2c..4e4c57951 100644 --- a/stickerpack/archive.go +++ b/stickerpack/archive.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "io/fs" + "math" "strings" "google.golang.org/protobuf/proto" @@ -18,7 +19,8 @@ import ( ) // Limits bound untrusted archives. These are library safety limits, not claimed -// WhatsApp service limits. Zero values use DefaultLimits. +// WhatsApp service limits. Zero values use DefaultLimits. The serialized ZIP +// also has a MaxTotalBytes + MaxEntries*1024 byte limit for headers and names. type Limits struct { MaxEntries int MaxFileBytes, MaxTotalBytes int64 @@ -52,6 +54,25 @@ func validName(name string) bool { return fs.ValidPath(name) && name != "." && !strings.ContainsAny(name, "\\:\x00") } +func archiveSizeLimit(l Limits) int64 { + if int64(l.MaxEntries) > (math.MaxInt64-l.MaxTotalBytes)/1024 { + return math.MaxInt64 + } + return l.MaxTotalBytes + int64(l.MaxEntries)*1024 +} + +type archiveBuffer struct { + data bytes.Buffer + limit int64 +} + +func (b *archiveBuffer) Write(data []byte) (int, error) { + if int64(b.data.Len()) > b.limit-int64(len(data)) { + return 0, fmt.Errorf("pack exceeds serialized size limit") + } + return b.data.Write(data) +} + // Build creates a ZIP containing the sticker files and optional tray icon. // The returned item metadata is cloned and uses the supplied file names. func Build(files []File, trayName string, tray []byte, limits Limits) ([]byte, []*waE2E.StickerPackMessage_Sticker, error) { @@ -69,7 +90,7 @@ func Build(files []File, trayName string, tray []byte, limits Limits) ([]byte, [ if count > l.MaxEntries || (trayName == "") != (len(tray) == 0) { return nil, nil, fmt.Errorf("invalid pack entry count or tray icon") } - var buf bytes.Buffer + buf := archiveBuffer{limit: archiveSizeLimit(l)} w := zip.NewWriter(&buf) seen := map[string]bool{} var total int64 @@ -107,7 +128,7 @@ func Build(files []File, trayName string, tray []byte, limits Limits) ([]byte, [ if err = w.Close(); err != nil { return nil, nil, err } - return buf.Bytes(), metadata, nil + return buf.data.Bytes(), metadata, nil } // Parse returns listed stickers in message order, skipping the tray icon. @@ -117,9 +138,12 @@ func Parse(data []byte, metadata []*waE2E.StickerPackMessage_Sticker, trayName s if err != nil { return nil, err } - if len(metadata) == 0 || len(metadata) > l.MaxEntries || int64(len(data)) > l.MaxTotalBytes+int64(l.MaxEntries)*1024 { + if len(metadata) == 0 || len(metadata) > l.MaxEntries || int64(len(data)) > archiveSizeLimit(l) { return nil, fmt.Errorf("invalid pack size or sticker count") } + if err := preflightDirectory(data, l.MaxEntries); err != nil { + return nil, fmt.Errorf("invalid pack ZIP: %w", err) + } r, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) if err != nil { return nil, fmt.Errorf("invalid pack ZIP: %w", err) diff --git a/stickerpack/archive_test.go b/stickerpack/archive_test.go index b63922259..ee07ca5d6 100644 --- a/stickerpack/archive_test.go +++ b/stickerpack/archive_test.go @@ -3,6 +3,9 @@ package stickerpack import ( "archive/zip" "bytes" + "encoding/binary" + "math" + "strings" "testing" "google.golang.org/protobuf/proto" @@ -55,3 +58,151 @@ func TestMalformedArchives(t *testing.T) { t.Fatal("truncated ZIP accepted") } } + +func TestBuildHonorsSerializedLimit(t *testing.T) { + limits := Limits{MaxEntries: 1, MaxFileBytes: 1, MaxTotalBytes: 1} + for _, length := range []int{455, 456, 1024} { + file := File{Metadata: &waE2E.StickerPackMessage_Sticker{FileName: proto.String(strings.Repeat("a", length))}, Data: []byte("x")} + data, metadata, err := Build([]File{file}, "", nil, limits) + if length > 455 { + if err == nil { + t.Fatalf("Build accepted %d-byte name producing %d serialized bytes", length, len(data)) + } + continue + } + if err != nil { + t.Fatal(err) + } + if len(data) != 1025 { + t.Fatalf("boundary fixture has %d serialized bytes", len(data)) + } + if _, err := Parse(data, metadata, "", limits); err != nil { + t.Fatalf("Build output did not parse at the serialized boundary: %v", err) + } + } +} + +// No local file bodies are needed: zip.NewReader allocates the central directory +// before Parse opens an entry or applies its own entry-count check. +func directoryFixture(records int, forgedSize bool) []byte { + data := make([]byte, records*47+22) + for offset := 0; offset < records*47; offset += 47 { + binary.LittleEndian.PutUint32(data[offset:], 0x02014b50) + binary.LittleEndian.PutUint16(data[offset+28:], 1) + data[offset+46] = 'a' + } + end := data[records*47:] + binary.LittleEndian.PutUint32(end, 0x06054b50) + binary.LittleEndian.PutUint16(end[8:], uint16(records)) + binary.LittleEndian.PutUint16(end[10:], uint16(records)) + binary.LittleEndian.PutUint32(end[12:], uint32(records*47)) + if forgedSize { + binary.LittleEndian.PutUint32(end[12:], 47) + } + return data +} + +func TestParseBoundsDirectoryAllocation(t *testing.T) { + for _, test := range []struct { + name string + records int + forgedSize bool + }{ + {"declared-over-limit", 4097, false}, + {"truncated-count", 65537, false}, + {"truncated-count-and-forged-size", 65537, true}, + } { + t.Run(test.name, func(t *testing.T) { + data := directoryFixture(test.records, test.forgedSize) + metadata := []*waE2E.StickerPackMessage_Sticker{{FileName: proto.String("a")}} + var err error + allocs := testing.AllocsPerRun(1, func() { + _, err = Parse(data, metadata, "", Limits{MaxEntries: 4}) + }) + if err == nil || !strings.Contains(err.Error(), "too many entries") { + t.Fatalf("entry limit not enforced: %v", err) + } + if allocs > 64 { + t.Fatalf("rejected directory of %d entries allocated %.0f objects", test.records, allocs) + } + t.Logf("rejected %d directory entries with %.0f allocations", test.records, allocs) + }) + } +} + +func zip64Fixture(data []byte) []byte { + end := len(data) - 22 + footer := bytes.Clone(data[end:]) + zip64 := make([]byte, 76) + binary.LittleEndian.PutUint32(zip64, 0x06064b50) + binary.LittleEndian.PutUint64(zip64[4:], 44) + binary.LittleEndian.PutUint16(zip64[12:], 45) + binary.LittleEndian.PutUint16(zip64[14:], 45) + binary.LittleEndian.PutUint64(zip64[24:], uint64(binary.LittleEndian.Uint16(footer[8:]))) + binary.LittleEndian.PutUint64(zip64[32:], uint64(binary.LittleEndian.Uint16(footer[10:]))) + binary.LittleEndian.PutUint64(zip64[40:], uint64(binary.LittleEndian.Uint32(footer[12:]))) + binary.LittleEndian.PutUint64(zip64[48:], uint64(binary.LittleEndian.Uint32(footer[16:]))) + binary.LittleEndian.PutUint32(zip64[56:], 0x07064b50) + binary.LittleEndian.PutUint64(zip64[64:], uint64(end)) + binary.LittleEndian.PutUint32(zip64[72:], 1) + binary.LittleEndian.PutUint16(footer[8:], 0xffff) + binary.LittleEndian.PutUint16(footer[10:], 0xffff) + binary.LittleEndian.PutUint32(footer[12:], 0xffffffff) + binary.LittleEndian.PutUint32(footer[16:], 0xffffffff) + return append(append(bytes.Clone(data[:end]), zip64...), footer...) +} + +func TestDirectoryPreflightPreservesZIPVariants(t *testing.T) { + data, metadata, err := Build([]File{{Metadata: &waE2E.StickerPackMessage_Sticker{FileName: proto.String("a.webp")}, Data: []byte("content")}}, "", nil, Limits{}) + if err != nil { + t.Fatal(err) + } + comment := bytes.Clone(data) + binary.LittleEndian.PutUint16(comment[len(comment)-2:], 7) + comment = append(comment, []byte("comment")...) + smallDirectory := bytes.Clone(data) + binary.LittleEndian.PutUint32(smallDirectory[len(data)-10:], 1) + for _, variant := range [][]byte{data, append([]byte("prefix"), data...), comment, smallDirectory, zip64Fixture(data)} { + got, err := Parse(variant, metadata, "", Limits{MaxEntries: 1}) + if err != nil || len(got) != 1 || string(got[0].Data) != "content" { + t.Fatalf("supported ZIP variant failed: %v", err) + } + } + for _, limits := range []Limits{ + {MaxEntries: math.MaxInt, MaxFileBytes: 1, MaxTotalBytes: 1}, + {MaxEntries: 1, MaxFileBytes: 1, MaxTotalBytes: math.MaxInt64}, + } { + data, metadata, err := Build([]File{{Metadata: &waE2E.StickerPackMessage_Sticker{FileName: proto.String("a")}, Data: []byte("x")}}, "", nil, limits) + if err != nil { + t.Fatal(err) + } + if _, err := Parse(data, metadata, "", limits); err != nil { + t.Fatalf("large caller limits overflowed: %v", err) + } + } + + badCount := zip64Fixture(data) + binary.LittleEndian.PutUint64(badCount[len(badCount)-98+32:], math.MaxUint64) + if _, err := Parse(badCount, metadata, "", Limits{}); err == nil || !strings.Contains(err.Error(), "too many entries") { + t.Fatalf("unbounded ZIP64 count accepted: %v", err) + } + badOffset := zip64Fixture(data) + binary.LittleEndian.PutUint64(badOffset[len(badOffset)-42+8:], math.MaxUint64) + if _, err := Parse(badOffset, metadata, "", Limits{}); err == nil { + t.Fatal("out-of-range ZIP64 locator accepted") + } +} + +func FuzzParse(f *testing.F) { + data, metadata, err := Build([]File{{Metadata: &waE2E.StickerPackMessage_Sticker{FileName: proto.String("a")}, Data: []byte("x")}}, "", nil, Limits{}) + if err != nil { + f.Fatal(err) + } + f.Add(data) + f.Add(zip64Fixture(data)) + f.Add([]byte{}) + f.Add(directoryFixture(5, true)) + f.Fuzz(func(t *testing.T, data []byte) { + _, _ = Parse(data, metadata, "", Limits{MaxEntries: 4, MaxFileBytes: 64, MaxTotalBytes: 256}) + }) +} diff --git a/stickerpack/zip_preflight.go b/stickerpack/zip_preflight.go new file mode 100644 index 000000000..b7c39dfc0 --- /dev/null +++ b/stickerpack/zip_preflight.go @@ -0,0 +1,93 @@ +package stickerpack + +import ( + "archive/zip" + "encoding/binary" + "errors" + "math" +) + +var errTooManyEntries = errors.New("pack contains too many entries") + +type directoryInfo struct { + end int + records, size, offset uint64 +} + +// preflightDirectory bounds both zip.NewReader's declared-count preallocation +// and its actual directory scan. ZIP counts can wrap at 65536, so neither the +// declared count nor the declared directory size alone bounds that scan. +func preflightDirectory(data []byte, maxEntries int) error { + d, err := findDirectory(data) + if err != nil { + return err + } + if d.records > uint64(maxEntries) { + return errTooManyEntries + } + if d.size > uint64(d.end) || d.offset > math.MaxInt64 { + return zip.ErrFormat + } + start := d.end - int(d.size) + if err := countDirectory(data, start, maxEntries); err != nil { + return err + } + // archive/zip may prefer the nominal offset when a prepended archive or an + // inaccurate directory size implies a positive base offset. Bound that path + // too, without allocating names, extra fields, comments or File values. + if d.offset < uint64(start) { + return countDirectory(data, int(d.offset), maxEntries) + } + return nil +} + +func countDirectory(data []byte, offset, maxEntries int) error { + for count := 0; offset <= len(data)-46 && binary.LittleEndian.Uint32(data[offset:]) == 0x02014b50; count++ { + if count == maxEntries { + return errTooManyEntries + } + header := data[offset : offset+46] + size := int(binary.LittleEndian.Uint16(header[28:])) + int(binary.LittleEndian.Uint16(header[30:])) + int(binary.LittleEndian.Uint16(header[32:])) + if size > len(data)-offset-46 { + break // zip.NewReader will report the malformed header. + } + offset += 46 + size + } + return nil +} + +func findDirectory(data []byte) (directoryInfo, error) { + for end := len(data) - 22; end >= max(0, len(data)-65*1024); end-- { + if binary.LittleEndian.Uint32(data[end:]) != 0x06054b50 { + continue + } + header := data[end : end+22] + if int(binary.LittleEndian.Uint16(header[20:])) > len(data)-end-22 { + return directoryInfo{}, zip.ErrFormat + } + d := directoryInfo{end: end, records: uint64(binary.LittleEndian.Uint16(header[10:])), + size: uint64(binary.LittleEndian.Uint32(header[12:])), offset: uint64(binary.LittleEndian.Uint32(header[16:]))} + // Match archive/zip's ZIP64 locator selection. Ordinary directories may + // also use a truncated 16-bit count without a ZIP64 locator. + if d.records == 0xffff || d.size == 0xffff || d.offset == 0xffffffff { + locator := end - 20 + if locator >= 0 && binary.LittleEndian.Uint32(data[locator:]) == 0x07064b50 && + binary.LittleEndian.Uint32(data[locator+4:]) == 0 && binary.LittleEndian.Uint32(data[locator+16:]) == 1 { + offset := binary.LittleEndian.Uint64(data[locator+8:]) + if len(data) < 56 || offset > uint64(len(data)-56) { + return directoryInfo{}, zip.ErrFormat + } + z := data[int(offset) : int(offset)+56] + if binary.LittleEndian.Uint32(z) != 0x06064b50 { + return directoryInfo{}, zip.ErrFormat + } + d.end = int(offset) + d.records = binary.LittleEndian.Uint64(z[32:]) + d.size = binary.LittleEndian.Uint64(z[40:]) + d.offset = binary.LittleEndian.Uint64(z[48:]) + } + } + return d, nil + } + return directoryInfo{}, zip.ErrFormat +} From abe7bb55ad529ee3566acbc9ce15a93a03368f4f Mon Sep 17 00:00:00 2001 From: Rajeh Taher Date: Wed, 11 Nov 2026 17:56:46 +0200 Subject: [PATCH 7/7] fix(stickerpack): guard extreme reads and both ZIP64 interpretations --- stickerpack/archive.go | 6 +++- stickerpack/archive_test.go | 57 ++++++++++++++++++++++++++++++++++++ stickerpack/zip_preflight.go | 49 ++++++++++++++++++------------- 3 files changed, 90 insertions(+), 22 deletions(-) diff --git a/stickerpack/archive.go b/stickerpack/archive.go index 4e4c57951..6f53ff26d 100644 --- a/stickerpack/archive.go +++ b/stickerpack/archive.go @@ -170,7 +170,11 @@ func Parse(data []byte, metadata []*waE2E.StickerPackMessage_Sticker, trayName s if err != nil { return nil, err } - content, readErr := io.ReadAll(io.LimitReader(reader, min(l.MaxFileBytes, l.MaxTotalBytes-total)+1)) + readLimit := min(l.MaxFileBytes, l.MaxTotalBytes-total) + if readLimit < math.MaxInt64 { + readLimit++ + } + content, readErr := io.ReadAll(io.LimitReader(reader, readLimit)) closeErr := reader.Close() if readErr != nil { return nil, fmt.Errorf("invalid pack entry: %w", readErr) diff --git a/stickerpack/archive_test.go b/stickerpack/archive_test.go index ee07ca5d6..0b72359d8 100644 --- a/stickerpack/archive_test.go +++ b/stickerpack/archive_test.go @@ -171,6 +171,7 @@ func TestDirectoryPreflightPreservesZIPVariants(t *testing.T) { for _, limits := range []Limits{ {MaxEntries: math.MaxInt, MaxFileBytes: 1, MaxTotalBytes: 1}, {MaxEntries: 1, MaxFileBytes: 1, MaxTotalBytes: math.MaxInt64}, + {MaxEntries: 1, MaxFileBytes: math.MaxInt64, MaxTotalBytes: math.MaxInt64}, } { data, metadata, err := Build([]File{{Metadata: &waE2E.StickerPackMessage_Sticker{FileName: proto.String("a")}, Data: []byte("x")}}, "", nil, limits) if err != nil { @@ -200,9 +201,65 @@ func FuzzParse(f *testing.F) { } f.Add(data) f.Add(zip64Fixture(data)) + for _, sentinel := range []uint32{0xffff, 0xffffffff} { + variant := zip64Fixture(data) + footer := variant[len(variant)-22:] + binary.LittleEndian.PutUint16(footer[8:], 1) + binary.LittleEndian.PutUint16(footer[10:], 1) + binary.LittleEndian.PutUint32(footer[12:], sentinel) + binary.LittleEndian.PutUint32(footer[16:], binary.LittleEndian.Uint32(data[len(data)-6:])) + f.Add(variant) + } f.Add([]byte{}) f.Add(directoryFixture(5, true)) f.Fuzz(func(t *testing.T, data []byte) { _, _ = Parse(data, metadata, "", Limits{MaxEntries: 4, MaxFileBytes: 64, MaxTotalBytes: 256}) }) } + +func TestParseBoundsAmbiguousZIP64Directory(t *testing.T) { + data := zip64Fixture(directoryFixture(65537, false)) + z := data[len(data)-98 : len(data)-42] + binary.LittleEndian.PutUint64(z[40:], 47) + binary.LittleEndian.PutUint64(z[48:], uint64(len(data)-98-47)) + footer := data[len(data)-22:] + binary.LittleEndian.PutUint16(footer[8:], 1) + binary.LittleEndian.PutUint16(footer[10:], 1) + binary.LittleEndian.PutUint32(footer[12:], 0xffff) + binary.LittleEndian.PutUint32(footer[16:], 0) + metadata := []*waE2E.StickerPackMessage_Sticker{{FileName: proto.String("a")}} + var err error + allocs := testing.AllocsPerRun(1, func() { + _, err = Parse(data, metadata, "", Limits{MaxEntries: 4}) + }) + if err == nil || !strings.Contains(err.Error(), "too many entries") { + t.Fatalf("ambiguous directory escaped the entry limit: %v", err) + } + if allocs > 64 { + t.Fatalf("ambiguous ZIP64 directory allocated %.0f objects", allocs) + } + t.Logf("rejected ambiguous ZIP64 directory with %.0f allocations", allocs) +} + +func TestZIP64SizeSentinelsMatchReader(t *testing.T) { + data, metadata, err := Build([]File{{Metadata: &waE2E.StickerPackMessage_Sticker{FileName: proto.String("a")}, Data: []byte("x")}}, "", nil, Limits{}) + if err != nil { + t.Fatal(err) + } + for _, sentinel := range []uint32{0xffff, 0xffffffff} { + variant := zip64Fixture(data) + footer := variant[len(variant)-22:] + binary.LittleEndian.PutUint16(footer[8:], 1) + binary.LittleEndian.PutUint16(footer[10:], 1) + binary.LittleEndian.PutUint32(footer[12:], sentinel) + binary.LittleEndian.PutUint32(footer[16:], binary.LittleEndian.Uint32(data[len(data)-6:])) + _, readerErr := zip.NewReader(bytes.NewReader(variant), int64(len(variant))) + got, parseErr := Parse(variant, metadata, "", Limits{MaxEntries: 1}) + if (readerErr == nil) != (parseErr == nil) { + t.Fatalf("size sentinel %#x: reader=%v, Parse=%v", sentinel, readerErr, parseErr) + } + if parseErr == nil && (len(got) != 1 || string(got[0].Data) != "x") { + t.Fatalf("size sentinel %#x changed the file", sentinel) + } + } +} diff --git a/stickerpack/zip_preflight.go b/stickerpack/zip_preflight.go index b7c39dfc0..e3e296bb8 100644 --- a/stickerpack/zip_preflight.go +++ b/stickerpack/zip_preflight.go @@ -18,26 +18,34 @@ type directoryInfo struct { // and its actual directory scan. ZIP counts can wrap at 65536, so neither the // declared count nor the declared directory size alone bounds that scan. func preflightDirectory(data []byte, maxEntries int) error { - d, err := findDirectory(data) - if err != nil { - return err - } - if d.records > uint64(maxEntries) { - return errTooManyEntries + valid := false + // Go 1.26 and 1.27 select ZIP64 using different directory-size sentinels. + // Bound both interpretations so an archive cannot hide extra entries from + // the preflight while the runtime reader selects the other interpretation. + for _, sentinel := range []uint64{0xffff, 0xffffffff} { + d, err := findDirectory(data, sentinel) + if err != nil || d.size > uint64(d.end) || d.offset > math.MaxInt64 { + continue // This interpretation fails before zip.NewReader allocates files. + } + valid = true + if d.records > uint64(maxEntries) { + return errTooManyEntries + } + start := d.end - int(d.size) + if err := countDirectory(data, start, maxEntries); err != nil { + return err + } + // archive/zip may prefer the nominal offset when a prepended archive or + // inaccurate directory size implies a positive base offset. + if d.offset < uint64(start) { + if err := countDirectory(data, int(d.offset), maxEntries); err != nil { + return err + } + } } - if d.size > uint64(d.end) || d.offset > math.MaxInt64 { + if !valid { return zip.ErrFormat } - start := d.end - int(d.size) - if err := countDirectory(data, start, maxEntries); err != nil { - return err - } - // archive/zip may prefer the nominal offset when a prepended archive or an - // inaccurate directory size implies a positive base offset. Bound that path - // too, without allocating names, extra fields, comments or File values. - if d.offset < uint64(start) { - return countDirectory(data, int(d.offset), maxEntries) - } return nil } @@ -56,7 +64,7 @@ func countDirectory(data []byte, offset, maxEntries int) error { return nil } -func findDirectory(data []byte) (directoryInfo, error) { +func findDirectory(data []byte, sizeSentinel uint64) (directoryInfo, error) { for end := len(data) - 22; end >= max(0, len(data)-65*1024); end-- { if binary.LittleEndian.Uint32(data[end:]) != 0x06054b50 { continue @@ -67,9 +75,8 @@ func findDirectory(data []byte) (directoryInfo, error) { } d := directoryInfo{end: end, records: uint64(binary.LittleEndian.Uint16(header[10:])), size: uint64(binary.LittleEndian.Uint32(header[12:])), offset: uint64(binary.LittleEndian.Uint32(header[16:]))} - // Match archive/zip's ZIP64 locator selection. Ordinary directories may - // also use a truncated 16-bit count without a ZIP64 locator. - if d.records == 0xffff || d.size == 0xffff || d.offset == 0xffffffff { + // Ordinary directories may use a truncated 16-bit count without a locator. + if d.records == 0xffff || d.size == sizeSentinel || d.offset == 0xffffffff { locator := end - 20 if locator >= 0 && binary.LittleEndian.Uint32(data[locator:]) == 0x07064b50 && binary.LittleEndian.Uint32(data[locator+4:]) == 0 && binary.LittleEndian.Uint32(data[locator+16:]) == 1 {