Bind Duo session tokens to their workflow
What does this MR do and why?
Rails mints a GitLab API token for each Duo session. Nothing in that token says which workflow it belongs to. So the GitLab MCP server can't tell a call from web Agentic Chat or a flow apart from one made by an external agent like Claude Code. The follow-up MR needs that to pick the right access column (see References).
This MR adds a duo_workflow:<id> scope to every token minted for a known workflow. Nothing reads the scope yet, so this MR changes no governance behaviour.
Ai::DuoWorkflows::WorkflowTokenScopebuilds and parses the scope.CreateOauthAccessTokenServiceandCreateCompositeOauthAccessTokenServicetake aworkflow_id:and add the scope. This covers both database OAuth tokens and stateless JWT tokens.WorkflowTokenScope.foronly accepts an integer ID.WorkflowContextGenerationServicetakesworkflow_id:in its initializer and passes it to those two services. The callers that already have the workflow set it: the:wsand:executeWorkhorse endpoints,start_workflow_params, the CI runtime, server-side execution,ExecuteWorkflowServiceandResumeWorkplanService. Each of those has a spec that fails if the ID stops reaching the service.- The token reuse check in
already_scoped_for_ai_workflows?still reuses session tokens. It now reuses a token only when the token names no workflow, or exactly the workflow being connected. A token bound to another workflow, to two workflows, or with a malformed workflow scope gets a fresh token instead.
Why users can't forge it
Only Rails can mint a token with this scope. I checked the paths a user could try:
| Path | Result |
|---|---|
Register an OAuth application with duo_workflow:5 |
Invalid: "Scopes doesn't match those configured on the server" |
Request duo_workflow:5 in an OAuth grant, even on an app with user:* |
Rejected (user:5 passes the same check) |
Personal access token with duo_workflow:5 |
Invalid: "Scopes can only contain available scopes" |
| MCP dynamic client registration | Grants a single MCP scope only |
/authn/token_exchange |
Issues only gitlab-artifact-registry tokens with no scopes |
The first three are covered in workflow_token_scope_unforgeable_spec.rb.
Not bound on purpose
CreateAutonomousOauthAccessTokenService tokens carry only ai_workflows, with no mcp scope, so they never reach the MCP server. Binding them would serve no caller.
Ai::FlowTriggers::RunService mints a token for third-party agents and hands it to a user-supplied CI image as AI_FLOW_GITLAB_TOKEN. Binding it would let anyone holding that token pick that workflow's rules from any client, so it stays unbound. Those agents keep the Local column.
Security
This changes how Duo session tokens are minted, so it needs an Application Security review.
How to reproduce the issue
- On
master, start an Agentic Chat in the GitLab UI. - In the Rails console, find the newest OAuth token for your user:
OauthAccessToken.where(resource_owner_id: User.find_by_username('<you>').id).last.scopes.to_a - The scopes are
ai_workflows,mcpand maybeai_features. Nothing names the workflow.
How to test the fix
- Repeat the steps on this branch.
- The scopes now include
duo_workflow:<id>, and<id>is the chat's workflow. - Generate a token from the IDE or Duo CLI (
direct_access). It has noduo_workflowscope. - Try to create a personal access token or OAuth application with the scope
duo_workflow:1. Both are rejected.
bin/rspec ee/spec/lib/ai/duo_workflows/workflow_token_scope_spec.rb
bin/rspec ee/spec/lib/ai/duo_workflows/workflow_token_scope_unforgeable_spec.rb \
ee/spec/services/ai/duo_workflows/create_oauth_access_token_service_spec.rb \
ee/spec/services/ai/duo_workflows/create_composite_oauth_access_token_service_spec.rb \
ee/spec/services/ai/duo_workflows/workflow_context_generation_service_spec.rb \
ee/spec/lib/api/helpers/duo_workflow_helpers_spec.rbReferences
- Follow-up that reads the scope: !266252 (merged)
- Rollout issue: https://gitlab.com/gitlab-org/gitlab/-/work_items/632906
- Discussion: https://gitlab.com/groups/gitlab-org/-/work_items/21113#note_4104983475
Screenshots or screen recordings
Not applicable. There is no UI change.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.