You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(user): validate group in UpdateUser (#5236) - #8499
This PR validates Group in PUT /api/user/ (UpdateUser) so administrators cannot write nonexistent group names to users.group. It keeps existing users.group values when group is omitted or blank, and stays limited to server-side validation and focused regression tests.
Summary
UpdateUser in controller/user.go trims updatedUser.Group, preserves originUser.Group when empty, and rejects groups missing from ratio_setting.ContainsGroupRatio.
UpdateUser (controller/user.go)
validate role and canManageTargetRole
+ trim updatedUser.Group+ if empty -> keep originUser.Group+ else if !ratio_setting.ContainsGroupRatio(updatedUser.Group) -> ApiErrorT("Invalid parameters")
requireAdminUserProof (when password or admin_permissions changed)
updatedUser.EditWithTx
Evidence
Before: Running TestUpdateUserValidatesGroup before the fix returned {"success":true} and wrote "defalut", "", "auto", and " vip " to users.group.
After: Running go test -race ./controller -run '^TestUpdateUserValidatesGroup$|^TestAdminUserRiskOperationsRequireProofBeforeMutation$|^TestAdminUserRoutineEditsDoNotRequireProof$' -count=1 -v and go vet ./controller passed on SQLite 3.50.4 (5 subtests in TestUpdateUserValidatesGroup, 11 subtests in TestAdminUserRiskOperationsRequireProofBeforeMutation, and TestAdminUserRoutineEditsDoNotRequireProof). MySQL >= 5.7.8 and PostgreSQL >= 9.6 were not run locally because this change touches no SQL queries, schema tags, or migrations.
Merge Danger
Door: two-way
Blast Radius: controller
Summary by CodeRabbit
Bug Fixes
Updating a user now accepts only configured groups and rejects invalid or unsupported group values.
Leading and trailing spaces are removed from submitted group names before validation.
Leaving the group field blank preserves the user’s current group.
FixesQuantumNous#5236. Trim updatedUser.Group in UpdateUser, preserve originUser.Group when empty, and reject unknown groups via ratio_setting.ContainsGroupRatio.
No actionable comments were generated in the recent review.
Walkthrough
UpdateUser now trims submitted group values, retains the existing group when the submitted value is blank, and rejects non-empty values that are not configured. A new test checks these cases, including acceptance of a configured group with surrounding whitespace.
Changes
User group validation
Layer / File(s)
Summary
Validate submitted groups controller/user.go, controller/admin_user_verification_test.go
UpdateUser trims the submitted group, preserves the existing group for blank input, and rejects groups absent from configured group ratios. The new test checks invalid and auto groups, omitted and blank values, and a whitespace-padded configured group.
[Bug] 编辑用户时分组框只列出 GroupRatio 中的分组,无法设置只在分组规则中定义的用户分组 #8238: The PR adds server-side validation for group names in UpdateUser, which addresses the issue’s report that this endpoint accepts arbitrary group names. The issue’s expectation for groups defined only in GroupGroupRatio may remain unresolved.
Possibly related PRs
QuantumNous/new-api#8235: Both changes affect which group values the user-edit operation accepts. This PR’s server-side check may reject values that PR #8235 allows in the form.
Suggested reviewers: calcium-ion
Merge Risk: Minimal · up to 47294
This change makes the user update endpoint reject group names that are not configured, which prevents invalid groups from being stored. It leaves existing groups unchanged when the group field is omitted or blank. No merge-blocking risk was identified.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "fix(user): validate group in UpdateUser (#5236)" names the primary change. The PR adds server-side group validation to UpdateUser, which trims the submitted group, keeps the existing group …
Linked Issues check
[#5236] Requires server-side validation of Group in UpdateUser so that nonexistent group names are not written to users.group. The PR adds this check to controller/user.go. UpdateUser trims updatedUse…
Out of Scope Changes check
The whole-PR diff against merge base 04dfcbb touches two files. controller/user.go adds the group trim, preserve, and validation logic in UpdateUser. controller/admin_user_verification_test.go adds Te…
Full details: Title check
Explanation
The title "fix(user): validate group in UpdateUser (#5236)" names the primary change. The PR adds server-side group validation to UpdateUser, which trims the submitted group, keeps the existing group when the value is blank, and rejects group names absent from the configured group ratios. The title is specific, concise, and matches the changed code in controller/user.go and its new test.
Full details: Linked Issues check
Explanation
[#5236] Requires server-side validation of Group in UpdateUser so that nonexistent group names are not written to users.group. The PR adds this check to controller/user.go. UpdateUser trims updatedUser.Group. If the trimmed value is empty, it keeps originUser.Group. If the value is non-empty and ratio_setting.ContainsGroupRatio returns false, it returns ApiErrorT("Invalid parameters") before EditWithTx runs. This covers the issue's expected behavior. The issue suggests GetGroupRatioCopy. The PR uses ContainsGroupRatio, which checks the same configured group ratios. The author reports that TestUpdateUserValidatesGroup failed before the fix and passed after it, with 5 subtests. The test covers nonexistent and auto groups, blank and omitted values, and trimmed valid values. [#5139] is separate context. The PR does not need to add AddToken/UpdateToken validation. No coding requirement from #5236 remains unmet based on the available evidence. MySQL and PostgreSQL were not run. This does not affect the requirement because the change adds no SQL, schema, or migration code.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff against merge base 04dfcbb touches two files. controller/user.go adds the group trim, preserve, and validation logic in UpdateUser. controller/admin_user_verification_test.go adds TestUpdateUserValidatesGroup and the ratio_setting import, and it restores the original group ratios after the test. Both changes support the #5236 objective. The PR does not change the token group validation or other unrelated controller logic. The summary reports no changes to exported declarations. The review used the AI-generated summary and did not read the full hunks, so this verdict rests on the summary's description of the changed files.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
A rabbit checks each group with care, Trims the spaces from what you share. Blank means keep the group in place, Unknown names must leave no trace. The configured ones pass with grace.
Recent review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: CHILL
Plan: Advanced
Run ID: f240e025-0996-4d6b-8865-69910953edeb
Commits
Reviewing files that changed from the base of the PR and between 04dfcbb and 47294c9.
Files selected for processing: 2
controller/admin_user_verification_test.go
controller/user.go
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Agent: hermes-agent unknown · gemini-4-hydrogen-xhigh · hermes-webui · 2026-11-30 UTC
Closes #5236
Request summary
This PR validates
GroupinPUT /api/user/(UpdateUser) so administrators cannot write nonexistent group names tousers.group. It keeps existingusers.groupvalues whengroupis omitted or blank, and stays limited to server-side validation and focused regression tests.Summary
UpdateUserincontroller/user.gotrimsupdatedUser.Group, preservesoriginUser.Groupwhen empty, and rejects groups missing fromratio_setting.ContainsGroupRatio.Evidence
TestUpdateUserValidatesGroupbefore the fix returned{"success":true}and wrote"defalut","","auto", and" vip "tousers.group.go test -race ./controller -run '^TestUpdateUserValidatesGroup$|^TestAdminUserRiskOperationsRequireProofBeforeMutation$|^TestAdminUserRoutineEditsDoNotRequireProof$' -count=1 -vandgo vet ./controllerpassed on SQLite 3.50.4 (5 subtests inTestUpdateUserValidatesGroup, 11 subtests inTestAdminUserRiskOperationsRequireProofBeforeMutation, andTestAdminUserRoutineEditsDoNotRequireProof). MySQL >= 5.7.8 and PostgreSQL >= 9.6 were not run locally because this change touches no SQL queries, schema tags, or migrations.Merge Danger
Door: two-way
Blast Radius: controller
Summary by CodeRabbit