Skip to content

Commit

Permalink
Enforce permissions for releases and patches
Browse files Browse the repository at this point in the history
TODO: do this for all workflows without deduplicating this code
  • Loading branch information
jonenst committed Mar 6, 2025
1 parent 4716775 commit 1d8fce7
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/release-backend-app-generic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ on:
required: false
default: 'release'
type: string
permission:
required: false
default: 'maintain' # "pull" or "triage" or "push" or "maintain" or "admin"
type: string
secrets:
VERSIONBUMP_GHAPP_PRIVATE_KEY:
required: true
Expand All @@ -40,6 +44,29 @@ jobs:
environment:
name: ${{ inputs.environment }}
steps:
# don't allow to disable this check, and for security only succeed if everything goes perfectly and outputs "true"
- name: Check Actor Permission
env:
# Need to properly pass permission all the way down to jq as data to avoid script injections
PERMISSION: ${{ inputs.permission }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "checking permission=$PERMISSION on repo=$GITHUB_REPOSITORY for user=$GITHUB_ACTOR"
# no need to escape GITHUB_REPOSITORY and GITHUB_ACTOR because github
# names can only contain ASCII letters, digits, and the characters ., # -, and _.
# permission: legacy, is actually a role (one per user) and doesn't
# have "maintain" role, only ["none", "read", "write", "admin"]
# role_name: one per user, new standard roles, custom roles, "" and
# "push" and "pull" instead of "none" and "read" and "write": "",
# "read", "triage", "write", "maintain", "admin" and also non
# standard custom roles
# user.permissions: boolean status only for each standard permissions "pull", "triage", "push", "maintain", "admin"
# So the best for now is to use user.permissions I think.
# Not using gh --jq gojq because we can't use variables with it and also it automatically removes quotes from strings which could be an attack vector
# the gh api returns a JSON boolean so jq can only output true or false without quotes.
allowed=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$GITHUB_ACTOR/permission" | jq --arg PERMISSION "$PERMISSION" '.user.permissions[$PERMISSION]')
[[ "$allowed" == "true" ]] || exit 1
- uses: actions/create-github-app-token@v1
id: app-token
name: Generate app token
Expand Down

0 comments on commit 1d8fce7

Please sign in to comment.