[repo] Auto label PR workflow permissions fix #5684
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
add-labels.yml
so that it is able to label PRs opened from forksDetails
There is a bit of knowledge required to understand what is going on here. The
pull_request
trigger is special on GitHub. It is essentially executing code from untrusted sources (forks) so by default it only hasread
permission to everything. To label something you needwrite
access. Thepull_request_target
trigger runs as the main repo and can havewrite
permission. It also sees secrets which for this workflow is undesirable.Info: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
What we don't want to do is execute anything from forks with a token granted under
pull_request_target
scope. If we did, someone could open a PR with some code to write out secrets and we would run it.To mitigate this what this PR does is checkout the main branch (
github.event.repository.default_branch
) to execute the powershell to label PRs.Here is a demo showing that whatever the user has for
add-labels.psm1
in their fork is NOT run: https://github.com/CodeBlanchOrg/opentelemetry-dotnet/pull/45/filesWe can see the correct branch being checked out here: https://github.com/CodeBlanchOrg/opentelemetry-dotnet/actions/runs/9453234045/job/26038183882?pr=45#step:2:170
For a standard
pull_request
trigger notice the merge branch is checked out: https://github.com/open-telemetry/opentelemetry-dotnet/actions/runs/9415790656/job/25937523267?pr=5647#step:2:170Merge requirement checklist