2021-12-12 GitHub Actionsのpull_request_targetについて / pull_request closedイベントは発火しないことがある
GitHub Actions の pull_request と pull_request_target イベントの存在についてわかりにくいのでメモ。
GitHub の詳しい解説は下記。
pull_request_target の間違った使用は危険だよ、と警鐘を鳴らしている。
pull_request_targetは fork repoにも書き込み権限・Secretsを渡すpull_requestは fork repo に書き込み権限・Secretsは渡さない
下記のワークフローfork先をチェックアウトした後に実行しているnpm install or npm buildが危険。なぜなら npm install or npm build の実行時に untrusted なコードが実行されるリスクがあるから。
原文は下記。
The potentially untrusted code is being run during npm install or npm build as the build scripts and referenced packages are controlled by the author of the PR.
# INSECURE. Provided as an example only.
on:
pull_request_target
jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: actions/setup-node@v1
- run: |
npm install
npm build
- uses: completely/fakeaction@v2
with:
arg1: ${{ secrets.supersecret }}
- uses: fakerepo/comment-on-pr@v1
with:
message: |
Thank you!
ただ、fork先のコードをチェックアウトしてコードをフォーマットする、diffを取る、grep検索する、などのユースケースにおいては untrusted なコード実行は伴わないので使用してOKっぽい。
- Secret にアクセス不要な場合は
pull_request_targetを使うのは避けること pull_requestandworkflow_runの組み合わせで権限を渡すワークフローを切り分けること
pull_request closedイベントは発火しないことがある
pull_request closedイベントは発火しないことがあるので、そのケースにもpull_request_target イベントを使ったほうが良いとのこと。
So workflows using
on: { pull_request: { types: [“closed”] } }will not consistently be triggered.The workaround is to use a
pull_request_targettrigger as those do not require a merge commit but run the workflow from the target branch. But take note of the warnings in the docs below.https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target