doc/development/permissions/granular_access/graphql_implementation_guide.md
To reduce the security impact of compromised Personal Access Tokens (PATs), granular or fine-grained PATs allow users to create tokens with fine-grained permissions limited to specific organizational boundaries (groups, projects, user, or instance-level). This enables users to follow the principle of least privilege by granting tokens only the permissions they need.
Granular PATs allow fine-grained access control through granular scopes that consist of a boundary and specific resource permissions. When authenticating GraphQL requests with a granular PAT, GitLab validates that the token's permissions include access to the requested resource at the specified boundary level.
This documentation is designed for community contributors and GitLab developers who want to make GraphQL queries and mutations compliant with granular PAT authorization.
This guide walks you through adding granular PAT authorization to GraphQL types and mutations. Before starting, review the Permission Naming Conventions documentation to understand the terminology used throughout.
[!note] These steps cover GraphQL types and mutations only. For REST API endpoint protection, refer to the REST API implementation guide.
For a detailed explanation of how the authorization system works internally, see the GraphQL architecture documentation.
The implementation follows this flow:
Goal: Find all GraphQL types and mutations for the resource you're working on.
Locate the GraphQL type for your resource in app/graphql/types/.
Example: For the issue resource, open app/graphql/types/issue_type.rb
Locate any related mutations in app/graphql/mutations/.
Example: For issues, check app/graphql/mutations/issues/
Identify which types and mutations need authorization:
IssueType, ProjectType)Mutations::Issues::Create)field :project on QueryType)Check if any types or mutations already have authorize_granular_token directives. You'll need to add directives to types/mutations that don't have them.
Goal: Define granular permissions following GitLab naming conventions.
For the naming conventions, see Naming Permissions in the conventions documentation.
When implementing granular PAT authorization, name permissions based on what the type represents or what the mutation does, not the GraphQL schema structure.
Examples:
IssueType → represents reading issues → permission name is read_issueMutations::Issues::Create → creates an issue → permission name is create_issueProjectType → represents reading project data → permission name is read_projectread_resource permission that covers all fields on the type
IssueType → read_issueProjectType → read_projectcreate_resource
Mutations::Issues::Create → create_issueupdate_resource
Mutations::Issues::Update → update_issuedelete_resource
Mutations::Issues::Destroy → delete_issueGoal: Create YAML definition files for each permission, if it doesn't exist yet.
Follow the instructions in the Permission Definition File section to create raw permission YAML files using the bin/permission command. This step is the same for both REST API and GraphQL implementations.
Goal: Bundle raw permissions into assignable permissions for a simpler user experience.
Follow the instructions in the Assignable Permissions section to create assignable permission YAML files. This step is the same for both REST API and GraphQL implementations.
Goal: Add granular PAT authorization directives to GraphQL types and mutations.
Use the authorize_granular_token method to declare permissions on types and mutations. This method is available on all GraphQL types (via Types::BaseObject) and mutations (via Mutations::BaseMutation).
Method Signature:
authorize_granular_token(permissions:, boundary_type:, boundary: nil, boundary_argument: nil)
Parameters:
| Parameter | Description |
|---|---|
permissions | (Required) Symbol representing the required permission (e.g., :read_issue). Can also be an array of permissions. Must be a valid permission from Authz::PermissionGroups::Assignable.all_permissions — validated by the gitlab:permissions:validate Rake task. |
boundary_type | (Required) Symbol declaring the type of authorization boundary (:project, :group, :user, :instance). Validated against the assignable permission boundaries by the gitlab:permissions:validate Rake task. |
boundary | Symbol representing the method to call on the resolved object to extract the boundary (e.g., :project). Use :user or :instance for standalone resources. |
boundary_argument | Symbol representing the argument name containing the boundary path (e.g., :project_path). |
For object types:
class IssueType < BaseObject
authorize_granular_token permissions: :read_issue, boundary: :project, boundary_type: :project
end
For mutations:
module Mutations
module Issues
class Create < BaseMutation
authorize_granular_token permissions: :create_issue, boundary_argument: :project_path, boundary_type: :project
end
end
end
boundary appliesissue.title when IssueType has directive):id argument returning the type (enables ID fallback)boundary: :user or boundary: :instance:id argument returning the type (object not available, raises ArgumentError)boundary_argument appliesboundary_argument directiveUse boundary: :user or boundary: :instance for resources that don't belong to a specific project or group:
class UserSettingType < BaseObject
authorize_granular_token permissions: :read_user_settings, boundary: :user, boundary_type: :user
end
boundary and boundary_argumentUse boundary when... | Use boundary_argument when... |
|---|---|
The type has a method to get the boundary (e.g., issue.project) | The boundary is passed as a field argument (e.g., projectPath) |
| Protecting an object type's fields | Protecting a mutation |
Protecting a query field with :id argument | Protecting a query field with a path argument |
Goal: Verify that granular PAT permissions are correctly enforced on GraphQL types and mutations.
Add the 'authorizing granular token permissions for GraphQL' shared example:
it_behaves_like 'authorizing granular token permissions for GraphQL', :<permission_name> do
let(:user) { current_user }
let(:boundary_object) { <boundary_object> }
let(:request) { post_graphql(query, token: { personal_access_token: pat }) }
end
Example:
it_behaves_like 'authorizing granular token permissions for GraphQL', :read_issue do
let(:user) { current_user }
let(:boundary_object) { project }
let(:request) { post_graphql(query, token: { personal_access_token: pat }) }
end
it_behaves_like 'authorizing granular token permissions for GraphQL', :<permission_name> do
let(:user) { current_user }
let(:boundary_object) { <boundary_object> }
let(:request) { post_graphql_mutation(mutation, token: { personal_access_token: pat }) }
end
The boundary_object must match the boundary_type:
| Boundary Type | Boundary Object |
|---|---|
:project | project |
:group | group |
:user | :user |
:instance | :instance |
Important: When the boundary object is a :project or :group, the user must be a member of that namespace (project or group) for the authorization to be granted.
What These Tests Verify:
granular_personal_access_tokens is properly enforced (denies access when disabled)Goal: Manually test your implementation in a local environment to verify permissions work as expected before creating a merge request.
Setup:
In Rails console, create a granular PAT for a user:
# Enable feature flags
Feature.enable(:granular_personal_access_tokens)
Feature.enable(:granular_personal_access_tokens_for_graphql)
user = User.human.first
# Create granular token
token = PersonalAccessTokens::CreateService.new(
current_user: user,
target_user: user,
organization_id: user.organization_id,
params: { expires_at: 1.month.from_now, scopes: ['granular'], granular: true, name: 'gPAT' }
).execute[:personal_access_token]
# Get the appropriate boundary object (project, group, :user, or :instance)
project = user.projects.first
boundary = Authz::Boundary.for(project)
# Create scope with the assignable permissions being tested
scope = Authz::GranularScope.new(namespace: boundary.namespace, access: boundary.access, permissions: [:read_work_item, :write_work_item])
# Add the scope to the token
Authz::GranularScopeService.new(token).add_granular_scopes(scope)
# Copy a curl command for testing a GraphQL query
query = '{ project(fullPath: \"' + project.full_path + '\") { issues { nodes { title } } } }'
IO.popen('pbcopy', 'w') { |f| f.puts "curl \"http://#{Gitlab.host_with_port}/api/graphql\" --request POST --header \"PRIVATE-TOKEN: #{token.token}\" --header \"Content-Type: application/json\" --data '{\"query\": \"#{query}\"}'" }