new-api icon indicating copy to clipboard operation
new-api copied to clipboard

[Bug] CC Switch dialog model list not filtered by API key's group

Open ppy-web opened this issue 3 months ago • 2 comments

Describe the bug

When exporting an API key to CC Switch (cc-switch-dialog.tsx), the model dropdown shows all models the user has access to, rather than only the models belonging to that specific key's group.

This means:

  • A key configured for group "vip" still sees models from the "default" group
  • Users may accidentally select a model the key cannot actually use

Root Cause

The dialog calls getUserModels() without any group parameter — equivalent to hitting GET /api/user/models with no query string. The backend GetUserModels controller already supports ?group=xxx filtering, but the CC Switch dialog never passes it.

Affected Files

File Issue
web/src/features/keys/components/dialogs/cc-switch-dialog.tsx Calls getUserModels() without group parameter; Props don't accept a token group
web/src/features/keys/components/api-keys-dialogs.tsx Only passes tokenKey to CCSwitchDialog, not the group from currentRow
web/src/lib/api.ts getUserModels() signature doesn't accept an optional group parameter

Proposed Fix

Three small changes:

  1. api.ts — Make getUserModels accept an optional group parameter:
export async function getUserModels(group?: string): Promise<{...}> {
  const params = group ? `?group=${encodeURIComponent(group)}` : 
  const res = await api.get(`/api/user/models${params}`)
  return res.data
}
  1. cc-switch-dialog.tsx — Accept tokenGroup prop and pass it to the API:
interface Props {
  open: boolean
  onOpenChange: (open: boolean) => void
  tokenKey: string
  tokenGroup?: string | null  // new
}

When tokenGroup is empty/falsy, no group param is sent (backward compatible — returns all models).

  1. api-keys-dialogs.tsx — Pass the group from the current row:
<CCSwitchDialog
  open={open === cc-switch}
  onOpenChange={(isOpen) => !isOpen && setOpen(null)}
  tokenKey={resolvedKey}
  tokenGroup={currentRow?.group ?? }  // new
/>

Behavior After Fix

Key's group API request Model list
Empty (unset) GET /api/user/models All accessible models (unchanged)
"auto" GET /api/user/models?group=auto Auto-group expanded models
"vip" GET /api/user/models?group=vip Only "vip" groups models

Additional Context

The GetUserModels controller in controller/user.go already handles ?group=, ?group=auto, and empty cases correctly — this is purely a frontend bug. The only other callers of getUserModels() (api-keys-mutate-drawer and overview-dashboard) are unaffected since the new parameter is optional.

ppy-web avatar Aug 10 '26 03:08 ppy-web

[Bot Review]

感谢提交 bug!

这个 bug 报告遵循了“Bug Report”的模板,并且关键信息都已填写。web/src/features/keys/components/dialogs/cc-switch-dialog.tsx 和 web/src/features/keys/components/api-keys-dialogs.tsx 的问题描述以及web/src/lib/api.ts 的代码修改建议都已包含。

期待后续的贡献!

This still happens on main (04dfcbb71). The same unfiltered list also feeds a second place: the Model Limits field in the API key create/edit drawer.

  • web/src/features/keys/components/api-keys-mutate-drawer.tsx:124 calls getUserModels() with no group and caches the result under ['user-models']. Changing the key's group does not refetch the list. A user can therefore whitelist models that the key's group cannot call, and requests for those models then fail.
  • web/src/features/keys/components/dialogs/cc-switch-dialog.tsx:107 is the CC Switch case described above.

The backend already filters. GetUserModels (controller/user.go:618) accepts ?group= and expands auto to the global Auto groups. Only the frontend changes:

  1. getUserModels(group?: string) sends params: { group } when a group is given.
  2. The drawer fetches models for the selected group, with the group in the query key, so the list follows group changes. An Auto key with a custom auto_groups order can only reach those groups, so the drawer fetches each group and merges the lists. An Auto key that inherits the global order sends group=auto.
  3. Saved model limits that the selected group cannot use show a warning under the field. They are not removed silently, so switching groups back and forth does not lose configuration.
  4. The CC Switch dialog receives group and auto_groups from the row and uses the same lookup.

PR #6766 changed only the CC Switch dialog and was closed without a merge. I applied the four changes above to a fork and added tests for them:

  • an ordinary group shows only that group's models
  • a custom Auto order shows the merged models
  • the list changes when the group changes
  • a saved limit outside the group shows the warning

With the fix reverted, those four tests fail; with it restored, the keys test files pass. I can open a PR against main if you want this fixed.

Hahrix avatar Dec 01 '26 13:12 Hahrix