doc/development/permissions/granular_access/graphql_architecture.md
This document explains how the GranularTokenAuthorization field extension works to enforce granular Personal Access Token (PAT) permissions on GraphQL queries and mutations. For a step-by-step implementation guide, see GraphQL implementation guide.
The granular token authorization system adds fine-grained permission checks to GraphQL fields based on directives applied to types, fields, and mutations. It ensures that granular PATs can only access resources they have explicit permissions for within specific project or group boundaries.
Feature Flag: This feature requires both the granular_personal_access_tokens and the granular_personal_access_tokens_for_graphql feature flags to be enabled for the token's user. When either one is disabled, granular PATs will not work for GraphQL requests.
lib/gitlab/graphql/authz/granular_token_authorization.rbTypes::BaseFieldapp/graphql/directives/authz/granular_scope.rbpermissions: Array of required permission strings (e.g., ['read_issue'])boundary: Method name to extract boundary from resolved objectboundary_argument: Argument name containing the boundaryboundary_type: The type of authorization boundary (project, group, user, instance). Used for validation and documentation of the permission boundarylib/gitlab/graphql/authz/directive_finder.rbTypeUnwrapper module for unwrapping GraphQL type wrapperslib/gitlab/graphql/authz/boundary_extractor.rblib/gitlab/graphql/authz/type_unwrapper.rblib/gitlab/graphql/authz/authorize_granular_token.rbauthorize_granular_token helper method for cleaner directive syntaxTypes::BaseObject, Types::BaseField, and Mutations::BaseMutationauthorize_granular_token(permissions:, boundary_type:, boundary: nil, boundary_argument: nil)gitlab:permissions:validate Rake task against Authz::PermissionGroups::Assignable.all_permissions.1. GraphQL request arrives (query or mutation)
2. GraphQL Ruby begins parsing and validation
3. Execution begins with root fields
For each field being resolved:
1. GraphQL Ruby calls field extensions in order
├─ CallsGitaly::FieldExtension (dev/test only)
├─ Present::FieldExtension
├─ Authorize::FieldExtension
└─ GranularTokenAuthorization ← WE ARE HERE
Step 1: Early Exit Conditions
def authorize_field(object, arguments, context)
return unless authorization_enabled?(context) # Only authorize granular PATs with feature flag enabled
return if SkipRules.new(@field).should_skip? # Skip certain fields
# ...
end
def authorization_enabled?(context)
token = context[:access_token]
token && token.try(:granular?) && Feature.enabled?(:granular_personal_access_tokens_for_graphql, token.user)
end
:granular_personal_access_tokens_for_graphql must be enabled for the usercreateIssue.issue) - Authorization happens on the mutation itself, not the response wrapperissue.userPermissions) - These return permission information, not actual dataStep 2: Directive Discovery
directive = DirectiveFinder.new(@field).find(object)
The DirectiveFinder checks for directives in this priority order, returning the first match found:
FIELD_DEFINITION): Applied directly to the fieldOBJECT): Applied to the type that owns the fieldobject is providedIssue) from GitlabSchema.types[Type] → TypeType! → TypeTypeConnection → Type (e.g., IssueConnection → IssueType)boundary_argument and boundary strategiesboundary with an :id argument, enables ID fallback for boundary extractionStep 3: Boundary Extraction
boundary = BoundaryExtractor.new(object:, arguments:, context:, directive:).extract
permissions = directive.arguments[:permissions]
[!note] When no directive is found,
boundaryandpermissionsare bothnil. The authorization service will return the error message: "Unable to determine boundaries and permissions for authorization".
The boundary extractor behavior:
boundary: 'user' or boundary: 'instance'): Returns Authz::Boundary::NilBoundaryProjectBoundary or GroupBoundary)nil (not wrapped in NilBoundary)Supported boundary types:
Authz::Boundary::ProjectBoundary - for Project resourcesAuthz::Boundary::GroupBoundary - for Group resourcesAuthz::Boundary::NilBoundary - for standalone resources (user-scoped or instance-wide)The extractor uses one of four strategies:
Strategy A: boundary_argument (for mutations and query fields)
# Directive says: boundary_argument: 'project_path'
# Field argument: project_path: "gitlab-org/gitlab"
extract_from_argument('project_path')
↓
args[:project_path] = "gitlab-org/gitlab"
↓
resolve_path("gitlab-org/gitlab")
↓
Project.find_by_full_path("gitlab-org/gitlab") || Group.find_by_full_path("gitlab-org/gitlab")
↓
returns Project or Group instance
Strategy B: boundary (for type fields with resolved object)
The boundary method must be one of the valid accessor methods: project, group, or itself. An ArgumentError is raised for any other value.
# Directive says: boundary: 'project'
# Object: Issue instance
extract_from_method('project')
↓
unwrap_object(object) # Issue
↓
object_matches_boundary_type?('project') # false (Issue ≠ Project)
↓
VALID_BOUNDARY_ACCESSOR_METHODS.include?('project') # true
↓
object.respond_to?(:project) # true
↓
object.project
↓
returns Project instance
When using boundary: 'itself', the object is returned as its own boundary. This is useful for types that are themselves a Project or Group:
# Directive says: boundary: 'itself'
# Object: Project instance
extract_from_method('itself')
↓
unwrap_object(object) # Project
↓
object_matches_boundary_type?('itself') # false (Project ≠ Itself)
↓
VALID_BOUNDARY_ACCESSOR_METHODS.include?('itself') # true
↓
object.itself # Ruby's Object#itself returns self
↓
returns Project instance
Strategy C: ID Fallback (for query fields with GlobalID)
Used when:
boundary: 'project':id argument with GlobalID# Query: issue(id: "gid://gitlab/Issue/123")
# Directive says: boundary: 'project'
# Object: nil (query field, not resolved yet)
extract_from_id_argument
↓
args[:id] = "gid://gitlab/Issue/123"
↓
GlobalID.parse("gid://gitlab/Issue/123")
↓
GlobalID::Locator.locate(gid) # Issue.find(123) - extra DB query
↓
extract_boundary_from_object(issue)
↓
issue.project
↓
returns Project instance
Performance note: This strategy fetches the record twice - once for authorization and once during field resolution, although the query will be cached.
Strategy D: Standalone boundaries (for user-scoped or instance-wide resources)
Used when:
boundary: 'user' (user-scoped resources)boundary: 'instance' (instance-wide resources)# Directive says: boundary: 'user'
# Resource doesn't belong to a specific project/group
standalone_boundary?('user')
↓
@boundary_accessor.to_sym # :user
↓
Authz::Boundary.for(:user)
↓
returns Authz::Boundary::NilBoundary.new(:user)
↓
Authorization checks token has appropriate permissions
This strategy is used for resources that don't belong to a specific project or group boundary but are user-scoped or instance-wide.
Step 4: Authorization Check
authorize_with_cache!(context, boundary, permissions)
This method:
Checks cache: context[:authz_cache] to avoid duplicate checks
Calls authorization service:
::Authz::Tokens::AuthorizeGranularScopesService.new(
boundaries: boundary,
permissions: permissions,
token: context[:access_token]
).execute
Verifies: Token has required permissions for the boundary
Raises error if unauthorized: raise_resource_not_available_error!(response.message)
Caches result to avoid redundant checks
Step 5: Field Resolution
yield(object, arguments, **rest)
If authorization passes, the field resolver executes and returns its value.
boundary_argumentGraphQL Request:
mutation {
createIssue(input: {
projectPath: "gitlab-org/gitlab",
title: "New issue"
}) {
issue { id }
}
}
Directive:
class Create < BaseMutation
authorize_granular_token permissions: :create_issue, boundary_argument: :project_path, boundary_type: :project
end
Timeline:
createIssue fieldobject = nil (root mutation field)arguments[:input][:project_path]Project.find_by_full_path("gitlab-org/gitlab") → Projectcreate_issue permission for this project?boundary (nested field)GraphQL Request:
query {
project(fullPath: "gitlab-org/gitlab") {
issues {
nodes {
title # ← Authorization here
description # ← And here
}
}
}
}
Directive:
class IssueType < BaseObject
authorize_granular_token permissions: :read_issue, boundary: :project, boundary_type: :project
end
Timeline (for title field):
title fieldobject = Issue instance (already resolved)IssueType (owner of title field)issue.projectread_issue permission for this project?description, etc.) - no additional DB queriesGraphQL Request:
query {
issue(id: "gid://gitlab/Issue/123") {
title
}
}
Directive:
class IssueType < BaseObject
authorize_granular_token permissions: :read_issue, boundary: :project, boundary_type: :project
end
Timeline:
issue field (returns IssueType)object = nil (root query field)IssueType):id argument presentissue.projectread_issue permission for this project?Per-Request Cache:
context[:authz_cache] = Set.new
cache_key = [permissions&.sort, boundary&.class, boundary&.namespace&.id]
# Example cache key for `read_issue` on a project:
# [["read_issue"], Authz::Boundary::ProjectBoundary, 123]
permissions&.sort: Sorted array of lowercase permission stringsboundary&.class: The boundary wrapper class (e.g., Authz::Boundary::ProjectBoundary)boundary&.namespace&.id: The namespace ID (varies by boundary type):
ProjectBoundary: project.project_namespace.idGroupBoundary: group.idNilBoundary: nilreturn unless authorization_enabled?(context)
return if SkipRules.new(@field).should_skip?
granular_personal_access_tokens_for_graphql must be enabledWhen authorization fails:
raise_resource_not_available_error!(response.message)
For GraphQL:
errors arraynullExample response:
{
"data": { "issue": null },
"errors": [{
"message": "Insufficient permissions",
"path": ["issue"]
}]
}
No directive found (with granular PAT)
boundary: nil, permissions: nil"Unable to determine boundaries and permissions for authorization"Directive has empty permissions array
permissions: [] (boundary provided)"Unable to determine permissions for authorization"permissions: []Boundary extraction returns nil (resource not found)
boundary: nil (permissions still provided)"Unable to determine boundaries for authorization"issue.project returns nil)boundary nor boundary_argument configuredNilBoundary objectInvalid GlobalID format
GlobalID.parse("invalid") returns nilnil → authorization error"Unable to determine boundaries for authorization"Boundary method returns nil
issue.project returns nilnil → authorization error"Unable to determine boundaries for authorization"GlobalID points to non-existent record
GlobalID::Locator.locate(gid) raises ActiveRecord::RecordNotFound, rescued and returns nilnil → authorization error"Unable to determine boundaries for authorization"Invalid boundary method
ArgumentError: "Invalid boundary method: 'foo'"boundary value not in the valid accessor methods (project, group, itself)Object doesn't respond to boundary method
Behavior: Raises ArgumentError: "Boundary method 'project' not found on Project"
Cause: Using a valid boundary method (e.g., boundary: 'project') but the object doesn't have that method
Exceptions:
:id argument, uses ID fallback insteadExample:
# IssueType has: boundary: 'project'
# Field: project.issue(iid: "1")
# object = Project (not Issue)
# Project matches 'project' → returns Project
Invalid permission name
gitlab:permissions:validate Rake taskAuthz::PermissionGroups::Assignable.all_permissionsMultiple directives found