Skip to content

[BUG]: RBAC middleware crashes on token creation #2821

@madhav165

Description

@madhav165

Description

Token creation via the API fails with an AttributeError when the RBAC middleware attempts to derive a team ID from the request payload.

The _derive_team_from_payload() function in mcpgateway/middleware/rbac.py assumes that any kwarg named request is a FastAPI Request object and calls .headers on it. However, several token endpoints (e.g. create_token, create_team_token, update_token) use request: TokenCreateRequest as a parameter name, passing a Pydantic model instead.

Steps to Reproduce

  1. Start the gateway (e.g. via Docker Compose with nginx)
  2. Authenticate and obtain a valid JWT
  3. POST /tokens with a valid TokenCreateRequest body
  4. Observe 500 error

Error

File "/app/mcpgateway/middleware/rbac.py", line 414, in _derive_team_from_payload
    content_type = request.headers.get("content-type", "")
                   ^^^^^^^^^^^^^^^
File ".../pydantic/main.py", line 1026, in __getattr__
    raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'TokenCreateRequest' object has no attribute 'headers'

Root Cause

_derive_team_from_payload() at line 412-413:

request = kwargs.get("request")
if request:
    content_type = request.headers.get("content-type", "")

No type check is performed, so any object passed as a kwarg named request is treated as a FastAPI Request.

Impact

  • All token creation endpoints are broken (POST /tokens, POST /tokens/teams/{team_id})
  • Token update endpoint is also affected (PUT /tokens/{token_id})
  • Any other endpoint using request as a Pydantic body parameter name would hit the same issue

Environment

  • Observed in Docker Compose deployment behind nginx
  • Affects any deployment where RBAC middleware is active

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingicaICA related issuestriageIssues / Features awaiting triage

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions