GLM-5.3 Flash · Heavy

fix-proper-handling-of-group-memberships-discourse-cursor

Discourse · Ruby · L · 300–799 lines · 6 files

The generalist plus 9 specialist lens agents in parallel, then the same verify and dedup steps. Judge: Claude Opus 5.5.

Gold bugs found
6 of 13
Comments published
6
Comments matching a bug
6
Comments not matching
0
Cost of this PR
$0.218

Published comments (6)

highbugapp/controllers/admin/groups_controller.rb:71-75

add_members crashes with RecordNotUnique when re-adding an existing member — A double click on Add resubmits the input (the UI never clears it), and `group.add` pushes unconditionally onto `group_users`, which has a unique index — so re-adding raises `ActiveRecord::RecordNotUnique` and returns a 500. Skip usernames already present in `group.users` before calling `group.add`.

mediumbugapp/controllers/admin/groups_controller.rb:90

Deleting group member bypasses GroupUser destroy, so user_count counter cache drifts — The stale count shows up in the admin groups list and on the group page and never self-corrects, because `group.users.delete` removes the join row without destroying `GroupUser`, so its `user_count` counter cache never fires. Use `group.remove` instead, which destroys the record.

mediumbugapp/controllers/groups_controller.rb:22-26

Unvalidated limit/offset params cause PostgreSQL errors and unbounded limit — Anyone can send `?offset=-1` to this public endpoint and get a 500, since PostgreSQL rejects negative `OFFSET`/`LIMIT`; `limit` is also uncapped. Clamp both params to non-negative values and cap `limit` before applying them.

mediumbugapp/assets/javascripts/admin/controllers/admin-group.js.es6:11-14

totalPages off by one when user_count is an exact multiple of limit — The pagination controls enable 'next' on the real last page; clicking it loads an empty member list ('2/2' with no rows). Use `Math.ceil(user_count / limit)` instead of the floor-plus-one form so `totalPages` matches the actual page count.

mediumbugapp/assets/javascripts/discourse/models/group.js:23

findMembers clamps offset to user_count, which can yield an empty page — `offset == user_count` is always past the last row, so the server returns an empty page — reachable after removing a member while on page 2. Clamp the offset to the start of the last page instead, rounded down to a multiple of `limit`.

mediumbugapp/assets/javascripts/discourse/routes/group-members.js.es6:8-12

Public group members page shows only first 50 members, no pagination — The backend now caps results at 50 and returns pagination metadata, but this route ignores it, so large groups silently lose the rest of their member list. Add pagination or a load-more here, or raise the limit for this consumer. **Also found in:** - `app/controllers/groups_controller.rb:22-26`

Gold bugs found (6)

mediumThe admin group pager computes total pages as floor(user_count / limit) + 1, so when the member count is an exact multiple of the limit it shows an extra page and Next navigates to an empty member list.
highThe public members endpoint now always caps results at 50 per page, but the public group members page has no pagination, so groups with more than 50 members are silently truncated for users.
mediumAdmin remove_member deletes memberships with group.users.delete, which skips GroupUser callbacks, so the cached groups.user_count is never decremented and stays inflated.
mediumAdmin add_members pushes every username into group.users without skipping existing members, so adding someone already in the group hits the unique group_users index, returns a 500 and leaves earlier users partially added.
mediumThe public group members endpoint takes `limit` from params with no upper bound or positivity check, letting a visitor request huge pages or trigger a 500 with a negative limit.
mediumAfter removeMember succeeds, findMembers reloads with the old offset, clamped only by the stale user_count; removing the only member on the last page (e.g. 51 members, offset 50) requests offset equal to the new total and the admin sees an empty, out-of-range page.

Gold bugs missed (7)

mediumPaging actions fire overlapping member requests and each response unconditionally overwrites offset and members, so a stale response arriving last can revert the list to an earlier page.
mediumCreating a group ignores the alias_level the admin selected, saving only name and visibility, so new groups keep the default alias level.
mediumRemoving a member deletes the membership row directly and skips clearing the user's primary_group_id, so a removed user can keep this group as their primary group.
mediumGroupsController#members now returns {members, meta} with pagination, but the public groups controller spec still expects a bare array and stays pending, so the new response contract is untested.
mediumGroup member pagination is off by one when user_count is a multiple of limit: Next moves to offset == user_count and shows an empty page.
mediumAdmin remove_member calls group.users.delete with a raw integer id, which raises RecordNotFound (404/500) when the user is not or no longer a member, e.g. on concurrent removal.
mediumGroup#removeMember and #addMembers have no rejection handler and the admin controller discards their promises, so failed member additions/removals are silently swallowed with no error shown to the admin.