GeezerVison is showing github.com — Detect missing fine grain permission scopes, notify user on steps to take to refresh scopes · Issue #9403 · cli/cli
Detect missing fine grain permission scopes, notify user on steps to take to refresh scopes #9403DescriptionDescribe the bug
The Github REST API docs specify that creating a PR using a fine-grained permissions token only requires the
"Pull requests" repository permissions (write)
Adding this permission to the fine-grained token also automatically adds the "Metadata" permission as mandatory.
However, this results in an error along the lines of: GraphQL: Resource not accessible by personal access token (repository.defaultBranchRef).
Steps to reproduce the behavior
- Create a fine-grained personal access token with the permissions:
- Pull Requests: Read and write
- Metadata (mandatory): Read-only
- Locally, create a new branch in a repository, make a commit to it and push the branch to the remote
- Run
gh pr create, either interactively, or with flags
- See an error like:
GraphQL: Resource not accessible by personal access token (repository.defaultBranchRef)
If the "Contents: Read-only" permission is also added to the fine-grained personal access token, this issue goes away.
Expected vs actual behavior
Expected behaviour: Able to create a PR using the GH CLI using a fine grained permissions token with permissions that are defined in the docs.
Actual behaviour: Extra permissions are required.
This seems to be either:
- An error in the docs
- REST API docs don't line up with the equivalent action(s) in the GH CLI
- A bug in the scope of the "Pull Requests: Read and write" permission.
Logs
>> DEBUG=api gh pr create
[git remote -v]
[git config --get-regexp ^remote\..*\.gh-resolved$]
* Request at 2024-08-01 11:47:35.435635 +0100 BST m=+0.183569918
* Request to https://api.github.com/graphql
> POST /graphql HTTP/1.1
> Host: api.github.com
> Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
> Authorization: token [READACTED]
> Content-Length: 405
> Content-Type: application/json; charset=utf-8
> Graphql-Features: merge_queue
> Time-Zone: Europe/London
> User-Agent: GitHub CLI 2.53.0
GraphQL query:
fragment repo on Repository {
id
name
owner { login }
viewerPermission
defaultBranchRef {
name
}
isPrivate
}
query RepositoryNetwork {
viewer { login }
[READACTED]: repository(owner: "[READACTED]", name: "[READACTED]") {
...repo
parent {
...repo
}
}
}
GraphQL variables: null
< HTTP/2.0 200 OK
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
< Content-Security-Policy: default-src 'none'
< Content-Type: application/json; charset=utf-8
< Date: Thu, 01 Aug 2024 10:47:35 GMT
< Github-Authentication-Token-Expiration: 2024-08-31 11:36:27 +0100
< Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
< Server: github.com
< Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
< Vary: Accept-Encoding, Accept, X-Requested-With
< X-Content-Type-Options: nosniff
< X-Frame-Options: deny
< X-Github-Media-Type: github.v4; param=merge-info-preview.nebula-preview; format=json
< X-Github-Request-Id: EA39:9537D:291A3FF:2BBE2CD:66AB67C7
< X-Ratelimit-Limit: 5000
< X-Ratelimit-Remaining: 4980
< X-Ratelimit-Reset: 1722510409
< X-Ratelimit-Resource: graphql
< X-Ratelimit-Used: 20
< X-Xss-Protection: 0
{
"data": {
"viewer": {
"login": "[READACTED]"
},
"[READACTED]": {
"id": "[READACTED]=",
"name": "[READACTED]",
"owner": {
"login": "[READACTED]"
},
"viewerPermission": "ADMIN",
"defaultBranchRef": null,
"isPrivate": true,
"parent": null
}
},
"errors": [
{
"type": "FORBIDDEN",
"path": [
"[READACTED]",
"defaultBranchRef"
],
"extensions": {
"saml_failure": false
},
"locations": [
{
"line": 7,
"column": 3
}
],
"message": "Resource not accessible by personal access token"
}
]
}
* Request took 331.800666ms
GraphQL: Resource not accessible by personal access token ([READACTED].defaultBranchRef)
Reacted by Fredrik Arvidsson, Maria Ines Parnisari, octvs, Craig Jones, Roman Cone, Aaron Hamilton, Kaden Wilkinson, Cameron, Eydel Rivero, Heitor de Bittencourt and 2 more Reacted by Andy Feller, Tanguy Krotoff, Craig Jones, Arthur Asenheimer and Cameron Activityandyfeller commented on Aug 6, 2024 @jjacobgreen : appreciate you creating this issue and apologies as I imagine this has caused some amount of frustration! 🙇
This seems to be either:
- An error in the docs
- REST API docs don't line up with the equivalent action(s) in the GH CLI
- A bug in the scope of the "Pull Requests: Read and write" permission.
I think an assumption is being made where the GitHub CLI gh pr create command is a 1:1 map to GitHub APIs, which is typically not the case as this command is an attempt to simplify multiple API interactions to create pull requests versus going to the web browser.
Aside from the fact that gh repo clone won't clone the repository with these limited permissions, let's dig into what is going on and why this is not an error of documentation or scope of GitHub API permissions but complexity of a product.
Code in question
|
var baseRepo *api.Repository |
|
if br, err := repoContext.BaseRepo(opts.IO); err == nil { |
|
if r, ok := br.(*api.Repository); ok { |
|
baseRepo = r |
|
} else { |
|
// TODO: if RepoNetwork is going to be requested anyway in `repoContext.HeadRepos()`, |
|
// consider piggybacking on that result instead of performing a separate lookup |
|
baseRepo, err = api.GitHubRepo(client, br) |
|
if err != nil { |
|
return nil, err |
|
} |
|
} |
|
} else { |
|
return nil, err |
|
} |
In the above snippet, gh pr create is pulling information about the repository in order to figure out how to create the PR including branch information, which fails when using the fine-grained PAT you described:
$ GH_DEBUG=api gh pr create
[git remote -v]
[git config --get-regexp ^remote\..*\.gh-resolved$]
* Request at 2024-08-05 16:47:50.324013 -0400 EDT m=+0.049310626
* Request to https://api.github.com/graphql
> POST /graphql HTTP/1.1
> Host: api.github.com
> Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
> Authorization: token ████████████████████
> Content-Length: 391
> Content-Type: application/json; charset=utf-8
> Graphql-Features: merge_queue
> Time-Zone: America/New_York
> User-Agent: GitHub CLI 2.54.0
GraphQL query:
fragment repo on Repository {
id
name
owner { login }
viewerPermission
defaultBranchRef {
name
}
isPrivate
}
query RepositoryNetwork {
viewer { login }
repo_000: repository(owner: "andyfeller", name: "test-3") {
...repo
parent {
...repo
}
}
}
GraphQL variables: null
< HTTP/2.0 200 OK
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
< Content-Security-Policy: default-src 'none'
< Content-Type: application/json; charset=utf-8
< Date: Mon, 05 Aug 2024 20:47:50 GMT
< Github-Authentication-Token-Expiration: 2024-08-12 16:40:34 -0400
< Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
< Server: GitHub.com
< Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
< Vary: Accept-Encoding, Accept, X-Requested-With
< X-Content-Type-Options: nosniff
< X-Frame-Options: deny
< X-Github-Media-Type: github.v4; param=merge-info-preview.nebula-preview; format=json
< X-Github-Request-Id: A292:5F13E:B2632A7:14B3A3F7:66B13A76
< X-Ratelimit-Limit: 5000
< X-Ratelimit-Remaining: 4747
< X-Ratelimit-Reset: 1722892210
< X-Ratelimit-Resource: graphql
< X-Ratelimit-Used: 253
< X-Xss-Protection: 0
{
"data": {
"viewer": {
"login": "andyfeller"
},
"repo_000": {
"id": "R_kgDOKBkG6g",
"name": "test-3",
"owner": {
"login": "andyfeller"
},
"viewerPermission": "ADMIN",
"defaultBranchRef": null,
"isPrivate": true,
"parent": null
}
},
"errors": [
{
"type": "FORBIDDEN",
"path": [
"repo_000",
"defaultBranchRef"
],
"extensions": {
"saml_failure": false
},
"locations": [
{
"line": 7,
"column": 3
}
],
"message": "Resource not accessible by personal access token"
}
]
}
* Request took 343.27975ms
GraphQL: Resource not accessible by personal access token (repo_000.defaultBranchRef)
Again, because this command is NOT a simple 1:1 mapping to the GitHub APIs, there is more going on here.
Where to go from here?
Ideally, the GitHub REST and GraphQL APIs would provide information about missing permission scopes, which gh could detect and tell you a gh auth refresh command to run to address the missing permissions. I say this because static documentation stating the scopes needed per gh command would easily become incorrect as the features develop.
However, fine-grained tokens are still in public beta status and there is very little information in API response to detect which scopes are missing.
So the question is how might gh commands, which span multiple API requests, accurately communicate the fine grained token scopes needed? jjacobgreen commented on Aug 6, 2024 @andyfeller Thanks very much for taking the time to explain the actual nature of the issue here. It's much appreciated. Now you mention it, of course, the gh pr create command is not going to be a 1:1 map to the underlying API calls but combines them together for ease (the whole point of gh!).
Re: where to go from here, I suppose the ideal scenario would be that the exact missing permissions in your fine-grained PAT are specified in the response/error message (which I think is loosely related to #7978).
I realise this might be a way off, so perhaps as an intermediary step, it could be useful to have more clarity in the response about which API call failed. This might already be detectable from the debug logs? Apologies if so - I'm not super familiar with GraphQL!
Happy to provide more feedback/suggestions if it helps. andyfeller commented on Aug 7, 2024
I realise this might be a way off, so perhaps as an intermediary step, it could be useful to have more clarity in the response about which API call failed.
This is a good question. 🤔
There are ways that we try to inspect the responses from the GitHub API, seeing if there are particular errors we can present an action to the user to fix it. For example, if we can detect this API called because of v1 coarse grain PAT permission scopes. Beyond that, every level bubbles the error up until we eventually exit, determining what type of exit code to return relative to the error.
The problem of writing maintainable code is the higher up the stack you go, the less that code should make about how the job is being done.
Take the actual logic querying the API for repository info:
|
func GitHubRepo(client *Client, repo ghrepo.Interface) (*Repository, error) { |
|
query := ` |
|
fragment repo on Repository { |
|
id |
|
name |
|
owner { login } |
|
hasIssuesEnabled |
|
description |
|
hasWikiEnabled |
|
viewerPermission |
|
defaultBranchRef { |
|
name |
|
} |
|
} |
|
|
|
query RepositoryInfo($owner: String!, $name: String!) { |
|
repository(owner: $owner, name: $name) { |
|
...repo |
|
parent { |
|
...repo |
|
} |
|
mergeCommitAllowed |
|
rebaseMergeAllowed |
|
squashMergeAllowed |
|
} |
|
}` |
|
variables := map[string]interface{}{ |
|
"owner": repo.RepoOwner(), |
|
"name": repo.RepoName(), |
|
} |
|
|
|
var result struct { |
|
Repository *Repository |
|
} |
|
if err := client.GraphQL(repo.RepoHost(), query, variables, &result); err != nil { |
|
return nil, err |
|
} |
|
// The GraphQL API should have returned an error in case of a missing repository, but this isn't |
|
// guaranteed to happen when an authentication token with insufficient permissions is being used. |
|
if result.Repository == nil { |
|
return nil, GraphQLError{ |
|
GraphQLError: &ghAPI.GraphQLError{ |
|
Errors: []ghAPI.GraphQLErrorItem{{ |
|
Type: "NOT_FOUND", |
|
Message: fmt.Sprintf("Could not resolve to a Repository with the name '%s/%s'.", repo.RepoOwner(), repo.RepoName()), |
|
}}, |
|
}, |
|
} |
|
} |
|
|
|
return InitRepoHostname(result.Repository, repo.RepoHost()), nil |
|
} |
The errors being returned here could be wrapped and say something like "Unable to retrieve repository info" but it's the error the HTTP client returned that actually has useful information to troubleshoot.
This is also just 1 situation where errors are bubbled up, which is idiomatic in Go. Meaning this would be a massive eating an elephant exercise to go through the whole code to improve error responses.
Without more information from the GitHub APIs regarding scopes, I don't know if there is a good middle ground solution. 😞 andyfeller commented on Aug 14, 2024 @jjacobgreen : Let me be clear: I think you're touching on a few important concerns:
- GitHub users want to adopt more secure, tightly scoped API tokens
- GitHub CLI users will benefit from having the necessary fine grained permissions documented
- GitHub CLI does not get enough information back from GitHub API to detect missing permission(s) to guide users to take action
Given the several issues raised around fine grained v2 PAT use, here's what I suggest:
- Create an issue to document fine grain PAT permissions per command
- Follow up with API team regarding V2 PATs and detecting missing permission scopes
- Based on ☝ create issue(s) to ensure GitHub CLI is capable of detecting and presenting this info
andyfeller commented on Aug 14, 2024 For completeness, this is the logic involved in detecting incorrect token permission scopes when 4XX HTTP errors are encountered, reading information out of X-Oauth-Scopes and X-Accepted-Oauth-Scopes to help users:
|
// handleResponse takes a ghAPI.HTTPError or ghAPI.GraphQLError and converts it into an |
|
// HTTPError or GraphQLError respectively. |
|
func handleResponse(err error) error { |
|
if err == nil { |
|
return nil |
|
} |
|
|
|
var restErr *ghAPI.HTTPError |
|
if errors.As(err, &restErr) { |
|
return HTTPError{ |
|
HTTPError: restErr, |
|
scopesSuggestion: generateScopesSuggestion(restErr.StatusCode, |
|
restErr.Headers.Get("X-Accepted-Oauth-Scopes"), |
|
restErr.Headers.Get("X-Oauth-Scopes"), |
|
restErr.RequestURL.Hostname()), |
|
} |
|
} |
|
|
|
var gqlErr *ghAPI.GraphQLError |
|
if errors.As(err, &gqlErr) { |
|
return GraphQLError{ |
|
GraphQLError: gqlErr, |
|
} |
|
} |
|
|
|
return err |
|
} |
|
|
|
// ScopesSuggestion is an error messaging utility that prints the suggestion to request additional OAuth |
|
// scopes in case a server response indicates that there are missing scopes. |
|
func ScopesSuggestion(resp *http.Response) string { |
|
return generateScopesSuggestion(resp.StatusCode, |
|
resp.Header.Get("X-Accepted-Oauth-Scopes"), |
|
resp.Header.Get("X-Oauth-Scopes"), |
|
resp.Request.URL.Hostname()) |
|
} |
|
|
|
// EndpointNeedsScopes adds additional OAuth scopes to an HTTP response as if they were returned from the |
|
// server endpoint. This improves HTTP 4xx error messaging for endpoints that don't explicitly list the |
|
// OAuth scopes they need. |
|
func EndpointNeedsScopes(resp *http.Response, s string) *http.Response { |
|
if resp.StatusCode >= 400 && resp.StatusCode < 500 { |
|
oldScopes := resp.Header.Get("X-Accepted-Oauth-Scopes") |
|
resp.Header.Set("X-Accepted-Oauth-Scopes", fmt.Sprintf("%s, %s", oldScopes, s)) |
|
} |
|
return resp |
|
} |
|
|
|
func generateScopesSuggestion(statusCode int, endpointNeedsScopes, tokenHasScopes, hostname string) string { |
|
if statusCode < 400 || statusCode > 499 || statusCode == 422 { |
|
return "" |
|
} |
|
|
|
if tokenHasScopes == "" { |
|
return "" |
|
} |
|
|
|
gotScopes := map[string]struct{}{} |
|
for _, s := range strings.Split(tokenHasScopes, ",") { |
|
s = strings.TrimSpace(s) |
|
gotScopes[s] = struct{}{} |
|
|
|
// Certain scopes may be grouped under a single "top-level" scope. The following branch |
|
// statements include these grouped/implied scopes when the top-level scope is encountered. |
|
// See https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps. |
|
if s == "repo" { |
|
gotScopes["repo:status"] = struct{}{} |
|
gotScopes["repo_deployment"] = struct{}{} |
|
gotScopes["public_repo"] = struct{}{} |
|
gotScopes["repo:invite"] = struct{}{} |
|
gotScopes["security_events"] = struct{}{} |
|
} else if s == "user" { |
|
gotScopes["read:user"] = struct{}{} |
|
gotScopes["user:email"] = struct{}{} |
|
gotScopes["user:follow"] = struct{}{} |
|
} else if s == "codespace" { |
|
gotScopes["codespace:secrets"] = struct{}{} |
|
} else if strings.HasPrefix(s, "admin:") { |
|
gotScopes["read:"+strings.TrimPrefix(s, "admin:")] = struct{}{} |
|
gotScopes["write:"+strings.TrimPrefix(s, "admin:")] = struct{}{} |
|
} else if strings.HasPrefix(s, "write:") { |
|
gotScopes["read:"+strings.TrimPrefix(s, "write:")] = struct{}{} |
|
} |
|
} |
|
|
|
for _, s := range strings.Split(endpointNeedsScopes, ",") { |
|
s = strings.TrimSpace(s) |
|
if _, gotScope := gotScopes[s]; s == "" || gotScope { |
|
continue |
|
} |
|
return fmt.Sprintf( |
|
"This API operation needs the %[1]q scope. To request it, run: gh auth refresh -h %[2]s -s %[1]s", |
|
s, |
|
ghinstance.NormalizeHostname(hostname), |
|
) |
|
} |
|
|
|
return "" |
|
} |
As we see in the debug data above, that information isn't readily available when using fine grained PATs. 🤔 andyfeller commented on Aug 14, 2024 Created #9461 to capture which fine grain PAT permissions are needed to use GitHub CLI. andyfeller commented on Aug 20, 2024 @jjacobgreen : After discussing with the Authorization team, it appears GraphQL doesn't readily have information about the necessary fine grain PAT scopes by object and mutation, making it impossible for gh to guide the user on how to solve this issue.
For now, I want to use this issue to track improving gh ability to detect missing fine grain PAT scopes despite being blocked by missing API capability. changed the title [-]Creating a pull request using a fine-grained permissions token requires more permissions than specified in the docs[/-] [+]Detect missing fine grain permission scopes, notify user on steps to take to refresh scopes[/+] on Aug 20, 2024jjacobgreen commented on Aug 21, 2024 @andyfeller sorry for the delayed response. I appreciate the attention that you've been giving to this. Sounds like a good plan. Let me know if there's any helpful input I can provide. MartinNowak commented on Oct 6, 2025 github-actions commented on Mar 9, 2026 Thank you for your issue! We have categorized it as an enhancement (feature or improvement) request, and it has been added to our backlog. In doing so, we are not committing to implementing this feature at this time, but, we will consider it for future releases based on community feedback and our own product roadmap.
Unless you see the
help wanted
Contributions welcome
label, we are not currently looking for external contributions for this feature.
If you come across this issue and would like to see it implemented, please add a thumbs up! This will help us prioritize the feature. Please only comment if you have additional information or viewpoints to contribute. romancone commented on May 16, 2026 Every coding LLM in the world asks to add Check to PAT permission, and each time I reply, there is no "Check" option available.
GitHub folks - you need to either update your documentation or fix this issue finally! Reacted by Aaron Hamilton, Juan J. Jimenez-Anca, Kaden Wilkinson, Adam Walsh, WhoMe192, Kenta Mukai, AriESQ, koziolek, Eydel Rivero, Richard Morgan and 8 more jaredrhall commented on Oct 30, 2026
Every coding LLM in the world asks to add Check to PAT permission, and each time I reply, there is no "Check" option available.
I'd happily take degraded functionality over none until this can be truly fixed. We use scoped fine-grained PATs for people and agents to limit unintended actions, and default gh pr view fails on any private-repo PR that has check runs. The API already returns the rest of the PR alongside the per-check errors. A welcome patch: have pr view note that the token can't read checks and still show everything else it can see.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|