Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
85 changes: 85 additions & 0 deletions group_cache_snapshot_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading