diff --git a/CHANGELOG.md b/CHANGELOG.md index 2865f6d7b..c11317619 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ All notable HyperMeow changes are documented here. HyperMeow uses commit pseudo- - Redact shared-device and root secrets from app-state mutations in trace logs. - Decode newsletter milestone positions from either quoted or numeric integers without losing precision. - Preserve reported newsletter counter presence, explicit zeros and distinct view definitions. Omit malformed or ambiguous observations, including duplicate view definitions, instead of reporting fabricated zeros or truncated reaction lists. +- Copy group-cache metadata under its lock before send-permission checks, including participant and admin slices, so notifications cannot race those reads. - Keep connection handler completion pending until an active handler exits, and process retired stream errors before automatic reconnect decisions. Manual disconnect retains its five-second wait bound. - Keep the newest pairing secret through bursts of rotation notifications and refresh the active QR code. - Report the destination in `SendFBMessage` responses and decrypt full bot edits using their target message ID. diff --git a/group.go b/group.go index 9b57a8c41..09773d3df 100644 --- a/group.go +++ b/group.go @@ -752,13 +752,25 @@ func (cli *Client) getCachedGroupData(ctx context.Context, jid types.JID) (*grou cli.groupCacheLock.Lock() defer cli.groupCacheLock.Unlock() if val, ok := cli.groupCache[jid]; ok { - return val, nil + return val.clone(), nil } _, err := cli.getGroupInfo(ctx, jid, false) if err != nil { return nil, err } - return cli.groupCache[jid], nil + return cli.groupCache[jid].clone(), nil +} + +// clone must be called while holding groupCacheLock. Callers use the result +// after releasing the lock, while notifications may update the cached metadata. +func (gmc *groupMetaCache) clone() *groupMetaCache { + if gmc == nil { + return nil + } + cloned := *gmc + cloned.Members = slices.Clone(gmc.Members) + cloned.Admins = slices.Clone(gmc.Admins) + return &cloned } func parseParticipant(childAG *waBinary.AttrUtility, child *waBinary.Node) types.GroupParticipant { diff --git a/group_cache_snapshot_test.go b/group_cache_snapshot_test.go new file mode 100644 index 000000000..41e8f50af --- /dev/null +++ b/group_cache_snapshot_test.go @@ -0,0 +1,85 @@ +package whatsmeow + +import ( + "context" + "slices" + "sync" + "testing" + + "github.com/polymorfa/hypermeow/proto/waE2E" + "github.com/polymorfa/hypermeow/types" + "github.com/polymorfa/hypermeow/types/events" +) + +func TestCachedGroupSnapshotIsolation(t *testing.T) { + group := types.NewJID("120363000000001", types.GroupServer) + admin := types.NewJID("100000000000001", types.HiddenUserServer) + member := types.NewJID("100000000000002", types.HiddenUserServer) + replacement := types.NewJID("100000000000003", types.HiddenUserServer) + cli := &Client{groupCache: map[types.JID]*groupMetaCache{ + group: {ReadOnly: true, Members: []types.JID{admin, member}, Admins: []types.JID{admin}}, + }} + snapshot, err := cli.getCachedGroupData(context.Background(), group) + if err != nil { + t.Fatal(err) + } + cli.updateGroupParticipantCache(&events.GroupInfo{ + JID: group, Leave: []types.JID{admin}, Promote: []types.JID{member}, + Announce: &types.GroupAnnounce{IsAnnounce: false}, + }) + if !snapshot.ReadOnly || !slices.Equal(snapshot.Members, []types.JID{admin, member}) || !slices.Equal(snapshot.Admins, []types.JID{admin}) { + t.Fatal("notification mutated a previously returned group snapshot") + } + message := &waE2E.Message{} + if !snapshot.CanSend(admin, message) || snapshot.CanSend(member, message) { + t.Fatal("notification changed the snapshot's send permissions") + } + snapshot.Members[0] = replacement + snapshot.Admins[0] = replacement + current, err := cli.getCachedGroupData(context.Background(), group) + if err != nil { + t.Fatal(err) + } + if current.ReadOnly || !slices.Equal(current.Members, []types.JID{member}) || !slices.Equal(current.Admins, []types.JID{member}) { + t.Fatal("caller mutated metadata owned by the group cache") + } +} + +func TestCachedGroupPermissionsConcurrentNotifications(t *testing.T) { + group := types.NewJID("120363000000001", types.GroupServer) + member := types.NewJID("100000000000001", types.HiddenUserServer) + cli := &Client{groupCache: map[types.JID]*groupMetaCache{ + group: {ReadOnly: true, Members: []types.JID{member}}, + }} + start := make(chan struct{}) + var updates sync.WaitGroup + updates.Add(1) + go func() { + defer updates.Done() + <-start + for range 1000 { + cli.updateGroupParticipantCache(&events.GroupInfo{ + JID: group, Promote: []types.JID{member}, + Announce: &types.GroupAnnounce{IsAnnounce: false}, + }) + cli.updateGroupParticipantCache(&events.GroupInfo{ + JID: group, Demote: []types.JID{member}, + Announce: &types.GroupAnnounce{IsAnnounce: true}, + }) + } + }() + close(start) + allowed := 0 + for range 1000 { + snapshot, err := cli.getCachedGroupData(context.Background(), group) + if err != nil { + updates.Wait() + t.Fatal(err) + } + if snapshot.CanSend(member, &waE2E.Message{}) { + allowed++ + } + } + updates.Wait() + t.Logf("checked 1000 independent permission snapshots, %d allowed", allowed) +}