Skip to content

fix(user): validate group in UpdateUser (#5236) - #8499

Open
Gaoshou101 wants to merge 1 commit into
QuantumNous:mainfrom
Gaoshou101:fix/validate-user-group-5236
Open

Gaoshou101 wants to merge 1 commit into
QuantumNous:mainfrom
Gaoshou101:fix/validate-user-group-5236

Conversation

@Gaoshou101

@Gaoshou101 Gaoshou101 commented Nov 30, 2026 •

Copy link
Copy Markdown

Agent: hermes-agent unknown · gemini-4-hydrogen-xhigh · hermes-webui · 2026-11-30 UTC

Closes #5236

Request summary

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.

Fixes QuantumNous#5236. Trim updatedUser.Group in UpdateUser, preserve originUser.Group when empty, and reject unknown groups via ratio_setting.ContainsGroupRatio.
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed [#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 Passed 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.

Share

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: UpdateUser 缺少 group 校验 — 可写入不存在的用户组

1 participant