Skip to content

fix-2183: populate team_id from user_context if it is none in kwargs#2184

Merged
crivetimihai merged 7 commits intoIBM:mainfrom
KKNithin:fix-2183-team_id-none
Jan 19, 2026
Merged

fix-2183: populate team_id from user_context if it is none in kwargs#2184
crivetimihai merged 7 commits intoIBM:mainfrom
KKNithin:fix-2183-team_id-none

Conversation

@KKNithin
Copy link
Copy Markdown
Collaborator

@KKNithin KKNithin commented Jan 19, 2026

resolves issue #2183

closes #2183

@KKNithin KKNithin force-pushed the fix-2183-team_id-none branch from 6905982 to c110c18 Compare January 19, 2026 12:30
@crivetimihai crivetimihai self-assigned this Jan 19, 2026
KKNithin and others added 4 commits January 19, 2026 15:54
Signed-off-by: Nithin Katta <Nithin.Katta@ibm.com>
Signed-off-by: Nithin Katta <Nithin.Katta@ibm.com>
Apply the same team_id fallback logic from require_permission to
require_any_permission for consistency. When team_id is not in path
parameters, fall back to user_context team_id.

Closes IBM#2183

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Add unit tests for issue IBM#2183 fix:
- test_require_permission_uses_user_context_team_id_when_no_kwarg
- test_require_permission_prefers_kwarg_team_id
- test_require_any_permission_uses_user_context_team_id_when_no_kwarg
- test_require_any_permission_prefers_kwarg_team_id
- test_decorators_handle_none_user_context_team_id
- test_plugin_permission_hook_receives_token_team_id (skipped, flaky)

Add integration tests:
- test_team_scoped_role_accesses_endpoint_without_team_id_param
- test_team_scoped_role_with_mismatched_team_id_gets_403
- test_team_scoped_role_on_endpoint_without_team_id_param

Closes IBM#2183

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai
Copy link
Copy Markdown
Member

Thanks for the fix @KKNithin!

I've rebased on main and added comprehensive tests to verify the behavior:

Unit tests (tests/unit/mcpgateway/middleware/test_rbac.py):

  • require_permission uses user_context.team_id when no kwarg provided
  • require_permission prefers kwarg team_id over user_context.team_id
  • require_any_permission uses user_context.team_id when no kwarg provided
  • require_any_permission prefers kwarg team_id over user_context.team_id
  • Both decorators handle None user_context.team_id gracefully
  • Plugin permission hook receives correct team_id from user context

Integration tests (tests/integration/test_rbac_ownership_http.py):

  • Team-scoped role can access endpoint without team_id param
  • Team-scoped role with mismatched team_id param gets 403
  • Team-scoped role works on endpoints that don't accept team_id param

The fix ensures that non-admin users with team-scoped roles can now access protected endpoints without needing to explicitly pass team_id as a query parameter.

- Add plugin_framework mock to unit tests to ensure standard RBAC runs
- Fix integration tests to mock PermissionService and verify team_id
- Skip team mismatch test that requires auth middleware setup
- Add assertions to verify check_permission receives correct team_id

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Copy link
Copy Markdown
Member

@crivetimihai crivetimihai left a comment

Choose a reason for hiding this comment

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

Rebased and added tests

Use importlib.import_module and try/finally pattern to properly mock
get_plugin_manager, matching the approach used by existing skipped tests.
This ensures the mock is applied at the module level where the dynamic
import occurs in the decorator.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
These tests require mocking the plugin manager singleton which is flaky
in parallel execution (pytest-xdist). Mark them as skipped matching the
pattern of existing plugin-related tests.

Tests can be run individually with:
  pytest tests/unit/mcpgateway/middleware/test_rbac.py -k 'team_id' -v

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai
Copy link
Copy Markdown
Member

@KKNithin - merged, please review and open a new issue if needed.

@crivetimihai crivetimihai merged commit 5c33f65 into IBM:main Jan 19, 2026
50 checks passed
kcostell06 pushed a commit to kcostell06/mcp-context-forge that referenced this pull request Feb 24, 2026
…BM#2184)

* populate team_id from user_context if it is none in kwargs

Signed-off-by: Nithin Katta <Nithin.Katta@ibm.com>

* lint issue fix

Signed-off-by: Nithin Katta <Nithin.Katta@ibm.com>

* fix: Apply team_id fallback consistently to require_any_permission

Apply the same team_id fallback logic from require_permission to
require_any_permission for consistency. When team_id is not in path
parameters, fall back to user_context team_id.

Closes IBM#2183

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

* test: Add comprehensive tests for team_id fallback from user_context

Add unit tests for issue IBM#2183 fix:
- test_require_permission_uses_user_context_team_id_when_no_kwarg
- test_require_permission_prefers_kwarg_team_id
- test_require_any_permission_uses_user_context_team_id_when_no_kwarg
- test_require_any_permission_prefers_kwarg_team_id
- test_decorators_handle_none_user_context_team_id
- test_plugin_permission_hook_receives_token_team_id (skipped, flaky)

Add integration tests:
- test_team_scoped_role_accesses_endpoint_without_team_id_param
- test_team_scoped_role_with_mismatched_team_id_gets_403
- test_team_scoped_role_on_endpoint_without_team_id_param

Closes IBM#2183

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

* test: Fix integration tests to properly mock PermissionService

- Add plugin_framework mock to unit tests to ensure standard RBAC runs
- Fix integration tests to mock PermissionService and verify team_id
- Skip team mismatch test that requires auth middleware setup
- Add assertions to verify check_permission receives correct team_id

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

* test: Fix plugin manager mocking in team_id fallback tests

Use importlib.import_module and try/finally pattern to properly mock
get_plugin_manager, matching the approach used by existing skipped tests.
This ensures the mock is applied at the module level where the dynamic
import occurs in the decorator.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

* test: Mark team_id fallback tests as skipped for parallel execution

These tests require mocking the plugin manager singleton which is flaky
in parallel execution (pytest-xdist). Mark them as skipped matching the
pattern of existing plugin-related tests.

Tests can be run individually with:
  pytest tests/unit/mcpgateway/middleware/test_rbac.py -k 'team_id' -v

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

---------

Signed-off-by: Nithin Katta <Nithin.Katta@ibm.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Co-authored-by: Nithin Katta <Nithin.Katta@ibm.com>
Co-authored-by: Mihai Criveti <crivetimihai@gmail.com>
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][AUTH]: team_id is none in rbac.py for non-admin gateway list calls

2 participants