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.

  1. Ai::DuoWorkflows::WorkflowTokenScope builds and parses the scope.
  2. CreateOauthAccessTokenService and CreateCompositeOauthAccessTokenService take a workflow_id: and add the scope. This covers both database OAuth tokens and stateless JWT tokens. WorkflowTokenScope.for only accepts an integer ID.
  3. WorkflowContextGenerationService takes workflow_id: in its initializer and passes it to those two services. The callers that already have the workflow set it: the :ws and :execute Workhorse endpoints, start_workflow_params, the CI runtime, server-side execution, ExecuteWorkflowService and ResumeWorkplanService. Each of those has a spec that fails if the ID stops reaching the service.
  4. 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

  1. On master, start an Agentic Chat in the GitLab UI.
  2. 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
  3. The scopes are ai_workflows, mcp and maybe ai_features. Nothing names the workflow.

How to test the fix

  1. Repeat the steps on this branch.
  2. The scopes now include duo_workflow:<id>, and <id> is the chat's workflow.
  3. Generate a token from the IDE or Duo CLI (direct_access). It has no duo_workflow scope.
  4. 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.rb

References

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.

Edited by Raounak Sharma

Merge request reports

Loading
Loading