🐧

actions/checkout の persist-credentials を false にする linter と修正ツール

2024/12/27に公開

GitHub Actions の actions/checkout の input persist-credentials を false にすることを強制する linter と自動で false に修正するツールの紹介です。

https://github.com/suzuki-shunsuke/ghalint
https://github.com/lintnet-modules/ghalint
https://github.com/suzuki-shunsuke/disable-checkout-persist-credentials

persist-credentials とは

GitHub Actions の actions/checkout の input persist-credentials は、リポジトリをチェックアウトしたあとに GitHub Access Token や SSH key などの認証情報を Git の config に残すか否かの設定です。
デフォルトでは true で job の終了時まで Git の config に保存されます。
false だと actions/checkout の終了時に Git の config から消されます。

なぜ persist-credentials は false にすべきなのか

セキュリティ的に良くないからです。
GitHub Actions で認証情報にアクセスできる step は必要最小限に制限するべきです。
そうでないと特定の step で実行している action や script で悪意のあるコードが実行された場合に認証情報が悪用されてしまいます。
persist-credentials が true だと actions/checkout の input token (デフォルトで ${{github.token}}) などの認証情報に後続の step からアクセスできてしまいます。
具体的なコードはここには敢えて載せませんが、興味のある人は適当なリポジトリを作って GitHub Actions 上で token を取得して GitHub CLI を実行して Pull Request にコメントとかしてみると理解が深まるでしょう。

そもそも persist-credentials はデフォルト false にすべきなのでは?

自分もそう思いますし、そういう issue は何年も前からあります。

https://github.com/actions/checkout/issues/485

Linter

自分がメンテしている linter に persist-credentials: false を強制する rule を追加しました。

https://github.com/suzuki-shunsuke/ghalint
https://github.com/lintnet-modules/ghalint

lintnet についてはこちらも参照してください。
ghalint ではデフォルトで rule が有効です。
どうしても persist-credentials を true にする必要がある場合は job 単位で許可することも出来ます。

ただ、 Linter で強制する前に既存のコードを修正する必要があります。
そこで自動で修正するツールを作りました。

自動修正ツール

actions/checkout は至るところで使われているため、手作業で修正するのは大変です。
そこで自動で修正するツールを作りました。

https://github.com/suzuki-shunsuke/disable-checkout-persist-credentials

使い方は簡単で引数無しで実行すると .github/workflows 配下の workflow が修正されます。

disable-checkout-persist-credentials

パスを指定することも出来ます。 composite action の修正にも対応しています。

disable-checkout-persist-credentials foo/action.yaml

multi-gitter と組み合わせてまとめて修正

https://github.com/lindell/multi-gitter と組み合わせて GitHub User や Organization のリポジトリをまとめて修正できます。
自分がまとめて修正した際のスクリプトなどがこちらに置いてあります。
あくまでサンプルコードなので、これを使う際は適当に修正してください。

https://github.com/suzuki-shunsuke/batch-bulk-disable-checkout-persist-credentials
https://github.com/suzuki-shunsuke/batch-bulk-disable-checkout-persist-credentials/issues/1

Discussion