Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: githubv4.Query: Resource not accessible by integration in Branch-Protection #1097

Open
laurentsimon opened this issue Oct 1, 2021 · 26 comments
Assignees
Labels
kind/docs Improvements or additions to documentation

Comments

@laurentsimon
Copy link
Contributor

laurentsimon commented Oct 1, 2021

An interesting error came up in the run for the GitHub action https://github.com/ossf/scorecard/security/code-scanning/2869?query=ref%3Arefs%2Fheads%2Fmain

error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration

Note: this was for a push event, not a PR. Let's see if this continues in next push. It did not happen before in previous pushes.

I've never seen it before. @azeemsgoogle ideas?

@laurentsimon laurentsimon added the kind/bug Something isn't working label Oct 1, 2021
@azeemshaikh38
Copy link
Contributor

Possibly similar to actions/first-interaction#10. Need to investigate further.

@azeemshaikh38 azeemshaikh38 self-assigned this Oct 15, 2021
@laurentsimon
Copy link
Contributor Author

@laurentsimon
Copy link
Contributor Author

another occurrence in step-security/agent#35 (comment)

@laurentsimon
Copy link
Contributor Author

another one #1074 (comment)

@laurentsimon
Copy link
Contributor Author

laurentsimon commented Nov 22, 2021

Apparently, this happened all the time in step-security/agent#35 (comment)
@varunsh-coder does the problem occur on pull requests or on push events to main branch?

@varunsh-coder
Copy link
Contributor

Apparently, this happened all the time in step-security/agent#35 (comment)
@varunsh-coder does the problem occur on pull requests or on push events to main branch?

I see this error both for pull requests and push. BTW this error is probably due to a token permissions issue. The workflow only has security-events: write. Are you calling a GitHub API to get this result?

@laurentsimon
Copy link
Contributor Author

laurentsimon commented Nov 23, 2021

I agree it looks like a permission problem. We use permissions: read-all and security-events: write; and this error only happens for the Branch Protection APIs using graphQl.

Mhhhh.. this page https://docs.github.com/en/graphql/guides/forming-calls-with-graphql states To communicate with the GraphQL server, you'll need an OAuth token with the right scopes. I think that's the cause. We're using the GitHub token provisioned to the workflow, but it's not an OAuth or PAT.

If that's the reason, looks like we would either need to use the RESTful APIs for the GitHub action, or disable branch protection in GitHub action. We initially moved away from REST APIs to graphQl because of rate limiting. In the case of a GitHub action, developers only need to access their own repo so we don't really need to worry about rate limiting: that seems to be confirmed since other checks use REST APIs and appear to be working as expected.

@azeemsgoogle wdut?

@laurentsimon
Copy link
Contributor Author

laurentsimon commented Dec 2, 2021

@josepalafox the graphQl APIs seem to require a PAT/OAuth token and don't automatically work with the GitHub token provisioned to workflows. Do you know the reasoning behind this? Is there a particular permission needed for the workflow GitHub token to make this work?

@varunsh-coder
Copy link
Contributor

I agree it looks like a permission problem. We use permissions: read-all and security-events: write; and this error only happens for the Branch Protection APIs using graphQl.

Mhhhh.. this page https://docs.github.com/en/graphql/guides/forming-calls-with-graphql states To communicate with the GraphQL server, you'll need an OAuth token with the right scopes. I think that's the cause. We're using the GitHub token provisioned to the workflow, but it's not an OAuth or PAT.

If that's the reason, looks like we would either need to use the RESTful APIs for the GitHub action, or disable branch protection in GitHub action. We initially moved away from REST APIs to graphQl because of rate limiting. In the case of a GitHub action, developers only need to access their own repo so we don't really need to worry about rate limiting: that seems to be confirmed since other checks use REST APIs and appear to be working as expected.

@azeemsgoogle wdut?

JFYI - I realized that looking up branch protection requires administration permission, and I believe GITHUB_TOKEN does not have administration permission. So even if Graph API had the same permission model, I doubt this would work with the GITHUB_TOKEN...

@laurentsimon
Copy link
Contributor Author

laurentsimon commented Dec 3, 2021

Interesting, thanks for sharing! I don't understand why the GitHu tokens have different permission models though :/

@josepalafox
Copy link

josepalafox commented Dec 3, 2021 via email

@laurentsimon
Copy link
Contributor Author

Thanks for the info! Do you know the reasoning behind this? Is there a security concern? Or is this something that may get fixed in the future?

@josepalafox
Copy link

josepalafox commented Dec 3, 2021 via email

@azeemshaikh38
Copy link
Contributor

The issue is that the generic actions does not get the right permissions.

@laurentsimon, if this is the case, then moving to REST API won't solve the problem. Basically, we'll need PAT if we want to run Branch-Protection. Have you tried running the Action without the Branch-Protection check if that works?

@laurentsimon
Copy link
Contributor Author

yes I meant to mention that. It may be the case that REST APIs will never work. I have a TODO item to test it out in the action using curl. Will post my result. But I'm afraid you''re right, @azeemshaikh38

@azeemshaikh38
Copy link
Contributor

SG. Will assign this to you for now. Can close this issue after confirming.

@laurentsimon
Copy link
Contributor Author

laurentsimon commented Jan 26, 2022

@josepalafox reviving this thread. As we try to make scorecard as friction-less as possible for installation, we realize the PAT requirement does create friction. I wanted to understand what you meant by This is evidently an intentional design limitation and exists across multiple API services for security purposes.

What can workflow tokens not expose a read permission to read branch protection settings? What is the security concern?
I know there's a new token-id permission that was introduced for OIDC, for example.

@josepalafox
Copy link

josepalafox commented Jan 26, 2022 via email

@laurentsimon
Copy link
Contributor Author

Thanks Jose. Let us know what you find.

So long as the permission is controlled by a workflow field, it does not strike me as being more dangerous than id-token or contents:write or packages:write.

@josepalafox
Copy link

Feedback from our team is that to get branch protection rules you'd have to have admin:read which is too broad to enable.

cc @cschleiden

@laurentsimon
Copy link
Contributor Author

laurentsimon commented Jan 27, 2022

Thanks for the info. Fyi, branch protection is accessible with a PAT owned by a non-repo maintainer, except 3 settings that require admin:read as as repo maintainer. I'd expect the same should work for a workflow token. I don't follow why admin:read for the repo itself should be necessary to read other settings, since it's available publicly anyway.

Also, in general, using ephemeral workflow tokens is beneficial because PATs cannot be rotated easily.

Let me know if I mis-understood something.

@Hritik14
Copy link

Hi. It's been a while since this issue is open. Is there any support planned for private repos ?

I found the following on the scorecard actions docs.

Private repositories need a PAT to use any Scorecard Action functions
...
We recommend that you do not use a PAT unless you feel that the risks introduced are outweighed by the functionalities they support.

This implies using scorecard on private repositories is risky by design...

@eddie-knight
Copy link
Contributor

eddie-knight commented Jul 27, 2023

I'm getting the following error at the moment:

2023/07/27 14:52:26 error during command execution: check runtime error: Branch-Protection: internal error: error during branchesHandler.setup: internal error: githubv4.Query: Your token has not been granted the required scopes to execute this query. The 'allowsDeletions' field requires one of the following scopes: ['public_repo'], but your token has only been granted the: [''] scopes. Please modify your token's scopes at: https://github.com/settings/tokens.

Edit: I resolved this with the following scope change on my token:
Screen Shot 2023-08-03 at 5 40 05 PM

I believe GitLab users solved it like this:
Screen Shot 2023-08-03 at 5 41 05 PM

@spencerschrock
Copy link
Member

Hi. It's been a while since this issue is open. Is there any support planned for private repos ?

I found the following on the scorecard actions docs.

Private repositories need a PAT to use any Scorecard Action functions
...
We recommend that you do not use a PAT unless you feel that the risks introduced are outweighed by the functionalities they support.

This implies using scorecard on private repositories is risky by design...

The fine-grained PAT should help aleviate this. But we'll need to update our instructions at:
https://github.com/ossf/scorecard-action#authentication-with-fine-grained-pat-optional

It will probably also need read permissions to a few more fields for private repos (I'm guessing actions, issues, pull_requests, contents, but just a guess not exhaustive.

I'm getting the following error at the moment:
2023/07/27 14:52:26 error during command execution: check runtime error: Branch-Protection: internal error: error during branchesHandler.setup: internal error: githubv4.Query: Your token has not been granted the required scopes to execute this query. The 'allowsDeletions' field requires one of the following scopes: ['public_repo'], but your token has only been granted the: [''] scopes. Please modify your token's scopes at: https://github.com/settings/tokens.

I believe this has been resolved in the slack, but the classic PATs need the public_repo scope to read branch protection, which the message is saying.

Note: Hopefully all of this will be a problem of the past soon due to Repository Rules (see: #3326)

@afmarcum
Copy link
Contributor

afmarcum commented Mar 7, 2024

Document on the Scorecard repo pointing to the scorecard-action documentation, which may need to be updated for clarity as well.

@afmarcum afmarcum added kind/docs Improvements or additions to documentation and removed kind/bug Something isn't working labels Mar 7, 2024
@afmarcum afmarcum moved this from Backlog - Bugs to Todo in Scorecard - NEW Mar 7, 2024
@mauricio-liatrio
Copy link

mauricio-liatrio commented Apr 19, 2024

I ran into the same exact error of this issue, I do have a private repo with branch protection. I resolved it by adding these two permissions to my PAT:
image
image

The documentation needs to be updated!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/docs Improvements or additions to documentation
Projects
Status: Todo
Development

No branches or pull requests

9 participants