Kimi K3.1 Pro · 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 Haiku 5.5.

Gold bugs found
6 of 10
Comments published
11
Comments matching a bug
6
Comments not matching
5
Cost of this PR
$2.535

Published comments (11)

mediumbugapp/controllers/groups_controller.rb:23

Omitting offset in members raises NoMethodError — Requests that omit `offset` now call `to_i` on `nil`, raising `NoMethodError` instead of returning the first page as the endpoint previously did. Restore the zero fallback with `(params[:offset] || 0).to_i`.

mediumbugapp/controllers/admin/groups_controller.rb:71-74

Adding an existing group member violates the unique membership index — Adding a username that is already in the group blindly pushes another join row, violating the unique `group_users(group_id,user_id)` index and returning a 500; the unchanged selector makes clicking Add twice a direct trigger. Make insertion idempotent by ignoring duplicate input and existing memberships before calling `group.add`.

mediumbugapp/controllers/admin/groups_controller.rb:90

Removing a group member leaves the cached user count stale — Deleting through `group.users` removes the join row without running `GroupUser`’s destroy callbacks, so its `counter_cache: "user_count"` is not decremented and serialized group counts remain stale after a refresh. Destroy the join record through `Group#remove` or explicitly reset the group’s `group_users` counter after deletion.

mediumperformanceapp/controllers/admin/groups_controller.rb:71-74

Bulk member addition runs an unbounded series of user lookups — Bulk-adding N members performs N separate `User.find_by_username` queries in series, with no bound on the comma-separated input, replacing the previous bulk username lookup. Split the names once, fetch all matching users with a single `User.where(username: names)` query, and iterate over that result.

mediumbugapp/assets/javascripts/admin/templates/group_member.hbs:1

Remove-member links are shown for automatic groups — The item view's context is each `member`, and `GroupUserSerializer` does not expose `automatic`, so this `unless` renders removal links even for automatic groups. Bind the condition to the parent group controller's `automatic` flag so the action is hidden when membership changes are forbidden.

mediumperformanceapp/controllers/groups_controller.rb:22-29

Unbounded members limit can load and serialize an entire large group — The action passes an arbitrary `params[:limit]` directly to ActiveRecord and serializes the result, allowing one request to load an entire large visible group's membership; the rewrite newly exposes this behavior for non-automatic groups too. Clamp `limit` to a reasonable maximum before querying.

mediumbugspec/controllers/admin/groups_controller_spec.rb:114-116

Automatic-group removal spec uses PUT instead of DELETE — The automatic-removal test uses `xhr :put`, but `remove_member` is exposed as `DELETE members`; PUT is the `add_members` route. It therefore does not exercise the HTTP operation used to remove members and can pass despite a regression in DELETE removal handling.

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

totalPages adds an extra page when user_count is a multiple of limit — `totalPages` is one too high whenever `user_count` is a positive exact multiple of `limit`. For example, 100 users with a limit of 50 produces 3 pages instead of 2; Next from page 2 then sets `offset` to 100 and displays an empty third page.

mediumbugapp/assets/javascripts/discourse/models/group.js:30-36

Every `findMembers` response unconditionally replaces the member list and pagination state, while… — Sequence the requests and ignore obsolete responses, or prevent another page load until the current one completes.

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

The offset is clamped using the cached `user_count` before the request, so a reload after removal… — With 51 users on the page at offset 50, removing its sole member leaves 50 users, but this still requests offset 50; the server returns `total: 50` with no members and the model stores that out-of-range offset, leaving an empty page. Reconcile the offset with the refreshed total (and refetch a valid page when necessary) instead of relying only on the stale count. **Also found in:** - `app/assets/javascripts/discourse/models/group.js:45-48`

mediumbugapp/controllers/groups_controller.rb:22

Default limit truncates public non-automatic group member lists — The new 50-member default also truncates the public group-members page, whose route calls `findMembers` only once and whose template has no pagination controls; groups that previously displayed more than 50 non-automatic members now hide the rest. Add pagination or continued loading to the public members route, or preserve the prior retrieval behavior for that caller.

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.
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.
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.

Gold bugs missed (4)

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.
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.