Skip to content

feat: trusted gateway authentication for per-user access - #2121

Open
unsupo wants to merge 4 commits into
developmentfrom
feat/trusted-gateway-auth
Open

unsupo wants to merge 4 commits into
developmentfrom
feat/trusted-gateway-auth

Conversation

@unsupo

@unsupo unsupo commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds trusted gateway authentication: when cidx is fronted by a gateway, per-user RS256 JWTs forwarded by the gateway are validated against the gateway JWKS and JIT-provisioned via the existing OIDC path. This lets cidx enforce its own per-user access control (groups / repo_group_access) for gateway-proxied users.

New files

  • gateway_jwt_validator.py — RS256 signature verification against gateway JWKS, with rate-limited refetches
  • gateway_identity_authenticator.py — bridges validator + OIDCManager; async JWKS fetch via worker thread
  • gateway_auth_wiring.py — fail-safe factory with isolated identity-link store
  • 4 test files covering validator, authenticator, wiring, and dispatch integration

Modified files

  • config_manager.py — adds TrustedGatewayConfig dataclass (disabled by default)
  • dependencies.py — adds Priority 1.5 dispatch between MCP credentials and OAuth/JWT

Security hardening

  • JWKS fetch offloaded to worker thread (was blocking the async event loop)
  • JWKS refetch rate-limited to prevent amplification via forged kid values
  • Gateway uses isolated identity-link store (gateway_oauth.db) so subjects from different trust domains cannot collide
  • Pre-elevation scope documented for TOTP audit trail

Test plan

  • test_gateway_jwt_validator.py — valid token, issuer routing, alg-confusion, wrong audience, expired, missing sub, custom claims
  • test_gateway_identity_authenticator.py — claim mapping, empty groups, invalid token, failed provisioning
  • test_gateway_auth_wiring.py — enabled/disabled/None/misconfigured, isolated store assertion
  • test_gateway_mcp_dispatch.py — gateway token provisioned, invalid 401, non-gateway fallthrough, no authenticator noop
  • CI passes (lint, test, rust, check-version, disclosure-scan)

Review findings addressed:

# Finding Commit Status
1 OIDCManager has no db_path at this head fix: add db_path parameter Fixed (cherry-picked from PR #2128)
2 JWKS cooldown is global — one forged token blocks all users fix: per-kid JWKS cooldown Fixed (per-kid dict + threading.Lock)
3 Every gateway bearer opens a full-scope elevation window fix: remove gateway pre-elevation Fixed (removed, gateway users step up via TOTP)
4 Synchronous Postgres upsert on event loop fix: remove gateway pre-elevation Fixed (pre-elevation removed entirely)
5 Full OIDC resolution on every request Deferred (follow-up: split lean per-request path)
6 Per-pod SQLite instead of cluster backend fix: wire oauth_backend Fixed (oauth_backend passthrough)
7 Email auto-link to any existing account fix: wire oauth_backend Fixed (allow_email_auto_link=False default)
8 Divergent copies in PR #2128 Coordination: PR #2128 should drop its 3 gateway files
9 Pre-elevation is pasted copy of OAuth block fix: remove gateway pre-elevation Fixed (block removed)
10 PyJWKClient has no timeout fix: per-kid JWKS cooldown Fixed (timeout=5)
11 JWKS URI scheme not enforced fix: per-kid JWKS cooldown Fixed (https:// required)
12 Missing elevation_username fix: remove gateway pre-elevation Fixed (set before return)
13 Nothing wires the authenticator at this head Coordination: PR #2133 supplies the lifespan call
14 Duplicate group_mappings normalisation fix: wire oauth_backend Fixed (_normalize_group_mappings extracted)
15 Commit message describes flaw Nit: future commits only

Merge order: PR #2128 (only its OIDCManager db_path change, already cherry-picked here) → this PR → PR #2133 (lifespan wiring). PR #2128 should drop its divergent copies of the 3 gateway modules.

Add support for validating per-user RS256 JWTs forwarded by a trusted
gateway. When enabled, cidx verifies the RS256 signature against the
gateway's JWKS endpoint and JIT-provisions the user via the existing
OIDC path, so cidx enforces its own per-user access control.

Components:
- GatewayJwtValidator: RS256 signature + iss/aud/exp verification with
  JWKS rate-limiting to prevent refetch amplification attacks
- GatewayIdentityAuthenticator: bridges the validator and OIDCManager;
  offloads the synchronous JWKS fetch to a worker thread via
  anyio.to_thread.run_sync so it never blocks the event loop
- gateway_auth_wiring: fail-safe factory with isolated identity-link
  store (gateway_oauth.db, separate from interactive SSO's oauth.db)
- TrustedGatewayConfig: dataclass with defaults (disabled by default)
- Priority 1.5 dispatch in dependencies.py between MCP credentials
  and OAuth/JWT, with session pre-elevation and TOTP documentation

Security hardening vs. the base implementation:
- JWKS fetch runs in worker thread (was blocking the event loop)
- JWKS refetch rate-limited (forged kids cannot amplify requests)
- Pre-elevation scope documented for TOTP audit trail
Jonathan Arndt added 3 commits November 6, 2026 16:22
- Replace the global _last_jwks_error with a per-kid cooldown dict
  so a forged token with an unknown kid cannot block legitimate
  tokens whose kid is already cached
- Require an explicit kid header; tokens without one are rejected
  without touching the cooldown
- Add timeout=5 to PyJWKClient so a hanging JWKS endpoint cannot
  exhaust the anyio thread limiter
- Reject non-https jwks_uri at construction time
Remove the automatic full-scope elevation window for gateway bearers.
The gateway token is not proof of a second factor (unlike OAuth tokens
cidx issued after its own MFA flow), so gateway users step up through
the TOTP elevate tool like JWT-bearer users. Also set elevation_username
so the elevation decorator can bind the window to this user.
- Pass oauth_backend through build_gateway_authenticator to OIDCManager
  so multi-replica deployments use cluster storage instead of per-pod
  SQLite
- Add allow_email_auto_link=False (default) to TrustedGatewayConfig;
  when disabled, gateway identity resolution never sees the email claim
  and cannot auto-link to existing local accounts
- Extract _normalize_group_mappings() and call it from both
  OIDCProviderConfig and TrustedGatewayConfig.__post_init__
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.

1 participant