feat: add extra claims to self-issued token and verify it
What does this merge request do and why?
Related issue: https://gitlab.com/gitlab-org/modelops/applied-ml/code-suggestions/ai-assist/-/issues/1735+
This MR enhances the self-issued token security by adding extra claims for project and namespace metadata, and implements verification to ensure these claims match the request headers.
Changes
Token Generation (/code/user_access_token endpoint)
- Removed validation for
X-Gitlab-Global-User-Id,X-Gitlab-Instance-Id, andX-Gitlab-Realmheaders (now sourced from token claims) - Added support for extra claims in self-issued tokens for SaaS environments:
gitlab_project_idgitlab_namespace_idgitlab_root_namespace_id
- Extra claims are only added when
gitlab_realmis "saas" to prevent arbitrary values from self-managed instances - Updated to use
current_user.global_user_idanduser_claimsfrom the authenticated token
Token Verification (Proxy endpoints)
- Added new
verify_project_namespace_metadata()decorator to validate request headers against token claims - Applied verification to all proxy endpoints (Anthropic, OpenAI, Vertex AI)
- Verifies that the following headers match the token claims:
- Instance ID
- Project ID
- Namespace ID
- Root namespace ID
- Returns 403 Forbidden on mismatch
Dependencies
- Updated
gitlab-cloud-connectorto version^3.15.1to support the newUserClaimsand extra claims functionality
Tests
- Refactored user access token tests to separate SaaS and self-managed scenarios
- Added tests for extra claims in SaaS tokens
- Added comprehensive mismatch tests for all proxy endpoints
- Updated all test fixtures to include required claims and headers
Edited by Igor Drozdov