■ はじめに
Third-Party Github Action を利用する際に、 ベストは、Commit hashで指定することらしいのでメモる 色々なサイトを見てみるが、 Commit hashの確認の仕方も載ってないので載せておく
目次
【1】Third-Party Github Action の指定方法 1)Branch 指定 2)Major tag 指定 3)Commit hash 指定 - Short 4)Commit hash 指定 - Full 【2】Github Actions の Commit hash の調べ方 【3】サンプル 1)actions/setup-python 2)actions/upload-artifact 3)slackapi/slack-github-action 【4】コードを書き変えてみる
【1】Third-Party Github Action の指定方法
* Third-Party Github Action を利用するのは、以下の通り 1)Branch 指定 << これは、完全にNG 2)Major tag 指定 3)Commit hash 指定 - Short 4)Commit hash 指定 - Full << ★セキュリティの観点からこれが一番いい★
補足:公式ドキュメントより
公式ドキュメントにも、そう書かれている
Pin actions to a full length commit SHA Pinning an action to a full length commit SHA is currently the only way to use an action as an immutable release. Pinning to a particular SHA helps mitigate the risk of a bad actor adding a backdoor to the action's repository, as they would need to generate a SHA-1 collision for a valid Git object payload. When selecting a SHA, you should verify it is from the action's repository and not a repository fork. <=★
1)Branch 指定
* これは完全にNG
例
- uses: actions/checkout@main
NG理由
[1] 変更が大きかった場合、失敗可能性がある [2] 悪意のある変更が含まれてしまった場合、防げない
2)Major tag 指定
* よくある方法。 * 1)よりいいが、タグが指す Git コミットは変更可能であり、 GitHub Actions が呼び出すアクションを正確に固定できているとは言えない
例
- uses: actions/checkout@v3
3)Commit hash 指定 - Short
* 1)~2)よりいいが、衝突に対して脆弱
例
https://github.com/actions/checkout/commits/v3.1.0
# - uses: actions/checkout@v3.1.0 - uses: actions/checkout@93ea575
4)Commit hash 指定 - Full
* 現状のBest
例
https://github.com/actions/checkout/commits/v3.1.0
# - uses: actions/checkout@v3.1.0 - uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8
【2】Github Actions の Commit hash の調べ方
* actions/checkout を例にやっていく
手順例:actions/checkoutの場合
[1] 対象のThird-Party Github Action のGithubまでいく => 今回は、「actions/checkout」なので、以下のサイトにアクセスする
https://github.com/actions/checkout
[2] [XXX commits (XXX:数字)]-[main]-[Tags]-[vX.X.X (対象Version)]を選択 => 下記の※1・※2参照 [3] Hash(v3.5.1の場合「83b7061」)の 右側のコピーアイコン「Copy SHA for XXXX (Hash shortl; v3.5.1の場合「83b7061」)」 を選択すると、Commit hash(Full)がコピーできる
※1:v3.5.1の場合
https://github.com/actions/checkout/commits/v3.5.1
# - uses: actions/checkout@v3.5.1 - uses: actions/checkout@83b7061638ee4956cf7545a6f7efe594e5ad0247
※2:v3.6.0の場合
https://github.com/actions/checkout/commits/v3.6.0
# - uses: actions/checkout@v3.6.0 - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744
※3:v4.1.2の場合
https://github.com/actions/checkout/commits/v4.1.2
# - uses: actions/checkout@v4.1.2 - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633
【3】サンプル
* 他のThird-Party Github Action でも確認
1)actions/setup-python
https://github.com/actions/setup-python
例1:setup-python@v4.8.0
https://github.com/actions/setup-python/commits/v4.8.0
# - uses: actions/setup-python@v4.8.0 - uses: actions/setup-python@b64ffcaf5b410884ad320a9cfac8866006a109aa
2)actions/upload-artifact
https://github.com/actions/upload-artifact
例1:upload-artifact@v3.1.3
https://github.com/actions/upload-artifact/commits/v3.1.3
# - uses: actions/upload-artifact@v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32
3)slackapi/slack-github-action
https://github.com/slackapi/slack-github-action
例1:slackapi/slack-github-action@v1.25.0
https://github.com/slackapi/slack-github-action/commits/v1.25.0
# - uses: slackapi/slack-github-action@v1.25.0 - uses: slackapi/slack-github-action@6c661ce58804a1a20f6dc5fbee7f0381b469e001
【4】コードを書き変えてみる
* 以下の関連記事のサンプルを書き変えてみる => 変更した箇所「★変更点1★」「★変更点2★」に注目 => 動作確認して、ちゃんと動いた
Github Actions ~ Slack連携 ~
https://dk521123.hatenablog.com/entry/2024/04/03/003053
name: RunDemoForSlack on: workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: # Check out theis repo #★変更点1★ #- uses: actions/checkout@v4.1.2 - uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # Start a Slack workflow using a webhook trigger # https://github.com/slackapi/slack-github-action - name: Send to Slack id: slack #★変更点2★ #uses: slackapi/slack-github-action@v1.25.0 uses: slackapi/slack-github-action@6c661ce58804a1a20f6dc5fbee7f0381b469e001 with: payload: | { "text": ":github: Result to build: ${{ job.status }}\n\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}", "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": ":github: Result to build: ${{ job.status }}\n\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" } }, { "type": "context", "elements": [ { "type": "mrkdwn", "text": "Author: <https://github.com/${{ github.event.sender.login }}|@${{ github.event.sender.login }}>" } ] } ] } env: SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
参考文献
GitHub Actions のアクションをコミットハッシュで固定した時に上手く更新する - azrsh
GitHub Actions Workflow チェックリスト | CyberAgent Developers Blog
https://engineering.mercari.com/blog/entry/20230609-github-actions-guideline/
https://zenn.dev/shunsuke_suzuki/articles/pinact-pin-github-actions-version
公式サイトのセキュリティガイド
https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions
関連記事
Github Actions ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2021/11/04/142835
Github Actions ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2022/06/16/151443
Github Actions ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2023/12/22/195715
Github Actions ~ Slack連携 ~
https://dk521123.hatenablog.com/entry/2024/04/03/003053
Github Actions ~ セキュリティ/インジェクション攻撃 ~
https://dk521123.hatenablog.com/entry/2024/04/16/222419