Skip to content

Internal 1215 - #7764

Merged
brian-hussey merged 4 commits into
mainfrom
internal_1215
Dec 4, 2026
Merged

brian-hussey merged 4 commits into
mainfrom
internal_1215

Conversation

@vishu-bh

@vishu-bh vishu-bh commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: internal 1215

@vishu-bh
vishu-bh marked this pull request as ready for review December 2, 2026 12:00
@ja8zyjits

Copy link
Copy Markdown
Collaborator

1. 🚨 Blocking

B1. except* PermissionError unreachable for new raise site

  • File: mcpgateway/services/gateway_service.py:3246-3258 (diff L281-293)
  • Cause: _enforce_mtls_path_admin_only is awaited at line 2715, before any TaskGroup. It raises plain PermissionError, not BaseExceptionGroup. except* never matches a bare exception.
  • Effect:
    • db.rollback() and gateway_register_permission_denied log never run on path-based denial.
    • test_registration_denial_logs_without_values (diff L645-654) expects that event. Expected to fail in CI.
    • HTTP behavior stays correct: main.py:8180 and admin.py:13653 catch bare PermissionError, return 403. Rollback benign (no DB write before check).
  • Fix: guard before the TaskGroup scope. Use nested try/except PermissionError before outer try/except*, or move the call before the outer try: (line 2713). Cannot mix except and except* in one try.
  • Related (same handler):
    • Log lacks error=, gateway_name, resource identity. Update path logs error=pe, resource_type, resource_id (gateway_service.py:4746-4758).
    • Handler also catches PermissionError from check_policy_permission (gateway_service.py:2716-2717). Operators cannot tell path-credential denial from validation-policy denial.
    • Fix: add error=denied.exceptions[0] and custom_fields={"gateway_name": gateway.name}.

2. ⚠️ Should fix (Medium)

M1. Stored host-path credentials not gated on destination change

  • File: mcpgateway/services/gateway_service.py:3910-3916 (diff L325-331)
  • Gate inspects only submitted values. Unchanged cert → None; masked key → None.
  • Gateway already storing host path (pre-fix or admin-created) works for non-admin owner. Owner changes url, transport, or ca_certificate. _is_destination_changing re-initializes with stored path creds against new destination. No admin check runs.
  • Issue requirement ("same authorization during registration, refresh, and tool invocation") met only for new input. Docs (docs/docs/manage/self-signed-certificates.md, diff L23-25) say refresh/invocation unchanged.
  • Fix: when _is_destination_changing(...) and client_credentials_use_paths(gateway.client_cert, None), run same admin check. Document any intended remaining gap in PR description.

M2. update_gateway skips enforcement for disabled gateway, no audit event

  • File: mcpgateway/services/gateway_service.py:3325-3331 (line 3909 guard if gateway.enabled or include_inactive:)
  • Developer submits path cert against disabled gateway → silent return None. No DB write, no security regression, but attempt unrecorded.
  • Fix: hoist _enforce_mtls_path_admin_only above the gateway.enabled guard.

M3. Catalog router scope derivation unverified

  • File: mcpgateway/services/catalog_service.py:796 (diff L183)
  • register_catalog_server forwards token_teams to register_gateway. Router not in diff. If router passes None or omits it, catalog gateways with filesystem certs blocked for all callers, admins included.
  • test_register_catalog_server_success (diff L447) passes token_teams=[]; does not prove router extracts scope.
  • Fix: confirm router uses get_scoped_resource_access_context. If out of PR scope, document gap.

M4. token_teams=() default: undocumented, opposite to sibling services

  • Files: mcpgateway/services/gateway_service.py:1578, 2662, 3852 (diff L219, L249, L301)
  • () = public-only, fail-closed (correct). Admin caller omitting arg is denied. tool_service, server_service, resource_service, prompt_service, a2a_service default to None (admin bypass). Same name, opposite default.
  • Fix: keep fail-closed. Add comment at default: # () = public-only; pass None for admin-bypass; resolve scope via get_scoped_resource_access_context. Note reversed default in PR description and docs/docs/manage/self-signed-certificates.md.
  • Ref: AGENTS.md Token Scoping Quick Reference (teams: [] → public-only).

3. Low

  • L1. import_platform_admin ignored by new gate.
    gateway_service.py:3901,3916; import_service.py:1148 (diff L331, L351). Ownership check skips on import_platform_admin; mTLS gate always calls check_platform_admin_permission(..., token_teams=token_teams). Two sources of truth for "unrestricted admin". Import with import_platform_admin=True and non-None scope is denied. Also inconsistent with _enforce_token_exchange_admin_only (requester_email=None trusted; new gate fails closed, gateway_service.py:1598, diff L239).
    Fix: pass flag into gate, or state in docstring that scope is authoritative. Add test for import-admin update path.
  • L2. token_teams rebound with new meaning.
    mcpgateway/main.py:8158 (diff L101); earlier binding main.py:8131. Raw request.state value overwritten by normalized scope. Fix: bind to distinct name (e.g. scoped_teams).
  • L3. GatewayRead description describes input policy.
    mcpgateway/schemas.py:4771-4772 (diff L170-171). client_key masked on read (schemas.py:5107); never returned as reference. Fix: restore read-model description; keep new wording on GatewayCreate/GatewayUpdate only.

4. Test quality

  • T1. Vacuous negative assertion. tests/unit/mcpgateway/services/test_gateway_service.py:12809-12817 (diff L645-654). assert "/operator/private.pem" not in str(event) always true; handler passes no credential value. Misses future leak and user_id. Fix: assert exact event kwargs, or drop check. (Test also fails until B1 fixed.)
  • T2. test_update_preserves_existing_configuration asserts nothing on preservation. Same file :12775-12795 (diff L611-631). Non-clear branch: status="pending" + async lifecycle → early return at gateway_service.py:3918-3925; only permission.assert_not_awaited(). Clear branch: success inferred from sentinel RuntimeError("after-validation") in GatewayError. Fix: assert stored cert/key unchanged; split clear case, assert _enforce_token_exchange_admin_only directly.
  • T3. No service-level admin success test. Same file :12746-12751 (diff L583-588). test_admin_can_supply_paths hits private helper only. register_gateway (gateway_service.py:2715) and update_gateway (:3916) never run with token_teams=None and admin allowed. Wrong arg order or user_email or modified_by fallback would pass. Fix: add one register and one update case with path creds and check_platform_admin_permission → True.
  • T4. Missing test for import-admin update path (see L1).

5. Cleanup (DRY / KISS)

  • C1. Repeated responses={403: ...} literal. mcpgateway/main.py:8099, 8100, 8271 (diff L92, L93, L118). Fix: one module constant (e.g. _GATEWAY_WRITE_RESPONSES).
  • C2. Duplicate token_teams branch for gateways/tools. mcpgateway/services/import_service.py:1138-1141, 1147-1150 (diff L342-353). Fix: set kwargs["token_teams"] = token_teams once under if kind in ("gateways", "tools"); keep only import_requires_credentials in gateway branch.
  • C3. Needless Sequence conversion. gateway_service.py:1598 (diff L239): token_teams=None if token_teams is None else list(token_teams); param Optional[Sequence[str]] = (). Siblings use Optional[List[str]]. Fix: type three new params Optional[List[str]]; drop Sequence import (diff L196) and list(...) copy.

@ja8zyjits ja8zyjits self-assigned this Dec 2, 2026

@ja8zyjits ja8zyjits left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Check

@vishu-bh

vishu-bh commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator Author

Feedback addressed.

@vishu-bh vishu-bh self-assigned this Dec 3, 2026
Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>
Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>
Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>
Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>

@ja8zyjits ja8zyjits left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@gandhipratik203 gandhipratik203 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

@brian-hussey brian-hussey 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

@brian-hussey
brian-hussey added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit f5a0f05 Dec 4, 2026
40 checks passed
@brian-hussey
brian-hussey deleted the internal_1215 branch December 4, 2026 15:47
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.

4 participants