Skip to content

fix: reduce JWT cookie size by removing teams/namespaces from session tokens#2765

Merged
crivetimihai merged 7 commits intomainfrom
fix-cookies-auth
Feb 8, 2026
Merged

fix: reduce JWT cookie size by removing teams/namespaces from session tokens#2765
crivetimihai merged 7 commits intomainfrom
fix-cookies-auth

Conversation

@crivetimihai
Copy link
Copy Markdown
Member

Summary

  • Session tokens (login/SSO) no longer embed teams or namespaces claims, resolving the 4KB cookie size limit (RFC 6265 §6.1) for users with many team memberships
  • Introduces token_use claim (session vs api) — session tokens resolve teams server-side via DB + L1/L2 cache, API tokens continue embedding teams
  • Adds cookie size validation with CookieTooLargeError (warn at 3800B, hard error at 4096B)
  • Improves error UX for cookie-related auth failures in login and SSO flows

Key changes across 21 files:

  • auth.py: _resolve_teams_from_db() (async) and _resolve_teams_from_db_sync() (sync with L1 cache) for server-side team resolution on session tokens
  • security_cookies.py: Cookie size validation, CookieTooLargeError exception
  • email_auth.py / sso.py: Removed dead code, session tokens no longer embed teams
  • token_scoping.py / rbac.py: Handle token_use: "session" by resolving teams from DB instead of JWT
  • streamablehttp_transport.py: Uses _resolve_teams_from_db_sync() with L1 cache (previously hit DB directly on every request)
  • admin.py: Cookie error handling in login/SSO redirects
  • create_jwt_token.py: Removed namespaces parameter

Results (verified on deployed environment with 109 teams):

Metric Before After
Admin session token ~4860 bytes (exceeds 4KB limit) 583 bytes
Non-admin session token ~variable 616 bytes
Size reduction ~8x

Closes #2757

Test plan

  • All 7960 unit tests pass
  • Linting clean: flake8, bandit (0 issues), pylint (10.00/10), interrogate (100%)
  • End-to-end verification on deployed environment with 109 teams
  • Admin sees all resources (29 tools, 23 servers, 20 prompts)
  • RBAC enforced for non-admin users (403 on unauthorized endpoints)
  • Cookie set correctly via admin UI login (583 bytes, well under 4KB)
  • token_use: "session" present in session tokens, no teams/namespaces embedded

… tokens

Session tokens (login/SSO) no longer embed teams or namespaces claims,
resolving the 4KB cookie size limit for users with many team memberships.

- Phase 1: Remove dead `namespaces` claim from all token creation paths
- Phase 2: Introduce `token_use` claim (session vs api) with server-side
  team resolution via DB/cache for session tokens
- Phase 3: Add cookie size validation (warn 3800B, error 4096B) with
  CookieTooLargeError exception
- Phase 4: Improve error UX for cookie auth failures in login and SSO

Closes #2757

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

- Remove unused get_teams() call and team_ids construction in
  create_access_token (dead code after teams removal from session tokens)
- Add _resolve_teams_from_db_sync() with L1 in-memory cache support
  for StreamableHTTP transport (was hitting DB on every request)

Signed-off-by: Mihai Criveti <crmihai1@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai crivetimihai added the revisit Revisit this PR at a later date to address further issues, or if problems arise. label Feb 8, 2026
@crivetimihai crivetimihai self-assigned this Feb 8, 2026
@crivetimihai crivetimihai added this to the Release 1.0.0-RC1 milestone Feb 8, 2026
@crivetimihai crivetimihai merged commit e24949d into main Feb 8, 2026
51 checks passed
@crivetimihai crivetimihai deleted the fix-cookies-auth branch February 8, 2026 18:06
@crivetimihai crivetimihai added the ica ICA related issues label Feb 11, 2026
kcostell06 pushed a commit to kcostell06/mcp-context-forge that referenced this pull request Feb 24, 2026
… tokens (IBM#2765)

* fix: reduce JWT cookie size by removing teams/namespaces from session tokens

Session tokens (login/SSO) no longer embed teams or namespaces claims,
resolving the 4KB cookie size limit for users with many team memberships.

- Phase 1: Remove dead `namespaces` claim from all token creation paths
- Phase 2: Introduce `token_use` claim (session vs api) with server-side
  team resolution via DB/cache for session tokens
- Phase 3: Add cookie size validation (warn 3800B, error 4096B) with
  CookieTooLargeError exception
- Phase 4: Improve error UX for cookie auth failures in login and SSO

Closes IBM#2757

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

* fix: remove dead code in email_auth and add sync cache for StreamableHTTP

- Remove unused get_teams() call and team_ids construction in
  create_access_token (dead code after teams removal from session tokens)
- Add _resolve_teams_from_db_sync() with L1 in-memory cache support
  for StreamableHTTP transport (was hitting DB on every request)

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

* Lint

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

* lint fixes

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

* lint

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

* lint

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

* lint

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

---------

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-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

ica ICA related issues revisit Revisit this PR at a later date to address further issues, or if problems arise.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][SCALE]: JWT cookie exceeds browser 4KB limit when user has many team memberships

1 participant