Skip to content

fix: preserve server team_id during admin UI edits - #3780

Merged
crivetimihai merged 6 commits into
IBM:mainfrom
kimsehwan96:fix/server-edit-team-id-overwrite
Mar 23, 2026
Merged

crivetimihai merged 6 commits into
IBM:mainfrom
kimsehwan96:fix/server-edit-team-id-overwrite

Conversation

@kimsehwan96

Copy link
Copy Markdown
Contributor

🔗 Related Issue

Closes: #3779


📝 Summary

When editing a virtual server via the admin UI, the server's team_id was silently overwritten on every save — even when the user did not intend to change the team.

Root cause: In admin_edit_server(), when no team_id is present in the form (e.g., editing from the "All Teams" view), verify_team_for_user() falls back to the current user's personal team, which is then unconditionally written to the server record.

Fix: Before calling verify_team_for_user(), the edit endpoint now looks up the existing server's team_id from the database and uses it as the default when the form does not include one. This preserves the original team assignment while still allowing explicit team changes via the API.


🏷️ Type of Change

  • Bug fix
  • Feature / Enhancement
  • Documentation
  • Refactor
  • Chore (deps, CI, tooling)
  • Other (describe below)

🧪 Verification

Check Command Status
Lint suite make lint pass
Unit tests make test pass
Coverage ≥ 80% make coverage

✅ Checklist

  • Code formatted (make black isort pre-commit)
  • Tests added/updated for changes
  • Documentation updated (if applicable)
  • No secrets or credentials committed

📓 Notes (optional)

Recommended future work

  • A2A Agent edit has the same pattern (a2a_service.py uses generic setattr loop)
  • Explicit "transfer team" UI/API for intentional team reassignment
  • Tool, Gateway, Prompt edit endpoints call verify_team_for_user() but their service layers don't persist the result (accidentally safe)

kimsehwan96 and others added 5 commits March 23, 2026 05:05
When editing a virtual server via the admin UI without an explicit
team_id in the form, verify_team_for_user() fell back to the current
user's personal team, silently reassigning the server on every edit.

Now the edit endpoint looks up the existing server's team_id and
preserves it when no team_id is provided in the form.

Closes: IBM#3779

Signed-off-by: kimsehwan96 <[email protected]>
Extend the server team_id preservation fix to gateway, resource, prompt,
and A2A agent edit endpoints. All five endpoints now look up the existing
entity's team_id when no explicit team_id is provided in the form, instead
of silently falling back to the user's personal team.

Also fixes:
- resource edit was missing team_id and owner_email entirely
- prompt edit had a debug log typo ("befor")

Adds comprehensive tests for all five entity types covering the team_id
preservation path and the explicit team_id override path.

Closes: IBM#3779
Signed-off-by: Mihai Criveti <[email protected]>
…ity types

The admin-side team_id preservation (previous commit) was only effective for
server and A2A agent edits because only those service-layer update methods
actually persisted team_id to the ORM row. Gateway, resource, and prompt
services accepted team_id in their update DTOs but silently ignored it.

This commit:
- Adds team_id persistence to gateway_service.update_gateway()
- Adds team_id persistence to resource_service.update_resource()
- Adds team_id persistence to prompt_service.update_prompt()
- Adds team reassignment validation to a2a_service.update_agent()
  (previously persisted via generic setattr without ownership checks)
- Each service now validates team existence and caller ownership before
  allowing team reassignment, matching server_service's pattern

Signed-off-by: Mihai Criveti <[email protected]>
The server service validates the effective team_id when visibility
changes to "team" (even when team_id is not explicitly provided in
the update), ensuring you cannot set visibility to "team" with an
invalid or missing team. The gateway, resource, prompt, and A2A
services were missing this validation.

Now all four services validate the target team_id (either from the
update DTO or from the existing entity) when visibility transitions
to "team", matching server_service's stricter pattern.

Signed-off-by: Mihai Criveti <[email protected]>
Add targeted deny-path tests for all four entity services verifying:
- Reassigning to a non-existent team is rejected
- Setting visibility to "team" without any team_id is rejected

These tests validate the service-layer validation logic, not just
admin-route DTO construction.

Signed-off-by: Mihai Criveti <[email protected]>
@crivetimihai
crivetimihai force-pushed the fix/server-edit-team-id-overwrite branch from 2566056 to 5323853 Compare March 23, 2026 09:05
@crivetimihai

Copy link
Copy Markdown
Member

Thanks for catching this @kimsehwan96 — the root cause analysis was spot-on.

I rebased onto main, expanded the fix to cover all entity types, and added service-layer persistence + validation. Here's what the final branch contains:

Commits:

  1. Your original fix — admin-side team_id preservation for server edits
  2. Extended admin-side fix — same pattern applied to gateway, resource, prompt, and A2A agent edit endpoints. Also fixed resource edit missing team_id/owner_email entirely, and a typo in a prompt edit log message.
  3. Service-layer persistence — gateway, resource, and prompt services were accepting team_id in their update DTOs but never writing it to the ORM row. Now all three persist it with ownership validation (matching server_service's pattern). Also added missing team reassignment validation to a2a_service (which was persisting via generic setattr without checks).
  4. Visibility transition validation — when visibility changes to "team", all services now validate the effective team_id (from the update or existing row), matching server_service's stricter pattern.
  5. Deny-path service tests — 8 tests verifying non-existent team rejection and missing team_id rejection at the service layer for all 4 entity types.

Test results: 6310 passed, 0 failures.

crivetimihai
crivetimihai previously approved these changes Mar 23, 2026

@crivetimihai crivetimihai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — fix is correct, complete, and well-tested across all entity types.

@kimsehwan96

Copy link
Copy Markdown
Contributor Author

@crivetimihai Thanks for fast review and addintional works!

Add tests for uncovered branches in team validation functions:
- Non-owner membership rejection (all 4 services)
- No-user-email early return path with team_id persistence
  (gateway, resource, prompt, A2A)

Differential coverage: 114 lines, 0 missing, 100%.

Signed-off-by: Mihai Criveti <[email protected]>
@crivetimihai

Copy link
Copy Markdown
Member

@kimsehwan96 thanks for the PR. Merging. Can you please test that everything still works correctly post merge?

Thank you!

@crivetimihai
crivetimihai merged commit db6f2a1 into IBM:main Mar 23, 2026
31 checks passed
@kimsehwan96

kimsehwan96 commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@crivetimihai I tested it from main branch. It correctly works.

But, If some user who are not member of that team try to change MCP server configuration (via admin UI edit), then just show Invalid team_id error message with 422 Unprocessable Entity. I think the message should be more clarifier like Permission deined: you are not a member of the team that owns this resource

I will create issue related this. Thanks!

msureshkumar88 pushed a commit that referenced this pull request May 13, 2026
* fix: preserve server team_id during admin UI edits

When editing a virtual server via the admin UI without an explicit
team_id in the form, verify_team_for_user() fell back to the current
user's personal team, silently reassigning the server on every edit.

Now the edit endpoint looks up the existing server's team_id and
preserves it when no team_id is provided in the form.

Closes: #3779

Signed-off-by: kimsehwan96 <[email protected]>

* fix: preserve entity team_id during admin UI edits for all entity types

Extend the server team_id preservation fix to gateway, resource, prompt,
and A2A agent edit endpoints. All five endpoints now look up the existing
entity's team_id when no explicit team_id is provided in the form, instead
of silently falling back to the user's personal team.

Also fixes:
- resource edit was missing team_id and owner_email entirely
- prompt edit had a debug log typo ("befor")

Adds comprehensive tests for all five entity types covering the team_id
preservation path and the explicit team_id override path.

Closes: #3779
Signed-off-by: Mihai Criveti <[email protected]>

* fix: add service-layer team_id persistence and validation for all entity types

The admin-side team_id preservation (previous commit) was only effective for
server and A2A agent edits because only those service-layer update methods
actually persisted team_id to the ORM row. Gateway, resource, and prompt
services accepted team_id in their update DTOs but silently ignored it.

This commit:
- Adds team_id persistence to gateway_service.update_gateway()
- Adds team_id persistence to resource_service.update_resource()
- Adds team_id persistence to prompt_service.update_prompt()
- Adds team reassignment validation to a2a_service.update_agent()
  (previously persisted via generic setattr without ownership checks)
- Each service now validates team existence and caller ownership before
  allowing team reassignment, matching server_service's pattern

Signed-off-by: Mihai Criveti <[email protected]>

* fix: validate visibility->"team" transitions in all entity services

The server service validates the effective team_id when visibility
changes to "team" (even when team_id is not explicitly provided in
the update), ensuring you cannot set visibility to "team" with an
invalid or missing team. The gateway, resource, prompt, and A2A
services were missing this validation.

Now all four services validate the target team_id (either from the
update DTO or from the existing entity) when visibility transitions
to "team", matching server_service's stricter pattern.

Signed-off-by: Mihai Criveti <[email protected]>

* test: add deny-path service tests for team_id validation

Add targeted deny-path tests for all four entity services verifying:
- Reassigning to a non-existent team is rejected
- Setting visibility to "team" without any team_id is rejected

These tests validate the service-layer validation logic, not just
admin-route DTO construction.

Signed-off-by: Mihai Criveti <[email protected]>

* test: achieve 100% differential coverage for team_id validation

Add tests for uncovered branches in team validation functions:
- Non-owner membership rejection (all 4 services)
- No-user-email early return path with team_id persistence
  (gateway, resource, prompt, A2A)

Differential coverage: 114 lines, 0 missing, 100%.

Signed-off-by: Mihai Criveti <[email protected]>

---------

Signed-off-by: kimsehwan96 <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
kamath-a pushed a commit to kamath-a/mcp-context-forge that referenced this pull request Jun 26, 2026
* fix: preserve server team_id during admin UI edits

When editing a virtual server via the admin UI without an explicit
team_id in the form, verify_team_for_user() fell back to the current
user's personal team, silently reassigning the server on every edit.

Now the edit endpoint looks up the existing server's team_id and
preserves it when no team_id is provided in the form.

Closes: IBM#3779

Signed-off-by: kimsehwan96 <[email protected]>

* fix: preserve entity team_id during admin UI edits for all entity types

Extend the server team_id preservation fix to gateway, resource, prompt,
and A2A agent edit endpoints. All five endpoints now look up the existing
entity's team_id when no explicit team_id is provided in the form, instead
of silently falling back to the user's personal team.

Also fixes:
- resource edit was missing team_id and owner_email entirely
- prompt edit had a debug log typo ("befor")

Adds comprehensive tests for all five entity types covering the team_id
preservation path and the explicit team_id override path.

Closes: IBM#3779
Signed-off-by: Mihai Criveti <[email protected]>

* fix: add service-layer team_id persistence and validation for all entity types

The admin-side team_id preservation (previous commit) was only effective for
server and A2A agent edits because only those service-layer update methods
actually persisted team_id to the ORM row. Gateway, resource, and prompt
services accepted team_id in their update DTOs but silently ignored it.

This commit:
- Adds team_id persistence to gateway_service.update_gateway()
- Adds team_id persistence to resource_service.update_resource()
- Adds team_id persistence to prompt_service.update_prompt()
- Adds team reassignment validation to a2a_service.update_agent()
  (previously persisted via generic setattr without ownership checks)
- Each service now validates team existence and caller ownership before
  allowing team reassignment, matching server_service's pattern

Signed-off-by: Mihai Criveti <[email protected]>

* fix: validate visibility->"team" transitions in all entity services

The server service validates the effective team_id when visibility
changes to "team" (even when team_id is not explicitly provided in
the update), ensuring you cannot set visibility to "team" with an
invalid or missing team. The gateway, resource, prompt, and A2A
services were missing this validation.

Now all four services validate the target team_id (either from the
update DTO or from the existing entity) when visibility transitions
to "team", matching server_service's stricter pattern.

Signed-off-by: Mihai Criveti <[email protected]>

* test: add deny-path service tests for team_id validation

Add targeted deny-path tests for all four entity services verifying:
- Reassigning to a non-existent team is rejected
- Setting visibility to "team" without any team_id is rejected

These tests validate the service-layer validation logic, not just
admin-route DTO construction.

Signed-off-by: Mihai Criveti <[email protected]>

* test: achieve 100% differential coverage for team_id validation

Add tests for uncovered branches in team validation functions:
- Non-owner membership rejection (all 4 services)
- No-user-email early return path with team_id persistence
  (gateway, resource, prompt, A2A)

Differential coverage: 114 lines, 0 missing, 100%.

Signed-off-by: Mihai Criveti <[email protected]>

---------

Signed-off-by: kimsehwan96 <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
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]: Editing a Virtual Server via the admin UI silently overwrites the server's team_id

2 participants